Add action statistics
This commit is contained in:
		| @@ -1,5 +1,4 @@ | |||||||
| import { ConsoleLogger, Logger, NullLogger } from './Logger'; | import { ConsoleLogger, Logger, NullLogger } from './Logger'; | ||||||
| import { Resources } from './Core/Resources'; |  | ||||||
|  |  | ||||||
| const NAMESPACE = 'travian:v1'; | const NAMESPACE = 'travian:v1'; | ||||||
|  |  | ||||||
| @@ -32,7 +31,11 @@ function createMapper<T>(options: ObjectMapperOptions<T>): ObjectMapper<T> { | |||||||
|     if (factory) { |     if (factory) { | ||||||
|         return plain => { |         return plain => { | ||||||
|             let item = factory(); |             let item = factory(); | ||||||
|  |             if (typeof plain === 'object' && typeof item === 'object') { | ||||||
|                 return Object.assign(item, plain) as T; |                 return Object.assign(item, plain) as T; | ||||||
|  |             } else { | ||||||
|  |                 return item; | ||||||
|  |             } | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -62,7 +65,7 @@ export class DataStorage { | |||||||
|         try { |         try { | ||||||
|             const serialized = storage.getItem(fullKey); |             const serialized = storage.getItem(fullKey); | ||||||
|             this.logger.log('GET', fullKey, serialized); |             this.logger.log('GET', fullKey, serialized); | ||||||
|             return JSON.parse(serialized || '"null"'); |             return JSON.parse(serialized || 'null'); | ||||||
|         } catch (e) { |         } catch (e) { | ||||||
|             if (e instanceof SyntaxError) { |             if (e instanceof SyntaxError) { | ||||||
|                 return null; |                 return null; | ||||||
|   | |||||||
| @@ -8,17 +8,20 @@ import { createTask } from './Task/TaskController'; | |||||||
| import { ConsoleLogger, Logger } from './Logger'; | import { ConsoleLogger, Logger } from './Logger'; | ||||||
| import { GrabberManager } from './Grabber/GrabberManager'; | import { GrabberManager } from './Grabber/GrabberManager'; | ||||||
| import { Scheduler } from './Scheduler'; | import { Scheduler } from './Scheduler'; | ||||||
|  | import { Statistics } from './Statistics'; | ||||||
|  |  | ||||||
| export class Executor { | export class Executor { | ||||||
|     private readonly version: string; |     private readonly version: string; | ||||||
|     private readonly scheduler: Scheduler; |     private readonly scheduler: Scheduler; | ||||||
|     private grabbers: GrabberManager; |     private grabbers: GrabberManager; | ||||||
|  |     private statistics: Statistics; | ||||||
|     private logger: Logger; |     private logger: Logger; | ||||||
|  |  | ||||||
|     constructor(version: string, scheduler: Scheduler) { |     constructor(version: string, scheduler: Scheduler) { | ||||||
|         this.version = version; |         this.version = version; | ||||||
|         this.scheduler = scheduler; |         this.scheduler = scheduler; | ||||||
|         this.grabbers = new GrabberManager(); |         this.grabbers = new GrabberManager(); | ||||||
|  |         this.statistics = new Statistics(); | ||||||
|         this.logger = new ConsoleLogger(this.constructor.name); |         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}`); |             throw new ActionError(`Action task id ${cmd.args.taskId} not equal current task id ${task.id}`); | ||||||
|         } |         } | ||||||
|         if (actionController) { |         if (actionController) { | ||||||
|  |             this.statistics.incrementAction(); | ||||||
|             await actionController.run(cmd.args, task); |             await actionController.run(cmd.args, task); | ||||||
|         } else { |         } else { | ||||||
|             this.logger.warn('ACTION NOT FOUND', cmd.name); |             this.logger.warn('ACTION NOT FOUND', cmd.name); | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								src/State/StatisticsState.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/State/StatisticsState.ts
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										25
									
								
								src/Statistics.ts
									
									
									
									
									
										Normal 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 {}; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user