api.service.ts 2.97 KB
Newer Older
1
2
3
4
5
6
7
import { Injectable } from '@angular/core';

export interface RawData {
  index: string[];
  values: number[];
}

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
8
9
10
export interface LshData {
  candidates: number[];
  distances: number[];
11
  hash_functions: number[];// number[][][][];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
12
13
14
15
16
17
18
19
20
21
22
23
  parameters?: number[];
}

export interface TableInfoData {
  prototypes: {
    average: number[];
    min: number[];
    max: number[];
  }[];
  distances: number[][];
}

24
25
26
27
28
29
30
31
32
33
@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor() { }

  // Read input data
  async readFile(): Promise<RawData> {
    const response = await fetch('http://127.0.0.1:5000/read-data');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
34
    return await response.json();
35
36
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
37
  // Split data into windows and normalize
38
39
  async createWindows(parameters): Promise<any> {
    const postData = {parameters};
40
41
42
43
44
45
46
47
48
49
    const response = await fetch('http://127.0.0.1:5000/create-windows', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(postData)
    });
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
50
51
  // Calculate parameters for LSH + find candidates using LSH
  async lshInitial(query): Promise<LshData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
52
    const response = await fetch('http://127.0.0.1:5000/initialize', {
53
54
55
56
57
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
58
      body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } )
59
60
61
62
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
63
  // Find candidates using LSH with weights
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
64
  async lshUpdate(query, weights, parameters): Promise<LshData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
65
    const response = await fetch('http://127.0.0.1:5000/update', {
66
67
68
69
70
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
71
      body: new Blob( [ JSON.stringify({query, weights, parameters}) ], { type: 'text/plain' } )
72
73
74
75
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
76
77
  // Get query window based on windows labeled correct
  async getQueryWindow(window): Promise<number[]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
78
    const response = await fetch('http://127.0.0.1:5000/query', {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
79
80
81
82
83
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
84
      body: JSON.stringify({window})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
85
86
87
88
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
89
90
  // Get data of a window by indices
  async getWindowByIndices(indices: number[]): Promise<number[][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
91
    const response = await fetch('http://127.0.0.1:5000/window', {
92
93
94
95
96
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
97
      body: JSON.stringify({indices})
98
99
100
101
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
102
103
  async getTableInfo(windows): Promise<TableInfoData> {
    const response = await fetch('http://127.0.0.1:5000/table-info', {
104
105
106
107
108
109
110
111
112
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({windows})
    });
    return await response.json();
  }
113
}