Project

General

Profile

1
import {Component, OnInit, ElementRef} from '@angular/core';
2
import {HelpContentService} from '../../services/help-content.service';
3
import {ActivatedRoute, Router} from '@angular/router';
4
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
5
import { Portal } from '../../domain/portal';
6
import {DomSanitizer, Title} from '@angular/platform-browser';
7
import {TitleCasePipe} from '@angular/common';
8
import {availableEntitiesMap, StatisticsDisplay, StatisticsSummary} from '../../openaireLibrary/connect/statistics/statisticsEntities';
9
import {ConfigurationService} from '../../openaireLibrary/utils/configuration/configuration.service';
10

    
11
import {Session} from '../../openaireLibrary/login/utils/helper.class';
12
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
13
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
14
import {CommunityCharts} from '../../openaireLibrary/connect/statistics/communityCharts';
15
import {properties} from '../../../environments/environment';
16

    
17
@Component({
18
    selector: 'stats',
19
    templateUrl: 'stats.component.html'
20
})
21

    
22
export class StatsComponent implements OnInit {
23
    // errorMessage: string;
24
    // loadingMessage: string;
25
    public showLoading: boolean = true;
26
    public errorMessage: string = '';
27
    public updateErrorMessage: string = '';
28

    
29
    communities: Portal[] = [];
30
    communityId: string;
31
    communityInfo = null;
32
    deactivatedEntity: boolean;
33

    
34
    displayedTimeline: string;
35
    displayedGraph: string;
36
    displayedProjectChart: string;
37
    displayedEntity: string;
38

    
39
    entitiesList: string[] = [];
40
    entitiesMap: Map<string, string> = availableEntitiesMap;
41

    
42
    statisticsSum: StatisticsSummary;
43
    statisticsDisplay: StatisticsDisplay;
44
    chartsInfoMap: {};
45

    
46
    showAllInMonitor: boolean = false;
47
    showAllInDashboard: boolean = false;
48

    
49
    public properties: EnvProperties = null;
50
  private communityName = null;
51

    
52
    constructor(private element: ElementRef,
53
                private contentService: HelpContentService,
54
                private configService: ConfigurationService,
55
                private route: ActivatedRoute,
56
                private _router: Router,
57
                private title: Title,
58
                private sanitizer: DomSanitizer,
59
                private titleCase: TitleCasePipe) {}
60

    
61
    ngOnInit() {
62

    
63
      this.properties = properties;
64
          this.route.params.subscribe(
65
              params => {
66
                  HelperFunctions.scroll();
67
                  this.title.setTitle('Administration Dashboard | Statistics');
68
                  this.communityId = params['community'];
69
                  this.getStatistics();
70
              });
71

    
72
    }
73

    
74
    getStatistics() {
75
      if(!Session.isLoggedIn()){
76
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
77
      } else {
78
        // this.loadingMessage = 'Retrieving statistics';
79
        // this.errorMessage = '';
80
        this.showLoading = true;
81
        this.updateErrorMessage = "";
82
        this.errorMessage = "";
83

    
84
        this.statisticsSum = null;
85
        this.statisticsDisplay = null;
86
        this.contentService.getCommunityStatistics(this.properties.statisticsAPIURL, this.communityId).subscribe(
87
            stats => {
88
              if(stats) {
89
                this.statisticsSum = stats;
90
                if(stats["other"]){ //hack because in stats API the entity name is "other" while in admin API is "orp". This component uses also "orp" name
91
                  this.statisticsSum["orp"]=stats["other"];
92
                }
93
              }else{
94
                console.debug("Aaaa")
95
              }
96
            },
97
            error => {
98
                //this.loadingMessage = '';
99
                //this.errorMessage = 'Failed to retrieve statistics for the chosen community!';
100
                //console.log(error);
101
                this.handleError('Failed to retrieve statistics for the chosen community!', error);
102
            },
103
            () => {
104
                //this.loadingMessage = '';
105
                this.getDisplayOptions();
106
            }
107
        );
108
      }
109
    }
110

    
111
    getDisplayOptions() {
112
      if(!Session.isLoggedIn()){
113
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
114
      } else {
115
        this.contentService.getCommunityAdminStatisticsChoices(this.properties.adminToolsAPIURL, this.communityId)
116
            .subscribe(
117
                res => {
118
                    this.statisticsDisplay = res;
119
                },
120
                error => {
121
                    //console.log(error);
122
                    this.handleError('Failed to retrieve statistics\' choices for the chosen community!', error);
123
                },
124
                () => {
125
                    this.getCommunityInfo();
126
                }
127
            );
128
        }
129
    }
130

    
131
    getCommunityInfo() {
132
      if(!Session.isLoggedIn()){
133
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
134
      } else {
135
        //console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`);
136
        this.configService.communityInformationState.subscribe(
137
            res => {
138
              console.debug(res);
139
              if(res) {
140
                this.communityInfo = res;
141
                this.communityName = this.communityInfo.name;
142
              }
143
            },
144
            error => this.handleError('Failed to retrieve information for the chosen community!', error),//console.log(error),
145
            () => {
146
                this.createChartUrlMap();
147
                this.entitiesList = Array.from( this.entitiesMap.keys() );
148
                this.onChangeEntity(this.entitiesList[0]);
149
            }
150
        );
151
      }
152
    }
153

    
154
    onChangeEntity (entity: string) {
155
      if(!Session.isLoggedIn()){
156
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
157
      } else {
158
        this.showLoading = true;
159

    
160
        this.deactivatedEntity = false;
161
        this.displayedEntity = entity;
162

    
163
        this.displayedTimeline = `${entity}Timeline`;
164
        this.displayedGraph = `${entity}Graph`;
165
        this.changeDisplayedProjectChart(`${entity}Projectcolumn`);
166
        if (this.statisticsDisplay.entities[entity].charts.map['projectColumn'].showInMonitor &&
167
            this.statisticsDisplay.entities[entity].charts.map['projectPie'].showInMonitor &&
168
            this.statisticsDisplay.entities[entity].charts.map['projectTable'].showInMonitor) {
169
            this.showAllInMonitor = true;
170
        }
171
        if (this.statisticsDisplay.entities[entity].charts.map['projectColumn'].showInDashboard &&
172
            this.statisticsDisplay.entities[entity].charts.map['projectPie'].showInDashboard &&
173
            this.statisticsDisplay.entities[entity].charts.map['projectTable'].showInDashboard) {
174
            this.showAllInDashboard = true;
175
        }
176

    
177
        const activatedEntities = this.communityInfo['entities'];
178
        if ( !activatedEntities.some(item => item.pid === entity) ) {
179
            this.deactivatedEntity = true;
180
        }
181

    
182
        this.showLoading = false;
183
      }
184
    }
185

    
186
    changeDisplayedProjectChart(chartName: string) {
187
        this.displayedProjectChart = chartName;
188
    }
189

    
190
    createChartUrlMap() {
191
      let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer);
192
      this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties);
