api.service.ts 3.05 KB
Newer Older
1
2
3
import { Injectable } from '@angular/core';

export interface RawData {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
4
5
  index: string[][];
  values: number[][];
6
7
}

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

export interface TableInfoData {
  prototypes: {
19
20
21
    average: number[][];
    min: number[][];
    max: number[][];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
22
23
24
25
  }[];
  distances: number[][];
}

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

  constructor() { }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
39
  // Split data into windows and normalize
40
41
  async createWindows(parameters): Promise<any> {
    const postData = {parameters};
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
42
    const response = await fetch('http://127.0.0.1:5000/create-mts-windows', {
43
44
45
46
47
48
49
50
51
      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
52
53
  // 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
54
    const response = await fetch('http://127.0.0.1:5000/initialize', {
55
56
57
58
59
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
60
      body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } )
61
62
63
64
    });
    return await response.json();
  }

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

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

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

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