Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metrics cron #1486

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/cron-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Cron Metrics

on:
schedule:
- cron: '*/10 * * * *'
- cron: '*/15 * * * *'
workflow_dispatch:

jobs:
Expand All @@ -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
Expand Down
38 changes: 33 additions & 5 deletions packages/cron/src/jobs/metrics.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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(),
])

Expand Down Expand Up @@ -100,3 +113,18 @@ async function updatePinsCount(roPg, rwPg, service, status) {
rows[0].total,
])
}

/**
* @template T
* @param {string} name
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
*/
async function withTimeLog(name, fn) {
const start = Date.now()
try {
return await fn()
} finally {
log(`${name} took: ${Date.now() - start}ms`)
}
}
2 changes: 1 addition & 1 deletion packages/cron/src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}