diff --git a/src/services/api.ts b/src/services/api.ts index b61d2e5..ff5160b 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -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( - 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() - ); + const getActivePlayersHistory = async () => { + const rows = await InfluxQueryAPI.collectRows( + formatString( + getPlayerHistoryQuery, + 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 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, }); });