import {Injectable} from '@angular/core'; import {HttpClient} from "@angular/common/http"; import {Observable} from 'rxjs'; import {Metrics} from '../utils/entities/metrics'; import{EnvProperties} from '../utils/properties/env-properties'; import {map} from "rxjs/operators"; @Injectable() export class MetricsService { metrics: Metrics; constructor(private http: HttpClient ) {} hasAltMetrics(url: string, doi: string): Observable { return this.http.get(url + doi).pipe(map(res => !!(res))); } getMetricsNumber (id: string, table: string, properties:EnvProperties):any { let url =properties.statisticsFrameNewAPIURL + 'raw?json=' + encodeURIComponent('{"series":[{"query":{"name":"' + table + '", "parameters":["' + id + '"], "profile":"OpenAIRE All-inclusive"}}],"verbose":true}'); return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => this.parseMetricsNumber(res))); } parseMetricsNumber(res){ try{ return +res["datasets"]["0"]["series"]["result"]["0"]["row"]["0"]; }catch(e){ return null; } } getMetricsNumbersByRepository (id: string, table: string, properties:EnvProperties):any { let url =properties.statisticsFrameNewAPIURL + 'raw?json=' + encodeURIComponent('{"series":[{"query":{"name":"' + table + '", "parameters":["' + id + '","' + id + '","' + id + '"], "profile":"OpenAIRE All-inclusive"}}],"verbose":true}'); return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => this.parseMetricsNumbersByRepository(res,properties))); } parseMetricsNumbersByRepository(res,properties){ let map = new Map(); try{ let results = res["datasets"]["0"]["series"]["result"]; for(let i=0; i 0) { map.set(id, info); } } } }catch(e){ console.error(e) } console.log(map) return map; } getMetrics (id: string, entityType: string, properties:EnvProperties):any { let url = properties.metricsAPIURL+entityType+"/"+id+"/clicks"; let key = url; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => this.parseMetrics(res["downloads"], res["views"], res["total_downloads"], res["total_views"], res["total_openaire_views"], res["total_openaire_downloads"], res["pageviews"], properties))); } parseMetrics(downloads: any, views: any, totalDownloads: number, totalViews: number, totalOpenaireViews: number, totalOpenaireDownloads: number, pageViews: number, properties:EnvProperties): 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