Project

General

Profile

1
import { Component, 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,
8
  RepositorySnippet,
9
  RepositorySummaryInfo, StoredJob, UsageSummary
10
} from '../../domain/typeScriptClasses';
11
import {
12
  loadingAggregationHistory, loadingAggregationHistoryError, loadingMetrics, loadingMetricsError,
13
  loadingReposMessage, loadingSubscriptions, loadingTopics, loadingTopicsError,
14
  loadingUserRepoInfoEmpty, noAggregationHistory, noSubscriptionsFound, noTopicsFound, reposRetrievalError,
15
  loadingJobSummary, loadingJobSummaryError
16
} from '../../domain/shared-messages';
17
import {DashboardService} from '../../services/dashboard.service';
18
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
19
import {PiwikService} from '../../services/piwik.service';
20
import {ValidatorService} from '../../services/validator.service';
21

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

    
27
export class DashboardComponent implements OnInit {
28

    
29
  reposOfUser: Repository[] = [];
30
  selectedRepo: Repository = null;
31
  // tilesView: boolean;
32
  errorMessage: string;
33
  noRepos: string;
34
  loadingMessage: string;
35

    
36
  constructor(private authService: AuthenticationService,
37
              private repositoryService: RepositoryService,
38
              private dashboardService: DashboardService,
39
              private piwikService: PiwikService,
40
              private validatorService: ValidatorService,
41
              private sanitizer: DomSanitizer) { }
42

    
43
  repositories: RepositorySummaryInfo[] = [];
44
  userEmail: string;
45

    
46
  loading: boolean = true;
47

    
48

    
49
  // Aggregations
50
  collectionMonitorSummary: CollectionMonitorSummary;
51
  lastIndexedVersion: AggregationDetails;
52
  latestAggregations: AggregationDetails[] = [];
53
  errorAggregationsMessage: string;
54
  noAggregations: string;
55
  loadingAggregationsMessage: string;
56

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

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

    
86
  // Validation
87
  storedJob: StoredJob[] = [];
88
  noValidationsMessage: string;
89
  errorValidationsMessage: string;
90
  loadingJobSummaryMessage: string;
91

    
92
  ngOnInit() {
93
    // this.getUserEmail();
94
    this.userEmail = sessionStorage.getItem('email');
95
    if (this.userEmail) {
96
      this.getRepositoriesSummaryInfo();
97
    }
98
    let body = document.getElementsByTagName('body')[0];
99
    body.classList.remove("top_bar_active");
100
    body.classList.remove("page_heading_active");
101

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

    
106
  getReposOfUser(): void {
107
    this.loadingMessage = loadingReposMessage;
108
    this.repositoryService.getRepositoriesOfUser(this.authService.getUserEmail())
109
      .subscribe(
110
        repos => {
111
          this.sortRepositoriesByName(repos);
112
          if (this.reposOfUser && this.reposOfUser.length > 0) {
113
            this.selectedRepo = this.reposOfUser[0];
114
            this.getSelectedRepositorySummaryInfo(this.reposOfUser[0]);
115
          }
116
        },
117
        error => {
118
          console.log(error);
119
          this.loadingMessage = '';
120
          this.errorMessage = reposRetrievalError;
121
        },
122
        () => {
123
          this.loadingMessage = '';
124
          if (!this.reposOfUser || !this.reposOfUser.length) {
125
            this.noRepos = loadingUserRepoInfoEmpty;
126
          }
127
        }
128
      );
129
  }
130

    
131
  sortRepositoriesByName(repos: Repository[]) {
132
    this.reposOfUser = repos.sort( function(a, b) {
133
      if (a.officialName < b.officialName) {
134
        return -1;
135
      } else if (a.officialName > b.officialName) {
136
        return 1;
137
      } else {
138
        return 0;
139
      }
140
    });
141
  }
142

    
143
  changeSelectedRepository(repoId: string) {
144
    this.selectedRepo =  this.reposOfUser.find(x => x.id == repoId);
145
    console.log('selectedRepo ' + this.selectedRepo.id);
146
    this.getSelectedRepositorySummaryInfo(this.selectedRepo);
147
  }
148

    
149
  getSelectedRepositorySummaryInfo(selectedRepo: Repository) {
150

    
151
    // Aggregations
152
    this.loadingAggregationsMessage = loadingAggregationHistory;
153
    this.latestAggregations = [];
154
    this.lastIndexedVersion = null;
155
    this.dashboardService.getCollectionMonitorSummary(selectedRepo.id, 5).subscribe(
156
      collectionMonitorSummary => this.getCollectionMonitorSummary(collectionMonitorSummary),
157
      error => {
158
        this.loadingAggregationsMessage = '';
159
        this.errorAggregationsMessage = loadingAggregationHistoryError;
160
      },
161
      () => {
162
        this.loadingAggregationsMessage = '';
163
        this.errorAggregationsMessage = '';
164
      }
165
    );
166

    
167
    // Usage Statistics
168
    this.loadingUsageStatsMessage = loadingMetrics;
169
    this.usageSummary = null;
170
    this.piwik = null;
171
    this.repoMetrics = null;
172
    this.pageViews = '--';
173
    this.totalViews = '--';
174
    this.totalDownloads = '--';
175
    this.viewsUrl = null;
176
    this.downloadsUrl = null;
177
    this.shortSelectedRepoId = null;
178
    this.dashboardService.getUsageSummary(selectedRepo.id).subscribe(
179
      usageSummary => this.getUsageSummary(usageSummary),
180
      error => {
181
        this.loadingUsageStatsMessage = '';
182
        this.errorUsageStatsMessage = loadingMetricsError;
183
        console.log(error);
184
      } ,
185
      () => {
186
        this.shortSelectedRepoId = selectedRepo.id.replace(/_/g, '').replace('::', ':');
187
        this.loadingUsageStatsMessage = '';
188
        this.errorUsageStatsMessage = '';
189
      }
190
    );
191

    
192
    // Broker
193
    this.loadingTopicsMessage = loadingTopics;
194
    this.loadingSubscriptionsMessage = loadingSubscriptions;
195
    this.brokerSummary = null;
196
    this.totalNumberOfEvents = 0;
197
    this.moreList = [];
198
    this.missingList = [];
199
    this.totalMore = 0;
200
    this.totalMissing = 0;
201
    this.dashboardService.getBrokerSummary(this.userEmail, this.getCorrectName()).subscribe(
202
      brokerSummary => this.getBrokerSummary(brokerSummary),
203
      error => {
204
        this.loadingTopicsMessage = '';
205
        this.loadingSubscriptionsMessage = '';
206
        this.errorTopicsMessage = loadingTopicsError;
207
        this.errorSubscriptionsMessage = 'Failed to load the subscriptions for your datasource';
208
        console.log(error);
209
      },
210
      () => {
211
        this.loadingTopicsMessage = '';
212
        this.loadingSubscriptionsMessage = '';
213
        this.errorTopicsMessage = '';
214
        this.errorSubscriptionsMessage = '';
215
      }
216
    );
217

    
218
    // Validation
219
    this.loadingJobSummaryMessage = loadingJobSummary;
220
    this.noValidationsMessage = '';
221
    this.errorValidationsMessage = '';
222
    this.validatorService.getValidationSummary(selectedRepo.id).subscribe(
223
      validationSummary => {
224
        this.storedJob = validationSummary;
225
        console.log(validationSummary);
226
      },
227
      error => {
228
        this.errorValidationsMessage = loadingJobSummaryError;
229
        this.loadingJobSummaryMessage = '';
230
        console.log(error);
231
      } ,
232
      () => {
233
        this.getValidationSummary(this.storedJob);
234
        this.loadingJobSummaryMessage = '';
235
      }
236
    );
237
  }
238

    
239
  getCollectionMonitorSummary(collectionMonitorSummary: CollectionMonitorSummary) {
240

    
241
    this.latestAggregations = collectionMonitorSummary.aggregationDetails;
242
    this.lastIndexedVersion = collectionMonitorSummary.lastIndexedVersion;
243

    
244
    if ( !this.latestAggregations || (this.latestAggregations.length === 0) ) {
245
      this.noAggregations = noAggregationHistory;
246
    }
247
  }
248

    
249
  getBrokerSummary(brokerSummary: BrokerSummary) {
250

    
251
    this.noSubscriptions = '';
252
    this.noTopics = '';
253

    
254
    this.brokerSummary = brokerSummary;
255

    
256
    if(this.brokerSummary.userSubs==null)
257
      this.noSubscriptions = noTopicsFound;
258
    if(this.brokerSummary.topicsForDatasource==null)
259
      this.noTopics = noSubscriptionsFound;
260

    
261
    this.totalNumberOfEvents = 0;
262
    this.totalMore = 0;
263
    this.totalMissing = 0;
264
    if(brokerSummary.topicsForDatasource) {
265
      for (let browseEntry of brokerSummary.topicsForDatasource) {
266
        this.totalNumberOfEvents += browseEntry.size;
267
        if (browseEntry.value.startsWith('ENRICH/MORE')) {
268
          this.totalMore += browseEntry.size;
269
          this.moreList.push(browseEntry);
270
        } else if (browseEntry.value.startsWith('ENRICH/MISSING')) {
271
          this.totalMissing += browseEntry.size;
272
          this.missingList.push(browseEntry);
273
        }
274
      }
275
    }
276

    
277

    
278
  }
279

    
280
  getUsageSummary(usageSummary: UsageSummary) {
281

    
282
    this.noUsageStats = '';
283

    
284
    if (usageSummary.piwikInfo == null) {
285
      this.noUsageStats = 'This repository does not have our Usage Statistics service enabled yet';
286
    } else {
287
      this.usageSummary = usageSummary;
288
      this.piwik = usageSummary.piwikInfo;
289
      this.repoMetrics = usageSummary.metricsInfo;
290
      if (this.repoMetrics.metricsNumbers.pageviews) {
291
        this.pageViews = this.repoMetrics.metricsNumbers.pageviews;
292
      }
293
      if (this.repoMetrics.metricsNumbers.total_views) {
294
        this.totalViews = this.repoMetrics.metricsNumbers.total_views;
295
      }
296
      if (this.repoMetrics.metricsNumbers.total_downloads) {
297
        this.totalDownloads = this.repoMetrics.metricsNumbers.total_downloads;
298
      }
299
      this.getViewsUrl();
300
      this.getDownloadsUrl();
301
    }
302
  }
303

    
304
  getValidationSummary(validationSummary: StoredJob[]) {
305
    if (validationSummary == null) { this.noValidationsMessage = 'There is no validation history for this repository at the moment'; }
306
  }
307

    
308
  getViewsUrl () {
309
    this.viewsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
310
      `${this.repoMetrics.diagramsBaseURL}merge.php?com=query
311
      &data=[{"query":"dtsrcRepoViews","dtsrcName":"${this.piwik.openaireId}",
312
      "table":"","fields":[{"fld":"sum","agg":"sum","type":"chart","yaxis":1,"c":false}],
313
      "xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,
314
      "sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"",
315
      "xaxistitle":"","yaxisheaders":["Monthly views"],"generalxaxis":"","theme":0,"in":[]}]
316
      &info_types=["spline"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]
317
      &style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0, 1)","size":"18"},
318
      {"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)
319
      &colors[]=rgba(124,181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125,1)
320
      &colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233,1)&colors[]=rgba(241, 92, 128, 1)
321
      &colors[]=rgba(228, 211, 84,1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91,1)
322
      &colors[]=rgba(145, 232, 225,1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false`
323
    );
324
  }
325

    
326
  getDownloadsUrl () {
327
    this.downloadsUrl = this.sanitizer.bypassSecurityTrustResourceUrl(
328
      `${this.repoMetrics.diagramsBaseURL}merge.php?com=query
329
      &data=[{"query":"dtsrcRepoDownloads","dtsrcName":"${this.piwik.openaireId}",
330
      "table":"","fields":[{"fld":"sum","agg":"sum","type":"chart","yaxis":1,"c":false}],
331
      "xaxis":{"name":"month","agg":"sum"},"group":"","color":"","type":"chart","size":30,
332
      "sort":"xaxis","xStyle":{"r":-30,"s":"0","l":"-","ft":"-","wt":"-"},"title":"","subtitle":"",
333
      "xaxistitle":"","yaxisheaders":["Monthly downloads"],"generalxaxis":"","theme":0,"in":[]}]
334
      &info_types=["spline"]&stacking=&steps=false&fontFamily=Courier&spacing=[5,0,0,0]
335
      &style=[{"color":"rgba(0, 0, 0, 1)","size":"18"},{"color":"rgba(0, 0, 0,1)","size":"18"},
336
      {"color":"000000","size":""},{"color":"000000","size":""}]&backgroundColor=rgba(255,255,255,1)
337
      &colors[]=rgba(124, 181, 236, 1)&colors[]=rgba(67, 67, 72, 1)&colors[]=rgba(144, 237, 125,1)
338
      &colors[]=rgba(247, 163, 92, 1)&colors[]=rgba(128, 133, 233,1)&colors[]=rgba(241, 92, 128, 1)
339
      &colors[]=rgba(228, 211, 84,1)&colors[]=rgba(43, 144, 143, 1)&colors[]=rgba(244, 91, 91,1)
340
      &colors[]=rgba(145, 232, 225,1)&xlinew=0&ylinew=1&legends=true&tooltips=true&persistent=false`
341
    );
342
  }
343

    
344
  getCorrectName() {
345
    const temp = this.selectedRepo.officialName.split('|');
346
    let correctName = temp[0];
347
    let repoName = temp[0];
348
    for (let i = 1; i < temp.length; i++) {
349
      correctName += `/${temp[i]}`;
350
      repoName += ` | ${temp[i]}`;
351
    }
352

    
353
    return correctName;
354
  }
355

    
356
  getIsUserLoggedIn() {
357
    return this.authService.getIsUserLoggedIn();
358
  }
359

    
360
  getUserEmail() {
361
    this.userEmail = this.authService.getUserEmail();
362
  }
363

    
364
  getRepos() {
365
    console.log('in getRepos');
366
    this.getRepositoriesSummaryInfo();
367
  }
368

    
369
  getRepositoriesSummaryInfo() {
370
    this.repositoryService.getRepositoriesSummaryInfo().subscribe(
371
      repositories => { this.repositories = repositories; this.loading=false },
372
      error => { console.log('getRepSumError'); this.loading=false },
373
      () => { console.log(this.repositories); this.loading=false }
374
    );
375
  }
376
}
(2-2/2)