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.getReposOfUser();
97
      // this.getRepositoriesSummaryInfo(this.userEmail);
98
    }
99
    let body = document.getElementsByTagName('body')[0];
100
    body.classList.remove("top_bar_active");
101
    body.classList.remove("page_heading_active");
102

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

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

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

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

    
150
  getSelectedRepositorySummaryInfo(selectedRepo: Repository) {
151

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

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

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

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

    
240
  getCollectionMonitorSummary(collectionMonitorSummary: CollectionMonitorSummary) {
241

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

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

    
250
  getBrokerSummary(brokerSummary: BrokerSummary) {
251

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

    
255
    this.brokerSummary = brokerSummary;
256

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

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

    
278

    
279
  }
280

    
281
  getUsageSummary(usageSummary: UsageSummary) {
282

    
283
    this.noUsageStats = '';
284

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

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

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

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

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

    
354
    return correctName;
355
  }
356

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

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

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

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