Skip to content
Snippets Groups Projects
tiles.service.ts 2.75 KiB
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from "@angular/common/http";
import {Observable} from "rxjs";
import {ConfigurationService} from "../env/configuration.service";
import {map} from "rxjs/operators";
import {ProgressSummary, Tile, TileDefinition} from "../model/tile";
import {FiltersService} from "./filters.service";
import {isArray} from "util";

@Injectable({
  providedIn: 'root'
})
export class TilesService {

  endPoint: string = '/services/minitiles/';

  constructor(
    private configService: ConfigurationService,
    private http: HttpClient,
    private filterService: FiltersService) {
  }

  public getTilesForEpoch(pattern: string, epoch: number): Observable<Array<Tile>> {
    let params = new HttpParams();

    const sortName = this.filterService.getCurrentSetting('TILE_SORT');
    const sortDir = this.filterService.getCurrentSetting('SORT_DIRECTION');

    params = params.append('pattern', pattern);
    params = params.append('columnNames', sortName);
    params = params.append('directions', sortDir);

    return this.http.get<Array<Tile>>(this.configService.config.url + this.endPoint + 'summary/epoch/' + epoch.toString(), {
      observe: 'response',
      params: params
    }).pipe(map(response => {
      return response.body;
    }));
  }

  public updateTileSummary(epoch: number): Observable<any> {
    return this.http.put<any>(this.configService.config.url + this.endPoint + 'summary/update?epoch=' + epoch.toString(), {}, {observe: 'response'})
      .pipe(map(response => {
        return response.body;
      }));
  }

  public getTileDefinition(id: number): Observable<TileDefinition> {
    return this.http.get<TileDefinition>(this.configService.config.url + this.endPoint + id + '/definitions',
      {observe: 'response'}).pipe(map(response => {
      return response.body;
    }));
  }

  public getProgressSummary(): Observable<Array<ProgressSummary>> {
    return this.http.get<Array<Array<any>>>(this.configService.config.url + this.endPoint + '/summary/progress',
      {observe: 'response'}).pipe(map(response => {
      const progress = [];
      if (response.body && response.body.length > 0) {
        for (const e of response.body) {
          if (isArray(e) && e.length >= 4) {
            const ps = new ProgressSummary();
            ps.epoch = typeof e[0] === "string" ? Math.round(parseFloat(e[0]) * 10) / 10 : e[0];
            ps.observed = typeof e[1] === "string" ? Math.round(parseFloat(e[1]) * 10) / 10 : e[1];
            ps.calibrated = typeof e[2] === "string" ? Math.round(parseFloat(e[2]) * 10) / 10 : e[2];
            ps.imaged = typeof e[3] === "string" ? Math.round(parseFloat(e[3]) * 10) / 10 : e[3];
            progress.push(ps);
          }
        }
      }
      return progress;
    }));
  }
}