diff --git a/app/public-area/pages/open-metrics.tsx b/app/public-area/pages/open-metrics.tsx
index 433cbaa..19f9c21 100644
--- a/app/public-area/pages/open-metrics.tsx
+++ b/app/public-area/pages/open-metrics.tsx
@@ -1,9 +1,20 @@
import type { BlitzPage } from "blitz";
-import { Head } from "blitz";
+import { Head, useQuery } from "blitz";
+
+import getMetrics from "../queries/get-metrics";
import Header from "../components/header";
+const initialData = {
+ phoneNumbers: 0,
+ smsExchanged: 0,
+ minutesCalled: 0,
+};
+
const OpenMetrics: BlitzPage = () => {
+ const [metrics] = useQuery(getMetrics, {}, { suspense: false, initialData });
+ const { phoneNumbers, smsExchanged, minutesCalled } = metrics ?? initialData;
+
return (
<>
@@ -29,9 +40,9 @@ const OpenMetrics: BlitzPage = () => {
diff --git a/app/public-area/queries/get-metrics.ts b/app/public-area/queries/get-metrics.ts
new file mode 100644
index 0000000..7c9111e
--- /dev/null
+++ b/app/public-area/queries/get-metrics.ts
@@ -0,0 +1,22 @@
+import { resolver } from "blitz";
+import db from "../../../db";
+
+export default resolver.pipe(async () => {
+ const phoneNumbers = await db.phoneNumber.count();
+ const smsExchanged = await db.message.count();
+ const allPhoneCalls = await db.phoneCall.findMany();
+ const secondsCalled = allPhoneCalls.reduce((minutes, phoneCall) => {
+ if (!phoneCall.duration) {
+ return minutes;
+ }
+
+ return minutes + Number.parseInt(phoneCall.duration);
+ }, 0);
+ const minutesCalled = Math.round(secondsCalled / 60);
+
+ return {
+ phoneNumbers,
+ smsExchanged,
+ minutesCalled,
+ };
+});