Project

General

Profile

1
import {Component, OnDestroy, OnInit} from '@angular/core';
2
import { AuthenticationService } from '../../../services/authentication.service';
3
import { RepositoryService } from '../../../services/repository.service';
4
import {
5
  AggregationDetails, BrokerSummary, BrowseEntry, CollectionMonitorSummary,
6
  MetricsInfo, PiwikInfo,
7
  Repository, StoredJob, UsageSummary
8
} from '../../../domain/typeScriptClasses';
9
import {
10
  loadingAggregationHistory, loadingAggregationHistoryError, loadingMetrics,
11
  loadingMetricsError, loadingSubscriptions, loadingTopics, loadingTopicsError,
12
  noAggregationHistory, noSubscriptionsFound, noTopicsFound,
13
  loadingJobSummary, loadingJobSummaryError
14
} from '../../../domain/shared-messages';
15
import {DashboardService} from '../../../services/dashboard.service';
16
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
17
import {PiwikService} from '../../../services/piwik.service';
18
import {ValidatorService} from '../../../services/validator.service';
19
import {ActivatedRoute} from '@angular/router';
20
import {SharedService} from '../../../services/shared.service';
21

    
22
@Component ({
23
  selector: 'app-dashboard',
24
  templateUrl: 'dashboard.component.html'
25
})
26

    
27
export class DashboardComponent implements OnInit, OnDestroy {
28

    
29
  repository: Repository = null;
30
  errorMessage: string;
31
  loadingMessage: string;
32

    
33
  constructor(private authService: AuthenticationService,
34
              private repositoryService: RepositoryService,
35
              private sharedService: SharedService,
36
              private dashboardService: DashboardService,
37
              private piwikService: PiwikService,
38
              private validatorService: ValidatorService,
39
              private sanitizer: DomSanitizer,
40
              private route: ActivatedRoute) {
41
  }
42

    
43
  loading: boolean = true;
44

    
45

    
46
  // Aggregations
47
  collectionMonitorSummary: CollectionMonitorSummary;
48
  lastIndexedVersion: AggregationDetails;
49
  latestAggregations: AggregationDetails[] = [];
50
  errorAggregationsMessage: string;
51
  noAggregations: string;
52
  loadingAggregationsMessage: string;
53

    
54
  // Usage Statistics
55
  usageSummary: UsageSummary;
56
  piwik: PiwikInfo;
57
  repoMetrics: MetricsInfo;
58
  errorUsageStatsMessage: string;
59
  noUsageStats: string;
60
  loadingUsageStatsMessage: string;
61
  pageViews = '--';
62
  totalViews = '--';
63
  totalDownloads = '--';
64
  viewsUrl: SafeResourceUrl;
65
  downloadsUrl: SafeResourceUrl;
66
  shortRepositoryId: string;
67
  currentDate: string;
68

    
69
  // Broker
70
  brokerSummary: BrokerSummary;
71
  errorTopicsMessage: string;
72
  noTopics: string;
73
  loadingTopicsMessage: string;
74
  errorSubscriptionsMessage: string;
75
  noSubscriptions: string;
76
  loadingSubscriptionsMessage: string;
77
  totalNumberOfEvents: number = 0;
78
  moreList: BrowseEntry[] = [];
79
  missingList: BrowseEntry[] = [];
80
  totalMore: number = 0;
81
  totalMissing: number = 0;
82

    
83
  // Validation
84
  storedJob: StoredJob[] = [];
85
  noValidationsMessage: string;
86
  errorValidationsMessage: string;
87
  loadingJobSummaryMessage: string;
88

    
89
  ngOnInit() {
90

    
91
    if (this.sharedService.getRepository()) {
92
      this.repository = this.sharedService.getRepository();
93
      this.getSelectedRepositorySummaryInfo(this.repository);
94
    }
95

    
96
    const body = document.getElementsByTagName('body')[0];
97
    body.classList.remove('top_bar_active');
98
    body.classList.remove('page_heading_active');
99
    body.classList.remove('landing');
100
    body.classList.add('dashboard');
101

    
102
    const currentTime = new Date();
103
    this.currentDate = currentTime.getFullYear() + '-' + (currentTime.getMonth() + 1);
104
  }
105

    
106
  ngOnDestroy() {
107
    // this.sharedService.repository.unsubscribe();
108
  }
109

    
110
  getSelectedRepositorySummaryInfo(repository: Repository) {
111

    
112
    // Aggregations
113
    this.loadingAggregationsMessage = loadingAggregationHistory;
114
    this.latestAggregations = [];
115
    this.lastIndexedVersion = null;
116
    this.dashboardService.getCollectionMonitorSummary(repository.id, 5).subscribe(
117
      collectionMonitorSummary => this.getCollectionMonitorSummary(collectionMonitorSummary),
118
      error => {
119
        this.loadingAggregationsMessage = '';
120
        this.errorAggregationsMessage = loadingAggregationHistoryError;
121
      },
122
      () => {
123
        this.loadingAggregationsMessage = '';
124
        this.errorAggregationsMessage = '';
125
      }
126
    );
127

    
128
    // Usage Statistics
129
    this.loadingUsageStatsMessage = loadingMetrics;
130
    this.usageSummary = null;
131
    this.piwik = null;
132
    this.repoMetrics = null;
133
    this.pageViews = '--';
134
    this.totalViews = '--';
135
    this.totalDownloads = '--';
136
    this.viewsUrl = null;
137
    this.downloadsUrl = null;
138
    this.shortRepositoryId = null;
139
    this.dashboardService.getUsageSummary(repository.id).subscribe(
140
      usageSummary => this.getUsageSummary(usageSummary),
141
      error => {
142
        this.loadingUsageStatsMessage = '';
143
        this.errorUsageStatsMessage = loadingMetricsError;
144
        console.log(error);
145
      } ,
146
      () => {
147
        this.shortRepositoryId = repository.id.replace(/_/g, '').replace('::', ':');
148
        this.loadingUsageStatsMessage = '';
149
        this.errorUsageStatsMessage = '';
150
      }
151
    );
152

    
153
    // Broker
154
    this.loadingTopicsMessage = loadingTopics;
155
    this.loadingSubscriptionsMessage = loadingSubscriptions;
156
    this.brokerSummary = null;
157
    this.totalNumberOfEvents = 0;
158
    this.moreList = [];
159
    this.missingList = [];
160
    this.totalMore = 0;
161
    this.totalMissing = 0;
162
    this.dashboardService.getBrokerSummary(this.getCorrectName()).subscribe(
163
      brokerSummary => this.getBrokerSummary(brokerSummary),
164
      error => {
165
        this.loadingTopicsMessage = '';
166
        this.loadingSubscriptionsMessage = '';
167
        this.errorTopicsMessage = loadingTopicsError;
168
        this.errorSubscriptionsMessage = 'Failed to load the subscriptions for your datasource';
169
        console.log(error);
170
      },
171
      () => {
172
        this.loadingTopicsMessage = '';
173
        this.loadingSubscriptionsMessage = '';
174
        this.errorTopicsMessage = '';
175
        this.errorSubscriptionsMessage = '';
176
      }
177
    );
178

    
179
    // Validation
180
    this.loadingJobSummaryMessage = loadingJobSummary;
181
    this.noValidationsMessage = '';
182
    this.errorValidationsMessage = '';
183
    this.validatorService.getValidationSummary(repository.id).subscribe(
184
      validationSummary => {
185
        this.storedJob = validationSummary;
186
        // console.log(validationSummary);
187
      },
188
      error => {
189
        this.errorValidationsMessage = loadingJobSummaryError;
190
        this.loadingJobSummaryMessage = '';
191
        console.log(error);
192
      } ,
193
      () => {
194
        this.getValidationSummary(this.storedJob);
195
        this.loadingJobSummaryMessage = '';
196
      }
197
    );
198
  }
199

    
200
  getCollectionMonitorSummary(collectionMonitorSummary: CollectionMonitorSummary) {
201

    
202
    this.latestAggregations = collectionMonitorSummary.aggregationDetails;
203
    this.lastIndexedVersion = collectionMonitorSummary.lastIndexedVersion;
204

    
205
    if ( !this.latestAggregations || (this.latestAggregations.length === 0) ) {
206
      this.noAggregations = noAggregationHistory;
207
    }
208
  }
209

    
210
  getBrokerSummary(brokerSummary: BrokerSummary) {
211

    
212
    this.noSubscriptions = '';
213
    this.noTopics = '';
214

    
215
    this.brokerSummary = brokerSummary;
216

    
217
    if (this.brokerSummary.userSubs == null)
218
      this.noSubscriptions = noTopicsFound;
219
    if (this.brokerSummary.topicsForDatasource == null)
220
      this.noTopics = noSubscriptionsFound;
221

    
222
    this.totalNumberOfEvents = 0;
223
    this.totalMore = 0;
224
    this.totalMissing = 0;
225
    if (brokerSummary.topicsForDatasource) {
226
      for (const browseEntry of brokerSummary.topicsForDatasource) {
227
        this.totalNumberOfEvents += browseEntry.size;
228
        if (browseEntry.value.startsWith('ENRICH/MORE')) {
229
          this.totalMore += browseEntry.size;
230
          this.moreList.push(browseEntry);
231
        } else if (browseEntry.value.startsWith('ENRICH/MISSING')) {
232
          this.totalMissing += browseEntry.size;
233
          this.missingList.push(browseEntry);
234
        }
235
      }
236
    }
237

    
238

    
239
  }
240

    
241
  getUsageSummary(usageSummary: UsageSummary) {
242

    
243
    this.noUsageStats = '';
244

    
245
    if (usageSummary.piwikInfo == null) {
246
      this.noUsageStats = 'This repository does not have our Usage Statistics service enabled yet';
247
    } else {
248
      this.usageSummary = usageSummary;
249
      this.piwik = usageSummary.piwikInfo;
250
      this.repoMetrics = usageSummary.metricsInfo;
251
      if (this.repoMetrics.metricsNumbers.pageviews) {
252
        this.pageViews = this.repoMetrics.metricsNumbers.pageviews;
253
      }
254
      if (this.repoMetrics.metricsNumbers.total_views) {
255
        this.totalViews = this.repoMetrics.metricsNumbers.total_views;
256
      }
257
      if (this.repoMetrics.metricsNumbers.total_downloads) {
258
        this.totalDownloads = this.repoMetrics.metricsNumbers.total_downloads;
259
      }
260
      this.getViewsUrl();
261
      this.getDownloadsUrl();
262
    }
263
  }
264

    
265
  getValidationSummary(validationSummary: StoredJob[]) {
266
    if (validationSummary == null) { this.noValidationsMessage = 'There is no validation history for this repository at the moment'; }
267
  }
268

    
269
  getViewsUrl () {
270

    
271
    const encodedURL = encodeURIComponent('{"library":"HighCharts","chartDescription":{"queries":[{"name":"Monthly views","type":"line","query":{"name":"usagestats.views.monthly", "parameters":["' + this.piwik.openaireId + '"], "profile":"OpenAIRE All-inclusive" }}],"chart":{"backgroundColor":"#FFFFFFFF","borderColor":"#335cadff","borderRadius":0,"borderWidth":0,"plotBorderColor":"#ccccccff","plotBorderWidth":0},"title":{"text":""},"subtitle":{},"yAxis":{"title":{"text":"Monthly views"}},"xAxis":{"title":{}},"lang":{"noData":"No Data available for the Query"},"exporting":{"enabled":false},"plotOptions":{"series":{"dataLabels":{"enabled":false}}},"legend":{"enabled":false},"credits":{"href":null,"enabled":true,"text":"Created by OpenAIRE via HighCharts"}}}');
272
    this.viewsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(`${this.repoMetrics.diagramsBaseURL}chart?json=${encodedURL}`);
273
  }
274

    
275
  getDownloadsUrl () {
276

    
277
    const encodedURL = encodeURIComponent('{"library":"HighCharts","chartDescription":{"queries":[{"name":"Monthly downloads","type":"line","query":{"name":"usagestats.downloads.monthly", "parameters":["' + this.piwik.openaireId + '"], "profile":"OpenAIRE All-inclusive" }}],"chart":{"backgroundColor":"#FFFFFFFF","borderColor":"#335cadff","borderRadius":0,"borderWidth":0,"plotBorderColor":"#ccccccff","plotBorderWidth":0},"title":{"text":""},"subtitle":{},"yAxis":{"title":{"text":"Monthly downloads"}},"xAxis":{"title":{}},"lang":{"noData":"No Data available for the Query"},"exporting":{"enabled":false},"plotOptions":{"series":{"dataLabels":{"enabled":false}}},"legend":{"enabled":false},"credits":{"href":null,"enabled":true,"text":"Created by OpenAIRE via HighCharts"}}}');
278
    this.downloadsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(`${this.repoMetrics.diagramsBaseURL}chart?json=${encodedURL}`);
279
  }
280

    
281
  getCorrectName() {
282
    const temp = this.repository.officialname.split('|');
283
    let correctName = temp[0];
284
    let repoName = temp[0];
285
    for (let i = 1; i < temp.length; i++) {
286
      correctName += `/${temp[i]}`;
287
      repoName += ` | ${temp[i]}`;
288
    }
289

    
290
    return correctName;
291
  }
292
}
(2-2/2)