Files
Showy/backend/controllers/statisticsController.js
T
Mohammed Abad ae67663623 feat(dashboard): add recharts data visualizations
- Install recharts
- Add WatchActivityChart (AreaChart, movies + episodes/month, year filter)
- Add WatchTimeChart (stacked BarChart, hours/month, synced year filter)
- Add RatingsChart (BarChart, user ratings distribution 1–10, color ramp)
- Add TopGenresChart (horizontal BarChart, top 10 genres, type filter)
- Add CouchDayChart (BarChart, watch count by weekday, peak day highlight)
- Add GET /statistics/watch-weekday backend endpoint (DAYOFWEEK aggregation)
- Extend statisticsApi with getYearlyWatchCount, getYearlyWatchTime, getWatchdayDistribution
- Add useYearlyWatchCount, useYearlyWatchTime, useWatchdayDistribution hooks
- Wire all charts into DashboardPage with responsive two-column grid layout
2026-04-01 16:04:43 +01:00

152 lines
4.5 KiB
JavaScript

const sequelize = require('../config/database');
const { Op } = require('sequelize');
const Entry = require('../models/Entry');
const EntrySeen = require('../models/EntrySeen');
const Episode = require('../models/Episode');
const EpisodeSeen = require('../models/EpisodeSeen');
class StatisticsController {
constructor() {
if (!StatisticsController.instance) {
StatisticsController.instance = this;
}
return StatisticsController.instance;
}
async getAllTimeWatchTime(req, res) {
const totalWatchTimeSeries = await EpisodeSeen.findOne({
include: [
{ model: Episode, as: 'episode', attributes: [] }
],
attributes: [
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
});
const totalWatchTimeMovies = await EntrySeen.findOne({
include: [
{ model: Entry, as: 'entry', attributes: [] }
],
attributes: [
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
});
const totalWatchTime = {
series: totalWatchTimeSeries.dataValues.watchTime,
movies: totalWatchTimeMovies.dataValues.watchTime
}
return res.status(200).json(totalWatchTime);
}
async getYearlyWatchTime(req, res) {
const { year } = req.params;
const totalWatchTimeSeries = await EpisodeSeen.findAll({
include: [
{ model: Episode, as: 'episode', attributes: [] }
],
attributes: [
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
where: {
date_seen: {
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
}
},
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
});
const totalWatchTimeMovies = await EntrySeen.findAll({
include: [
{ model: Entry, as: 'entry', attributes: [] }
],
attributes: [
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
[sequelize.fn('SUM', sequelize.col('runtime')), 'watchTime']
],
where: {
date_seen: {
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
}
},
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
});
const totalWatchTime = {
series: totalWatchTimeSeries,
movies: totalWatchTimeMovies
}
return res.status(200).json(totalWatchTime);
}
async getYearlyWatchCountGroupByMonth(req, res) {
try {
const { year } = req.params;
const seriesCount = await EpisodeSeen.findAll({
attributes: [
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
],
where: {
date_seen: {
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
}
},
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
});
const moviesCount = await EntrySeen.findAll({
attributes: [
[sequelize.fn('MONTH', sequelize.col('date_seen')), 'month'],
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
],
where: {
date_seen: {
[Op.between]: [new Date(year, 0, 1), new Date(year, 11, 31)]
}
},
group: [sequelize.fn('MONTH', sequelize.col('date_seen'))]
});
res.status(200).json({ series: seriesCount, movies: moviesCount });
} catch (error) {
console.log(error)
res.status(500).json({ error: error.message });
}
}
async getWatchdayDistribution(req, res) {
try {
// DAYOFWEEK: 1=Sunday, 2=Monday ... 7=Saturday
const episodeDays = await EpisodeSeen.findAll({
attributes: [
[sequelize.fn('DAYOFWEEK', sequelize.col('date_seen')), 'day'],
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
],
group: [sequelize.fn('DAYOFWEEK', sequelize.col('date_seen'))]
});
const movieDays = await EntrySeen.findAll({
attributes: [
[sequelize.fn('DAYOFWEEK', sequelize.col('date_seen')), 'day'],
[sequelize.fn('COUNT', sequelize.col('*')), 'count']
],
group: [sequelize.fn('DAYOFWEEK', sequelize.col('date_seen'))]
});
res.status(200).json({ episodes: episodeDays, movies: movieDays });
} catch (error) {
console.log(error);
res.status(500).json({ error: error.message });
}
}
}
module.exports = new StatisticsController();