From c1c78667513408cda467ae9c8a5234d48bba3c1a Mon Sep 17 00:00:00 2001 From: Cooper Ransom Date: Fri, 15 Mar 2024 13:41:15 -0400 Subject: [PATCH] Add 'Overall Views' box --- src/pages/TopFlix.tsx | 67 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/src/pages/TopFlix.tsx b/src/pages/TopFlix.tsx index 9c4466a4..e6281c2d 100644 --- a/src/pages/TopFlix.tsx +++ b/src/pages/TopFlix.tsx @@ -8,6 +8,8 @@ import { Divider } from "@/components/utils/Divider"; import { Heading1, Paragraph } from "@/components/utils/Text"; import { SubPageLayout } from "./layouts/SubPageLayout"; +// import { MediaGrid } from "@/components/media/MediaGrid" +// import { TopFlixCard } from "@/components/media/FlixCard"; function Button(props: { className: string; @@ -83,21 +85,48 @@ async function getRecentPlayedItems() { throw new Error("RECENT_PLAYED_ITEMS not found"); } +async function getTotalViews() { + const response = await fetch("https://backend.sudo-flix.lol/metrics"); + const text = await response.text(); + + // Regex to match the view counts of all shows/movies with success="true" + const regex = /mw_media_watch_count{[^}]*,success="true"} (\d+)/g; + let totalViews = 0; + let match = regex.exec(text); + + while (match !== null) { + totalViews += parseInt(match[1], 10); + match = regex.exec(text); + } + + if (totalViews > 0) { + return totalViews.toString(); + } + throw new Error("TOTAL_VIEWS not found"); +} + export function TopFlix() { const [recentPlayedItems, setRecentPlayedItems] = useState([]); + const [totalViews, setTotalViews] = useState(null); const [loading, setLoading] = useState(true); const [currentPage, setCurrentPage] = useState(1); const itemsPerPage = 10; + const maxItemsToShow = 100; // Maximum items to show + const maxPageCount = Math.ceil(maxItemsToShow / itemsPerPage); // Calculate max page count based on maxItemsToShow useEffect(() => { getRecentPlayedItems() .then((items) => { - const uniqueItems = items.filter( - (item, index, self) => - index === self.findIndex((t2) => t2.tmdbFullId === item.tmdbFullId), - ); + // Limit the items to the first 100 to ensure we don't exceed the max page count + const limitedItems = items + .slice(0, maxItemsToShow) + .filter( + (item, index, self) => + index === + self.findIndex((t2) => t2.tmdbFullId === item.tmdbFullId), + ); - setRecentPlayedItems(uniqueItems); + setRecentPlayedItems(limitedItems); }) .catch((error) => { console.error("Error fetching recent played items:", error); @@ -105,6 +134,13 @@ export function TopFlix() { .finally(() => { setLoading(false); }); + getTotalViews() + .then((views) => { + setTotalViews(views); + }) + .catch((error) => { + console.error("Error fetching total views:", error); + }); }, []); function getItemsForCurrentPage() { @@ -122,12 +158,17 @@ export function TopFlix() { return ( - + Top flix The most popular movies on sudo-flix.lol, this data is fetched from the current backend deployment. +
+
+

Overall Views: {totalViews}

+
+
@@ -137,11 +178,13 @@ export function TopFlix() { {`${item.providerId} - Views: `} {item.count} - {/* {item.title} */} ); })} -
+
- {currentPage} /{" "} - {Math.ceil(recentPlayedItems.length / itemsPerPage)} + {currentPage} / {maxPageCount}