openaire-library/landingPages/landing-utils/metrics.component.ts

171 lines
7.0 KiB
TypeScript
Raw Normal View History

import {Component, Input, Output, EventEmitter} from '@angular/core';
import {Metrics} from '../../utils/entities/metrics';
import {MetricsService } from '../../services/metrics.service';
import {ErrorCodes} from '../../utils/properties/errorCodes';
import { Subscription } from 'rxjs/Subscription';
import{EnvProperties} from '../../utils/properties/env-properties';
@Component({
selector: 'metrics',
template: `
<div *ngIf="!shortView">
<errorMessages [status]="[status]" [type]="'metrics'" tab_error_class=true></errorMessages>
<div *ngIf="metrics && pageViews == 0 && metrics.totalViews == 0 && metrics.totalDownloads == 0" class="uk-alert uk-alert-primary">
No metrics available
</div>
<div *ngIf="metrics != undefined && (pageViews >0 || metrics.totalViews > 0|| metrics.totalDownloads >0)" class="uk-child-width-1-3@m uk-grid-small uk-grid-match uk-grid" uk-grid="">
<div *ngIf="entityType == 'projects'" class="uk-width-1-1 uk-text-center uk-text-meta uk-margin">Project metrics are derived from aggregating individual research results metrics.</div>
<div class="metrics-openaire uk-first-column">
<div class="uk-tile uk-tile-default uk-padding-small">
<div class="uk-text-center">{{pageViews | number}}</div>
<div class="uk-text-center">views in OpenAIRE</div>
</div>
</div>
<div class="metrics-views">
<div class="uk-tile uk-tile-muted uk-padding-small">
<div class="uk-text-center"> {{metrics.totalViews | number}}
<span *ngIf="metrics.totalViews > 0 && metrics.totalOpenaireViews > 0">
( {{metrics.totalOpenaireViews | number}} from OpenAIRE )
</span></div>
<div class="uk-text-center">views in local repository</div>
</div>
</div>
<div class="metrics-downloads">
<div class="uk-tile uk-tile-primary uk-padding-small">
<div class="uk-text-center"> {{metrics.totalDownloads | number}}
<span *ngIf="metrics.totalDownloads > 0 && metrics.totalOpenaireDownloads > 0">
( {{metrics.totalOpenaireDownloads | number}} from OpenAIRE )
</span></div>
<div class="uk-text-center">downloads in local repository</div>
</div>
</div>
</div>
<p *ngIf="metrics != undefined && metrics.infos.size > 0" >The information is available from the following content providers: </p>
<table *ngIf="metrics != undefined && metrics.infos.size > 0"
class="uk-table uk-table-small uk-table-striped">
<thead>
<tr>
<th class="uk-text-center uk-text-bold">From</th>
<th class="uk-text-center uk-text-bold">Number Of Views</th>
<th class="uk-text-center uk-text-bold">Number Of Downloads</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let key of getKeys(metrics.infos)">
<td class="uk-text-center">
<a href="{{metrics.infos.get(key).url}}">
{{metrics.infos.get(key).name}}
</a>
</td>
<td class="uk-text-center">
{{metrics.infos.get(key).numOfViews | number}}
<span *ngIf="metrics.infos.get(key).numOfViews > 0 && metrics.infos.get(key).openaireViews > 0">
( {{metrics.infos.get(key).openaireViews | number}} from OpenAIRE )
</span>
</td>
<td class="uk-text-center">
{{metrics.infos.get(key).numOfDownloads | number}}
<span *ngIf="metrics.infos.get(key).numOfDownloads > 0 && metrics.infos.get(key).openaireDownloads > 0">
( {{metrics.infos.get(key).openaireDownloads | number}} from OpenAIRE )
</span>
</td>
</tr>
</tbody>
</table>
</div>
<div *ngIf="shortView && metrics && (metrics.pageViews >0 || metrics.totalViews > 0|| metrics.totalDownloads >0)">
<dl *ngIf="metrics" class="uk-description-list-line">
<dt class="sideInfoTitle">Metrics</dt>
<dd class="line" >
{{metrics.pageViews | number}} views in OpenAIRE
</dd>
<dd class="line">
{{metrics.totalViews | number}}
<span *ngIf="metrics.totalViews > 0 && metrics.totalOpenaireViews > 0">
( {{metrics.totalOpenaireViews | number}} from OpenAIRE )
</span>
views in local repository
</dd>
<dd class="line">
{{metrics.totalDownloads | number}}
<span *ngIf="metrics.totalDownloads > 0 && metrics.totalOpenaireDownloads > 0">
( {{metrics.totalOpenaireDownloads | number}} from OpenAIRE )
</span> downloads in local repository
</dd>
</dl>
</div>
`
})
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;
public metrics: Metrics;
public errorCodes:ErrorCodes;
private sub: Subscription;
public status: number;
constructor (private _metricsService: MetricsService) {}
ngOnInit() {
this.errorCodes = new ErrorCodes();
this.status = this.errorCodes.LOADING;
this.getMetrics();
}
ngOnDestroy() {
this.sub.unsubscribe();
}
private getMetrics() {
//if(this.id == undefined || this.id == "") {
// console.log("supplied id in metrics is not acceptable");
//}
//if(this.type == undefined || this.type == "") {
// console.log("supplied id in metrics is not acceptable");
//}
this.sub = this._metricsService.getMetrics(this.id, this.entityType, this.properties).subscribe(
data => {
this.metrics = data;
this.status = this.errorCodes.DONE;
this.metricsResults.emit({
totalViews: this.metrics.totalViews,
totalDownloads: this.metrics.totalDownloads,
pageViews: this.metrics.pageViews
});
},
err => {
console.log(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
});
}
);
}
public getKeys( map) {
return Array.from(map.keys());
}
}