193
    }
194

    
195
    toggleShowAllInMonitor(entity: string) {
196
      if(!Session.isLoggedIn()){
197
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
198
      } else {
199
        this.showLoading = true;
200
        this.updateErrorMessage = "";
201
        //this.loadingMessage = 'Saving changes';
202
        this.showAllInMonitor = !this.showAllInMonitor;
203

    
204
        this.contentService.postCommunityAdminStatisticsChoices(
205
            this.properties.adminToolsAPIURL,
206
            this.communityId,
207
            entity,
208
            'charts',
209
            'projectColumn',
210
            this.showAllInMonitor,
211
            true).subscribe(
212
            response => {},//console.log(JSON.stringify(response)),
213
            error => {
214
                //console.log(error);
215
                //this.loadingMessage = '';
216
                //this.errorMessage = 'The changes could not be saved';
217

    
218
                this.handleUpdateError('The changes could not be saved', error);
219
            },
220
            () => {
221

    
222
                this.contentService.postCommunityAdminStatisticsChoices(
223
                    this.properties.adminToolsAPIURL,
224
                    this.communityId,
225
                    entity,
226
                    'charts',
227
                    'projectPie',
228
                    this.showAllInMonitor,
229
                    true).subscribe(
230
                    response => {},//console.log(JSON.stringify(response)),
231
                    error => {
232
                        //console.log(error);
233
                        //this.loadingMessage = '';
234
                        //this.errorMessage = 'The changes could not be saved';
235

    
236
                        this.handleUpdateError('The changes could not be saved', error);
237
                    },
238
                    () => {
239

    
240
                        this.contentService.postCommunityAdminStatisticsChoices(
241
                            this.properties.adminToolsAPIURL,
242
                            this.communityId,
243
                            entity,
244
                            'charts',
245
                            'projectTable',
246
                            this.showAllInMonitor,
247
                            true).subscribe(
248
                            response => {},//console.log(JSON.stringify(response)),
249
                            error => {
250
                                //console.log(error);
251
                                //this.loadingMessage = '';
252
                                //this.errorMessage = 'The changes could not be saved';
253

    
254
                                this.handleUpdateError('The changes could not be saved' ,error);
255
                            },
256
                            () => {
257

    
258
                                this.statisticsDisplay.entities[entity]['charts'].map['projectColumn'].showInMonitor = this.showAllInMonitor;
259
                                this.statisticsDisplay.entities[entity]['charts'].map['projectPie'].showInMonitor = this.showAllInMonitor;
260
                                this.statisticsDisplay.entities[entity]['charts'].map['projectTable'].showInMonitor = this.showAllInMonitor;
261
                                //this.loadingMessage = '';
262

    
263
                                this.showLoading = false;
264
                            }
265
                        );
266
                    }
267
                );
268
            }
269
        );
270

    
271
        /*forkJoin didn't work properly - maybe try another way*/
272
        /*Observable.forkJoin(
273
            this.contentService.postCommunityAdminStatisticsChoices(
274
                this.properties.adminToolsAPIURL,
275
                this.communityId,
276
                entity,
277
                'charts',
278
                'projectColumn',
279
                this.showAllInMonitor,
280
            true),
281
            this.contentService.postCommunityAdminStatisticsChoices(
282
                this.properties.adminToolsAPIURL,
283
                this.communityId,
284
                entity,
285
                'charts',
286
                'projectPie',
287
                this.showAllInMonitor,
288
                true),
289
            this.contentService.postCommunityAdminStatisticsChoices(
290
                this.properties.adminToolsAPIURL,
291
                this.communityId,
292
                entity,
293
                'charts',
294
                'projectTable',
295
                this.showAllInMonitor,
296
                true)
297

    
298
        ).subscribe(
299
            response => console.log('All went well in forkJoin!'),
300
            error => {
301
                console.log(error);
302
                this.loadingMessage = '';
303
                this.errorMessage = 'The changes could not be saved';
304
            },
305
            () => {
306
                this.statisticsDisplay.entities[entity]['charts'].map['projectColumn'].showInMonitor = this.showAllInMonitor;
307
                this.statisticsDisplay.entities[entity]['charts'].map['projectPie'].showInMonitor = this.showAllInMonitor;
308
                this.statisticsDisplay.entities[entity]['charts'].map['projectTable'].showInMonitor = this.showAllInMonitor;
309
                this.loadingMessage = '';
310
            }
311
        );*/
312
      }
313
    }
