import {EventEmitter, Injectable} from '@angular/core'; import {ApiService, LshData, RawData, TableInfoData} from './api.service'; @Injectable({ providedIn: 'root' }) export class StateService { public loadingProgress: number = 0; private _rawData: RawData[]; private _lshData: LshData; private _tableInfo: TableInfoData; private _queryWindow: number[][]; private _table: {[bucket: string]: number[]}[]; public _averageTable: {[bucket: string]: number[]}; private _weights: number[]; private _currentTab: number; private _labels = {}; private _sliderValue; private _lshParameters: number[]; private states = []; public windowSize = 120; public nrOfTables = 5; public hashSize = 5; public stepSize = 200; public querySelectionMode = true; public onNewData: EventEmitter = new EventEmitter(); public onNewWindows: EventEmitter = new EventEmitter(); public onNewQuery: EventEmitter = new EventEmitter(); public onNewTable: EventEmitter = new EventEmitter(); public onNewTableInfo: EventEmitter = new EventEmitter(); public onNewLshData: EventEmitter = new EventEmitter(); public onNewLabels: EventEmitter = new EventEmitter(); public onNewTab: EventEmitter = new EventEmitter(); public onNewSlider: EventEmitter = new EventEmitter(); constructor(private api: ApiService) { this.initialize(); } async initialize(): Promise { this.loadingProgress = 0; await this.getRawData(); this.loadingProgress = 50; await this.createWindows(); this.loadingProgress = 100; } async reset(): Promise { this.loadingProgress = 50; await this.createWindows(); this.loadingProgress = 100; } async getRawData(): Promise { this.rawData = await this.api.readFile(); } async createWindows(): Promise { await this.api.createWindows(this.parameters); this.onNewWindows.emit(); } async lshInitial(): Promise { this.lshData = await this.api.lshInitial(this._queryWindow); console.log('data loaded'); this._lshParameters = this.lshData.parameters; this._weights = [1, 1, 1]; this.createTable(); } async update(labels): Promise { this._weights = await this.api.getWeights(this._queryWindow, labels, this._weights); console.log(this._weights); this.lshData = await this.api.lshUpdate(this._queryWindow, this._weights, this._lshParameters); this.createTable(); } async getTableInfo(table: number[][]): Promise { // console.log(this.tableInfo); return await this.api.getTableInfo(table); } async getQueryWindow(windowIndex: number | {[index: number]: boolean}): Promise { this.queryWindow = await this.api.getQueryWindow(windowIndex); console.log(this.queryWindow); return this._queryWindow; } async getWindow(indices: number[]): Promise { return await this.api.getWindowByIndices(indices); } async createTable() { console.log('setting table param'); this.table = this.lshData.tables; console.log('table param set'); const averageTable = {}; const length = this.lshData.average_distances.length; const median = this.lshData.average_distances[Math.ceil(length / 2)]; const stepsize = median / 10; const indices: number[] = this.lshData.average_distances.map((x) => x > median * 2 ? 19 : Math.floor(x / stepsize)); this.lshData.average_candidates.forEach((candidate: number, index: number) => { if (averageTable[indices[index]] === undefined) { averageTable[indices[index]] = []; } averageTable[indices[index]].push(candidate); }); this._averageTable = averageTable; console.log('table created'); this.tableInfo = await this.getTableInfo(Object.values(this._averageTable)); } public set rawData(v: RawData[]) { this._rawData = v; console.log(this._rawData); this.onNewData.emit(); } public get rawData(): RawData []{ return this._rawData; } public set lshData(v: LshData) { console.log(v); this._lshData = v; this.onNewLshData.emit(); } public get lshData(): LshData { return this._lshData; } public set tableInfo(v: TableInfoData) { this._tableInfo = v; this.onNewTableInfo.emit(); } public get tableInfo(): TableInfoData { return this._tableInfo; } public set table(v: {[bucket: string]: number[]}[]) { console.log(v); this._table = v; console.log('emitting onNewTable'); this.onNewTable.emit(); } public get table(): {[bucket: string]: number[]}[] { return this._table; } public set labels(v) { this._labels = v; this.onNewLabels.emit(); } public get labels() { return this._labels; } public set currentTab(v: number) { this._currentTab = v; this.onNewTab.emit(); } public get currentTab(): number { return this._currentTab; } public set sliderValue(v: number) { this._sliderValue = v; this.onNewSlider.emit(v); } public get sliderValue(): number { return this._sliderValue; } public set queryWindow(v: number[][]) { this._queryWindow = v; this.onNewQuery.emit(); } public get queryWindow(): number[][] { return this._queryWindow; } public get lshParameters(): number[] { return this._lshParameters; } public get parameters(): {[parameter: string]: number} { return { windowsize: this.windowSize, hashsize: this.hashSize, tablesize: this.nrOfTables, stepsize: this.stepSize }; } }