import {EventEmitter, Injectable} from '@angular/core'; import {ApiService, RawData} from './api.service'; @Injectable({ providedIn: 'root' }) export class CacheService { public rawValues: number[]; public rawIndices: string[]; private _currentTab: number; private _query = undefined; private _labels = {}; private _tables; private _windowSimilarity; public windowSize = 500; public nrOfTables = 10; public hashSize = 5; public stepSize = 1; public onNewData: EventEmitter = new EventEmitter(); public onNewSimilarity: EventEmitter = new EventEmitter(); public onNewLabels: EventEmitter = new EventEmitter(); public onNewQuery: EventEmitter = new EventEmitter(); public onNewTables: EventEmitter = new EventEmitter(); public onNewWindows: EventEmitter = new EventEmitter(); public onNewTab: EventEmitter = new EventEmitter(); public initialized: Promise; constructor(private api: ApiService) { this.initialized = this.initialize(); } async initialize(): Promise { await this.getRawData(); await this.createWindows(); await this.createTables(); } async reset(): Promise { this.windowSimilarity = undefined; await this.createWindows(); await this.createTables(); } async getRawData(): Promise { const rawData: RawData = await this.api.readFile(); this.rawIndices = rawData.index; this.rawValues = rawData.values; this.onNewData.emit(); } async createWindows(): Promise { await this.api.createWindows(this.parameters); this.onNewWindows.emit(); } async createTables(): Promise { this.tables = await this.api.createTables(this.parameters); } async getSimilarWindows(query): Promise { this.windowSimilarity = await this.api.getSimilarWindows(query, this.tables); return this.windowSimilarity; } async getAverageWindow(windows): Promise { return await this.api.getAverageWindow(windows); } async updateTables(): Promise { this.tables = await this.api.updateTables(this.labels, this.tables, this.parameters); } public set query(v) { this._query = v; this.onNewQuery.emit(); } public get query(): number { return this._query; } public get windows(): number[][] { return []; } public set tables(v) { console.log(v); this._tables = v; this.onNewTables.emit(); } public get tables() { return this._tables; } public set labels(v) { this._labels = v; this.onNewLabels.emit(); } public get labels() { return this._labels; } public set windowSimilarity(v) { this._windowSimilarity = v; console.log(v); this.onNewSimilarity.emit(); } public get windowSimilarity() { return this._windowSimilarity; } public set currentTab(v) { this._currentTab = v; this.onNewTab.emit(); } public get currentTab() { return this._currentTab; } public get parameters(): {[parameter: string]: any} { return { windowsize: this.windowSize, hashsize: this.hashSize, tablesize: this.nrOfTables, stepsize: this.stepSize }; } }