From e7525603019b3e1202f4598c25fb5a99a9affbcd Mon Sep 17 00:00:00 2001 From: Cooper Ransom Date: Thu, 14 Mar 2024 00:01:56 -0400 Subject: [PATCH] Add account statistic to admin page --- src/pages/parts/admin/ConfigValuesPart.tsx | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/pages/parts/admin/ConfigValuesPart.tsx b/src/pages/parts/admin/ConfigValuesPart.tsx index e696f241..15b748ec 100644 --- a/src/pages/parts/admin/ConfigValuesPart.tsx +++ b/src/pages/parts/admin/ConfigValuesPart.tsx @@ -1,9 +1,25 @@ -import { ReactNode } from "react"; +import { ReactNode, useEffect, useState } from "react"; import { Divider } from "@/components/utils/Divider"; import { Heading2 } from "@/components/utils/Text"; import { conf } from "@/setup/config"; +async function getAccountNumber() { + const response = await fetch("https://backend.sudo-flix.lol/metrics"); + const text = await response.text(); + + const regex1 = + /mw_provider_hostname_count{hostname="https:\/\/sudo-flix.lol"} (\d+)/; + const match1 = text.match(regex1); + const regex2 = /mw_user_count{namespace="movie-web"} (\d+)/; + const match2 = text.match(regex2); + + if (match1 && match2) { + return match1[1] + match2[1]; + } + throw new Error("ACCOUNT_NUMBER not found"); +} + function ConfigValue(props: { name: string; children?: ReactNode }) { return ( <> @@ -17,17 +33,36 @@ function ConfigValue(props: { name: string; children?: ReactNode }) { } export function ConfigValuesPart() { + const [accountNumber, setAccountNumber] = useState(null); + const [loading, setLoading] = useState(true); const normalRouter = conf().NORMAL_ROUTER; const appVersion = conf().APP_VERSION; const backendUrl = conf().BACKEND_URL; + useEffect(() => { + getAccountNumber() + .then((number) => { + setAccountNumber(number); + setLoading(false); + }) + .catch((error) => { + console.error("Error fetching account number:", error); + setLoading(false); + }); + }, []); + + if (loading) { + return

Loading...

; + } + return ( <> - Configured values + Site Constants {normalRouter ? "Normal routing" : "Hash based routing"} v{appVersion} + {accountNumber} {backendUrl} );