canopy/src/schemas/statSchema.js

156 lines
No EOL
4.8 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const {mongoose} = require('mongoose');
//Local Imports
const config = require('./../../config.json');
const package = require('../../package.json');
const tokeSchema = require('./tokebot/tokeSchema');
const loggerUtils = require('./../utils/loggerUtils');
/**
* DB Schema for single document for keeping track of server stats
*/
const statSchema = new mongoose.Schema({
//This does NOT handle deleted accounts/channels. Use userModel.estimatedDocumentCount() for number of active users.
userCount: {
type: mongoose.SchemaTypes.Number,
required: true,
default: 0
},
channelCount: {
type: mongoose.SchemaTypes.Number,
required: true,
default: 0
},
launchCount: {
type: mongoose.SchemaTypes.Number,
required: true,
default: 0
},
firstLaunch: {
type: mongoose.SchemaTypes.Date,
required: true,
default: new Date()
},
version:{
type: mongoose.SchemaTypes.String,
default: "<0.2.0"
}
});
//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
*/
statSchema.statics.getStats = async function(){
//Get the first document we find
var stats = await this.findOne({});
if(stats){
//If the current listed version doesnt match current running version
if(stats.version != package.version){
//Update stat version
stats.version = package.version;
//Save stat document
await stats.save();
}
//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?)
return stats;
}else{
//Otherwise this is the first launch of the install, say hello
console.log("First launch detected! Initializing statistics document in Database!");
//create and save the statistics document
stats = await this.create({});
//Update stat version
stats.version = package.version;
//Save stat document
await stats.save();
//live up to the name of the function
return stats;
}
}
/**
* Increments Lunach count upon server launch and prints out amount of launches since server initialization
*/
statSchema.statics.incrementLaunchCount = async function(){
//get our statistics document
const stats = await this.getStats();
//increment counter and save
stats.launchCount++;
stats.save();
//Cache first launch
this.firstLaunch = stats.firstLaunch;
//print bootup message to console.
loggerUtils.welcomeWagon(stats.launchCount, stats.firstLaunch, tokeSchema.count);
}
/**
* Increments user count upon new user registration
* @returns {Number} Number of users before count was incremented
*/
statSchema.statics.incrementUserCount = async function(){
//get our statistics document
const stats = await this.getStats();
//temporarily keep old count so we can return it for the users ID
const oldCount = stats.userCount;
//increment counter and save
stats.userCount++;
stats.save();
//return the count from beggining of function for user ID
return oldCount;
}
/**
* Increments channel count upon new channel registration
* @returns {Number} Number of channels before count was incremented
*/
statSchema.statics.incrementChannelCount = async function(){
//get our statistics document
const stats = await this.getStats();
//temporarily keep old count so we can return it for the channel ID
const oldCount = stats.channelCount;
//increment counter and save
stats.channelCount++;
stats.save();
//return the count from beggining of function for channel ID
return oldCount;
}
module.exports = mongoose.model("statistics", statSchema);