use real player count
All checks were successful
deploy / deploy (push) Successful in 30s

This commit is contained in:
Lee 2023-11-08 07:43:00 +00:00
parent 5b3e7e8198
commit f9077f739c

@ -1,85 +1,85 @@
import { INFLUXDB_BUCKET, InfluxQueryAPI } from "../.."; import { INFLUXDB_BUCKET, InfluxQueryAPI } from "../..";
import { formatString } from "../../utils/stringUtils"; import { formatString } from "../../utils/stringUtils";
import { parseTimeToMilliseconds } from "../../utils/timeUtils"; import { parseTimeToMilliseconds } from "../../utils/timeUtils";
// Query to get the player count history for tge last 24 hours in 1 hour intervals // Query to get the player count history for tge last 24 hours in 1 hour intervals
const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}") const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> range(start: -{}) |> range(start: -{})
|> filter(fn: (r) => r["_measurement"] == "scoresaber") |> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["_field"] == "value") |> filter(fn: (r) => r["type"] == "score")
|> filter(fn: (r) => r["type"] == "player_count") |> filter(fn: (r) => r["_field"] == "player_id")
|> aggregateWindow(every: {}, fn: mean) |> unique()
|> yield() |> aggregateWindow(every: {}, fn: count)
`; `;
const getScoreCountHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}") const getScoreCountHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> range(start: -{}) |> range(start: -{})
|> filter(fn: (r) => r["_measurement"] == "scoresaber") |> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["_field"] == "value") |> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["type"] == "score_count") |> filter(fn: (r) => r["type"] == "score_count")
|> aggregateWindow(every: {}, fn: spread, createEmpty: true) |> aggregateWindow(every: {}, fn: spread, createEmpty: true)
`; `;
export default function analyticsRoute(app: any) { export default function analyticsRoute(app: any) {
app.get("/analytics", async (req: any, res: any) => { app.get("/analytics", async (req: any, res: any) => {
const before = new Date().getTime(); const before = new Date().getTime();
const timeQuery = req.query.time || "30d"; const timeQuery = req.query.time || "30d";
const timeInMs = parseTimeToMilliseconds(timeQuery.toString()); const timeInMs = parseTimeToMilliseconds(timeQuery.toString());
if (timeInMs > 30 * 24 * 60 * 60 * 1000) { if (timeInMs > 30 * 24 * 60 * 60 * 1000) {
return res.status(400).json({ return res.status(400).json({
error: "Time range too large. Max time range is 30 days.", error: "Time range too large. Max time range is 30 days.",
}); });
} }
const shouldUseLongerIntervals = timeInMs > 24 * 60 * 60 * 1000 * 7; // 7 days const shouldUseLongerIntervals = timeInMs > 24 * 60 * 60 * 1000 * 7; // 7 days
const getActivePlayersHistory = async () => { const getActivePlayersHistory = async () => {
const rows = await InfluxQueryAPI.collectRows( const rows = await InfluxQueryAPI.collectRows(
formatString( formatString(
getPlayerHistoryQuery, getPlayerHistoryQuery,
false, false,
timeQuery, timeQuery,
shouldUseLongerIntervals ? "1d" : "1h" shouldUseLongerIntervals ? "1d" : "1h"
) )
); );
let history = rows.map((row: any) => ({ let history = rows.map((row: any) => ({
time: row._time, time: row._time,
value: row._value !== null ? row._value.toFixed(0) : null, value: row._value !== null ? row._value.toFixed(0) : null,
})); }));
return history.sort( return history.sort(
(a: any, b: any) => (a: any, b: any) =>
new Date(a.time).getTime() - new Date(b.time).getTime() new Date(a.time).getTime() - new Date(b.time).getTime()
); );
}; };
const getScoreCountHistory = async () => { const getScoreCountHistory = async () => {
const rows = await InfluxQueryAPI.collectRows( const rows = await InfluxQueryAPI.collectRows(
formatString( formatString(
getScoreCountHistoryQuery, getScoreCountHistoryQuery,
false, false,
timeQuery, timeQuery,
shouldUseLongerIntervals ? "1d" : "1h" shouldUseLongerIntervals ? "1d" : "1h"
) )
); );
let history = rows.map((row: any) => ({ let history = rows.map((row: any) => ({
time: row._time, time: row._time,
value: row._value !== null ? row._value.toFixed(0) : null, value: row._value !== null ? row._value.toFixed(0) : null,
})); }));
return history.sort( return history.sort(
(a: any, b: any) => (a: any, b: any) =>
new Date(a.time).getTime() - new Date(b.time).getTime() new Date(a.time).getTime() - new Date(b.time).getTime()
); );
}; };
const [activePlayersHistory, scoreCountHistory] = await Promise.all([ const [activePlayersHistory, scoreCountHistory] = await Promise.all([
getActivePlayersHistory(), getActivePlayersHistory(),
getScoreCountHistory(), getScoreCountHistory(),
]); ]);
return res.json({ return res.json({
serverTimeTaken: new Date().getTime() - before + "ms", serverTimeTaken: new Date().getTime() - before + "ms",
activePlayersHistory, activePlayersHistory,
scoreCountHistory, scoreCountHistory,
}); });
}); });
} }