2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
|
|
|
import {Observable} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Metrics} from '../utils/entities/metrics';
|
2019-06-03 15:20:36 +02:00
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../utils/properties/env-properties';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {map} from "rxjs/operators";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MetricsService {
|
|
|
|
metrics: Metrics;
|
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-03-16 14:09:46 +01:00
|
|
|
hasAltMetrics(url: string, doi: string): Observable<boolean> {
|
|
|
|
return this.http.get(url + doi).pipe(map(res => !!(res)));
|
|
|
|
}
|
2021-09-08 14:21:30 +02:00
|
|
|
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<string, {"name": string, "url": string, "numOfDownloads": string, "numOfViews": string, "openaireDownloads": string, "openaireViews": string}>();
|
|
|
|
try{
|
|
|
|
|
|
|
|
let results = res["datasets"]["0"]["series"]["result"];
|
|
|
|
for(let i=0; i<results.length; i++) {
|
|
|
|
let result = results[i]["row"];
|
|
|
|
let id: string = result[1];
|
|
|
|
if(map.has(id)) {
|
|
|
|
if(result[0] == "views"){
|
|
|
|
map.get(id).numOfViews = result[3];
|
|
|
|
map.get(id).openaireViews = result[4];
|
|
|
|
}else if(result[0] == "downloads") {
|
|
|
|
map.get(id).numOfDownloads = result[3];
|
|
|
|
map.get(id).openaireDownloads = result[4];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
|
|
|
|
info = {};
|
|
|
|
|
|
|
|
info.name = result[2];
|
|
|
|
info.url = properties.searchLinkToDataProvider+id;
|
|
|
|
info.numOfDownloads = "0";
|
|
|
|
info.openaireDownloads = "0";
|
|
|
|
info.numOfViews = "0";
|
|
|
|
info.openaireViews = "0";
|
|
|
|
if(result[0] == "views"){
|
|
|
|
info.numOfViews = result[3];
|
|
|
|
info.openaireViews = result[4];
|
|
|
|
}else if(result[0] == "downloads") {
|
|
|
|
info.numOfDownloads = result[3];
|
|
|
|
info.openaireDownloads = result[4];
|
|
|
|
}
|
|
|
|
if(info.numOfViews + info.openaireViews + info.numOfDownloads + info.openaireDownloads > 0) {
|
|
|
|
map.set(id, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch(e){
|
|
|
|
console.error(e)
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
getMetrics (id: string, entityType: string, properties:EnvProperties):any {
|
|
|
|
let url = properties.metricsAPIURL+entityType+"/"+id+"/clicks";
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url;
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2019-06-03 15:20:36 +02:00
|
|
|
.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)));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-26 12:09:06 +02:00
|
|
|
parseMetrics(downloads: any, views: any, totalDownloads: number, totalViews: number,
|
|
|
|
totalOpenaireViews: number, totalOpenaireDownloads: number, pageViews: number, properties:EnvProperties): any {
|
2017-12-19 13:53:46 +01:00
|
|
|
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<string, {"name": string, "url": string, "numOfDownloads": string, "numOfViews": string, "openaireDownloads": string, "openaireViews": string}>();
|
|
|
|
|
|
|
|
for(let i=0; i<downloads.length; i++) {
|
|
|
|
let id: string = downloads[i]['datasource_id'];
|
|
|
|
if(this.metrics.infos.has(id)) {
|
|
|
|
this.metrics.infos.get(id).numOfDownloads = downloads[i]['value'];
|
|
|
|
this.metrics.infos.get(id).openaireDownloads = downloads[i]['openaire'];
|
|
|
|
} else {
|
|
|
|
let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
|
|
|
|
info = {};
|
|
|
|
|
|
|
|
info.name = downloads[i]['datasource_name'];
|
2018-02-05 14:14:59 +01:00
|
|
|
info.url = properties.searchLinkToDataProvider+id;
|
2017-12-19 13:53:46 +01:00
|
|
|
info.numOfDownloads = downloads[i]['value'];
|
|
|
|
info.openaireDownloads = downloads[i]['openaire'];
|
|
|
|
info.numOfViews = "0";
|
|
|
|
info.openaireViews = "0";
|
|
|
|
|
|
|
|
this.metrics.infos.set(id, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(let i=0; i<views.length; i++) {
|
|
|
|
let id: string = views[i]['datasource_id'];
|
|
|
|
if(this.metrics.infos.has(id)) {
|
|
|
|
this.metrics.infos.get(id).numOfViews = views[i]['value'];
|
|
|
|
this.metrics.infos.get(id).openaireViews = views[i]['openaire'];
|
|
|
|
} else {
|
|
|
|
let info;//: {"url": string, "numOfDownloads": string, "numOfViews": string};
|
|
|
|
info = {};
|
|
|
|
|
|
|
|
info.name = views[i]['datasource_name'];
|
2018-02-05 14:14:59 +01:00
|
|
|
info.url = properties.searchLinkToDataProvider + id;
|
2017-12-19 13:53:46 +01:00
|
|
|
info.numOfDownloads = "0";
|
|
|
|
info.openaireDownloads = "0";
|
|
|
|
info.numOfViews = views[i]['value'];
|
|
|
|
info.openaireViews = views[i]['openaire'];
|
|
|
|
|
|
|
|
this.metrics.infos.set(id, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.metrics;
|
|
|
|
}
|
|
|
|
}
|