cache.service.ts 3.42 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

11
  private _currentTab: number;
12
  private _query = undefined;
13
14
15
  private _labels = {};
  private _tables;
  private _windowSimilarity;
16
  private _sliderValue;
17

18
  public windowSize = 500;
19
  public nrOfTables = 10;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
20
21
  public hashSize = 5;
  public stepSize = 1;
22

23
  public onNewData: EventEmitter<void> = new EventEmitter<void>();
24
25
26
  public onNewSimilarity: EventEmitter<void> = new EventEmitter<void>();
  public onNewLabels: EventEmitter<void> = new EventEmitter<void>();
  public onNewQuery: EventEmitter<void> = new EventEmitter<void>();
27
28
  public onNewTables: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();
29
  public onNewTab: EventEmitter<void> = new EventEmitter<void>();
30
  public onNewSlider: EventEmitter<void> = new EventEmitter<void>();
31
32
33
34
35
36
37
38
39
40


  public initialized: Promise<void>;

  constructor(private api: ApiService) {
     this.initialized = this.initialize();
  }

  async initialize(): Promise<void> {
    await this.getRawData();
41
    await this.createWindows();
42
43
44
45
46
    await this.createTables();
  }

  async reset(): Promise<void> {
    this.windowSimilarity = undefined;
47
    await this.createWindows();
48
49
50
51
52
53
54
    await this.createTables();
  }

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

58
59
60
  async createWindows(): Promise<void> {
    await this.api.createWindows(this.parameters);
    this.onNewWindows.emit();
61
62
63
  }

  async createTables(): Promise<void> {
64
    this.tables = await this.api.createTables(this.parameters);
65
66
  }

67
68
  async getSimilarWindows(query): Promise<any> {
    this.windowSimilarity = await this.api.getSimilarWindows(query, this.tables);
69
70
71
    return this.windowSimilarity;
  }

72
73
74
75
  async getAverageWindow(windows): Promise<number[][]> {
    return await this.api.getAverageWindow(windows);
  }

76
  async updateTables(): Promise<void> {
77
    this.tables = await this.api.updateTables(this.labels, this.tables, this.parameters);
78
79
80
81
82
83
84
85
86
87
88
89
  }

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

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

  public get windows(): number[][] {
90
    return [];
91
92
93
  }

  public set tables(v) {
94
    console.log(v);
95
    this._tables = v;
96
97
98
    this.onNewTables.emit();
  }

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  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;
114
    console.log(v);
115
116
117
118
119
120
121
    this.onNewSimilarity.emit();
  }

  public get windowSimilarity() {
    return this._windowSimilarity;
  }

122
123
124
125
126
127
128
129
130
  public set currentTab(v) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

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

131
132
133
134
135
136
137
138
139
  public set sliderValue(v) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

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

140
141
142
143
  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
144
145
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
146
147
148
    };
  }
}