Small storage refactoring

This commit is contained in:
Anton Vakhrushev 2020-06-24 13:49:24 +03:00
parent 625e7f9836
commit a884fdaee7
5 changed files with 46 additions and 19 deletions

View File

@ -6,11 +6,10 @@ import { Executor } from './Executor';
import { ControlPanel } from './ControlPanel'; import { ControlPanel } from './ControlPanel';
import { DataStorageTaskProvider } from './Queue/DataStorageTaskProvider'; import { DataStorageTaskProvider } from './Queue/DataStorageTaskProvider';
import { Statistics } from './Statistics'; import { Statistics } from './Statistics';
import { StatisticsStorage } from './Storage/StatisticsStorage';
import { VillageRepository } from './VillageRepository'; import { VillageRepository } from './VillageRepository';
import { LogStorage } from './Storage/LogStorage';
import { VillageFactory } from './VillageFactory'; import { VillageFactory } from './VillageFactory';
import { GrabberManager } from './Grabber/GrabberManager'; import { GrabberManager } from './Grabber/GrabberManager';
import { StorageContainer } from './Storage/StorageContainer';
export class Container { export class Container {
private readonly version: string; private readonly version: string;
@ -19,6 +18,13 @@ export class Container {
this.version = version; this.version = version;
} }
private _storageContainer: StorageContainer | undefined;
get storageContainer(): StorageContainer {
this._storageContainer = this._storageContainer || (() => new StorageContainer())();
return this._storageContainer;
}
private _villageRepository: VillageRepository | undefined; private _villageRepository: VillageRepository | undefined;
get villageRepository(): VillageRepository { get villageRepository(): VillageRepository {
@ -36,7 +42,7 @@ export class Container {
this._statistics = this._statistics =
this._statistics || this._statistics ||
(() => { (() => {
return new Statistics(new StatisticsStorage()); return new Statistics(this.storageContainer.statisticsStorage);
})(); })();
return this._statistics; return this._statistics;
} }
@ -91,10 +97,12 @@ export class Container {
this._executor = this._executor =
this._executor || this._executor ||
(() => { (() => {
const logger = new AggregateLogger([ const consoleLogger = new ConsoleLogger(Executor.name);
new ConsoleLogger(Executor.name), const storageLogger = new StorageLogger(
new StorageLogger(new LogStorage(), LogLevel.warning), this.storageContainer.logStorage,
]); LogLevel.warning
);
const logger = new AggregateLogger([consoleLogger, storageLogger]);
return new Executor( return new Executor(
this.version, this.version,
this.scheduler, this.scheduler,

View File

@ -1,9 +1,9 @@
import Vuex from 'vuex'; import Vuex from 'vuex';
import { LogStorage } from '../Storage/LogStorage'; import { VillageSettings, VillageSettingsDefaults } from '../Core/Village';
import { ReceiveResourcesMode, VillageSettings, VillageSettingsDefaults } from '../Core/Village';
import { getNumber, notify } from '../utils'; import { getNumber, notify } from '../utils';
import { VillageStorage } from '../Storage/VillageStorage'; import { VillageStorage } from '../Storage/VillageStorage';
import { VillageFactory } from '../VillageFactory'; import { VillageFactory } from '../VillageFactory';
import { StorageContainer } from '../Storage/StorageContainer';
export enum Mutations { export enum Mutations {
showLogs = 'showLogs', showLogs = 'showLogs',
@ -107,7 +107,8 @@ export function createStore(villageFactory: VillageFactory) {
}); });
setInterval(() => { setInterval(() => {
const logStorage = new LogStorage(); const stContainer = new StorageContainer();
const logStorage = stContainer.logStorage;
const logs = logStorage.getRecords(); const logs = logStorage.getRecords();
store.commit(Mutations.updateLogs, { logs }); store.commit(Mutations.updateLogs, { logs });
}, 1000); }, 1000);

View File

@ -1,15 +1,14 @@
import { DataStorage } from '../DataStorage'; import { DataStorage } from '../DataStorage';
import { LogStorageInterface, StorageLogRecord } from '../Logger'; import { LogStorageInterface, StorageLogRecord } from '../Logger';
const NAMESPACE = 'logs.v1';
const RECORD_LIST_KEY = 'records'; const RECORD_LIST_KEY = 'records';
const RECORD_COUNT = 200; const RECORD_COUNT = 200;
export class LogStorage implements LogStorageInterface { export class LogStorage implements LogStorageInterface {
private storage: DataStorage; private readonly storage: DataStorage;
constructor() {
this.storage = new DataStorage(NAMESPACE); constructor(storage: DataStorage) {
this.storage = storage;
} }
write(record: StorageLogRecord): void { write(record: StorageLogRecord): void {

View File

@ -1,14 +1,13 @@
import { DataStorage } from '../DataStorage'; import { DataStorage } from '../DataStorage';
import { ActionStatistics, StatisticsStorageInterface } from '../Statistics'; import { ActionStatistics, StatisticsStorageInterface } from '../Statistics';
const NAMESPACE = 'statistics.v1';
const ACTION_STATISTICS_KEY = 'actions'; const ACTION_STATISTICS_KEY = 'actions';
export class StatisticsStorage implements StatisticsStorageInterface { export class StatisticsStorage implements StatisticsStorageInterface {
private storage: DataStorage; private storage: DataStorage;
constructor() {
this.storage = new DataStorage(NAMESPACE); constructor(storage: DataStorage) {
this.storage = storage;
} }
getActionStatistics(): ActionStatistics { getActionStatistics(): ActionStatistics {

View File

@ -0,0 +1,20 @@
import { LogStorage } from './LogStorage';
import { DataStorage } from '../DataStorage';
import { StatisticsStorage } from './StatisticsStorage';
export class StorageContainer {
private _logStorage: LogStorage | undefined;
get logStorage(): LogStorage {
this._logStorage = this._logStorage || new LogStorage(new DataStorage('logs.v1'));
return this._logStorage;
}
private _statisticsStorage: StatisticsStorage | undefined;
get statisticsStorage(): StatisticsStorage {
this._statisticsStorage =
this._statisticsStorage || new StatisticsStorage(new DataStorage('statistics.v1'));
return this._statisticsStorage;
}
}