Project

General

Profile

1
/*
2
* Created by myrto on 12/05/2017
3
*/
4

    
5
/*
6
*  !!! USING TEMPORARY API ADDRESS AND USER
7
*/
8

    
9
import { Injectable } from '@angular/core';
10

    
11
import { Observable } from 'rxjs/Observable';
12

    
13
import 'rxjs/add/operator/map';
14
import { Http, Response, Headers, RequestOptions } from '@angular/http';
15
import { InterfaceInformation, JobsOfUser, StoredJob } from "../domain/typeScriptClasses";
16
import { apiUrl } from '../domain/tempAPI';
17

    
18
let headers = new Headers({ 'Content-Type': 'application/json' });
19
let httpOptions = new RequestOptions({ headers: headers });
20

    
21
@Injectable ()
22
export class MonitorService {
23
  private apiUrl = apiUrl + '/monitor/';
24

    
25
  constructor(private http: Http) { }
26

    
27
  getJobSummary(jobId: string, groupBy: string): Observable<StoredJob> {
28
    let url = `${this.apiUrl}getJobSummary?jobId=${jobId}&groupBy=${groupBy}`;
29
    console.log(`knocking on: ${url}`);
30
    return this.http.get(url)
31
      .map(res => <StoredJob>res.json() )
32
      .catch(this.handleError);
33
  }
34

    
35
  getJobsOfUser(userEmail: string,
36
                jobType:string,
37
                offset: string,
38
                limit: string,
39
                dateFrom: string,
40
                dateTo: string,
41
                validationStatus: string,
42
                includeJobsTotal): Observable<JobsOfUser> {
43
    let url = `${this.apiUrl}getJobsOfUser?user=${userEmail}`;
44
    if (jobType != '') {
45
      url = `${url}&jobType=${encodeURI(jobType)}&offset=${offset}&limit=${limit}&validationStatus=${validationStatus}&includeJobsTotal=${includeJobsTotal}`;
46
    } else {
47
      url = `${url}&offset=${offset}&limit=${limit}&validationStatus=${validationStatus}&includeJobsTotal=${includeJobsTotal}`;
48
    }
49
    console.log(`knocking on: ${url}`);
50
    let body = JSON.stringify({
51
      userEmail: userEmail,
52
      jobType: jobType,
53
      offset: offset,
54
      limit: limit,
55
      dateFrom: dateFrom,
56
      dateTo: dateTo,
57
      validationStatus: validationStatus,
58
      includeJobsTotal: includeJobsTotal
59
    });
60

    
61
    return this.http.get(url)
62
      .map(res => <JobsOfUser>res.json() )
63
      .catch(this.handleError);
64
  }
65

    
66

    
67
  private handleError(error: Response | any) {
68
    // In a real world app, we might use a remote logging infrastructure
69
    // We'd also dig deeper into the error to get a better message
70
    let errMsg = "";
71
    console.log(`E R R O R !!!`);
72
    console.log(error);
73
    if (error instanceof Response) {
74
      const body = error.text() || '';
75
      //const err = body.error || JSON.stringify(body);
76
      errMsg = `${error.status} - ${error.statusText || ''} ${body}`;
77
      console.log(errMsg);
78
    } else {
79
      errMsg = (error.message) ? error.message :
80
        error.status ? `${error.status} - ${error.statusText}` : 'Server error';
81
      console.error(errMsg); // log to console instead
82
    }
83
    return Observable.throw(errMsg);
84
  }
85

    
86
}
(5-5/8)