import {Injectable} from '@angular/core'; import {Http, Response} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {Metrics} from '../utils/entities/metrics'; import {OpenaireProperties} from '../utils/properties/openaireProperties'; import 'rxjs/add/operator/do'; @Injectable() export class MetricsService { metrics: Metrics; constructor(private http: Http ) {} getMetrics (id: string, entityType: string):any { console.info("getMetrics in service"); //let url = OpenaireProperties. getSearchAPIURLLast() + 'publications/' +id+"?format=json"; let url = OpenaireProperties.getMetricsAPIURL()+entityType+"/"+id+"/clicks"; let key = url; return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url) .map(res => res.json()) .map(res => this.parseMetrics(res["downloads"], res["views"], res["total_downloads"], res["total_views"], res["total_openaire_views"], res["total_openaire_downloads"], res["pageviews"])); } parseMetrics(downloads: any, views: any, totalDownloads: string, totalViews: string, totalOpenaireViews: string, totalOpenaireDownloads: string, pageViews: string): any { this.metrics = new Metrics(); this.metrics.totalDownloads = totalDownloads; this.metrics.totalViews = totalViews; this.metrics.totalOpenaireViews = totalOpenaireViews; this.metrics.totalOpenaireDownloads = totalOpenaireDownloads; this.metrics.pageViews = pageViews; this.metrics.infos = new Map(); for(let i=0; i