beatsaber-metrics-tracker/src/index.ts

37 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-10-27 11:10:20 +00:00
import { InfluxDB, Point } from "@influxdata/influxdb-client";
import * as dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const INFLUXDB_URL = process.env.INFLUXDB_URL;
const INFLUXDB_ORG = process.env.INFLUXDB_ORG;
const INFLUXDB_BUCKET = process.env.INFLUXDB_BUCKET;
const INFLUXDB_TOKEN = process.env.INFLUXDB_TOKEN;
if (!INFLUXDB_URL || !INFLUXDB_ORG || !INFLUXDB_BUCKET || !INFLUXDB_TOKEN) {
throw new Error("Missing InfluxDB environment variables");
}
const writeApi = new InfluxDB({
url: INFLUXDB_URL,
token: INFLUXDB_TOKEN,
}).getWriteApi(INFLUXDB_ORG, INFLUXDB_BUCKET, "ms");
console.log(writeApi);
async function update() {
const response = await fetch("https://scoresaber.com/api/players/count");
const count = await response.text();
const point = new Point("scoresaber")
.tag("type", "player_count")
.intField("value", parseInt(count))
.timestamp(new Date());
writeApi.writePoint(point);
console.log(`Updated player count to ${count}`);
}
update();
setInterval(update, 60_000); // 1 minute