add score count history
All checks were successful
deploy / deploy (push) Successful in 29s

This commit is contained in:
Lee 2023-10-27 18:23:13 +01:00
parent 3c8b605fec
commit b720e96431

@ -16,6 +16,14 @@ const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> yield()
`;
const getScoreCountHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> range(start: -{})
|> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["type"] == "score_count")
|> aggregateWindow(every: -{}, fn: spread, createEmpty: true)
`;
app.get("/", (req, res) => {
res.send("Hello!");
});
@ -32,25 +40,53 @@ app.get("/analytics", async (req, res) => {
}
const shouldUseLongerIntervals = timeInMs > 24 * 60 * 60 * 1000 * 7; // 7 days
const query = formatString(
const getActivePlayersHistory = async () => {
const rows = await InfluxQueryAPI.collectRows(
formatString(
getPlayerHistoryQuery,
false,
timeQuery,
shouldUseLongerIntervals ? "1d" : "1h"
)
);
console.log(query);
const rows = await InfluxQueryAPI.collectRows(query);
let history = rows.map((row: any) => ({
time: row._time,
value: row._value !== null ? row._value.toFixed(0) : null,
}));
history = history.sort(
(a: any, b: any) => new Date(a.time).getTime() - new Date(b.time).getTime()
return history.sort(
(a: any, b: any) =>
new Date(a.time).getTime() - new Date(b.time).getTime()
);
};
const getScoreCountHistory = async () => {
const rows = await InfluxQueryAPI.collectRows(
formatString(
getScoreCountHistoryQuery,
false,
timeQuery,
shouldUseLongerIntervals ? "1d" : "1h"
)
);
let history = rows.map((row: any) => ({
time: row._time,
value: row._value !== null ? row._value.toFixed(0) : null,
}));
return history.sort(
(a: any, b: any) =>
new Date(a.time).getTime() - new Date(b.time).getTime()
);
};
const [activePlayersHistory, scoreCountHistory] = await Promise.all([
getActivePlayersHistory(),
getScoreCountHistory(),
]);
return res.json({
serverTimeTaken: new Date().getTime() - before + "ms",
history: history,
activePlayersHistory,
scoreCountHistory,
});
});