From 6ce839f4be33472db2d84acfaa8f245773989385 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Mon, 27 Apr 2020 21:14:46 +0300 Subject: [PATCH] Add action statistics --- src/DataStorage.ts | 9 ++++++--- src/Executor.ts | 4 ++++ src/State/StatisticsState.ts | 23 +++++++++++++++++++++++ src/Statistics.ts | 25 +++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/State/StatisticsState.ts create mode 100644 src/Statistics.ts diff --git a/src/DataStorage.ts b/src/DataStorage.ts index 180e448..584cd68 100644 --- a/src/DataStorage.ts +++ b/src/DataStorage.ts @@ -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(options: ObjectMapperOptions): ObjectMapper { if (factory) { return plain => { let item = factory(); - return Object.assign(item, plain) as T; + 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; diff --git a/src/Executor.ts b/src/Executor.ts index 3fadb92..98430b4 100644 --- a/src/Executor.ts +++ b/src/Executor.ts @@ -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); diff --git a/src/State/StatisticsState.ts b/src/State/StatisticsState.ts new file mode 100644 index 0000000..f83b591 --- /dev/null +++ b/src/State/StatisticsState.ts @@ -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(ACTION_STATISTICS_KEY, { + factory: () => ({}), + }); + } + + setActionStatistics(statistics: ActionStatistics): void { + this.storage.set(ACTION_STATISTICS_KEY, statistics); + } +} diff --git a/src/Statistics.ts b/src/Statistics.ts new file mode 100644 index 0000000..28d62cd --- /dev/null +++ b/src/Statistics.ts @@ -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 {}; + } +}