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 {OpenaireProperties} from '../utils/properties/openaireProperties';
6
import 'rxjs/add/operator/do';
7
import { CacheService  } from '../shared/cache.service';
8
@Injectable()
9
export class MetricsService {
10
    metrics: Metrics;
11

    
12
    constructor(private http: Http, public _cache: CacheService) {}
13

    
14
    getMetrics (id: string, entity: string):any {
15
        console.info("getPublicationViews in service");
16
        //let url = OpenaireProperties. getSearchAPIURLLast() + 'publications/' +id+"?format=json";
17
        let url = OpenaireProperties.getMetricsAPIURL()+entity+"/"+id+"/clicks";
18
        let key = url;
19
        if (this._cache.has(key)) {
20
          return Observable.of(this._cache.get(key));
21
        }
22
        return this.http.get(url)
23
                    .map(res => <any> res.json())
24
                    .map(res => this.parseMetrics(res["downloads"], res["views"], res["total_downloads"], res["total_views"]))
25
                    .do(res => {
26
                      this._cache.set(key, res);
27
                    });
28
    }
29

    
30

    
31
    parseMetrics(downloads: string, views: string, totalDownloads: any, totalViews: any): any {
32

    
33
        this.metrics = new Metrics();
34

    
35
        this.metrics.totalDownloads = totalDownloads;
36
        this.metrics.totalViews = totalViews;
37

    
38
        this.metrics.infos = new Map<string, {"name": string, "url": string, "numOfDownloads": string, "numOfViews": string}>();
39

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

    
48
                info.name = downloads[i]['datasource_name'];
49
                info.url = OpenaireProperties.getsearchLinkToDataProvider()+id;
50
                info.numOfDownloads = downloads[i]['value'];
51
                info.numOfViews = "0";
52

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

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

    
65
                info.name = views[i]['datasource_name'];
66
                info.url = OpenaireProperties.getsearchLinkToDataProvider()+id;
67
                info.numOfDownloads = "0";
68
                info.numOfViews = views[i]['value'];
69

    
70
                this.metrics.infos.set(id, info);
71
            }
72
        }
73
        console.info(this.metrics.infos.size);
74

    
75
        return this.metrics;
76
    }
77
}
(5-5/24)