314

    
315
    toggleShowAllInDashboard(entity: string) {
316
      if(!Session.isLoggedIn()){
317
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
318
      } else {
319
        this.showLoading = true;
320
        this.updateErrorMessage = "";
321

    
322
        //this.loadingMessage = 'Saving changes';
323
        this.contentService.postCommunityAdminStatisticsChoices(
324
            this.properties.adminToolsAPIURL,
325
            this.communityId,
326
            entity,
327
            'charts',
328
            'projectColumn',
329
            !this.showAllInDashboard,
330
            false).subscribe(
331
            response => {},//console.log(JSON.stringify(response)),
332
            error => {
333
                //console.log(error);
334
                //this.loadingMessage = '';
335
                //this.errorMessage = 'The changes could not be saved';
336
                this.handleUpdateError('The changed could not be saved', error);
337
            },
338
            () => {
339

    
340
                this.contentService.postCommunityAdminStatisticsChoices(
341
                    this.properties.adminToolsAPIURL,
342
                    this.communityId,
343
                    entity,
344
                    'charts',
345
                    'projectPie',
346
                    !this.showAllInDashboard,
347
                    false).subscribe(
348
                    response => {},//console.log(JSON.stringify(response)),
349
                    error => {
350
                        //console.log(error);
351
                        //this.loadingMessage = '';
352
                        //this.errorMessage = 'The changes could not be saved';
353
                        this.handleUpdateError('The changes could not be saved', error);
354
                    },
355
                    () => {
356

    
357
                        this.contentService.postCommunityAdminStatisticsChoices(
358
                            this.properties.adminToolsAPIURL,
359
                            this.communityId,
360
                            entity,
361
                            'charts',
362
                            'projectTable',
363
                            !this.showAllInDashboard,
364
                            false).subscribe(
365
                            response => {},//console.log(JSON.stringify(response)),
366
                            error => {
367
                                //console.log(error);
368
                                //this.loadingMessage = '';
369
                                //this.errorMessage = 'The changes could not be saved';
370
                                this.handleUpdateError('The changes could not be saved', error);
371
                            },
372
                            () => {
373

    
374
                                this.showAllInDashboard = !this.showAllInDashboard;
375
                                this.statisticsDisplay.entities[entity]['charts'].map['projectColumn'].showInDashboard = this.showAllInDashboard;
376
                                this.statisticsDisplay.entities[entity]['charts'].map['projectPie'].showInDashboard = this.showAllInDashboard;
377
                                this.statisticsDisplay.entities[entity]['charts'].map['projectTable'].showInDashboard = this.showAllInDashboard;
378
                                //this.loadingMessage = '';
379

    
380
                                this.showLoading = false;
381
                            }
382
                        );
383
                    }
384
                );
385
            }
386
        );
387
      }
388
    }
