import {Component, Input, Output, EventEmitter, ChangeDetectorRef} from '@angular/core'; import {Metrics} from '../../../utils/entities/metrics'; import {MetricsService} from '../../../services/metrics.service'; import {ErrorCodes} from '../../../utils/properties/errorCodes'; import {Subscription, zip} from 'rxjs'; import {EnvProperties} from '../../../utils/properties/env-properties'; import {ClickEvent} from "../../../utils/click/click-outside-or-esc.directive"; import {NumberUtils} from "../../../utils/number-utils.class"; import {OpenaireEntities} from "../../../utils/properties/searchFields"; @Component({ selector: 'metrics', styleUrls: ['metrics.component.css'], template: `
usage counts
{{formatNumber(total)}} {{total | number}}
{{openaireEntities.PROJECT}} metrics are derived from aggregating individual {{openaireEntities.RESULTS}} metrics.
- {{formatNumber(metrics.totalDownloads)}}
Downloads
- {{formatNumber(pageViews)}}
OpenAIRE views
- {{formatNumber(metrics.totalViews)}}
Total views
  • From Views Downloads
    {{metrics.infos.get(key).name}} {{formatNumber(metrics.infos.get(key).numOfViews)}} {{metrics.infos.get(key).numOfViews | number}} {{formatNumber(metrics.infos.get(key).numOfDownloads)}} {{metrics.infos.get(key).numOfDownloads | number}}
Powered by usage counts
` }) export class MetricsComponent { @Output() metricsResults = new EventEmitter(); @Input() id: string; @Input() entityType: string; @Input() entity: string; //@Input() name: string = ""; @Input() pageViews: number = 0; @Input() properties: EnvProperties; @Input() shortView: boolean = false; @Input() open = false; @Input() viewsFrameUrl: string; @Input() downloadsFrameUrl: string; public metrics: Metrics; public errorCodes: ErrorCodes; private sub: Subscription; private timeouts: any[] = []; public metricsClicked: boolean = false; public status: number; public state: number = -1; public openaireEntities = OpenaireEntities; constructor(private metricsService: MetricsService, private cdr: ChangeDetectorRef) { } ngOnInit() { this.errorCodes = new ErrorCodes(); if (typeof document !== 'undefined') { this.status = this.errorCodes.LOADING; this.getMetrics(); } } ngOnDestroy() { if(this.sub) { this.sub.unsubscribe(); } } public get total(): number { return +this.pageViews + +this.metrics.totalViews + +this.metrics.totalDownloads; } private getMetrics() { //queries from old API - replaced with queries to the stats tool /* this.sub = this.metricsService.getMetrics(this.id, this.entityType, this.properties).subscribe( data => { this.metrics = data; this.cdr.detectChanges(); this.status = this.errorCodes.DONE; this.metricsResults.emit({ totalViews: this.metrics.totalViews, totalDownloads: this.metrics.totalDownloads, pageViews: this.metrics.pageViews }); }, err => { if (err.status == '404') { this.status = this.errorCodes.NOT_FOUND; } else if (err.status == '500') { this.status = this.errorCodes.ERROR; } else { this.status = this.errorCodes.NOT_AVAILABLE; } this.metricsResults.emit({ totalViews: 0, totalDownloads: 0 }); } );*/ let obs; if (this.entityType == "results") { obs = zip(this.metricsService.getMetricsNumber(this.id, "usagestats.results.views", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.results.downloads", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.results.views.openaire", this.properties), this.metricsService.getMetricsNumbersByRepository(this.id, "usagestats.results.viewsdownloads.repository", this.properties) ); } else if (this.entityType == "projects") { obs = zip(this.metricsService.getMetricsNumber(this.id, "usagestats.projects.views", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.projects.downloads", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.projects.views.openaire", this.properties)); } else if (this.entityType == "datasources") { obs = zip(this.metricsService.getMetricsNumber(this.id, "usagestats.views.repository.local", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.downloads.repository.local", this.properties), this.metricsService.getMetricsNumber(this.id, "usagestats.views.openaire", this.properties), // this.metricsService.getMetricsNumber(this.id, "usagestats.downloads.repository.openaire", this.properties) ); } this.sub = obs.subscribe( data => { this.metrics = new Metrics(); this.metrics.infos = new Map(); this.metrics.totalViews = data[0] ? data[0] : 0; this.metrics.totalDownloads = data[1] ? data[1] : 0; this.metrics.pageViews = data[2] ? data[2] : 0; this.metrics.infos = data[3] ? data[3] : 0; this.cdr.detectChanges(); this.status = this.errorCodes.DONE; this.metricsResults.emit({ totalViews: this.metrics.totalViews, totalDownloads: this.metrics.totalDownloads, pageViews: this.metrics.pageViews }); }, err => { if (err.status == '404') { this.status = this.errorCodes.NOT_FOUND; } else if (err.status == '500') { this.status = this.errorCodes.ERROR; } else { this.status = this.errorCodes.NOT_AVAILABLE; } this.metricsResults.emit({ totalViews: 0, totalDownloads: 0, pageViews: 0 }); } ); } public close(event: ClickEvent) { if(event.clicked && this.state !== -1) { this.timeouts.forEach(timeout => { clearTimeout(timeout); }); this.state = -1; this.timeouts = []; } } public clickedMetrics() { setTimeout( () => { this.metricsClicked = true; }); } public toggle(event) { this.metricsClicked = true; event.stopPropagation(); if(this.state !== -1) { this.timeouts.forEach(timeout => { clearTimeout(timeout); }); this.state = -1; this.timeouts = []; } else { this.state++; this.timeouts.push(setTimeout(() => { this.state++; this.timeouts.push(setTimeout(() => { this.state++; this.timeouts.push(setTimeout(() => { this.state++; this.timeouts.push(setTimeout(() => { this.state++; }, 400)); }, 800)); }, 300)); }, 100)); } this.cdr.detectChanges(); } public getKeys(map) { return Array.from(map.keys()); } public isNumberLarge(num: number | string, bigNum: boolean = false) { let limit: number = bigNum ? 100000000 : 100000; return (+num) >= limit; } public formatNumber(num: number | string) { let formatted = NumberUtils.roundNumber(+num); return formatted.number + formatted.size; } }