Project

General

Profile

1
import {Component, Input, Output, EventEmitter} from '@angular/core';
2
import {Metrics} from '../utils/entities/metrics';
3
import {MetricsService } from '../services/metrics.service';
4
import {ErrorCodes} from '../utils/properties/openaireProperties';
5

    
6
@Component({
7
    selector: 'metrics',
8
    template: `
9
    <!--div *ngIf="i>0 && tab.content=='metricsTab' && metrics != undefined" class="uk-panel uk-panel-box"-->
10
    <!--/div-->
11

    
12
    <!--dl *ngIf="metrics != undefined"
13
        class="uk-description-list-horizontal uk-panel uk-panel-box">
14
        <dt *ngIf="type=='results'">Total Downloads: </dt>
15
        <dt *ngIf="type!='results'" class="uk-text-break">
16
            Total <span [innerHTML]="name"></span> Publication Downloads:
17
        </dt>
18
        <dd>{{metrics.totalDownloads}}</dd>
19
        <dt>Total Views: </dt>
20
        <dd>{{metrics.totalViews}}</dd>
21
    </dl-->
22

    
23
    <!--div *ngIf="metrics == undefined" class="uk-alert uk-alert-warning">
24
        Metrics are currently unavailable
25
    </div-->
26

    
27
    <div *ngIf="status == errorCodes.LOADING" class="uk-alert uk-alert-primary" role="alert">Loading...</div>
28
    <div *ngIf="status == errorCodes.NONE" class="uk-alert uk-alert-primary" role="alert">No Results found</div>
29
    <div *ngIf="status == errorCodes.ERROR" class="uk-alert uk-alert-warning" role="alert">An Error Occured</div>
30
    <div *ngIf="status == errorCodes.NOT_AVAILABLE" class="uk-alert uk-alert-danger" role="alert">Service not available</div>
31

    
32
    <table *ngIf="metrics != undefined"
33
            class="uk-table uk-panel-box">
34
        <thead>
35
            <tr>
36
                <th *ngIf="type=='results'" class="uk-text-center">Total Views</th>
37
                <th *ngIf="type!='results'" class="uk-text-center">
38
                    Total <span [innerHTML]="name"></span> Publication Views
39
                </th>
40
                <th *ngIf="type=='results'" class="uk-text-center">Total Downloads</th>
41
                <th *ngIf="type!='results'" class="uk-text-center">
42
                    Total <span [innerHTML]="name"></span> Publication Downloads
43
                </th>
44
            </tr>
45
        </thead>
46
        <tbody>
47
            <tr>
48
                <td class="uk-text-center">
49
                    {{metrics.totalViews}}
50
                    <span *ngIf="metrics.totalViews > 0 && metrics.totalOpenaireViews > 0">
51
                      ( {{metrics.totalOpenaireViews}} from OpenAIRE )
52
                    </span>
53
                </td>
54
                <td class="uk-text-center">
55
                    {{metrics.totalDownloads}}
56
                    <span *ngIf="metrics.totalDownloads > 0 && metrics.totalOpenaireDownloads > 0">
57
                      ( {{metrics.totalOpenaireDownloads}} from OpenAIRE )
58
                    </span>
59
                </td>
60
            </tr>
61
        </tbody>
62
    </table>
63

    
64
    <table *ngIf="metrics != undefined && metrics.infos.size > 0"
65
            class="uk-table uk-table-striped">
66
        <thead>
67
            <tr>
68
                <th class="uk-text-center">From</th>
69
                <th class="uk-text-center">Number Of Views</th>
70
                <th class="uk-text-center">Number Of Downloads</th>
71
            </tr>
72
        </thead>
73
        <tbody>
74
            <tr *ngFor="let key of metrics.infos.keys()">
75
                <td class="uk-text-center">
76
                    <a href="{{metrics.infos.get(key).url}}">
77
                        {{metrics.infos.get(key).name}}
78
                    </a>
79
                </td>
80
                <td class="uk-text-center">
81
                    {{metrics.infos.get(key).numOfViews}}
82
                    <span *ngIf="metrics.infos.get(key).numOfViews > 0 && metrics.infos.get(key).openaireViews > 0">
83
                      ( {{metrics.infos.get(key).openaireViews}} from OpenAIRE )
84
                    </span>
85
                </td>
86
                <td class="uk-text-center">
87
                    {{metrics.infos.get(key).numOfDownloads}}
88
                    <span *ngIf="metrics.infos.get(key).numOfDownloads > 0 && metrics.infos.get(key).openaireDownloads > 0">
89
                      ( {{metrics.infos.get(key).openaireDownloads}} from OpenAIRE )
90
                    </span>
91
                </td>
92
            </tr>
93
        </tbody>
94
    </table>
95
    `
96
    })
97

    
98
export class MetricsComponent {
99
    @Output() metricsResults = new EventEmitter();
100
    @Input() id: string;
101
    @Input() type: string;
102
    @Input() name: string = "";
103
    public metrics: Metrics;
104
    public errorCodes:ErrorCodes;
105
    public status: number;
106

    
107
    constructor (private _metricsService: MetricsService) {}
108

    
109
    ngOnInit() {
110
        this.errorCodes = new ErrorCodes();
111
        this.status = this.errorCodes.LOADING;
112
        this.getMetrics();
113
    }
114

    
115

    
116
    getMetrics() {
117
        //if(this.id == undefined || this.id == "") {
118
        //    console.log("supplied id in metrics is not acceptable");
119
        //}
120
        //if(this.type == undefined || this.type == "") {
121
        //    console.log("supplied id in metrics is not acceptable");
122
        //}
123

    
124
        this._metricsService.getMetrics(this.id, this.type).subscribe(
125
         data => {
126
             this.metrics = data;
127
             this.status = this.errorCodes.DONE;
128
             this.metricsResults.emit({
129
                 totalViews: this.metrics.totalViews,
130
                 totalDownloads: this.metrics.totalDownloads,
131
                 pageViews: this.metrics.pageViews
132
             });
133
         },
134
         err => {
135
           console.log(err);
136
           this.status = this.errorCodes.ERROR;
137
           if(err.status == '404') {
138
               this.status = this.errorCodes.NOT_AVAILABLE;
139
           }
140
           this.metricsResults.emit({
141
               totalViews: 0,
142
               totalDownloads: 0
143
           });
144
         }
145
       );
146
    }
147
}
(3-3/13)