Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import { Observable } from 'rxjs/Rx';
4
import {StatisticsDisplay, StatisticsSummary} from "../../openaireLibrary/connect/statistics/statisticsEntities";
5
import {catchError, map} from "rxjs/operators";
6
import {throwError} from "rxjs";
7
import {EnvProperties} from "../../openaireLibrary/utils/properties/env-properties";
8

    
9

    
10
@Injectable()
11
export class StatisticsService {
12

    
13

    
14
    constructor(private http:HttpClient) { }
15

    
16
    getCommunityStatistics(properties:EnvProperties, communityId: string): Observable<StatisticsSummary> {
17
        let url = properties.statisticsAPIURL+"/communities/"+communityId;
18
        //console.log(`getting statistics summary from: ${url}`);
19
        return this.http.get<any>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
20
            //.map(res => <any>res.json())
21
            .pipe(map(res => res['statistics']));
22
    }
23

    
24
    getCommunityAdminStatisticsChoices(properties: EnvProperties, communityId: string): Observable<StatisticsDisplay> {
25
        let url = properties.adminToolsAPIURL+"/statistics/"+communityId;
26
        //console.log(`getting admin choices for statistics from: ${url}`);
27
        return this.http.get<StatisticsDisplay>((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
28
            //.map(stats => <StatisticsDisplay>stats.json())
29
            .pipe(catchError(this.handleError));
30
    }
31

    
32

    
33
    private handleError (error: HttpErrorResponse) {
34
        // in a real world app, we may send the error to some remote logging infrastructure
35
        // instead of just logging it to the console
36
        console.log(error);
37
        return throwError(error  || 'Server error');
38
    }
39
}
    (1-1/1)