Add action statistics

This commit is contained in:
Anton Vakhrushev 2020-04-27 21:14:46 +03:00
parent 8807c2b070
commit 6ce839f4be
4 changed files with 58 additions and 3 deletions

View File

@ -1,5 +1,4 @@
import { ConsoleLogger, Logger, NullLogger } from './Logger';
import { Resources } from './Core/Resources';
const NAMESPACE = 'travian:v1';
@ -32,7 +31,11 @@ function createMapper<T>(options: ObjectMapperOptions<T>): ObjectMapper<T> {
if (factory) {
return plain => {
let item = factory();
if (typeof plain === 'object' && typeof item === 'object') {
return Object.assign(item, plain) as T;
} else {
return item;
}
};
}
@ -62,7 +65,7 @@ export class DataStorage {
try {
const serialized = storage.getItem(fullKey);
this.logger.log('GET', fullKey, serialized);
return JSON.parse(serialized || '"null"');
return JSON.parse(serialized || 'null');
} catch (e) {
if (e instanceof SyntaxError) {
return null;

View File

@ -8,17 +8,20 @@ import { createTask } from './Task/TaskController';
import { ConsoleLogger, Logger } from './Logger';
import { GrabberManager } from './Grabber/GrabberManager';
import { Scheduler } from './Scheduler';
import { Statistics } from './Statistics';
export class Executor {
private readonly version: string;
private readonly scheduler: Scheduler;
private grabbers: GrabberManager;
private statistics: Statistics;
private logger: Logger;
constructor(version: string, scheduler: Scheduler) {
this.version = version;
this.scheduler = scheduler;
this.grabbers = new GrabberManager();
this.statistics = new Statistics();
this.logger = new ConsoleLogger(this.constructor.name);
}
@ -86,6 +89,7 @@ export class Executor {
throw new ActionError(`Action task id ${cmd.args.taskId} not equal current task id ${task.id}`);
}
if (actionController) {
this.statistics.incrementAction();
await actionController.run(cmd.args, task);
} else {
this.logger.warn('ACTION NOT FOUND', cmd.name);

View File

@ -0,0 +1,23 @@
import { DataStorage } from '../DataStorage';
import { ActionStatistics } from '../Statistics';
const NAMESPACE = 'statistics.v1';
const ACTION_STATISTICS_KEY = 'actions';
export class StatisticsState {
private storage: DataStorage;
constructor() {
this.storage = new DataStorage(NAMESPACE);
}
getActionStatistics(): ActionStatistics {
return this.storage.getTyped<ActionStatistics>(ACTION_STATISTICS_KEY, {
factory: () => ({}),
});
}
setActionStatistics(statistics: ActionStatistics): void {
this.storage.set(ACTION_STATISTICS_KEY, statistics);
}
}

25
src/Statistics.ts Normal file
View File

@ -0,0 +1,25 @@
import { StatisticsState } from './State/StatisticsState';
import * as dateFormat from 'dateformat';
export interface ActionStatistics {
[key: string]: number;
}
export class Statistics {
private state: StatisticsState;
constructor() {
this.state = new StatisticsState();
}
incrementAction(): void {
const stat = this.state.getActionStatistics();
const key = dateFormat(Date.now(), 'yyyy-mm-dd-HH');
stat[key] = (stat[key] || 0) + 1;
this.state.setActionStatistics(stat);
}
getActionStatistics(): ActionStatistics {
return {};
}
}