Skip to content

Commit

Permalink
Port limits-alert cronjob to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinnl committed Jul 11, 2024
1 parent 96bc287 commit 9dc22f5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dev:cron:first-data-broker-removal-fixed": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/firstDataBrokerRemovalFixed.tsx",
"dev:cron:monthly-activity": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/monthlyActivity.tsx",
"dev:cron:db-delete-unverified-subscribers": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/deleteUnverifiedSubscribers.ts",
"dev:cron:onerep-limits-alert": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/onerepStatsAlert.ts",
"dev:nimbus": "node --watch-path config/nimbus.yaml src/scripts/build/nimbusTypes.js",
"build": "npm run get-location-data && npm run build-glean && npm run build-nimbus && next build && npm run build-cronjobs",
"cloudrun": "npm run db:migrate && npm start",
Expand All @@ -28,7 +29,7 @@
"cron:db-delete-unverified-subscribers": "node dist/scripts/cronjobs/deleteUnverifiedSubscribers.js",
"cron:db-pull-breaches": "node src/scripts/syncBreaches.js",
"cron:remote-settings-pull-breaches": "node scripts/updatebreaches.js",
"cron:onerep-limits-alert": "node src/scripts/onerepStatsAlert.js",
"cron:onerep-limits-alert": "node dist/scripts/cronjobs/onerepStatsAlert.js",
"db:migrate": "node -r dotenv-flow/config node_modules/knex/bin/cli.js migrate:latest --knexfile src/db/knexfile.js",
"db:rollback": "node -r dotenv-flow/config node_modules/knex/bin/cli.js migrate:rollback --knexfile src/db/knexfile.js",
"prepare": "husky",
Expand Down
4 changes: 2 additions & 2 deletions src/db/tables/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* @param {string} name
* @param {string} current
* @param {string} max
* @returns {Promise<import('knex/types/tables').SubscriberRow | null>} updated subscriber
* @returns {Promise<import('knex/types/tables').StatsRow | null>} inserted stats
*/
export async function addOnerepStats (name, current, max) {
const res = await knex("stats").insert({ name, current, max, type: "onerep"}).returning("*");
return res[0];
}

/**
* @returns {object}
* @returns {Promise<Pick<import('knex/types/tables').StatsRow, "name" | "current" | "max"> | undefined>} stats
*/
export async function getOnerepStats () {
const res = await knex("stats").select("name", "current", "max").where("type", "onerep");
Expand Down
29 changes: 23 additions & 6 deletions src/knex-tables.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,20 @@ declare module "knex/types/tables" {
"id" | "created_at" | "updated_at"
>;

interface StatsRow {
id: number;
name: string;
current: string;
max: string;
type: string;
created_at: Date;
modified_at: Date;
}
type StatsAutoInsertedColumns = Extract<
keyof StatsRow,
"id" | "created_at" | "modified_at"
>;

interface Tables {
attributions: Knex.CompositeTableType<
AttributionRow,
Expand Down Expand Up @@ -430,11 +444,14 @@ declare module "knex/types/tables" {
Partial<Omit<EmailNotificationRow, "id" | "created_at">> &
Pick<EmailNotificationRow, "updated_at">
>;
}
interface StatsRow {
name: string;
current: string;
max: string;
type: string;

stats: Knex.CompositeTableType<
StatsRow,
// On updates, auto-generated columns cannot be set:
Omit<StatsRow, StatsAutoInsertedColumns> & Partial<StatsRow>,
// On updates, don't allow updating the ID and created date; all other fields are optional, except modified_at:
Partial<Omit<StatsRow, "id" | "created_at">> &
Pick<StatsRow, "modified_at">
>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import "dotenv-flow/config";
import Sentry from "@sentry/nextjs";
import { addOnerepStats, knexStats } from "../db/tables/stats.js";
import { addOnerepStats, knexStats } from "../../db/tables/stats.js";

const SENTRY_SLUG = "cron-onerep-stats-alerts";

const MAX_MANUAL_SCANS = parseInt(process.env.MAX_MANUAL_SCANS) || 0;
const MAX_INITIAL_SCANS = parseInt(process.env.MAX_INITIAL_SCANS) || 0;
const MAX_PROFILES_ACTIVATED =
parseInt(process.env.MAX_PROFILES_ACTIVATED) || 0;
const MAX_PROFILES_CREATED = parseInt(process.env.MAX_PROFILES_CREATED) || 0;
const parseEnvVarNr = (value: string | undefined) => parseInt(value ?? "0", 10);
const MAX_MANUAL_SCANS = parseEnvVarNr(process.env.MAX_MANUAL_SCANS);
const MAX_INITIAL_SCANS = parseEnvVarNr(process.env.MAX_INITIAL_SCANS);
const MAX_PROFILES_ACTIVATED = parseEnvVarNr(
process.env.MAX_PROFILES_ACTIVATED,
);
const MAX_PROFILES_CREATED = parseEnvVarNr(process.env.MAX_PROFILES_CREATED);

Sentry.init({
environment: process.env.APP_ENV,
Expand Down Expand Up @@ -52,7 +55,7 @@ export async function checkStats() {
}

// TODO use the shared version when this is converted to Typescript.
async function onerepFetch(path, options = {}) {
async function onerepFetch(path: string, options: RequestInit = {}) {
const onerepApiBase = process.env.ONEREP_API_BASE;
if (!onerepApiBase) {
throw new Error("ONEREP_API_BASE env var not set");
Expand Down

0 comments on commit 9dc22f5

Please sign in to comment.