aboutsummaryrefslogtreecommitdiff
path: root/web/ui/src/lib/server/usage.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/ui/src/lib/server/usage.ts')
-rw-r--r--web/ui/src/lib/server/usage.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/web/ui/src/lib/server/usage.ts b/web/ui/src/lib/server/usage.ts
new file mode 100644
index 0000000..721cc3c
--- /dev/null
+++ b/web/ui/src/lib/server/usage.ts
@@ -0,0 +1,33 @@
+import { queries } from './db';
+
+export function startOfCurrentMonth(): number {
+ const d = new Date();
+ d.setUTCDate(1);
+ d.setUTCHours(0, 0, 0, 0);
+ return d.getTime();
+}
+
+export function usageCountThisMonth(accountId: string): number {
+ return queries.usageCountSince.get(accountId, startOfCurrentMonth())?.c ?? 0;
+}
+
+export function usageByDataset(
+ accountId: string
+): Array<{ dataset: string; count: number }> {
+ const rows = queries.usageByDataset.all(accountId, startOfCurrentMonth());
+ return rows.map((r: { dataset: string; c: number }) => ({
+ dataset: r.dataset,
+ count: r.c
+ }));
+}
+
+export function usageByKey(
+ accountId: string
+): Array<{ keyId: string; name: string; count: number }> {
+ const rows = queries.usageByKey.all(startOfCurrentMonth(), accountId);
+ return rows.map((r: { key_id: string; name: string; c: number }) => ({
+ keyId: r.key_id,
+ name: r.name,
+ count: r.c
+ }));
+}