2023-10-09 19:03:31 +00:00
|
|
|
import { addDays } from "date-fns";
|
|
|
|
import { decodeTime } from "ulid";
|
|
|
|
import { getDailyStats } from "./dailyStatsStore.ts";
|
|
|
|
import { generationStore } from "./generationStore.ts";
|
|
|
|
|
2023-11-20 00:33:38 +00:00
|
|
|
export interface GlobalStats {
|
|
|
|
userIds: number[];
|
|
|
|
imageCount: number;
|
|
|
|
stepCount: number;
|
|
|
|
pixelCount: number;
|
|
|
|
pixelStepCount: number;
|
|
|
|
timestamp: number;
|
|
|
|
}
|
2023-10-09 19:03:31 +00:00
|
|
|
|
2023-10-13 11:47:57 +00:00
|
|
|
export const globalStats: GlobalStats = await getGlobalStats();
|
2023-10-09 19:03:31 +00:00
|
|
|
|
2023-10-13 11:47:57 +00:00
|
|
|
async function getGlobalStats(): Promise<GlobalStats> {
|
2023-10-09 19:03:31 +00:00
|
|
|
// find the year/month/day of the first generation
|
|
|
|
const startDate = await generationStore.getAll({}, { limit: 1 })
|
|
|
|
.then((generations) => generations[0]?.id)
|
|
|
|
.then((generationId) => generationId ? new Date(decodeTime(generationId)) : new Date());
|
|
|
|
|
|
|
|
// iterate to today and sum up stats
|
|
|
|
const userIdSet = new Set<number>();
|
|
|
|
let imageCount = 0;
|
2023-10-13 11:47:57 +00:00
|
|
|
let stepCount = 0;
|
2023-10-09 19:03:31 +00:00
|
|
|
let pixelCount = 0;
|
2023-10-13 11:47:57 +00:00
|
|
|
let pixelStepCount = 0;
|
2023-10-09 19:03:31 +00:00
|
|
|
|
|
|
|
const tomorrow = addDays(new Date(), 1);
|
|
|
|
|
|
|
|
for (
|
|
|
|
let date = startDate;
|
|
|
|
date < tomorrow;
|
|
|
|
date = addDays(date, 1)
|
|
|
|
) {
|
|
|
|
const dailyStats = await getDailyStats(
|
|
|
|
date.getUTCFullYear(),
|
|
|
|
date.getUTCMonth() + 1,
|
|
|
|
date.getUTCDate(),
|
|
|
|
);
|
|
|
|
for (const userId of dailyStats.userIds) userIdSet.add(userId);
|
|
|
|
imageCount += dailyStats.imageCount;
|
2023-10-13 11:47:57 +00:00
|
|
|
stepCount += dailyStats.stepCount;
|
2023-10-09 19:03:31 +00:00
|
|
|
pixelCount += dailyStats.pixelCount;
|
2023-10-13 11:47:57 +00:00
|
|
|
pixelStepCount += dailyStats.pixelStepCount;
|
2023-10-09 19:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
userIds: [...userIdSet],
|
|
|
|
imageCount,
|
2023-10-13 11:47:57 +00:00
|
|
|
stepCount,
|
2023-10-09 19:03:31 +00:00
|
|
|
pixelCount,
|
2023-10-13 11:47:57 +00:00
|
|
|
pixelStepCount,
|
2023-10-09 19:03:31 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
|
|
|
}
|