diff --git a/controllers/statsController.js b/controllers/statsController.js index 7df80bad..1b400e02 100644 --- a/controllers/statsController.js +++ b/controllers/statsController.js @@ -147,7 +147,7 @@ module.exports = { const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); return new Promise((resolve, reject) => { // get the raw requests data - db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) + db.getTrendingClaims(dateTime) .then(results => { resolve(results); }) @@ -157,11 +157,11 @@ module.exports = { }); }); }, - getRecentClaims (startDate) { + getRecentClaims () { logger.debug('retrieving most recent claims'); return new Promise((resolve, reject) => { // get the raw requests data - db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) + db.getRecentClaims() .then(results => { resolve(results); }) diff --git a/models/index.js b/models/index.js index ce5b27c3..8c0fbce5 100644 --- a/models/index.js +++ b/models/index.js @@ -43,6 +43,9 @@ Object.keys(db).forEach(modelName => { } }); +db.sequelize = sequelize; +db.Sequelize = Sequelize; + db['upsert'] = (Model, values, condition, tableName) => { return Model .findOne({ where: condition }) @@ -59,7 +62,12 @@ db['upsert'] = (Model, values, condition, tableName) => { }); }; -db.sequelize = sequelize; -db.Sequelize = Sequelize; +db['getTrendingClaims'] = (startDate) => { + return db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${startDate}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); +}; + +db['getRecentClaims'] = () => { + return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); +}; module.exports = db;