Project

General

Profile

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

    
8

    
9
@Injectable()
10
export class StatisticsService {
11

    
12

    
13
    constructor(private http:HttpClient) { }
14

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

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

    
31

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