import {EventEmitter, Injectable} from '@angular/core'; import {ApiService, RawData} from './api.service'; @Injectable({ providedIn: 'root' }) export class CacheService { public rawValues: number[]; public rawIndices: string[]; public _windows: number[][]; private _query = undefined; public _labels = {}; public _tables; public _windowSimilarity; public windowSize = 20; public nrOfTables = 10; public hashSize = 10; 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 initialized: Promise; constructor(private api: ApiService) { this.initialized = this.initialize(); } async initialize(): Promise { await this.getRawData(); await this.getWindows(); await this.createTables(); } async reset(): Promise { this.windowSimilarity = undefined; await this.getWindows(); await this.createTables(); } async getRawData(): Promise { const rawData: RawData = await this.api.readFile(); this.rawIndices = rawData.index; this.rawValues = rawData.values; } async getWindows(): Promise { this.windows = await this.api.createWindows(this.rawValues, this.parameters); } async createTables(): Promise { this.tables = await this.api.createTables(this.windows, this.parameters); } async getSimilarWindows(window): Promise { this.windowSimilarity = await this.api.getSimilarWindows(window, this.tables); return this.windowSimilarity; } async updateTables(): Promise { this.tables = await this.api.updateTables(this.windows, this.labels, this.tables, this.parameters); } public set query(v) { this._query = v; this.onNewQuery.emit(); } public get query(): number { return this._query; } public set windows(v) { this._windows = v; this.onNewWindows.emit(); } public get windows(): number[][] { return this._windows; } public set tables(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; this.onNewSimilarity.emit(); } public get windowSimilarity() { return this._windowSimilarity; } public get parameters(): {[parameter: string]: any} { return { windowsize: this.windowSize, hashsize: this.hashSize, tablesize: this.nrOfTables }; } }