Simplify resource stats generation

This commit is contained in:
FoxxMD
2021-07-23 11:46:37 -04:00
parent 484931d8b5
commit 12a4e0436e
5 changed files with 55 additions and 40 deletions

View File

@@ -14,7 +14,7 @@ import sharedSession from 'express-socket.io-session';
import Submission from "snoowrap/dist/objects/Submission";
import EventEmitter from "events";
import {
boolToString,
boolToString, cacheStats,
COMMENT_URL_ID,
filterLogBySubreddit,
formatLogLineToHtml, formatNumber,
@@ -285,25 +285,10 @@ const rcbServer = async function (options: OperatorConfig) {
sort,
limit
});
const resCum: ResourceStats = {
author: {requests: 0, miss: 0},
authorCrit: {requests: 0, miss: 0},
content: {requests: 0, miss: 0}
};
const subManagerData = [];
for (const s of subreddits) {
const m = bot.subManagers.find(x => x.displayLabel === s) as Manager;
let cacheTypes = resCum;
if(m.resources !== undefined) {
cacheTypes = Object.keys(m.resources.stats.cache).reduce((acc, curr) => {
const per = acc[curr].miss === 0 ? 0 : formatNumber(acc[curr].miss / acc[curr].requests) * 100;
// @ts-ignore
acc[curr].missPercent = `${per}%`;
return acc;
}, m.resources.stats.cache)
}
const sd = {
name: s,
logs: logs.get(s) || [], // provide a default empty value in case we truly have not logged anything for this subreddit yet
@@ -331,11 +316,6 @@ const rcbServer = async function (options: OperatorConfig) {
startedAt: 'Not Started',
startedAtHuman: 'Not Started',
delayBy: m.delayBy === undefined ? 'No' : `Delayed by ${m.delayBy} sec`,
cache: {
//currentKeyCount: await m.resources.getCacheKeyCount(),
totalRequests: m.resources !== undefined ? Object.values(m.resources.stats.cache).reduce((acc, curr) => acc + curr.requests, 0) : 0,
types: cacheTypes,
}
};
// TODO replace indicator data with js on client page
let indicator;
@@ -383,12 +363,12 @@ const rcbServer = async function (options: OperatorConfig) {
const {checks, ...rest} = totalStats;
let cumRaw = subManagerData.reduce((acc, curr) => {
Object.keys(curr.cache.types as ResourceStats).forEach((k) => {
acc[k].requests += curr.cache.types[k].requests;
acc[k].miss += curr.cache.types[k].miss;
Object.keys(curr.stats.cache.types as ResourceStats).forEach((k) => {
acc[k].requests += curr.stats.cache.types[k].requests;
acc[k].miss += curr.stats.cache.types[k].miss;
});
return acc;
}, resCum);
}, cacheStats());
cumRaw = Object.keys(cumRaw).reduce((acc, curr) => {
const per = acc[curr].miss === 0 ? 0 : formatNumber(acc[curr].miss / acc[curr].requests) * 100;
// @ts-ignore
@@ -406,15 +386,16 @@ const rcbServer = async function (options: OperatorConfig) {
checks: checks,
softLimit: bot.softLimit,
hardLimit: bot.hardLimit,
stats: rest,
cache: {
// naive
currentKeyCount: await bot.subManagers[0].resources.getCacheKeyCount(),
totalRequests: subManagerData.reduce((acc, curr) => acc + curr.cache.totalRequests, 0),
types: {
...cumRaw
stats: {
...rest,
cache: {
currentKeyCount: await bot.subManagers[0].resources.getCacheKeyCount(),
totalRequests: subManagerData.reduce((acc, curr) => acc + curr.stats.cache.totalRequests, 0),
types: {
...cumRaw,
}
}
}
},
};
if (allManagerData.logs === undefined) {
// this should happen but saw an edge case where potentially did

View File

@@ -297,12 +297,12 @@
<span><%= data.stats.actionsRunTotal %> Run</span>
<% if (data.name === 'All') { %>
<label>Cached Keys</label>
<span><%= data.cache.currentKeyCount %></span>
<span><%= data.stats.cache.currentKeyCount %></span>
<% } %>
<label>Total Cache Reqs</label>
<span><%= data.cache.totalRequests %></span>
<span><%= data.stats.cache.totalRequests %></span>
<label>Author Cache</label>
<span><%= data.cache.types.author.requests %> (<%= data.cache.types.author.missPercent %> Miss)</span>
<span><%= data.stats.cache.types.author.requests %> (<%= data.stats.cache.types.author.missPercent %> Miss)</span>
</div>
</div>
</div>

View File

@@ -3,6 +3,7 @@ import {Logger} from "winston";
import {SubmissionCheck} from "../Check/SubmissionCheck";
import {CommentCheck} from "../Check/CommentCheck";
import {
cacheStats,
createRetryHandler,
determineNewResults, formatNumber,
mergeArr, parseFromJsonOrYamlToObject, pollingInfo, sleep, totalFromMapStats,
@@ -122,7 +123,7 @@ export class Manager {
actionsRunSinceStart: Map<string, number> = new Map();
getStats = () => {
return {
const data: any = {
eventsCheckedTotal: this.eventsCheckedTotal,
eventsCheckedSinceStartTotal: this.eventsCheckedSinceStartTotal,
eventsAvg: formatNumber(this.eventsRollingAvg),
@@ -142,8 +143,18 @@ export class Manager {
actionsRun: this.actionsRun,
actionsRunTotal: totalFromMapStats(this.actionsRun),
actionsRunSinceStart: this.actionsRunSinceStart,
actionsRunSinceStartTotal: totalFromMapStats(this.actionsRunSinceStart)
actionsRunSinceStartTotal: totalFromMapStats(this.actionsRunSinceStart),
cache: {
totalRequests: 0,
types: cacheStats()
},
};
if (this.resources !== undefined) {
const resStats = this.resources.getStats();
data.cache = resStats.cache;
}
return data;
}
getCurrentLabels = () => {

View File

@@ -10,7 +10,7 @@ import {
import Subreddit from 'snoowrap/dist/objects/Subreddit';
import winston, {Logger} from "winston";
import fetch from 'node-fetch';
import {mergeArr, parseExternalUrl, parseWikiContext} from "../util";
import {formatNumber, mergeArr, parseExternalUrl, parseWikiContext} from "../util";
import LoggedError from "../Utils/LoggedError";
import {
CacheOptions, CacheProvider,
@@ -147,6 +147,21 @@ export class SubredditResources {
return 0;
}
getStats() {
return {
cache: {
// TODO could probably combine these two
totalRequests: Object.values(this.stats.cache).reduce((acc, curr) => acc + curr.requests, 0),
types: Object.keys(this.stats.cache).reduce((acc, curr) => {
const per = acc[curr].miss === 0 ? 0 : formatNumber(acc[curr].miss / acc[curr].requests) * 100;
// @ts-ignore
acc[curr].missPercent = `${per}%`;
return acc;
}, this.stats.cache)
}
}
}
async getAuthorActivities(user: RedditUser, options: AuthorTypedActivitiesOptions): Promise<Array<Submission | Comment>> {
if (this.cache !== undefined && this.authorTTL > 0) {
const userName = user.name;

View File

@@ -13,7 +13,7 @@ import {
ActivityWindowCriteria,
DurationComparison,
GenericComparison, NamedGroup,
PollingOptionsStrong, RegExResult,
PollingOptionsStrong, RegExResult, ResourceStats,
StringOperator
} from "./Common/interfaces";
import JSON5 from "json5";
@@ -866,3 +866,11 @@ export const removeUndefinedKeys = (obj: any) => {
//Object.keys(newObj).forEach(key => newObj[key] === undefined || newObj[key] && delete newObj[key])
return newObj;
}
export const cacheStats = (): ResourceStats => {
return {
author: {requests: 0, miss: 0},
authorCrit: {requests: 0, miss: 0},
content: {requests: 0, miss: 0}
};
}