This commit is contained in:
parent
42ee829fc0
commit
21fed8b935
43
src/index.js
43
src/index.js
@ -4,6 +4,16 @@ const { Client: FortniteAPI, Language } = require("fnapicom");
|
||||
const { sleep } = require("./utils");
|
||||
const express = require("express");
|
||||
|
||||
const DATA_TO_TRACK = [
|
||||
"score",
|
||||
"wins",
|
||||
"kills",
|
||||
"deaths",
|
||||
"matches",
|
||||
"minutesPlayed",
|
||||
"kd",
|
||||
];
|
||||
|
||||
// Prometheus metrics
|
||||
const promClient = require("prom-client");
|
||||
const Registry = promClient.Registry;
|
||||
@ -20,12 +30,19 @@ const client = new FortniteAPI({
|
||||
});
|
||||
|
||||
// Define metrics
|
||||
const trackedAccountsGauge = new promClient.Gauge({
|
||||
name: "fortnite_tracked_accounts",
|
||||
help: "Number of Fortnite accounts being tracked",
|
||||
const metricTrackers = {};
|
||||
|
||||
DATA_TO_TRACK.forEach((metric) => {
|
||||
metricTrackers[metric] = new promClient.Gauge({
|
||||
name: `fortnite_${metric}`,
|
||||
help: `Fortnite ${metric}`,
|
||||
labelNames: ["username", "gameType"],
|
||||
});
|
||||
});
|
||||
|
||||
register.registerMetric(trackedAccountsGauge);
|
||||
for (const metric of Object.values(metricTrackers)) {
|
||||
register.registerMetric(metric);
|
||||
}
|
||||
|
||||
// Track accounts before starting the server so we dont get no data
|
||||
trackAccounts();
|
||||
@ -48,17 +65,27 @@ app.get("/metrics", async (req, res) => {
|
||||
});
|
||||
|
||||
async function trackAccounts() {
|
||||
trackedAccountsGauge.set(ACCOUNTS_TO_TRACK.length);
|
||||
for (const username of ACCOUNTS_TO_TRACK) {
|
||||
try {
|
||||
const account = await client.brStats({
|
||||
name: username,
|
||||
});
|
||||
if (account.status !== 200) {
|
||||
console.error(`Error: ${account.status}`);
|
||||
const { status, data } = account;
|
||||
if (status !== 200) {
|
||||
console.error(`Error getting stats for ${username}: ${status}`);
|
||||
continue;
|
||||
}
|
||||
console.log("works");
|
||||
const { stats } = data;
|
||||
const { all } = stats;
|
||||
|
||||
for (const [key, value] of Object.entries(all)) {
|
||||
if (value == null) continue;
|
||||
for (const [keyy, valuee] of Object.entries(value)) {
|
||||
if (DATA_TO_TRACK.includes(keyy)) {
|
||||
metricTrackers[keyy].set({ username, gameType: key }, valuee);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sleep(1000);
|
||||
} catch (error) {
|
||||
|
Reference in New Issue
Block a user