forked from num-codex/codex-feasibility-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.service.ts
125 lines (104 loc) · 4.07 KB
/
backend.service.ts
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
import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { CategoryEntry, TerminologyEntry } from '../model/api/terminology/terminology'
import { AppConfigService } from '../../../config/app-config.service'
import { Observable, of } from 'rxjs'
import { FeatureService } from '../../../service/feature.service'
import { Query } from '../model/api/query/query'
import { QueryResponse } from '../model/api/result/QueryResponse'
import { QueryResult } from '../model/api/result/QueryResult'
import { MockBackendDataProvider } from './MockBackendDataProvider'
import { ApiTranslator } from '../controller/ApiTranslator'
@Injectable({
providedIn: 'root',
})
export class BackendService {
constructor(
private config: AppConfigService,
private feature: FeatureService,
private http: HttpClient
) {}
private static PATH_ROOT_ENTRIES = 'terminology/root-entries'
private static PATH_TERMINOLOGY_SUBTREE = 'terminology/entries'
private static PATH_SEARCH = 'terminology/selectable-entries'
private static PATH_RUN_QUERY = 'query-handler/run-query'
public static MOCK_RESULT_URL = 'http://localhost:9999/result-of-query/12345'
private readonly mockBackendDataProvider = new MockBackendDataProvider()
lowerBoundary: number = this.feature.getPatientResultLowerBoundary()
public getCategories(): Observable<Array<CategoryEntry>> {
if (this.feature.mockTerminology()) {
return of(this.mockBackendDataProvider.getCategoryEntries())
}
return this.http.get<Array<CategoryEntry>>(this.createUrl(BackendService.PATH_ROOT_ENTRIES))
}
public getTerminolgyTree(id: string): Observable<TerminologyEntry> {
if (this.feature.mockTerminology()) {
return of(this.mockBackendDataProvider.getTerminologyEntry(id))
}
return this.http.get<TerminologyEntry>(
this.createUrl(BackendService.PATH_TERMINOLOGY_SUBTREE + '/' + id)
)
}
public getTerminolgyEntrySearchResult(
catId: string,
search: string
): Observable<Array<TerminologyEntry>> {
if (this.feature.mockTerminology()) {
return of(this.mockBackendDataProvider.getTerminolgyEntrySearchResult(catId, search))
}
const queryParam = 'query=' + search.toUpperCase() + (catId ? '&categoryId=' + catId : '')
const url = this.createUrl(BackendService.PATH_SEARCH, queryParam)
return this.http.get<Array<TerminologyEntry>>(url)
}
public postQuery(query: Query): Observable<QueryResponse> {
if (this.feature.mockQuery()) {
return of({ location: BackendService.MOCK_RESULT_URL })
}
if (this.feature.getQueryVersion() === 'v1') {
const queryV1 = new ApiTranslator().translateToV1(query)
return this.http.post<QueryResponse>(this.createUrl(BackendService.PATH_RUN_QUERY), queryV1)
}
if (this.feature.getQueryVersion() === 'v2') {
const queryV2 = new ApiTranslator().translateToV2(query)
return this.http.post<QueryResponse>(this.createUrl(BackendService.PATH_RUN_QUERY), queryV2)
}
}
public getResult(resultUrl: string): Observable<QueryResult> {
if (this.feature.mockResult()) {
const result = {
totalNumberOfPatients: Math.floor(Math.random() * 1000),
queryId: '12345',
resultLines: [
{ siteName: 'Standort 1', numberOfPatients: 351 },
{ siteName: 'Standort 2', numberOfPatients: 1277 },
{ siteName: 'Standort 3', numberOfPatients: 63 },
{ siteName: 'Standort 4', numberOfPatients: 0 },
],
}
return of(result)
}
return this.http.get<QueryResult>(resultUrl)
}
createUrl(pathToResource: string, paramString?: string): string {
let url = this.config.getConfig().uiBackendApi.baseUrl
if (!url.endsWith('/')) {
url += '/'
}
url += pathToResource
if (paramString) {
url += '?' + paramString
}
return url
}
obfuscateResult(result: number): string {
if (result === 0) {
return '0'
} else {
if (result <= this.lowerBoundary) {
return '< ' + this.lowerBoundary.toString()
} else {
return result.toString()
}
}
}
}