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 { sleep } = require("./utils");
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
|
|
||||||
|
const DATA_TO_TRACK = [
|
||||||
|
"score",
|
||||||
|
"wins",
|
||||||
|
"kills",
|
||||||
|
"deaths",
|
||||||
|
"matches",
|
||||||
|
"minutesPlayed",
|
||||||
|
"kd",
|
||||||
|
];
|
||||||
|
|
||||||
// Prometheus metrics
|
// Prometheus metrics
|
||||||
const promClient = require("prom-client");
|
const promClient = require("prom-client");
|
||||||
const Registry = promClient.Registry;
|
const Registry = promClient.Registry;
|
||||||
@ -20,12 +30,19 @@ const client = new FortniteAPI({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Define metrics
|
// Define metrics
|
||||||
const trackedAccountsGauge = new promClient.Gauge({
|
const metricTrackers = {};
|
||||||
name: "fortnite_tracked_accounts",
|
|
||||||
help: "Number of Fortnite accounts being tracked",
|
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
|
// Track accounts before starting the server so we dont get no data
|
||||||
trackAccounts();
|
trackAccounts();
|
||||||
@ -48,17 +65,27 @@ app.get("/metrics", async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
async function trackAccounts() {
|
async function trackAccounts() {
|
||||||
trackedAccountsGauge.set(ACCOUNTS_TO_TRACK.length);
|
|
||||||
for (const username of ACCOUNTS_TO_TRACK) {
|
for (const username of ACCOUNTS_TO_TRACK) {
|
||||||
try {
|
try {
|
||||||
const account = await client.brStats({
|
const account = await client.brStats({
|
||||||
name: username,
|
name: username,
|
||||||
});
|
});
|
||||||
if (account.status !== 200) {
|
const { status, data } = account;
|
||||||
console.error(`Error: ${account.status}`);
|
if (status !== 200) {
|
||||||
|
console.error(`Error getting stats for ${username}: ${status}`);
|
||||||
continue;
|
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);
|
sleep(1000);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
Reference in New Issue
Block a user