cache.service.ts 4.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
import {EventEmitter, Injectable} from '@angular/core';
import {ApiService, RawData} from './api.service';

@Injectable({
  providedIn: 'root'
})
export class CacheService {
  public rawValues: number[];
  public rawIndices: string[];
10
  public loadingProgress: number = 0;
11

12
  private _currentTab: number;
13
  private _query = undefined;
14
15
16
  private _labels = {};
  private _tables;
  private _windowSimilarity;
17
  private _sliderValue;
18
  private _queryWindow = [];
19

20
  public windowSize = 500;
21
  public nrOfTables = 10;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
22
23
  public hashSize = 5;
  public stepSize = 1;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
24
  public querySelectionMode = true;
25

26
  public onNewData: EventEmitter<void> = new EventEmitter<void>();
27
28
29
  public onNewSimilarity: EventEmitter<void> = new EventEmitter<void>();
  public onNewLabels: EventEmitter<void> = new EventEmitter<void>();
  public onNewQuery: EventEmitter<void> = new EventEmitter<void>();
30
31
  public onNewTables: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();
32
  public onNewTab: EventEmitter<void> = new EventEmitter<void>();
33
  public onNewSlider: EventEmitter<void> = new EventEmitter<void>();
34
35

  constructor(private api: ApiService) {
36
     this.initialize();
37
38
39
  }

  async initialize(): Promise<void> {
40
    this.loadingProgress = 0;
41
    await this.getRawData();
42
    this.loadingProgress = 30;
43
    await this.createWindows();
44
    this.loadingProgress = 60;
45
    await this.createTables();
46
    this.loadingProgress = 100;
47
48
49
50
  }

  async reset(): Promise<void> {
    this.windowSimilarity = undefined;
51
    this.loadingProgress = 30;
52
    await this.createWindows();
53
    this.loadingProgress = 60;
54
    await this.createTables();
55
    this.loadingProgress = 100;
56
57
58
59
60
61
  }

  async getRawData(): Promise<void> {
    const rawData: RawData = await this.api.readFile();
    this.rawIndices = rawData.index;
    this.rawValues = rawData.values;
62
    this.onNewData.emit();
63
64
  }

65
66
67
  async createWindows(): Promise<void> {
    await this.api.createWindows(this.parameters);
    this.onNewWindows.emit();
68
69
70
  }

  async createTables(): Promise<void> {
71
    this.tables = await this.api.createTables(this.parameters);
72
73
  }

74
75
76
  async getSimilarWindows(): Promise<any> {
    this.windowSimilarity = await this.api.getSimilarWindows(this._queryWindow, this.tables);
    console.log(this.windowSimilarity);
77
78
79
    return this.windowSimilarity;
  }

80
81
82
83
84
85
86
87
88
89
90
91
  async getAverageProgressWindows(windows): Promise<number[][]> {
    return await this.api.getAverageProgressWindows(windows);
  }

  async getAverageTableWindows(windows): Promise<number[][]> {
    return await this.api.getAverageTableWindows(windows);
  }

  async getQueryWindow(queryIndex): Promise<number[]> {
    this._queryWindow = await this.api.getQueryWindow(this.rawValues.slice(queryIndex, queryIndex + this.windowSize));
    console.log(this._queryWindow);
    return this._queryWindow;
92
93
  }

94
  async updateTables(): Promise<void> {
95
    this.tables = await this.api.updateTables(this.labels, this.tables, this.parameters);
96
97
98
99
100
101
102
103
104
105
106
107
  }

  public set query(v) {
    this._query = v;
    this.onNewQuery.emit();
  }

  public get query(): number {
    return this._query;
  }

  public get windows(): number[][] {
108
    return [];
109
110
111
112
  }

  public set tables(v) {
    this._tables = v;
113
114
115
    this.onNewTables.emit();
  }

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  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;
  }

138
139
140
141
142
143
144
145
146
  public set currentTab(v) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

  public get currentTab() {
    return this._currentTab;
  }

147
148
149
150
151
152
153
154
155
  public set sliderValue(v) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

  public get sliderValue() {
    return this._sliderValue;
  }

156
157
158
159
160
  public set queryWindow(v) {
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

161
162
163
164
  public get parameters(): {[parameter: string]: any} {
    return {
      windowsize: this.windowSize,
      hashsize: this.hashSize,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
165
166
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
167
168
169
    };
  }
}