1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-29 16:07:40 +01:00

Add recently played list! (this could be the stepping stone for a basic recommendation algo)

This commit is contained in:
Cooper Ransom 2024-03-14 14:00:47 -04:00
parent e752560301
commit 44ad56d654
2 changed files with 117 additions and 17 deletions

View file

@ -1,5 +1,8 @@
import { ReactNode, useEffect, useState } from "react";
import { ThinContainer } from "@/components/layout/ThinContainer"; import { ThinContainer } from "@/components/layout/ThinContainer";
import { Heading1, Paragraph } from "@/components/utils/Text"; import { Divider } from "@/components/utils/Divider";
import { Heading1, Heading2, Paragraph } from "@/components/utils/Text";
import { SubPageLayout } from "@/pages/layouts/SubPageLayout"; import { SubPageLayout } from "@/pages/layouts/SubPageLayout";
import { ConfigValuesPart } from "@/pages/parts/admin/ConfigValuesPart"; import { ConfigValuesPart } from "@/pages/parts/admin/ConfigValuesPart";
import { TMDBTestPart } from "@/pages/parts/admin/TMDBTestPart"; import { TMDBTestPart } from "@/pages/parts/admin/TMDBTestPart";
@ -7,7 +10,69 @@ import { WorkerTestPart } from "@/pages/parts/admin/WorkerTestPart";
import { BackendTestPart } from "../parts/admin/BackendTestPart"; import { BackendTestPart } from "../parts/admin/BackendTestPart";
function ConfigValue(props: { name: string; children?: ReactNode }) {
return (
<>
<div className="flex">
<p className="flex-1 font-bold text-white pr-5">{props.name}</p>
<p>{props.children}</p>
</div>
<Divider marginClass="my-3" />
</>
);
}
async function getRecentPlayedItems() {
const response = await fetch("https://backend.sudo-flix.lol/metrics");
const text = await response.text();
const regex =
/mw_media_watch_count{tmdb_full_id="([^"]+)",provider_id="([^"]+)",title="([^"]+)",success="([^"]+)"} (\d+)/g;
let match;
const loop = true;
const items = [];
while (loop) {
match = regex.exec(text);
if (match === null) break;
const [_, tmdbFullId, providerId, title, success, count] = match;
items.push({
tmdbFullId,
providerId,
title,
success: success === "true",
count: parseInt(count, 10),
});
}
if (items.length > 0) {
return items;
}
throw new Error("RECENT_PLAYED_ITEMS not found");
}
export function AdminPage() { export function AdminPage() {
const [recentPlayedItems, setRecentPlayedItems] = useState<any[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
getRecentPlayedItems()
.then((items) => {
setRecentPlayedItems(items);
})
.catch((error) => {
console.error("Error fetching recent played items:", error);
})
.finally(() => {
setLoading(false);
});
}, []);
if (loading) {
return <p>Loading...</p>;
}
return ( return (
<SubPageLayout> <SubPageLayout>
<ThinContainer> <ThinContainer>
@ -18,6 +83,20 @@ export function AdminPage() {
<BackendTestPart /> <BackendTestPart />
<WorkerTestPart /> <WorkerTestPart />
<TMDBTestPart /> <TMDBTestPart />
<div className="mt-8 w-full max-w-none">
<Heading2 className="mb-8">Recently Played List</Heading2>
<p className="mb-8">
This data is fetched from the current backend deployment.
</p>
{recentPlayedItems.map((item) => {
const successText = item.success ? "Yes" : "No"; // Convert bool to "Yes" or "No"
return (
<ConfigValue key={item.tmdbFullId} name={item.title}>
{`${item.providerId} - Provided: ${successText}, Views: ${item.count}`}
</ConfigValue>
);
})}
</div>
</ThinContainer> </ThinContainer>
</SubPageLayout> </SubPageLayout>
); );

View file

@ -8,23 +8,41 @@ async function getAccountNumber() {
const response = await fetch("https://backend.sudo-flix.lol/metrics"); const response = await fetch("https://backend.sudo-flix.lol/metrics");
const text = await response.text(); const text = await response.text();
const regex1 = // Adjusted regex to match any hostname
/mw_provider_hostname_count{hostname="https:\/\/sudo-flix.lol"} (\d+)/; const regex =
const match1 = text.match(regex1); /mw_provider_hostname_count{hostname="https?:\/\/[^"}]+"} (\d+)/g;
const regex2 = /mw_user_count{namespace="movie-web"} (\d+)/; let total = 0;
const match2 = text.match(regex2); let match = regex.exec(text); // Initial assignment outside the loop
if (match1 && match2) { while (match !== null) {
return match1[1] + match2[1]; total += parseInt(match[1], 10);
match = regex.exec(text); // Update the assignment at the end of the loop body
}
if (total > 0) {
return total.toString();
} }
throw new Error("ACCOUNT_NUMBER not found"); throw new Error("ACCOUNT_NUMBER not found");
} }
async function getAllAccounts() {
const response = await fetch("https://backend.sudo-flix.lol/metrics");
const text = await response.text();
const regex = /mw_user_count{namespace="movie-web"} (\d+)/;
const match = text.match(regex);
if (match) {
return match[1];
}
throw new Error("USER_COUNT not found");
}
function ConfigValue(props: { name: string; children?: ReactNode }) { function ConfigValue(props: { name: string; children?: ReactNode }) {
return ( return (
<> <>
<div className="flex"> <div className="flex">
<p className="flex-1 font-bold text-white">{props.name}</p> <p className="flex-1 font-bold text-white pr-5">{props.name}</p>
<p>{props.children}</p> <p>{props.children}</p>
</div> </div>
<Divider marginClass="my-3" /> <Divider marginClass="my-3" />
@ -34,7 +52,7 @@ function ConfigValue(props: { name: string; children?: ReactNode }) {
export function ConfigValuesPart() { export function ConfigValuesPart() {
const [accountNumber, setAccountNumber] = useState<string | null>(null); const [accountNumber, setAccountNumber] = useState<string | null>(null);
const [loading, setLoading] = useState(true); const [allAccounts, setAllAccounts] = useState<string | null>(null);
const normalRouter = conf().NORMAL_ROUTER; const normalRouter = conf().NORMAL_ROUTER;
const appVersion = conf().APP_VERSION; const appVersion = conf().APP_VERSION;
const backendUrl = conf().BACKEND_URL; const backendUrl = conf().BACKEND_URL;
@ -43,18 +61,20 @@ export function ConfigValuesPart() {
getAccountNumber() getAccountNumber()
.then((number) => { .then((number) => {
setAccountNumber(number); setAccountNumber(number);
setLoading(false);
}) })
.catch((error) => { .catch((error) => {
console.error("Error fetching account number:", error); console.error("Error fetching account number:", error);
setLoading(false); });
getAllAccounts()
.then((accounts) => {
setAllAccounts(accounts);
})
.catch((error) => {
console.error("Error fetching all accounts:", error);
}); });
}, []); }, []);
if (loading) {
return <p>Loading...</p>;
}
return ( return (
<> <>
<Heading2 className="mb-8 mt-12">Site Constants</Heading2> <Heading2 className="mb-8 mt-12">Site Constants</Heading2>
@ -62,7 +82,8 @@ export function ConfigValuesPart() {
{normalRouter ? "Normal routing" : "Hash based routing"} {normalRouter ? "Normal routing" : "Hash based routing"}
</ConfigValue> </ConfigValue>
<ConfigValue name="Application version">v{appVersion}</ConfigValue> <ConfigValue name="Application version">v{appVersion}</ConfigValue>
<ConfigValue name="Backend Accounts">{accountNumber}</ConfigValue> <ConfigValue name="Backend requests">{accountNumber}</ConfigValue>
<ConfigValue name="Total User Accounts">{allAccounts}</ConfigValue>
<ConfigValue name="Backend URL">{backendUrl}</ConfigValue> <ConfigValue name="Backend URL">{backendUrl}</ConfigValue>
</> </>
); );