From c1800334a6d9eb7075cf620d0657551b84d01042 Mon Sep 17 00:00:00 2001 From: Sanidhya Singh Date: Mon, 5 May 2025 22:56:18 +0530 Subject: [PATCH] Daily watch time changes --- backend/routes/stats.js | 3 +- public/locales/en-UK/translation.json | 2 ++ src/pages/components/statistics/chart.jsx | 34 ++++++++++++------- .../statistics/daily-play-count.jsx | 12 +++++-- src/pages/statistics.jsx | 13 ++++--- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/backend/routes/stats.js b/backend/routes/stats.js index a727991..2cfe320 100644 --- a/backend/routes/stats.js +++ b/backend/routes/stats.js @@ -423,6 +423,7 @@ router.get("/getViewsOverTime", async (req, res) => { stats.forEach((item) => { const library = item.Library; const count = item.Count; + const watchTime = item.TotalTime; const date = new Date(item.Date).toLocaleDateString("en-US", { year: "numeric", month: "short", @@ -435,7 +436,7 @@ router.get("/getViewsOverTime", async (req, res) => { }; } - reorganizedData[date] = { ...reorganizedData[date], [library]: count }; + reorganizedData[date] = { ...reorganizedData[date], [library]: { count, watchTime } }; }); const finalData = { libraries: libraries, stats: Object.values(reorganizedData) }; res.send(finalData); diff --git a/public/locales/en-UK/translation.json b/public/locales/en-UK/translation.json index bd048d4..2d38e64 100644 --- a/public/locales/en-UK/translation.json +++ b/public/locales/en-UK/translation.json @@ -167,7 +167,9 @@ "STAT_PAGE": { "STATISTICS": "Statistics", "DAILY_PLAY_PER_LIBRARY": "Daily Play Count Per Library", + "DAILY_TIME_PER_LIBRARY": "Daily Watch Time Per Library", "PLAY_COUNT_BY": "Play Count By", + "PLAY_TIME_BY": "Play Time By", "COUNT_VIEW": "Total Count", "TIME_VIEW": "Total Time" }, diff --git a/src/pages/components/statistics/chart.jsx b/src/pages/components/statistics/chart.jsx index 7204f0b..1ba9762 100644 --- a/src/pages/components/statistics/chart.jsx +++ b/src/pages/components/statistics/chart.jsx @@ -1,6 +1,6 @@ import { ResponsiveContainer, AreaChart, Area, XAxis, YAxis, Tooltip, Legend } from "recharts"; -function Chart({ stats, libraries }) { +function Chart({ stats, libraries, viewName }) { const colors = [ "rgb(54, 162, 235)", // blue "rgb(255, 99, 132)", // pink @@ -24,13 +24,25 @@ function Chart({ stats, libraries }) { "rgb(147, 112, 219)", // medium purple ]; + const flattenedStats = stats.map(item => { + const flatItem = { Key: item.Key }; + for (const [libraryName, data] of Object.entries(item)) { + if (libraryName === "Key") continue; + flatItem[libraryName] = data[viewName] ?? 0; + } + return flatItem; + }); + const CustomTooltip = ({ payload, label, active }) => { if (active) { return (

{label}

{libraries.map((library, index) => ( -

{`${library.Name} : ${payload[index].value} Views`}

+ //

{`${library.Name} : ${payload[index].value} Views`}

+

+ {`${library.Name} : ${payload?.find(p => p.dataKey === library.Name).value} ${viewName === "count" ? "Views" : "Minutes"}`} +

))}
); @@ -41,16 +53,14 @@ function Chart({ stats, libraries }) { const getMaxValue = () => { let max = 0; - if (stats) { - stats.forEach((datum) => { - Object.keys(datum).forEach((key) => { - if (key !== "Key") { - max = Math.max(max, parseInt(datum[key])); - } - }); + flattenedStats.forEach(datum => { + libraries.forEach(library => { + const value = parseFloat(datum[library.Name]); + if (!isNaN(value)) { + max = Math.max(max, value); + } }); - } - + }); return max; }; @@ -58,7 +68,7 @@ function Chart({ stats, libraries }) { return ( - + {libraries.map((library, index) => ( diff --git a/src/pages/components/statistics/daily-play-count.jsx b/src/pages/components/statistics/daily-play-count.jsx index e7b9316..a79f358 100644 --- a/src/pages/components/statistics/daily-play-count.jsx +++ b/src/pages/components/statistics/daily-play-count.jsx @@ -10,6 +10,7 @@ function DailyPlayStats(props) { const [stats, setStats] = useState(); const [libraries, setLibraries] = useState(); const [days, setDays] = useState(20); + const [viewName, setViewName] = useState("viewCount"); const token = localStorage.getItem("token"); @@ -45,6 +46,9 @@ function DailyPlayStats(props) { setDays(props.days); fetchLibraries(); } + if (props.viewName !== viewName) { + setViewName(props.viewName); + } const intervalId = setInterval(fetchLibraries, 60000 * 5); return () => clearInterval(intervalId); @@ -54,10 +58,12 @@ function DailyPlayStats(props) { return <>; } + const titleKey = viewName === "count" ? "STAT_PAGE.DAILY_PLAY_PER_LIBRARY" : "STAT_PAGE.DAILY_TIME_PER_LIBRARY"; + if (stats.length === 0) { return (
-

- {days} 1 ? 'S':''}`}/>

+

- {days} 1 ? 'S':''}`}/>

@@ -65,10 +71,10 @@ function DailyPlayStats(props) { } return (
-

- {days} 1 ? 'S':''}`}/>

+

- {days} 1 ? 'S':''}`}/>

- +
); diff --git a/src/pages/statistics.jsx b/src/pages/statistics.jsx index 1e38913..a81cb50 100644 --- a/src/pages/statistics.jsx +++ b/src/pages/statistics.jsx @@ -56,7 +56,6 @@ function Statistics() { activeKey={activeTab} onSelect={setTab} variant="pills" - className="custom-tabs" > } /> } /> @@ -77,20 +76,20 @@ function Statistics() { {activeTab === "tabCount" && ( <> - +
- - + +
)} {activeTab === "tabTime" && ( <> - +
- - + +
)}