Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {Metrics} from '../utils/entities/metrics';
5
import 'rxjs/add/operator/do';
6
import{EnvProperties} from '../utils/properties/env-properties';
7

    
8
@Injectable()
9
export class MetricsService {
10
    metrics: Metrics;
11

    
12
    constructor(private http: Http ) {}
13

    
14
    getMetrics (id: string, entityType: string, properties:EnvProperties):any {
15
        console.info("getMetrics in service");
16
        let url = properties.metricsAPIURL+entityType+"/"+id+"/clicks";
17
        let key = url;
18

    
19
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
20
                    .map(res => <any> res.json())
21
                    .map(res => this.parseMetrics(res["downloads"], res["views"], res["total_downloads"], res["total_views"],
22
                                                  res["total_openaire_views"], res["total_openaire_downloads"], res["pageviews"], properties));
23
    }
24

    
25

    
26
    parseMetrics(downloads: any, views: any, totalDownloads: number, totalViews: number,
27
                totalOpenaireViews: number, totalOpenaireDownloads: number, pageViews: number, properties:EnvProperties): any {
28
        this.metrics = new Metrics();
29

    
30
        this.metrics.totalDownloads = totalDownloads;
31
        this.metrics.totalViews = totalViews;
32
        this.metrics.totalOpenaireViews = totalOpenaireViews;
33
        this.metrics.totalOpenaireDownloads = totalOpenaireDownloads;
34
        this.metrics.pageViews = pageViews;
35

    
36
        this.metrics.infos = new Map<string, {"name": string, "url": string, "numOfDownloads": string, "numOfViews": string, "openaireDownloads": string, "openaireViews": string}>();
37

    
38
        for(let i=0; i<downloads.length; i++) {
39
            let id: string = downloads[i]['datasource_id'];
40
            if(this.metrics.infos.has(id)) {
41
                this.metrics.infos.get(id).numOfDownloads = downloads[i]['value'];
42
                this.metrics.infos.get(id).openaireDownloads = downloads[i]['openaire'];
43
            } else {
44
                let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
45
                info = {};
46

    
47
                info.name = downloads[i]['datasource_name'];
48
                info.url = properties.searchLinkToDataProvider+id;
49
                info.numOfDownloads = downloads[i]['value'];
50
                info.openaireDownloads = downloads[i]['openaire'];
51
                info.numOfViews = "0";
52
                info.openaireViews = "0";
53

    
54
                this.metrics.infos.set(id, info);
55
            }
56
        }
57

    
58
        for(let i=0; i<views.length; i++) {
59
            let id: string = views[i]['datasource_id'];
60
            if(this.metrics.infos.has(id)) {
61
                this.metrics.infos.get(id).numOfViews = views[i]['value'];
62
                this.metrics.infos.get(id).openaireViews = views[i]['openaire'];
63
            } else {
64
                let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
65
                info = {};
66

    
67
                info.name = views[i]['datasource_name'];
68
                info.url = properties.searchLinkToDataProvider + id;
69
                info.numOfDownloads = "0";
70
                info.openaireDownloads = "0";
71
                info.numOfViews = views[i]['value'];
72
                info.openaireViews = views[i]['openaire'];
73

    
74
                this.metrics.infos.set(id, info);
75
            }
76
        }
77
        console.info("Metrics : "+this.metrics.infos.size);
78

    
79
        return this.metrics;
80
    }
81
}
(3-3/21)