Skip to content

Commit

Permalink
add metrics for owned capes
Browse files Browse the repository at this point in the history
  • Loading branch information
InventivetalentDev committed Jul 12, 2024
1 parent 599314c commit 19471c8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
44 changes: 43 additions & 1 deletion src/generator/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,23 @@ export class Stats {
this.queryDurationStats(),
// this.queryCountDuplicateViewStats(),
this.queryTimeFrameStats(),
this.pushCountDuplicateViewStats(),
this.pushCountDuplicateViewStats()
]).then((ignored: any) => {
console.log(debug(`Complete stats query took ${ (Date.now() - queryStart) / 1000 }s`));
});
}


static async slowQuery(): Promise<void> {
console.log(debug(`Querying stats (slow)...`));
const queryStart = Date.now();
return Promise.all([
this.queryAccountCapeStats()
]).then((ignored: any) => {
console.log(debug(`Slow stats query took ${ (Date.now() - queryStart) / 1000 }s`));
});
}

protected static async queryAccountStats(): Promise<void> {
const config = await getConfig();
const time = Date.now() / 1000;
Expand Down Expand Up @@ -194,6 +205,37 @@ export class Stats {
});
}

protected static async queryAccountCapeStats(): Promise<void>{
const accountCapes = await Account.aggregate([
{$match:{ ownedCapes : { $exists : true, $ne : [ ] } }},
{ $unwind: "$ownedCapes" }, // flatten the ownedCapes array
{ $group: { _id: "$ownedCapes", count: { $sum: 1 } } } // count the occurrences of each unique value in the ownedCapes field
]);

const points = accountCapes.map((entry) => {
return {
measurement: 'account_capes',
tags: {
cape: entry._id
},
fields: {
count: entry.count
}
}
});

try {
const metrics = await MineSkinMetrics.get();
await metrics.metrics!.influx.writePoints(points, {
database: 'mineskin',
precision: 's'
})
} catch (e) {
console.warn(e);
Sentry.captureException(e);
}
}

protected static async queryDurationStats(): Promise<void> {
return Skin.aggregate([
{"$sort": {time: -1}},
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,21 @@ async function init() {
}
}, 1000 * 60 * 2);

setInterval(() => {
try {
Stats.slowQuery();
} catch (e) {
Sentry.captureException(e);
}
}, 1000 * 60 * 60);
setTimeout(() => {
try {
Stats.slowQuery();
} catch (e) {
Sentry.captureException(e);
}
}, 1000 * 60)

if (config.migrateRedisStats) {
console.log("running redis migration");
try {
Expand Down
2 changes: 2 additions & 0 deletions src/util/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MineSkinMetrics {
public readonly hashMismatch: Metric;
public readonly urlMismatch: Metric;
public readonly accountNotifications: Metric;
public readonly accountCapes: Metric;

public readonly tester: Metric;

Expand Down Expand Up @@ -65,6 +66,7 @@ export class MineSkinMetrics {
this.urlMismatch = this.metrics.metric('mineskin', 'url_mismatch');
this.tester = this.metrics.metric('mineskin', 'tester');
this.accountNotifications = this.metrics.metric('mineskin', 'account_notifications');
this.accountCapes = this.metrics.metric('mineskin', 'account_capes');
}

apiRequestsMiddleware(req: Request, res: Response, next: NextFunction) {
Expand Down

0 comments on commit 19471c8

Please sign in to comment.