fix countcontributingusers

This commit is contained in:
Michael C
2023-02-20 22:56:38 -05:00
parent 1bda331b0c
commit 31a460e750
4 changed files with 35 additions and 28 deletions

View File

@@ -27,30 +27,32 @@ let lastFetch: DBStatsData = {
minutesSaved: 0 minutesSaved: 0
}; };
updateExtensionUsers();
export async function getTotalStats(req: Request, res: Response): Promise<void> { export async function getTotalStats(req: Request, res: Response): Promise<void> {
const row = await getStats(!!req.query.countContributingUsers); const countContributingUsers = Boolean(req.query?.countContributingUsers == "true");
const row = await getStats(countContributingUsers);
lastFetch = row; lastFetch = row;
if (row !== undefined) { if (!row) res.sendStatus(500);
const extensionUsers = chromeUsersCache + firefoxUsersCache; const extensionUsers = chromeUsersCache + firefoxUsersCache;
//send this result //send this result
res.send({ res.send({
userCount: row.userCount, userCount: row.userCount ?? 0,
activeUsers: extensionUsers, activeUsers: extensionUsers,
apiUsers: Math.max(apiUsersCache, extensionUsers), apiUsers: Math.max(apiUsersCache, extensionUsers),
viewCount: row.viewCount, viewCount: row.viewCount,
totalSubmissions: row.totalSubmissions, totalSubmissions: row.totalSubmissions,
minutesSaved: row.minutesSaved, minutesSaved: row.minutesSaved,
}); });
// Check if the cache should be updated (every ~14 hours) // Check if the cache should be updated (every ~14 hours)
const now = Date.now(); const now = Date.now();
if (now - lastUserCountCheck > 5000000) { if (now - lastUserCountCheck > 5000000) {
lastUserCountCheck = now; lastUserCountCheck = now;
await updateExtensionUsers(); updateExtensionUsers();
}
} }
} }
@@ -66,7 +68,7 @@ function getStats(countContributingUsers: boolean): Promise<DBStatsData> {
} }
async function updateExtensionUsers() { function updateExtensionUsers() {
if (config.userCounterURL) { if (config.userCounterURL) {
axios.get(`${config.userCounterURL}/api/v1/userCount`) axios.get(`${config.userCounterURL}/api/v1/userCount`)
.then(res => apiUsersCache = Math.max(apiUsersCache, res.data.userCount)) .then(res => apiUsersCache = Math.max(apiUsersCache, res.data.userCount))
@@ -77,13 +79,18 @@ async function updateExtensionUsers() {
const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone"; const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone";
const chromeExtId = "mnjggcdmjocbbbhaepdhchncahnbgone"; const chromeExtId = "mnjggcdmjocbbbhaepdhchncahnbgone";
firefoxUsersCache = await axios.get(mozillaAddonsUrl) axios.get(mozillaAddonsUrl)
.then(res => res.data.average_daily_users ) .then(res => firefoxUsersCache = res.data.average_daily_users )
.catch( /* istanbul ignore next */ () => { .catch( /* istanbul ignore next */ () => {
Logger.debug(`Failing to connect to ${mozillaAddonsUrl}`); Logger.debug(`Failing to connect to ${mozillaAddonsUrl}`);
return 0; return 0;
}); });
chromeUsersCache = await getCWSUsers(chromeExtId) ?? await getChromeUsers(chromeExtensionUrl); getCWSUsers(chromeExtId)
.then(res => chromeUsersCache = res)
.catch(() =>
getChromeUsers(chromeExtensionUrl)
.then(res => chromeUsersCache = res)
);
} }
function getChromeUsers(chromeExtensionUrl: string): Promise<number> { function getChromeUsers(chromeExtensionUrl: string): Promise<number> {

View File

@@ -9,7 +9,7 @@ describe("getTotalStats", () => {
it("Can get total stats", async () => { it("Can get total stats", async () => {
const result = await client({ url: endpoint }); const result = await client({ url: endpoint });
const data = result.data; const data = result.data;
assert.ok(data.userCount >= 0); assert.strictEqual(data.userCount, 0, "User count should default false");
assert.ok(data.activeUsers >= 0); assert.ok(data.activeUsers >= 0);
assert.ok(data.apiUsers >= 0); assert.ok(data.apiUsers >= 0);
assert.ok(data.viewCount >= 0); assert.ok(data.viewCount >= 0);
@@ -31,9 +31,9 @@ describe("getTotalStats", () => {
it("Can get total stats with old cws method", async () => { it("Can get total stats with old cws method", async () => {
const stub = sinon.stub(getCWSUsers, "getCWSUsers"); const stub = sinon.stub(getCWSUsers, "getCWSUsers");
stub.resolves(undefined); stub.resolves(undefined);
const result = await client({ url: `${endpoint}?countContributingUsers=false` }); const result = await client({ url: `${endpoint}?countContributingUsers=true` });
const data = result.data; const data = result.data;
assert.strictEqual(data.userCount, 0); assert.ok(data.userCount >= 0);
assert.ok(data.activeUsers >= 0); assert.ok(data.activeUsers >= 0);
assert.ok(data.apiUsers >= 0); assert.ok(data.apiUsers >= 0);
assert.ok(data.viewCount >= 0); assert.ok(data.viewCount >= 0);

View File

@@ -20,8 +20,8 @@ async function init() {
// delete old test database // delete old test database
if (fs.existsSync(config.db)) fs.unlinkSync(config.db); if (fs.existsSync(config.db)) fs.unlinkSync(config.db);
if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB); if (fs.existsSync(config.privateDB)) fs.unlinkSync(config.privateDB);
await resetRedis(); if (config?.redis?.enabled) await resetRedis();
await resetPostgres(); if (config?.postgres) await resetPostgres();
await initDb(); await initDb();

View File

@@ -6,7 +6,7 @@ import { Pool } from "pg";
import { Logger } from "../../src/utils/logger"; import { Logger } from "../../src/utils/logger";
export async function resetRedis() { export async function resetRedis() {
if (config.redis) { if (config?.redis?.enabled) {
const client = createClient(config.redis); const client = createClient(config.redis);
await client.connect(); await client.connect();
await client.flushAll(); await client.flushAll();