Moved toke log to it's own DB collection w/ cached statistics. Tokebot statistics page-load time decreased by up to 20-30x
This commit is contained in:
parent
42ad17072f
commit
a1b602efdb
7 changed files with 281 additions and 189 deletions
|
|
@ -44,22 +44,16 @@ const statSchema = new mongoose.Schema({
|
|||
type: mongoose.SchemaTypes.Date,
|
||||
required: true,
|
||||
default: new Date()
|
||||
},
|
||||
tokes: [{
|
||||
toke: {
|
||||
type: mongoose.SchemaTypes.Map,
|
||||
required: true,
|
||||
default: new Map()
|
||||
},
|
||||
date: {
|
||||
type: mongoose.SchemaTypes.Date,
|
||||
required: true,
|
||||
default: new Date()
|
||||
}
|
||||
}]
|
||||
}
|
||||
});
|
||||
|
||||
//statics
|
||||
|
||||
/**
|
||||
* Set placeholder variable to hold cached firstLaunch date from stat document
|
||||
*/
|
||||
statSchema.statics.firstLaunch = null;
|
||||
|
||||
/**
|
||||
* Get's servers sole stat document from the DB
|
||||
* @returns {Mongoose.Document} Server's sole statistics document
|
||||
|
|
@ -67,7 +61,7 @@ const statSchema = new mongoose.Schema({
|
|||
statSchema.statics.getStats = async function(){
|
||||
//Get the first document we find
|
||||
var stats = await this.findOne({});
|
||||
|
||||
|
||||
if(stats){
|
||||
//If we found something then the statistics document exist and this is it,
|
||||
//So long as no one else has fucked with the database it should be the only one. (is this forshadowing for a future bug?)
|
||||
|
|
@ -96,6 +90,9 @@ statSchema.statics.incrementLaunchCount = async function(){
|
|||
stats.launchCount++;
|
||||
stats.save();
|
||||
|
||||
//Cache first launch
|
||||
this.firstLaunch = stats.firstLaunch;
|
||||
|
||||
//print bootup message to console.
|
||||
console.log(`${config.instanceName}(Powered by Canopy) initialized. This server has booted ${stats.launchCount} time${stats.launchCount == 1 ? '' : 's'}.`)
|
||||
console.log(`First booted on ${stats.firstLaunch}.`);
|
||||
|
|
@ -137,147 +134,4 @@ statSchema.statics.incrementChannelCount = async function(){
|
|||
return oldCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tattoo's toke to the server statistics document
|
||||
* @param {Map} toke - Tokemap handed down from Tokebot
|
||||
*/
|
||||
statSchema.statics.tattooToke = async function(toke){
|
||||
//Get the statistics document
|
||||
const stats = await this.getStats();
|
||||
|
||||
//Add the toke to the stat document
|
||||
stats.tokes.push({toke});
|
||||
|
||||
//Save the stat document
|
||||
await stats.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ingests legacy tokes handed over by the migration model
|
||||
* @param {Array} rawLegacyTokes - List of strings containing contents of legacy cytube/fore.st toke logs
|
||||
*/
|
||||
statSchema.statics.ingestLegacyTokes = async function(rawLegacyTokes){
|
||||
//If migration is disabled
|
||||
if(!config.migrate){
|
||||
//BAIL!
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
const statDB = await this.getStats();
|
||||
|
||||
//For each toke log
|
||||
for(const tokeLog of rawLegacyTokes){
|
||||
//Split and iterate toke log by new line
|
||||
for(const tokeLine of tokeLog.split('\n')){
|
||||
//Ensure line is a valid toke log line (this will break if your tokes take place after 12:46:40PM on Nov 20th 2286... Or before 21:46:40 Sep 08 2001)
|
||||
//You'll probably want to have migrated from cytube/fore.st to canopy by then :)
|
||||
//Also splits tokers array off for easier processing
|
||||
const splitToke = tokeLine.match(/^\[.+\]|,[0-9]{1,4},|[0-9]{13}$/g)
|
||||
if(splitToke != null){
|
||||
|
||||
//Create empty tokers map
|
||||
const toke = new Map();
|
||||
|
||||
//Add qoutes around strings in the tokers line
|
||||
let tokersLine = splitToke[0].replaceAll('[', '["');
|
||||
tokersLine = tokersLine.replaceAll(']','"]');
|
||||
tokersLine = tokersLine.replaceAll(',','","');
|
||||
|
||||
//Force feed doctored line into the JSON parser, and iterate by the array it shits out
|
||||
for(const toker of JSON.parse(tokersLine)){
|
||||
toke.set(toker,"Legacy Tokes");
|
||||
}
|
||||
|
||||
const date = new Date(Number(splitToke[2]));
|
||||
|
||||
//Push toke on to statDB
|
||||
statDB.tokes.push({
|
||||
toke,
|
||||
date
|
||||
});
|
||||
|
||||
console.log(`Adding legacy toke: ${tokersLine} from: ${date.toLocaleString()}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Save toke to file
|
||||
await statDB.save();
|
||||
|
||||
console.log("Legacy tokes commited to server-wide database statistics file!");
|
||||
}catch(err){
|
||||
return loggerutils.localexceptionhandler(err);
|
||||
}
|
||||
}
|
||||
|
||||
statSchema.statics.dropLegacyTokes = async function(){
|
||||
try{
|
||||
//If legacy toke dropping is disabled or migration is enabled
|
||||
if(!config.dropLegacyTokes || config.migrate){
|
||||
//return
|
||||
return;
|
||||
}
|
||||
|
||||
//pull stat doc
|
||||
const statDB = await this.getStats();
|
||||
|
||||
//Create temporary toke array
|
||||
const tokes = [];
|
||||
|
||||
//Iterate through server toke history
|
||||
for(const toke of statDB.tokes){
|
||||
//If it's not a legacy toke
|
||||
if(Array.from(toke.toke)[0][1] != "Legacy Tokes"){
|
||||
//Add it to the temp array
|
||||
tokes.push(toke);
|
||||
}
|
||||
}
|
||||
|
||||
//Replace the server-wide toke log with our newly doctored one
|
||||
statDB.tokes = tokes;
|
||||
|
||||
//Save the stat document
|
||||
await statDB.save();
|
||||
|
||||
//Tell of our success
|
||||
console.log("Removed migration tokes!");
|
||||
}catch(err){
|
||||
return loggerutils.localexceptionhandler(err);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Methods
|
||||
|
||||
/**
|
||||
* Gets toke counts for each individual callout in a map
|
||||
* @returns {Map} Map of toke counts for each individual callout registered to the server
|
||||
*/
|
||||
statSchema.methods.calculateTokeCommandCounts = async function(){
|
||||
//Create empty map to hold toke command counts
|
||||
const count = new Map();
|
||||
|
||||
//for each toke
|
||||
this.tokes.forEach((toke) => {
|
||||
//For each toke command called in the current toke
|
||||
toke.toke.forEach((command) => {
|
||||
//Get the current count for the current command
|
||||
var curCount = count.get(command);
|
||||
|
||||
//if the current count is null
|
||||
if(curCount == null){
|
||||
//Set it to one
|
||||
count.set(command, 1);
|
||||
}else{
|
||||
//Set it to ++curCount
|
||||
count.set(command, curCount + 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//return the toke command count
|
||||
return count;
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("statistics", statSchema);
|
||||
Loading…
Add table
Add a link
Reference in a new issue