associated the File and Request dbs
This commit is contained in:
parent
9df408690a
commit
99c7c84cfb
12 changed files with 124 additions and 120 deletions
controllers
|
@ -5,8 +5,7 @@ const db = require('../models');
|
|||
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
|
||||
|
||||
module.exports = {
|
||||
postToStats (action, url, ipAddress, name, claimId, fileName, fileType, nsfw, result) {
|
||||
logger.silly(`creating ${action} record for statistics db`);
|
||||
postToStats (action, url, ipAddress, name, claimId, result) {
|
||||
// make sure the result is a string
|
||||
if (result && (typeof result !== 'string')) {
|
||||
result = result.toString();
|
||||
|
@ -15,22 +14,30 @@ module.exports = {
|
|||
if (ipAddress && (typeof ipAddress !== 'string')) {
|
||||
ipAddress = ipAddress.toString();
|
||||
}
|
||||
// create record in the db
|
||||
db.Stats.create({
|
||||
action,
|
||||
url,
|
||||
ipAddress,
|
||||
name,
|
||||
claimId,
|
||||
fileName,
|
||||
fileType,
|
||||
nsfw,
|
||||
result,
|
||||
})
|
||||
.then()
|
||||
.catch(error => {
|
||||
logger.error('sequelize error', error);
|
||||
});
|
||||
logger.silly(name, claimId);
|
||||
db.File
|
||||
.findOne({where: { name, claimId }})
|
||||
.then(file => {
|
||||
// create record in the db
|
||||
let FileId;
|
||||
if (file) {
|
||||
FileId = file.dataValues.id;
|
||||
} else {
|
||||
FileId = null;
|
||||
}
|
||||
logger.silly('file id:', FileId);
|
||||
return db.Request
|
||||
.create({
|
||||
action,
|
||||
url,
|
||||
ipAddress,
|
||||
result,
|
||||
FileId,
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('sequelize error', error);
|
||||
});
|
||||
},
|
||||
sendGoogleAnalytics (action, headers, ip, originalUrl) {
|
||||
const visitorId = ip.replace(/\./g, '-');
|
||||
|
@ -64,10 +71,10 @@ module.exports = {
|
|||
});
|
||||
},
|
||||
getStatsSummary (startDate) {
|
||||
logger.debug('retrieving statistics');
|
||||
logger.debug('retrieving request records');
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
// get the raw statistics data
|
||||
db.Stats
|
||||
// get the raw Requests data
|
||||
db.Request
|
||||
.findAll({
|
||||
where: {
|
||||
createdAt: {
|
||||
|
@ -138,61 +145,56 @@ module.exports = {
|
|||
return deferred;
|
||||
},
|
||||
getTrendingClaims (startDate) {
|
||||
logger.debug('retrieving trending statistics');
|
||||
logger.debug('retrieving trending requests');
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
// get the raw statistics data
|
||||
db.Stats
|
||||
// get the raw requests data
|
||||
db.Request
|
||||
.findAll({
|
||||
where: {
|
||||
createdAt: {
|
||||
gt: startDate,
|
||||
},
|
||||
name: {
|
||||
not: null,
|
||||
},
|
||||
claimId: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
include: [db.File],
|
||||
})
|
||||
.then(data => {
|
||||
let resultHashTable = {};
|
||||
let sortableArray = [];
|
||||
let sortedArray;
|
||||
// summarise the data
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
let key = `${data[i].name}#${data[i].claimId}`;
|
||||
if (resultHashTable[key] === undefined) {
|
||||
resultHashTable[key] = {
|
||||
count : 0,
|
||||
details: {
|
||||
name : data[i].name,
|
||||
claimId : data[i].claimId,
|
||||
fileName: data[i].fileName,
|
||||
fileType: data[i].fileType,
|
||||
nsfw : data[i].nsfw,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
resultHashTable[key]['count'] += 1;
|
||||
}
|
||||
}
|
||||
for (let objKey in resultHashTable) {
|
||||
if (resultHashTable.hasOwnProperty(objKey)) {
|
||||
sortableArray.push([
|
||||
resultHashTable[objKey]['count'],
|
||||
resultHashTable[objKey]['details'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
sortableArray.sort((a, b) => {
|
||||
return b[0] - a[0];
|
||||
});
|
||||
sortedArray = sortableArray.map((a) => {
|
||||
return a[1];
|
||||
});
|
||||
// return results
|
||||
resolve(sortedArray);
|
||||
// let resultHashTable = {};
|
||||
// let sortableArray = [];
|
||||
// let sortedArray;
|
||||
// // summarise the data
|
||||
// for (let i = 0; i < data.length; i++) {
|
||||
// let key = data[i].fileId;
|
||||
// if (resultHashTable[key] === undefined) {
|
||||
// resultHashTable[key] = {
|
||||
// count : 0,
|
||||
// details: {
|
||||
// name : data[i].name,
|
||||
// claimId : data[i].claimId,
|
||||
// fileName: data[i].fileName,
|
||||
// fileType: data[i].fileType,
|
||||
// nsfw : data[i].nsfw,
|
||||
// },
|
||||
// };
|
||||
// } else {
|
||||
// resultHashTable[key]['count'] += 1;
|
||||
// }
|
||||
// }
|
||||
// for (let objKey in resultHashTable) {
|
||||
// if (resultHashTable.hasOwnProperty(objKey)) {
|
||||
// sortableArray.push([
|
||||
// resultHashTable[objKey]['count'],
|
||||
// resultHashTable[objKey]['details'],
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
// sortableArray.sort((a, b) => {
|
||||
// return b[0] - a[0];
|
||||
// });
|
||||
// sortedArray = sortableArray.map((a) => {
|
||||
// return a[1];
|
||||
// });
|
||||
// // return results
|
||||
resolve();
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('sequelize error', error);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue