2023-11-20 00:33:38 +00:00
|
|
|
import { Static, t } from "elysia";
|
|
|
|
import { kvMemoize } from "../utils/kvMemoize.ts";
|
2023-10-10 16:21:25 +00:00
|
|
|
import { db } from "./db.ts";
|
|
|
|
import { generationStore } from "./generationStore.ts";
|
2023-11-11 00:11:35 +00:00
|
|
|
import { hoursToMilliseconds, isSameDay, minutesToMilliseconds } from "date-fns";
|
|
|
|
import { UTCDateMini } from "date-fns/utc";
|
2023-10-10 16:21:25 +00:00
|
|
|
|
2023-11-20 00:33:38 +00:00
|
|
|
export const userDailyStatsSchema = t.Object({
|
|
|
|
imageCount: t.Number(),
|
|
|
|
pixelCount: t.Number(),
|
|
|
|
pixelStepCount: t.Number(),
|
|
|
|
timestamp: t.Number(),
|
|
|
|
});
|
2023-10-10 16:21:25 +00:00
|
|
|
|
2023-11-20 00:33:38 +00:00
|
|
|
export type UserDailyStats = Static<typeof userDailyStatsSchema>;
|
2023-10-10 16:21:25 +00:00
|
|
|
|
|
|
|
export const getUserDailyStats = kvMemoize(
|
|
|
|
db,
|
|
|
|
["userDailyStats"],
|
|
|
|
async (userId: number, year: number, month: number, day: number): Promise<UserDailyStats> => {
|
2023-11-11 00:11:35 +00:00
|
|
|
let imageCount = 0;
|
|
|
|
let pixelCount = 0;
|
2023-11-20 00:33:38 +00:00
|
|
|
let pixelStepCount = 0;
|
2023-11-11 00:11:35 +00:00
|
|
|
|
|
|
|
for await (
|
|
|
|
const generation of generationStore.listBy("fromId", {
|
2023-11-12 02:33:35 +00:00
|
|
|
after: new Date(Date.UTC(year, month - 1, day)),
|
|
|
|
before: new Date(Date.UTC(year, month - 1, day + 1)),
|
2023-11-11 00:11:35 +00:00
|
|
|
value: userId,
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
imageCount++;
|
|
|
|
pixelCount += (generation.value.info?.width ?? 0) * (generation.value.info?.height ?? 0);
|
2023-11-20 00:33:38 +00:00
|
|
|
pixelStepCount += (generation.value.info?.width ?? 0) *
|
|
|
|
(generation.value.info?.height ?? 0) *
|
|
|
|
(generation.value.info?.steps ?? 0);
|
2023-11-11 00:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
imageCount,
|
|
|
|
pixelCount,
|
2023-11-20 00:33:38 +00:00
|
|
|
pixelStepCount,
|
2023-11-11 00:11:35 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// expire in 1 minute if was calculated on the same day, otherwise 7-14 days.
|
|
|
|
expireIn: (result, year, month, day) => {
|
|
|
|
const requestDate = new UTCDateMini(year, month - 1, day);
|
|
|
|
const calculatedDate = new UTCDateMini(result.timestamp);
|
|
|
|
return isSameDay(requestDate, calculatedDate)
|
|
|
|
? minutesToMilliseconds(1)
|
|
|
|
: hoursToMilliseconds(24 * 7 + Math.random() * 24 * 7);
|
|
|
|
},
|
|
|
|
// should cache if the stats are non-zero
|
2023-11-20 00:33:38 +00:00
|
|
|
shouldCache: (result) =>
|
|
|
|
result.imageCount > 0 || result.pixelCount > 0 || result.pixelStepCount > 0,
|
2023-10-10 16:21:25 +00:00
|
|
|
},
|
|
|
|
);
|