Bunch small things: Notifs for react, subsplash...

This commit is contained in:
aeroreyna
2022-12-28 22:54:19 -05:00
parent 6b6f263821
commit 1f3cc06470
10 changed files with 686 additions and 295 deletions

103
dbTools/logger.js Normal file
View File

@@ -0,0 +1,103 @@
const hash = require('object-hash');
const DBName = "EMI_SOCIAL";
postDB = (DB) => {
DB.pathLogs = DB.db.db(DBName).collection("pathLogs");
DB.profileLogs = DB.db.db(DBName).collection("profileLogs");
let loggerPathData = {};
let loggerProfileData = {};
DB.logger = async (req, res, next) => {
// hash should not include query parameters
let hashIndx = hash(req.url.split('/'));
// Log url based
if (!loggerPathData[hashIndx]) {
loggerPathData[hashIndx] = {
url: req.url,
urlParts: req.url.split('/'),
count: 1
}
} else {
loggerPathData[hashIndx].count++;
}
// Log user based
let profile_id = req.cookies?.profile_id;
if (!profile_id) return next();
if (!loggerProfileData[profile_id]) {
loggerProfileData[profile_id] = {}
}
if (!loggerProfileData[profile_id][hashIndx]) {
loggerProfileData[profile_id][hashIndx] = {
url: req.url,
urlParts: req.url.split('/'),
count: 1
}
} else {
loggerProfileData[profile_id][hashIndx].count++;
}
//console.log(loggerData);
next();
};
// Update counters in DB
setInterval(async () => {
// Restart global counters
let localPathData = loggerPathData;
loggerPathData = {};
let localProfileData = loggerProfileData;
loggerProfileData = {};
return 0;
console.log('updating', localPathData);
Object.keys(localPathData).forEach(key => {
DB.pathLogs.findOne({ hash: key }).then(async (doc) => {
if (doc) {
//Update
const update = {
$inc: {
count: localPathData[key].count
}
};
console.log('updating one ' + key)
await DB.pathLogs.updateOne({ hash: key }, update);
} else {
//Insert
const update = localPathData[key];
update.hash = key;
console.log('inserting one ' + key)
await DB.pathLogs.insertOne(update);
}
}).catch(console.error);
});
//DB.pathLogs.
Object.keys(localProfileData).forEach(key => {
DB.profileLogs.findOne({profile_id: key}).then(async (doc) => {
if (doc) {
//Update for user log
const update = {
$inc: {
//count: localPathData[key].count
}
};
loggerProfileData[key].forEach((hashIndx)=>{
//each hash has its counter.
update.$inc['hashIndx'] = {
count: loggerProfileData[key]['hashIndx'].count
};
});
console.log('updating one user' + key)
await DB.profileLogs.updateOne({ profile_id: key }, update);
} else {
//Insert
const update = loggerProfileData[key];
update.profile_id = key;
console.log('inserting one user' + key)
await DB.profileLogs.insertOne(update);
}
});
});
}, 1000 * 60 * 1); //Each 10 mins
}
module.exports = postDB;