Files
Showy/backend/controllers/statisticsController.js
T

145 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
// Only count records with EXACT accuracy
const episodeDays = await EpisodeSeen.findAll({
attributes: [
[sequelize.fn("DAYOFWEEK", sequelize.col("date_seen")), "day"],
[sequelize.fn("COUNT", sequelize.col("*")), "count"],
],
where: { accuracy: "EXACT" },
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"],
],
where: { accuracy: "EXACT" },
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();