1
|
/*
|
2
|
* Created by myrto on 12/05/2017
|
3
|
*/
|
4
|
|
5
|
|
6
|
import { Injectable } from '@angular/core';
|
7
|
|
8
|
import { Observable } from 'rxjs/Observable';
|
9
|
|
10
|
import 'rxjs/add/operator/map';
|
11
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
12
|
import { InterfaceInformation, JobsOfUser, StoredJob } from "../domain/typeScriptClasses";
|
13
|
import { apiUrl } from '../domain/tempAPI';
|
14
|
import { URLParameter } from '../domain/url-parameter';
|
15
|
|
16
|
let headers = new Headers({ 'Content-Type': 'application/json' });
|
17
|
let httpOptions = new RequestOptions({ headers: headers });
|
18
|
|
19
|
@Injectable ()
|
20
|
export class MonitorService {
|
21
|
/*private apiUrl = apiUrl + '/monitor/';*/
|
22
|
private apiUrl = process.env.API_ENDPOINT + '/monitor/';
|
23
|
|
24
|
constructor(private http: Http) { }
|
25
|
|
26
|
getJobSummary(jobId: string, groupBy: string): Observable<StoredJob> {
|
27
|
let url = `${this.apiUrl}getJobSummary?jobId=${jobId}&groupBy=${groupBy}`;
|
28
|
console.log(`knocking on: ${url}`);
|
29
|
httpOptions.withCredentials = true;
|
30
|
return this.http.get(url,httpOptions)
|
31
|
.map(res => <StoredJob>res.json() )
|
32
|
.catch(this.handleError);
|
33
|
}
|
34
|
|
35
|
getJobsOfUser(params: URLParameter[]): Observable<JobsOfUser> {
|
36
|
let url = `${this.apiUrl}getJobsOfUser`;
|
37
|
for (let param of params) {
|
38
|
if (param.key == 'user'){
|
39
|
url += `?${param.key}=${param.value[0]}`;
|
40
|
} else {
|
41
|
url += `&${param.key}=${param.value[0]}`;
|
42
|
}
|
43
|
}
|
44
|
console.log(`knocking on: ${url}`);
|
45
|
httpOptions.withCredentials = true;
|
46
|
return this.http.get(url,httpOptions)
|
47
|
.map(res => <JobsOfUser>res.json() )
|
48
|
.catch(this.handleError);
|
49
|
}
|
50
|
|
51
|
|
52
|
private handleError(error: Response | any) {
|
53
|
// In a real world app, we might use a remote logging infrastructure
|
54
|
// We'd also dig deeper into the error to get a better message
|
55
|
let errMsg = "";
|
56
|
console.log(`E R R O R !!!`);
|
57
|
console.log(error);
|
58
|
if (error instanceof Response) {
|
59
|
const body = error.text() || '';
|
60
|
//const err = body.error || JSON.stringify(body);
|
61
|
errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
|
62
|
console.log(errMsg);
|
63
|
} else {
|
64
|
errMsg = (error.message) ? error.message :
|
65
|
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
|
66
|
console.error(errMsg); // log to console instead
|
67
|
}
|
68
|
return Observable.throw(errMsg);
|
69
|
}
|
70
|
|
71
|
}
|