api.service.ts 2.63 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
    const temp = await response.json();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
19 20
    const index = temp.index;
    const values = temp.values;
21
    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
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
39 40
  async initialize(query): Promise<any> {
    const response = await fetch('http://127.0.0.1:5000/initialize', {
41 42 43 44 45
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
46
      body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } )
47 48 49 50
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
51 52 53
  // Update
  async update(query, hashFunctions, parameters): Promise<any> {
    const response = await fetch('http://127.0.0.1:5000/update', {
54 55 56 57 58
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
59
      body: new Blob( [ JSON.stringify({query, hash_functions: hashFunctions, parameters}) ], { type: 'text/plain' } )
60 61 62 63
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
64 65
  async getQueryWindow(window) {
    const response = await fetch('http://127.0.0.1:5000/query', {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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: JSON.stringify({window})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
72 73 74 75
    });
    return await response.json();
  }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
88 89
  async getAverageWindows(windows): Promise<any> {
    const response = await fetch('http://127.0.0.1:5000/average', {
90 91 92 93 94 95 96 97 98
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({windows})
    });
    return await response.json();
  }
99
}