api.service.ts 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { Injectable } from '@angular/core';

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

@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');
18
19
20
21
    const temp = await response.json();
    const index = JSON.parse(temp.index);
    const values = JSON.parse(temp.values).map(Number);
    return {index, values};
22
23
24
  }

// Split data into windows and normalize
25
26
  async createWindows(parameters): Promise<any> {
    const postData = {parameters};
27
28
29
30
31
32
33
34
    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
35
    return await response.json();
36
37
38
  }

// Generate LSH-tables by hashing each window
39
  async createTables(parameters): Promise<any> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
40
    console.log("creating tables");
41
    const postData = {parameters};
42
43
44
45
46
47
    const response = await fetch('http://127.0.0.1:5000/create-tables', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
48
      body: new Blob( [ JSON.stringify(postData) ], { type: 'text/plain' } )
49
50
51
52
53
    });
    return await response.json();
  }

// Return similar windows based on an input query
54
  async getSimilarWindows(query, tables) {
55
56
57
58
59
60
    const response = await fetch('http://127.0.0.1:5000/query', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
61
      body: JSON.stringify({query, tables})
62
63
64
65
    });
    return await response.json();
  }

66
67
68
69
70
71
72
73
74
75
76
77
78
79
  async getAverageWindow(windows): Promise<number[][]> {
    const response = await fetch('http://127.0.0.1:5000/average', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({windows})
    });
    return await response.json();
  }

  async updateTables(labelData, tables, parameters): Promise<any> {
    const postData = {labelData, tables, parameters};
80
81
82
83
84
85
    const response = await fetch('http://127.0.0.1:5000/update', {
      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: new Blob( [ JSON.stringify(postData) ], { type: 'text/plain' } )
87
88
89
90
    });
    return await response.json();
  }
}