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

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

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

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

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

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

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

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

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

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

    
60
    ngOnInit() {
61
        this.route.data
62
            .subscribe((data: { envSpecific: EnvProperties }) => {
63
                this.properties = data.envSpecific;
64
                    this.route.queryParams.subscribe(
65
                        params => {
66
                            HelperFunctions.scroll();
67
                            this.title.setTitle('Administration Dashboard | Statistics');
68
                            this.communityId = params['communityId'];
69
                            this.getStatistics();
70
                        });
71
                },
72
                error => console.log(`E R R O R!!`)
73
            );
74
    }
75

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

    
86
        this.statisticsSum = null;
87
        this.statisticsDisplay = null;
88
        this.contentService.getCommunityStatistics(this.properties.statisticsAPIURL, this.communityId).subscribe(
89
            stats => {
90
              if(stats) {
91
                this.statisticsSum = stats;
92
                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
93
                  this.statisticsSum["orp"]=stats["other"];
94
                }
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.getCommunityInformation(this.properties, this.communityId).subscribe(
137
            res => {
138
                this.communityInfo = res;
139
                this.communityName = this.communityInfo.name;
140
            },
141
            error => this.handleError('Failed to retrieve information for the chosen community!', error),//console.log(error),
142
            () => {
143
                this.createChartUrlMap();
144
                this.entitiesList = Array.from( this.entitiesMap.keys() );
145
                this.onChangeEntity(this.entitiesList[0]);
146
            }
147
        );
148
      }
149
    }
150

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

    
157
        this.deactivatedEntity = false;
158
        this.displayedEntity = entity;
159

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

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

    
179
        this.showLoading = false;
180
      }
181
    }
182

    
183
    changeDisplayedProjectChart(chartName: string) {
184
        this.displayedProjectChart = chartName;
185
    }
186

    
187
    createChartUrlMap() {
188
      let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer);
189
      this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties);
190
    }
191

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

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

    
215
                this.handleUpdateError('The changes could not be saved', error);
216
            },
217
            () => {
218

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

    
233
                        this.handleUpdateError('The changes could not be saved', error);
234
                    },
235
                    () => {
236

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

    
251
                                this.handleUpdateError('The changes could not be saved' ,error);
252
                            },
253
                            () => {
254

    
255
                                this.statisticsDisplay.entities[entity]['charts'].map['projectColumn'].showInMonitor = this.showAllInMonitor;
256
                                this.statisticsDisplay.entities[entity]['charts'].map['projectPie'].showInMonitor = this.showAllInMonitor;
257
                                this.statisticsDisplay.entities[entity]['charts'].map['projectTable'].showInMonitor = this.showAllInMonitor;
258
                                //this.loadingMessage = '';
259

    
260
                                this.showLoading = false;
261
                            }
262
                        );
263
                    }
264
                );
265
            }
266
        );
267

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

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

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

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

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

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

    
371
                                this.showAllInDashboard = !this.showAllInDashboard;
372
                                this.statisticsDisplay.entities[entity]['charts'].map['projectColumn'].showInDashboard = this.showAllInDashboard;
373
                                this.statisticsDisplay.entities[entity]['charts'].map['projectPie'].showInDashboard = this.showAllInDashboard;
374
                                this.statisticsDisplay.entities[entity]['charts'].map['projectTable'].showInDashboard = this.showAllInDashboard;
375
                                //this.loadingMessage = '';
376

    
377
                                this.showLoading = false;
378
                            }
379
                        );
380
                    }
381
                );
382
            }
383
        );
384
      }
385
    }
386

    
387
    toggleShowGraphAnalysisView() {
388
      this.contentService.statisticsIsActiveToggle(this.properties.adminToolsAPIURL, this.statisticsDisplay._id).subscribe(status => {
389
        this.statisticsDisplay.isActive = status;
390
      });
391
    }
392

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

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

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

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

    
439
    getCamelCaseString (inputString: string) {
440
        return this.titleCase.transform(inputString);
441
    }
442

    
443

    
444
    handleUpdateError(message: string, error) {
445
        this.updateErrorMessage = message;
446
        console.log('Server responded: ' +error);
447

    
448
        this.showLoading = false;
449
    }
450

    
451
    handleError(message: string, error) {
452
        this.errorMessage = message;
453
        console.log('Server responded: ' + error);
454

    
455
        this.showLoading = false;
456
    }
457
}
(3-3/4)