Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr SMS Gateway8 * File : backend/src/analytics/usage.ts9 * Module : analytics10 * Repository : https://git.spboucher.ai/maclustr-sms11 * Project Website : https://sms.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { sql, type Tx } from "../db.ts"4344export type Metric = "outbound_created" | "outbound_segments" | "submitted" | "delivered" | "failed" | "expired" | "unknown" | "cancelled" | "inbound"4546export async function bump(tx: Tx, metric: Metric, applicationId: string | null, simId: string | null, n = 1) {47 await tx`insert into usage_metrics(day, application_id, sim_id, metric, value)48 values ((now() at time zone 'utc')::date, ${applicationId ?? ""}, ${simId ?? ""}, ${metric}, ${n})49 on conflict (day, application_id, sim_id, metric) do update set value = usage_metrics.value + excluded.value`50}5152export async function usage(opts: { applicationId?: string | null; from?: string; to?: string }) {53 const from = opts.from ?? new Date(Date.now() - 29 * 86400_000).toISOString().slice(0, 10)54 const to = opts.to ?? new Date().toISOString().slice(0, 10)55 const app = opts.applicationId56 const rows = await sql`57 select day::text as day, metric, sum(value)::int as value from usage_metrics58 where day between ${from} and ${to} ${app ? sql`and application_id = ${app}` : sql``}59 group by day, metric order by day`60 const days: Record<string, Record<string, number>> = {}61 const totals: Record<string, number> = {}62 for (const r of rows) { (days[r.day] ??= {})[r.metric] = r.value; totals[r.metric] = (totals[r.metric] ?? 0) + r.value }63 return { from, to, totals, days: Object.entries(days).map(([day, m]) => ({ day, ...m })) }64}6566/** Compteurs du jour (UTC) pour les quotas. */67export async function sentToday(tx: Tx, by: { applicationId?: string; simId?: string }): Promise<number> {68 const [r] = await tx`69 select coalesce(sum(value), 0)::int as n from usage_metrics70 where day = (now() at time zone 'utc')::date and metric = 'outbound_created'71 ${by.applicationId ? tx`and application_id = ${by.applicationId}` : tx``}72 ${by.simId ? tx`and sim_id = ${by.simId}` : tx``}`73 return r.n74}75