All files / src/app/group-monitor backend.service.ts

9.09% Statements 2/22
0% Branches 0/1
0% Functions 0/11
4.76% Lines 1/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                      1x                                                                                                                                    
import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import {
  BookletError, CommandResponse, Profile, TestSessionData
} from './group-monitor.interfaces';
import { WebsocketBackendService } from '../shared/shared.module';
import { AppError } from '../app.interfaces';
 
@Injectable()
export class BackendService extends WebsocketBackendService<TestSessionData[]> {
  pollingEndpoint = 'monitor/test-sessions';
  pollingInterval = 5000;
  wsChannelName = 'test-sessions';
  initialData: TestSessionData[] = [];
 
  observeSessionsMonitor(groupName: string): Observable<TestSessionData[]> {
    this.pollingEndpoint = `monitor/group/${groupName}/test-sessions`;
    return this.observeEndpointAndChannel();
  }
 
  getBooklet(bookletName: string): Observable<string | BookletError> {
    const headers = new HttpHeaders({ 'Content-Type': 'text/xml' }).set('Accept', 'text/xml');
    const missingFileError: BookletError = { error: 'missing-file', species: null };
    const generalError: BookletError = { error: 'general', species: null };
 
    return this.http
      .get(`${this.serverUrl}booklet/${bookletName}`, { headers, responseType: 'text' })
      .pipe(
        catchError((err: AppError) => {
          // we don't rely on general error handler here, because this is to interwoven with subsequent requests
          Iif (err.code === 404) {
            // could potentially happen when booklet file was removed since test was started
            return of(missingFileError);
          }
          return of(generalError);
        })
      );
  }
 
  command(keyword: string, args: string[], testIds: number[]): Observable<CommandResponse> {
    return this.http
      .put(
        `${this.serverUrl}monitor/command`,
        {
          keyword,
          arguments: args,
          timestamp: Date.now() / 1000,
          testIds
        }
      )
      .pipe(
        map(() => ({ commandType: keyword, testIds }))
      );
  }
 
  unlock(groupName: string, testIds: number[]): Observable<CommandResponse> {
    return this.http
      .post(`${this.serverUrl}monitor/group/${groupName}/tests/unlock`, { testIds })
      .pipe(
        map(() => ({ commandType: 'unlock', testIds }))
      );
  }
 
  lock(groupName: string, testIds: number[]): Observable<CommandResponse> {
    return this.http
      .post(`${this.serverUrl}monitor/group/${groupName}/tests/lock`, { testIds })
      .pipe(
        map(() => ({ commandType: 'unlock', testIds }))
      );
  }
 
  getProfile(profileId: string): Observable<Profile> {
    return this.http.get<Profile>(`${this.serverUrl}monitor/profile/${profileId}`);
  }
}