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("getMetrics 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
|
.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"]));
|
23
|
}
|
24
|
|
25
|
return this.http.get(url)
|
26
|
.map(res => <any> res.json())
|
27
|
.do(res => {
|
28
|
this._cache.set(key, res);
|
29
|
})
|
30
|
.map(res => this.parseMetrics(res["downloads"], res["views"], res["total_downloads"], res["total_views"],
|
31
|
res["total_openaire_views"], res["total_openaire_downloads"], res["pageviews"]));
|
32
|
}
|
33
|
|
34
|
|
35
|
parseMetrics(downloads: any, views: any, totalDownloads: string, totalViews: string,
|
36
|
totalOpenaireViews: string, totalOpenaireDownloads: string, pageViews: string): any {
|
37
|
this.metrics = new Metrics();
|
38
|
|
39
|
this.metrics.totalDownloads = totalDownloads;
|
40
|
this.metrics.totalViews = totalViews;
|
41
|
this.metrics.totalOpenaireViews = totalOpenaireViews;
|
42
|
this.metrics.totalOpenaireDownloads = totalOpenaireDownloads;
|
43
|
this.metrics.pageViews = pageViews;
|
44
|
|
45
|
this.metrics.infos = new Map<string, {"name": string, "url": string, "numOfDownloads": string, "numOfViews": string, "openaireDownloads": string, "openaireViews": string}>();
|
46
|
|
47
|
for(let i=0; i<downloads.length; i++) {
|
48
|
let id: string = downloads[i]['datasource_id'];
|
49
|
if(this.metrics.infos.has(id)) {
|
50
|
this.metrics.infos.get(id).numOfDownloads = downloads[i]['value'];
|
51
|
this.metrics.infos.get(id).openaireDownloads = downloads[i]['openaire'];
|
52
|
} else {
|
53
|
let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
|
54
|
info = {};
|
55
|
|
56
|
info.name = downloads[i]['datasource_name'];
|
57
|
info.url = OpenaireProperties.getsearchLinkToDataProvider()+id;
|
58
|
info.numOfDownloads = downloads[i]['value'];
|
59
|
info.openaireDownloads = downloads[i]['openaire'];
|
60
|
info.numOfViews = "0";
|
61
|
info.openaireViews = "0";
|
62
|
|
63
|
this.metrics.infos.set(id, info);
|
64
|
}
|
65
|
}
|
66
|
|
67
|
for(let i=0; i<views.length; i++) {
|
68
|
let id: string = views[i]['datasource_id'];
|
69
|
if(this.metrics.infos.has(id)) {
|
70
|
this.metrics.infos.get(id).numOfViews = views[i]['value'];
|
71
|
this.metrics.infos.get(id).openaireViews = views[i]['openaire'];
|
72
|
} else {
|
73
|
let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
|
74
|
info = {};
|
75
|
|
76
|
info.name = views[i]['datasource_name'];
|
77
|
info.url = OpenaireProperties.getsearchLinkToDataProvider()+id;
|
78
|
info.numOfDownloads = "0";
|
79
|
info.openaireDownloads = "0";
|
80
|
info.numOfViews = views[i]['value'];
|
81
|
info.openaireViews = views[i]['openaire'];
|
82
|
|
83
|
this.metrics.infos.set(id, info);
|
84
|
}
|
85
|
}
|
86
|
console.info("Metrics : "+this.metrics.infos.size);
|
87
|
|
88
|
return this.metrics;
|
89
|
}
|
90
|
}
|