389

    
390
    toggleShowGraphAnalysisView() {
391
      this.contentService.statisticsIsActiveToggle(this.properties.adminToolsAPIURL, this.statisticsDisplay._id).subscribe(status => {
392
        this.statisticsDisplay.isActive = status;
393
      });
394
    }
395

    
396
    toggleShowInMonitor(entity: string, chartsOrNumbers: string, title: string) {
397
      if(!Session.isLoggedIn()){
398
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
399
      } else {
400
        this.updateErrorMessage = "";
401

    
402
        this.contentService.postCommunityAdminStatisticsChoices(
403
            this.properties.adminToolsAPIURL,
404
            this.communityId,
405
            entity,
406
            chartsOrNumbers,
407
            title,
408
            !this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInMonitor,
409
            true).subscribe(
410
                    response => {},//console.log(JSON.stringify(response)),
411
                    error => this.handleUpdateError('The changes could not be saved', error),//console.log(error),
412
                    () => {
413
                      this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInMonitor = !this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInMonitor;
414
                    }
415
                );
416
      }
417
    }
418

    
419
    toggleShowInDashboard(entity: string, chartsOrNumbers: string, title: string) {
420
      if(!Session.isLoggedIn()){
421
        this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl":  this._router.url} });
422
      } else {
423
        this.updateErrorMessage = "";
424

    
425
        this.contentService.postCommunityAdminStatisticsChoices(
426
            this.properties.adminToolsAPIURL,
427
            this.communityId,
428
            entity,
429
            chartsOrNumbers,
430
            title,
431
            !this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInDashboard,
432
            false).subscribe(
433
                response => {},//console.log(JSON.stringify(response)),
434
                error => this.handleUpdateError('The changes could not be saved', error),//console.log(error),
435
                () => {
436
                  this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInDashboard = !this.statisticsDisplay.entities[entity][chartsOrNumbers].map[title].showInDashboard;
437
                }
438
            );
439
      }
440
    }
441

    
442
    getCamelCaseString (inputString: string) {
443
        return this.titleCase.transform(inputString);
444
    }
445

    
446

    
447
    handleUpdateError(message: string, error) {
448
        this.updateErrorMessage = message;
449
        console.log('Server responded: ' +error);
450

    
451
        this.showLoading = false;
452
    }
453

    
454
    handleError(message: string, error) {
455
        this.errorMessage = message;
456
        console.log('Server responded: ' + error);
457

    
458
        this.showLoading = false;
459
    }
460
}
(3-3/4)