feat: implement PNID and NEXToken metrics

This commit is contained in:
mrjvs
2026-09-01 21:29:29 +02:00
parent 8a1964fab0
commit 7babb418df
2 changed files with 31 additions and 0 deletions

View File

@@ -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<void> {
// * 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<void> {
// * 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();

View File

@@ -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