All files / src/app/test-controller/services backend.service.ts

3.44% Statements 1/29
0% Branches 0/12
0% Functions 0/14
3.44% Lines 1/29

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                1x                                                                                                                                                                                                                                                  
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpEventType } from '@angular/common/http';
import { Observable, Subscription } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import {
  KeyValuePairString,
  LoadingFile,
  StateReportEntry,
  TestData,
  UnitData
} from '../interfaces/test-controller.interfaces';
import { MainDataService } from '../../shared/services/maindata/maindata.service';
 
@Injectable({
  providedIn: 'root'
})
export class BackendService {
  constructor(
    @Inject('BACKEND_URL') public backendUrl: string,
    private http: HttpClient,
    private mds: MainDataService
  ) {
  }
 
  saveReview(
    testId: string,
    unitName: string | null,
    page: number | null,
    pagelabel: string | null,
    priority: number,
    categories: string,
    entry: string,
    userAgent: string,
    originalUnitId: string
  ): Observable<void> {
    return this.http.put<void>(
      `${this.backendUrl}test/${testId}${unitName ? `/unit/${unitName}` : ''}/review`,
      {
        priority, categories, entry, page, pagelabel, userAgent, originalUnitId
      }
    );
  }
 
  getTestData(testId: string): Observable<TestData> {
    return this.http.get<TestData>(`${this.backendUrl}test/${testId}`);
  }
 
  getUnitData(testId: string, unitid: string, unitalias: string): Observable<UnitData> {
    return this.http.get<UnitData>(`${this.backendUrl}test/${testId}/unit/${unitid}/alias/${unitalias}`);
  }
 
  updateTestState(testId: string, newState: StateReportEntry[]): Subscription {
    return this.http.patch(`${this.backendUrl}test/${testId}/state`, newState).subscribe();
  }
 
  addTestLog(testId: string, logEntries: StateReportEntry[]): Subscription {
    return this.http.put(`${this.backendUrl}test/${testId}/log`, logEntries).subscribe();
  }
 
  updateUnitState(testId: string, unitName: string, originalUnitId: string, newState: StateReportEntry[]): Subscription {
    return this.http.patch(`${this.backendUrl}test/${testId}/unit/${unitName}/state`, {
      newState,
      originalUnitId
    }).subscribe();
  }
 
  addUnitLog(testId: string, unitName: string, originalUnitId:string, logEntries: StateReportEntry[]): Subscription {
    return this.http.put(`${this.backendUrl}test/${testId}/unit/${unitName}/log`, {
      logEntries,
      originalUnitId
    }).subscribe();
  }
 
  notifyDyingTest(testId: string): void {
    if (navigator.sendBeacon) {
      navigator.sendBeacon(`${this.backendUrl}test/${testId}/connection-lost`);
    } else {
      fetch(`${this.backendUrl}test/${testId}/connection-lost`, {
        keepalive: true,
        method: 'POST'
      });
    }
  }
 
  updateDataParts(testId: string, unitId: string, originalUnitId: string, dataParts: KeyValuePairString, responseType: string): Subscription {
    const timeStamp = Date.now();
    return this.http
      .put(`${this.backendUrl}test/${testId}/unit/${unitId}/response`, {
        timeStamp, dataParts, originalUnitId, responseType
      })
      .subscribe();
  }
 
  lockTest(testId: string, timeStamp: number, message: string): Subscription {
    return this.http
      .patch<boolean>(`${this.backendUrl}test/${testId}/lock`, { timeStamp, message })
      .subscribe();
  }
 
  getResource(workspaceId: number, path: string): Observable<LoadingFile> {
    const resourceUri = this.mds.appConfig?.fileServiceUri ?? this.backendUrl;
    return this.http
      .get(
        `${resourceUri}file/${this.mds.getAuthData()?.groupToken}/ws_${workspaceId}/${path}`,
        {
          responseType: 'text',
          reportProgress: true,
          observe: 'events'
        }
      )
      .pipe(
        map((event: HttpEvent<any>) => {
          switch (event.type) {
            case HttpEventType.ResponseHeader:
              return { progress: 0 };
 
            case HttpEventType.DownloadProgress:
              Iif (!event.total) { // happens if file is huge because browser switches to chunked loading
                return <LoadingFile>{ progress: 'UNKNOWN' };
              }
              return { progress: Math.round(100 * (event.loaded / event.total)) };
 
            case HttpEventType.Response:
              Iif (!event.body.length) {
                // this might happen when file is so large, that memory size get exhausted
                throw new Error(`Empty response for  '${path}'. Most likely the browsers memory was exhausted.`);
              }
              return { content: event.body };
 
            default:
              return null;
          }
        }),
        filter((progressOfContent): progressOfContent is LoadingFile => progressOfContent != null)
      );
  }
}