From c753c784292532d37fd3a222bc42008574139fd4 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 28 Feb 2022 10:24:18 +0000 Subject: [PATCH] fix: metrics cron --- .github/workflows/cron-metrics.yml | 4 ++-- packages/cron/src/jobs/metrics.js | 38 ++++++++++++++++++++++++++---- packages/cron/src/lib/utils.js | 2 +- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/.github/workflows/cron-metrics.yml b/.github/workflows/cron-metrics.yml index 50fb0800ff..233f4f3dcb 100644 --- a/.github/workflows/cron-metrics.yml +++ b/.github/workflows/cron-metrics.yml @@ -2,7 +2,7 @@ name: Cron Metrics on: schedule: - - cron: '*/10 * * * *' + - cron: '*/15 * * * *' workflow_dispatch: jobs: @@ -12,7 +12,7 @@ jobs: strategy: matrix: env: ['staging', 'production'] - timeout-minutes: 10 + timeout-minutes: 15 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 diff --git a/packages/cron/src/jobs/metrics.js b/packages/cron/src/jobs/metrics.js index fa9516a060..d5934a98a8 100644 --- a/packages/cron/src/jobs/metrics.js +++ b/packages/cron/src/jobs/metrics.js @@ -1,10 +1,13 @@ import settle from 'p-settle' +import debug from 'debug' import { UPLOAD_TYPES, PIN_SERVICES, PIN_STATUSES, } from '../../../api/src/utils/db-client.js' +const log = debug('metrics:updateMetrics') + /** * @typedef {import('pg').Client} Client * @typedef {{ name: string, value: number }} Metric @@ -17,7 +20,7 @@ const COUNT_UPLOADS = 'SELECT COUNT(*) AS total FROM upload WHERE type = $1' const COUNT_PINS = 'SELECT COUNT(*) AS total FROM pin WHERE service = $1 AND status = $2' -const SUM_CONTENT_DAG_SIZE = `SELECT SUM(c.dag_size) AS "total" FROM content c` +const SUM_CONTENT_DAG_SIZE = 'SELECT SUM(c.dag_size) AS "total" FROM content c' const UPDATE_METRIC = ` INSERT INTO metric (name, value, updated_at) @@ -33,11 +36,21 @@ ON CONFLICT (name) DO UPDATE */ export async function updateMetrics({ roPg, rwPg }) { const results = await settle([ - updateUsersCount(roPg, rwPg), - updateContentRootDagSizeSum(roPg, rwPg), - ...UPLOAD_TYPES.map((t) => updateUploadsCount(roPg, rwPg, t)), + withTimeLog('updateUsersCount', () => updateUsersCount(roPg, rwPg)), + withTimeLog('updateContentRootDagSizeSum', () => + updateContentRootDagSizeSum(roPg, rwPg) + ), + ...UPLOAD_TYPES.map((t) => + withTimeLog(`updateUploadsCount[${t}]`, () => + updateUploadsCount(roPg, rwPg, t) + ) + ), ...PIN_SERVICES.map((svc) => - PIN_STATUSES.map((s) => updatePinsCount(roPg, rwPg, svc, s)) + PIN_STATUSES.map((s) => + withTimeLog(`updatePinsCount[${svc}][${s}]`, () => + updatePinsCount(roPg, rwPg, svc, s) + ) + ) ).flat(), ]) @@ -100,3 +113,18 @@ async function updatePinsCount(roPg, rwPg, service, status) { rows[0].total, ]) } + +/** + * @template T + * @param {string} name + * @param {() => Promise} fn + * @returns {Promise} + */ +async function withTimeLog(name, fn) { + const start = Date.now() + try { + return await fn() + } finally { + log(`${name} took: ${Date.now() - start}ms`) + } +} diff --git a/packages/cron/src/lib/utils.js b/packages/cron/src/lib/utils.js index 52ef441491..167afcfd22 100644 --- a/packages/cron/src/lib/utils.js +++ b/packages/cron/src/lib/utils.js @@ -97,5 +97,5 @@ export function getPg(env, mode = 'rw') { mode === 'rw' ? env.DATABASE_CONNECTION : env.RO_DATABASE_CONNECTION } if (!connectionString) throw new Error('missing Postgres connection string') - return new pg.Client({ connectionString }) + return new pg.Pool({ connectionString }) }