diff --git a/src/metrics.ts b/src/metrics.ts index 5991220..d2b2f9f 100644 --- a/src/metrics.ts +++ b/src/metrics.ts @@ -1,10 +1,38 @@ import { format } from 'node:util'; +import { Gauge } from 'prom-client'; import expressMetrics from 'express-prom-bundle'; import express from 'express'; import { LOG_ERROR, LOG_INFO, LOG_SUCCESS } from '@/logger'; import { config } from '@/config-manager'; +import { PNID } from '@/models/pnid'; +import { NEXToken } from '@/models/nex-token'; import type { Express, NextFunction, Request, Response } from 'express'; +export const pnidTotalGauge = new Gauge({ + name: 'pn_account_pnid_total', + help: 'Total number of registered PNIDs', + async collect(): Promise { + // * Aggregations are faster on large collections + const [result] = await PNID.aggregate<{ n: number } | undefined>([ + { $match: { deleted: false } }, + { $count: 'n' } + ]); + this.set(result?.n ?? 0); + } +}); + +export const nexTokenTotalGauge = new Gauge({ + name: 'pn_account_nex_token_total', + help: 'Total number of NEX tokens', + async collect(): Promise { + // * Aggregations are faster on large collections + const [result] = await NEXToken.aggregate<{ n: number } | undefined>([ + { $count: 'n' } + ]); + this.set(result?.n ?? 0); + } +}); + export function registerMetrics(app: Express): Express { const metrics = express(); diff --git a/src/models/pnid.ts b/src/models/pnid.ts index 81d523d..0b2d8ca 100644 --- a/src/models/pnid.ts +++ b/src/models/pnid.ts @@ -144,6 +144,9 @@ PNIDSchema.index({ 'pid': 1, 'username': 1, 'connections.discord.id': 1 }); PNIDSchema.plugin(uniqueValidator, { message: '{PATH} already in use.' }); +// * Used by metrics +PNIDSchema.index({ deleted: 1 }); + /* According to http://pf2m.com/tools/rank.php Nintendo PID's start at 1,800,000,000 and count down with each account This means the max PID is 1799999999 and hard-limits the number of potential accounts to 1,800,000,000