feat: move perf,logs and user into sqlite
This commit is contained in:
37
lib/cache/logs.ts
vendored
37
lib/cache/logs.ts
vendored
@ -1,37 +0,0 @@
|
||||
import { createLogger } from "@lib/log.ts";
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { getTimeCacheKey, parseTimeCacheKey } from "@lib/string.ts";
|
||||
|
||||
const log = createLogger("");
|
||||
log.addEventListener("log", (data) => {
|
||||
cache.set(`log:${getTimeCacheKey()}`, JSON.stringify(data.detail), {
|
||||
noLog: true,
|
||||
});
|
||||
});
|
||||
|
||||
export type Log = {
|
||||
scope: string;
|
||||
level: number;
|
||||
date: Date;
|
||||
args: unknown[];
|
||||
};
|
||||
|
||||
export async function getLogs() {
|
||||
const d = new Date();
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth().toString();
|
||||
const day = d.getDate().toString();
|
||||
|
||||
const keys = await cache.keys(
|
||||
`log:${year}:${month}:${day}:*`,
|
||||
);
|
||||
|
||||
const logs = await Promise.all(
|
||||
keys.map(async (key) => {
|
||||
const date = parseTimeCacheKey(key);
|
||||
return { ...JSON.parse(await cache.get<string>(key)), date } as Log;
|
||||
}),
|
||||
);
|
||||
|
||||
return logs.sort((a, b) => a.date.getTime() > b.date.getTime() ? -1 : 1);
|
||||
}
|
83
lib/cache/performance.ts
vendored
83
lib/cache/performance.ts
vendored
@ -1,83 +0,0 @@
|
||||
import * as cache from "@lib/cache/cache.ts";
|
||||
import { getTimeCacheKey } from "@lib/string.ts";
|
||||
|
||||
export type PerformancePoint = {
|
||||
path: string;
|
||||
search: string;
|
||||
time: number;
|
||||
date: Date;
|
||||
};
|
||||
|
||||
export type PerformanceRes = {
|
||||
max: number;
|
||||
res: {
|
||||
url: string;
|
||||
data: readonly [number, number, number, number];
|
||||
}[];
|
||||
};
|
||||
|
||||
export const savePerformance = (url: string, milliseconds: number) => {
|
||||
const cacheKey = getTimeCacheKey();
|
||||
|
||||
const u = new URL(url);
|
||||
if (u.pathname.includes("_frsh/")) return;
|
||||
u.searchParams.delete("__frsh_c");
|
||||
|
||||
cache.set(
|
||||
`performance:${cacheKey}`,
|
||||
JSON.stringify({
|
||||
path: decodeURIComponent(u.pathname),
|
||||
search: u.search,
|
||||
time: Math.floor(milliseconds * 1000),
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
export async function getPerformances(): Promise<PerformanceRes> {
|
||||
const d = new Date();
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth().toString();
|
||||
const day = d.getDay().toString();
|
||||
|
||||
const keys = await cache.keys(
|
||||
`performance:${year}:${month}:${day}:*`,
|
||||
);
|
||||
|
||||
console.log(`performance:${year}:${month}:${day}:*`);
|
||||
|
||||
const performances = await Promise.all(
|
||||
keys.map(async (key) =>
|
||||
JSON.parse(await cache.get<string>(key)) as PerformancePoint
|
||||
),
|
||||
);
|
||||
|
||||
let maximum = 0;
|
||||
|
||||
const res = new Map<string, number[]>();
|
||||
for (const p of performances) {
|
||||
const id = p.path;
|
||||
const timings = res.get(id) || [];
|
||||
if (p.time > maximum) maximum = p.time;
|
||||
timings.push(p.time);
|
||||
res.set(id, timings);
|
||||
}
|
||||
|
||||
return {
|
||||
max: maximum,
|
||||
res: [...res.entries()].map(([url, timings]) => {
|
||||
let total = 0;
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
for (const t of timings) {
|
||||
total += t;
|
||||
if (t < min) min = t;
|
||||
if (t > max) max = t;
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
data: [timings.length, min, total / timings.length, max] as const,
|
||||
};
|
||||
}).sort((a, b) => a.data[2] < b.data[2] ? 1 : -1),
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user