Added categoryStats for getTopUsers

This commit is contained in:
Nanobyte 2020-07-14 18:15:23 +02:00
parent da9ab111bc
commit c4f9515c09

View file

@ -2,6 +2,7 @@ var db = require('../databases/databases.js').db;
module.exports = function getTopUsers (req, res) {
let sortType = req.query.sortType;
let categoryStatsEnabled = req.query.categoryStats == undefined ? false : true;
if (sortType == undefined) {
//invalid request
@ -27,9 +28,21 @@ module.exports = function getTopUsers (req, res) {
let viewCounts = [];
let totalSubmissions = [];
let minutesSaved = [];
let categoryStats = [];
let additionalFields = '';
if (categoryStatsEnabled) {
additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " +
"SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " +
"SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " +
"SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " +
"SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " +
"SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, ";
}
let rows = db.prepare("SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," +
"SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " +
additionalFields +
"IFNULL(userNames.userName, sponsorTimes.userID) as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID " +
"WHERE sponsorTimes.votes > -1 AND sponsorTimes.shadowHidden != 1 GROUP BY IFNULL(userName, sponsorTimes.userID) ORDER BY " + sortBy + " DESC LIMIT 100").all();
@ -39,6 +52,16 @@ module.exports = function getTopUsers (req, res) {
viewCounts[i] = rows[i].viewCount;
totalSubmissions[i] = rows[i].totalSubmissions;
minutesSaved[i] = rows[i].minutesSaved;
if (categoryStatsEnabled) {
categoryStats[i] = [
rows[i].categorySponsor,
rows[i].categorySumInteraction,
rows[i].categorySumIntro,
rows[i].categorySumOutro,
rows[i].categorySelfpromo,
rows[i].categoryMusicOfftopic,
];
}
}
//send this result
@ -46,6 +69,7 @@ module.exports = function getTopUsers (req, res) {
userNames: userNames,
viewCounts: viewCounts,
totalSubmissions: totalSubmissions,
minutesSaved: minutesSaved
minutesSaved: minutesSaved,
categoryStats: categoryStats
});
}