Compare commits

..

No commits in common. "fbbf6776678bf776d2fff6db6d821a484e69de4c" and "28acbce1acdb1e793f453101832285370a26a5f1" have entirely different histories.

4 changed files with 3 additions and 81 deletions

View File

@ -1,5 +1,5 @@
{
"name": "beatsaber-metrics-tracker",
"name": "Sample node.js express app",
"repository": "https://git.fascinated.cc/Fascinated/beatsaber-metrics-tracker",
"healthchecks": {}
}

View File

@ -1,13 +1,12 @@
import express from "express";
import { INFLUXDB_BUCKET, InfluxQueryAPI } from "../index";
import { formatString } from "../utils/stringUtils";
const app = express();
const port = process.env.PORT || 3000;
// Query to get the player count history for tge last 24 hours in 1 hour intervals
const getPlayerHistoryQuery = `from(bucket: "${INFLUXDB_BUCKET}")
|> range(start: {})
|> range(start: -24h)
|> filter(fn: (r) => r["_measurement"] == "scoresaber")
|> filter(fn: (r) => r["_field"] == "value")
|> filter(fn: (r) => r["type"] == "player_count")
@ -21,18 +20,7 @@ app.get("/", (req, res) => {
app.get("/analytics", async (req, res) => {
const before = new Date().getTime();
const timeQuery = req.query.time || "24h";
const timeInMs = parseTimeToMilliseconds(timeQuery.toString());
if (timeInMs > 30 * 24 * 60 * 60 * 1000) {
return res.status(400).json({
error: "Time range too large. Max time range is 30 days.",
});
}
const rows = await InfluxQueryAPI.collectRows(
formatString(getPlayerHistoryQuery, false, timeQuery)
);
const rows = await InfluxQueryAPI.collectRows(getPlayerHistoryQuery);
let history = rows.map((row: any) => ({
time: row._time,
value: row._value !== null ? row._value.toFixed(0) : null,

View File

@ -1,26 +0,0 @@
/**
* Formats a string with the given arguments.
*
* @param str the string to check
* @param uriEncodeStrings whether to uri encode the strings
* @param args the arguments to replace
* @returns the formatted string
*/
export function formatString(
str: string,
uriEncodeStrings: boolean,
...args: any[]
): string {
return str.replace(/{}/g, (match) => {
// If there are no arguments, return the match
if (args.length === 0) {
return match;
}
// Otherwise, return the next argument
if (uriEncodeStrings) {
return encodeURIComponent(String(args.shift()));
}
return String(args.shift());
});
}

View File

@ -1,40 +0,0 @@
/**
* Parses a time string to milliseconds.
*
* @param time the time to parse
* @returns the time in milliseconds
*/
function parseTimeToMilliseconds(time: string) {
// Regular expression to match the numeric value and unit
const regex = /^(\d+)([smhdwMy])$/;
const match = time.match(regex);
if (!match) {
throw new Error(
"Invalid time format. Example valid formats: 1s, 5m, 2h, 3d, 1w, 2M, 1y"
);
}
const value = parseInt(match[1]);
const unit = match[2];
switch (unit) {
case "s":
return value * 1000; // seconds to milliseconds
case "m":
return value * 60 * 1000; // minutes to milliseconds
case "h":
return value * 60 * 60 * 1000; // hours to milliseconds
case "d":
return value * 24 * 60 * 60 * 1000; // days to milliseconds
case "w":
return value * 7 * 24 * 60 * 60 * 1000; // weeks to milliseconds
case "M":
return value * 30 * 24 * 60 * 60 * 1000; // months (approximate) to milliseconds
case "y":
return value * 365 * 24 * 60 * 60 * 1000; // years (approximate) to milliseconds
default:
throw new Error("Invalid time unit. Use one of: s, m, h, d, w, M, y");
}
}