Merge pull request #122 from ajayyy/experimental

Added option to use new user counter
This commit is contained in:
Ajay Ramachandran 2020-08-30 00:25:56 -04:00 committed by GitHub
commit ffa6b4fac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 27 deletions

View file

@ -6,6 +6,7 @@ var config = require('./config.js');
// Middleware // Middleware
var corsMiddleware = require('./middleware/cors.js'); var corsMiddleware = require('./middleware/cors.js');
var loggerMiddleware = require('./middleware/logger.js'); var loggerMiddleware = require('./middleware/logger.js');
const userCounter = require('./middleware/userCounter.js');
// Routes // Routes
var getSkipSegments = require('./routes/getSkipSegments.js').endpoint; var getSkipSegments = require('./routes/getSkipSegments.js').endpoint;
@ -31,6 +32,8 @@ app.use(corsMiddleware);
app.use(loggerMiddleware); app.use(loggerMiddleware);
app.use(express.json()) app.use(express.json())
if (config.userCounterURL) app.use(userCounter);
// Setup pretty JSON // Setup pretty JSON
if (config.mode === "development") app.set('json spaces', 2); if (config.mode === "development") app.set('json spaces', 2);

View file

@ -0,0 +1,11 @@
var request = require('request');
var config = require('../config.js');
var getIP = require('../utils/getIP.js');
const getHash = require('../utils/getHash.js');
module.exports = function userCounter(req, res, next) {
request.post(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1));
next();
}

View file

@ -1,11 +1,15 @@
var db = require('../databases/databases.js').db; const db = require('../databases/databases.js').db;
var request = require('request'); const request = require('request');
const config = require('../config.js');
// A cache of the number of chrome web store users // A cache of the number of chrome web store users
var chromeUsersCache = null; let chromeUsersCache = null;
var firefoxUsersCache = null; let firefoxUsersCache = null;
var lastUserCountCheck = 0;
// By the privacy friendly user counter
let apiUsersCache = null;
let lastUserCountCheck = 0;
module.exports = function getTotalStats (req, res) { module.exports = function getTotalStats (req, res) {
let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " + let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " +
@ -16,6 +20,7 @@ module.exports = function getTotalStats (req, res) {
res.send({ res.send({
userCount: row.userCount, userCount: row.userCount,
activeUsers: chromeUsersCache + firefoxUsersCache, activeUsers: chromeUsersCache + firefoxUsersCache,
apiUsers: apiUsersCache,
viewCount: row.viewCount, viewCount: row.viewCount,
totalSubmissions: row.totalSubmissions, totalSubmissions: row.totalSubmissions,
minutesSaved: row.minutesSaved minutesSaved: row.minutesSaved
@ -26,7 +31,18 @@ module.exports = function getTotalStats (req, res) {
if (now - lastUserCountCheck > 5000000) { if (now - lastUserCountCheck > 5000000) {
lastUserCountCheck = now; lastUserCountCheck = now;
// Get total users updateExtensionUsers();
}
}
}
function updateExtensionUsers() {
if (config.userCounterURL) {
request.get(config.userCounterURL + "/api/v1/userCount", (err, response, body) => {
apiUsersCache = JSON.parse(body).userCount;
});
}
request.get("https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/", function (err, firefoxResponse, body) { request.get("https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/", function (err, firefoxResponse, body) {
try { try {
firefoxUsersCache = parseInt(JSON.parse(body).average_daily_users); firefoxUsersCache = parseInt(JSON.parse(body).average_daily_users);
@ -49,5 +65,3 @@ module.exports = function getTotalStats (req, res) {
} }
}); });
} }
}
}