This commit is contained in:
parent
3c8b605fec
commit
b720e96431
@ -16,6 +16,14 @@ const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|
|||||||
|> yield()
|
|> 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) => {
|
app.get("/", (req, res) => {
|
||||||
res.send("Hello!");
|
res.send("Hello!");
|
||||||
});
|
});
|
||||||
@ -32,25 +40,53 @@ app.get("/analytics", async (req, res) => {
|
|||||||
}
|
}
|
||||||
const shouldUseLongerIntervals = timeInMs > 24 * 60 * 60 * 1000 * 7; // 7 days
|
const shouldUseLongerIntervals = timeInMs > 24 * 60 * 60 * 1000 * 7; // 7 days
|
||||||
|
|
||||||
const query = formatString(
|
const getActivePlayersHistory = async () => {
|
||||||
|
const rows = await InfluxQueryAPI.collectRows(
|
||||||
|
formatString(
|
||||||
getPlayerHistoryQuery,
|
getPlayerHistoryQuery,
|
||||||
false,
|
false,
|
||||||
timeQuery,
|
timeQuery,
|
||||||
shouldUseLongerIntervals ? "1d" : "1h"
|
shouldUseLongerIntervals ? "1d" : "1h"
|
||||||
|
)
|
||||||
);
|
);
|
||||||
console.log(query);
|
|
||||||
const rows = await InfluxQueryAPI.collectRows(query);
|
|
||||||
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,
|
||||||
}));
|
}));
|
||||||
history = history.sort(
|
return history.sort(
|
||||||
(a: any, b: any) => new Date(a.time).getTime() - new Date(b.time).getTime()
|
(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({
|
return res.json({
|
||||||
serverTimeTaken: new Date().getTime() - before + "ms",
|
serverTimeTaken: new Date().getTime() - before + "ms",
|
||||||
history: history,
|
activePlayersHistory,
|
||||||
|
scoreCountHistory,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user