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}