Skip to content

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 20, 2022
1 parent 4bc0ac2 commit 7572f10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
17 changes: 10 additions & 7 deletions packages/api/src/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export async function metricsGet(request, env, ctx) {
return res
}

const [usersTotal, urlsTotal, eventsTotal, sizeTotal] = await Promise.all([
env.db.getMetricsValue('users_total'),
env.db.getMetricsValue('urls_total'),
env.db.getMetricsValue('events_total'),
env.db.getMetricsValue('size_total'),
])
const [usersTotal, urlsTotal, putEventsTotal, deleteEventsTotal, sizeTotal] =
await Promise.all([
env.db.getMetricsValue('users_total'),
env.db.getMetricsValue('urls_total'),
env.db.getMetricsValue('events_put_total'),
env.db.getMetricsValue('events_delete_total'),
env.db.getMetricsValue('size_total'),
])

const metrics = [
`# HELP nftlinkapi_permacache_urls_total Total perma cached urls.`,
Expand All @@ -36,7 +38,8 @@ export async function metricsGet(request, env, ctx) {
`nftlinkapi_permacache_size_total ${sizeTotal}`,
`# HELP nftlinkapi_permacache_events_total Total perma cache events.`,
`# TYPE nftlinkapi_permacache_events_total counter`,
`nftlinkapi_permacache_events_total ${eventsTotal}`,
`nftlinkapi_permacache_events_total{type="Put"} ${putEventsTotal}`,
`nftlinkapi_permacache_events_total{type="Delete"} ${deleteEventsTotal}`,
].join('\n')

res = new Response(metrics, {
Expand Down
13 changes: 12 additions & 1 deletion packages/api/test/metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@ test('Gets metrics content when empty state', async (t) => {
t.is(metricsResponse.includes('nftlinkapi_permacache_urls_total 0'), true)
t.is(metricsResponse.includes('nftlinkapi_permacache_users_total 0'), true)
t.is(metricsResponse.includes('nftlinkapi_permacache_size_total 0'), true)
t.is(metricsResponse.includes('nftlinkapi_permacache_events_total 0'), true)
t.is(
metricsResponse.includes(
'nftlinkapi_permacache_events_total{type="Put"} 0'
),
true
)
t.is(
metricsResponse.includes(
'nftlinkapi_permacache_events_total{type="Delete"} 0'
),
true
)
})
25 changes: 17 additions & 8 deletions packages/cron/src/jobs/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const log = debug('metrics:updateMetrics')

const COUNT_USERS = `
SELECT COUNT(*) AS total
FROM nftstorage.user u JOIN nftstorage.user_tag ut
ON u.id = ut.user_id
WHERE ut.tag = 'HasSuperHotAccess'::user_tag_type AND ut.value = 'true'
FROM nftstorage.user_tag
WHERE tag = 'HasSuperHotAccess'::user_tag_type AND value = 'true'
`

const COUNT_URLS = `
SELECT COUNT(*) AS total
FROM perma_cache
`

const COUNT_EVENTS = `
const COUNT_EVENTS_PER_TYPE = `
SELECT COUNT(*) AS total
FROM perma_cache_event
WHERE type = $1
`

const SUM_SIZE = `
Expand All @@ -47,7 +47,12 @@ export async function updateMetrics({ roPg, rwPg }) {
const results = await settle([
withTimeLog('updateUsersCount', () => updateUsersCount(roPg, rwPg)),
withTimeLog('updateUrlsCount', () => updateUrlsCount(roPg, rwPg)),
withTimeLog('updateEventsCount', () => updateEventsCount(roPg, rwPg)),
withTimeLog('updatePutEventsCount', () =>
updateEventsCount(roPg, rwPg, 'Put')
),
withTimeLog('updateDeleteEventsCount', () =>
updateEventsCount(roPg, rwPg, 'Delete')
),
withTimeLog('updateSizeSum', () => updateSizeSum(roPg, rwPg)),
{ concurrency: MAX_CONCURRENT_QUERIES },
])
Expand Down Expand Up @@ -86,11 +91,15 @@ async function updateUrlsCount(roPg, rwPg) {
/**
* @param {Client} roPg
* @param {Client} rwPg
* @param {'Put' | 'Delete'} type
*/
async function updateEventsCount(roPg, rwPg) {
const { rows } = await roPg.query(COUNT_EVENTS)
async function updateEventsCount(roPg, rwPg, type) {
const { rows } = await roPg.query(COUNT_EVENTS_PER_TYPE, [type])
if (!rows.length) throw new Error('no rows returned counting events')
return rwPg.query(UPDATE_METRIC, ['events_total', rows[0].total])
return rwPg.query(UPDATE_METRIC, [
`events_${type.toLowerCase()}_total`,
rows[0].total,
])
}

/**
Expand Down

0 comments on commit 7572f10

Please sign in to comment.