Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Location, TitleCasePipe} from '@angular/common';
4
import {Title, Meta} from '@angular/platform-browser';
5
import {DomSanitizer} from '@angular/platform-browser';
6
import "rxjs/add/observable/zip";
7
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
8
import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes';
9
import {StatisticsService} from '../utils/services/statistics.service';
10
import {ConfigurationService} from '../openaireLibrary/utils/configuration/configuration.service';
11
import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service';
12
import {ConnectHelper} from '../openaireLibrary/connect/connectHelper';
13
import {
14
  availableCharts, availableEntitiesMap, StatisticsDisplay,
15
  StatisticsSummary
16
} from "../openaireLibrary/connect/statistics/statisticsEntities";
17
import {PiwikHelper} from '../utils/piwikHelper';
18
import {CommunityCharts} from "../openaireLibrary/connect/statistics/communityCharts";
19
import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class";
20
import {CommunityService} from "../openaireLibrary/connect/community/community.service";
21
import {Subscriber, Subscription} from "rxjs";
22
import {properties} from "../../environments/environment";
23

    
24

    
25
@Component({
26
  selector: 'statistics',
27
  templateUrl: 'statistics.component.html',
28
})
29

    
30
export class StatisticsComponent {
31
  public pageTitle = "OpenAIRE";
32
  properties: EnvProperties = properties;
33
  @Input() communityId = null;
34
  @Input() currentMode = 'showInMonitor';
35
  communityInfo: any = null;
36
  entitiesList: string[] = [];
37
  entitiesMap: Map<string, string> = availableEntitiesMap;
38
  chartCatsList: string[] = availableCharts;
39
  //allowedCharts = {};
40
  allowedCharts: Map<string, string[]> = new Map<string, string[]>();
41
  allowedEntities: string[] = [];
42
  //allowedChartsMode = {showInMonitor: {}/, showInDashboard: {}};
43
  allowedChartsMode = {showInMonitor: new Map<string, string[]>(), showInDashboard: new Map<string, string[]>()};
44
  allowedEntitiesMode = {showInMonitor: [], showInDashboard: []};
45
  
46
  statisticsSum: StatisticsSummary = null;
47
  statisticsDisplay: StatisticsDisplay = null;
48
  chartTitlesMode = {showInMonitor: {}, showInDashboard: {}};
49
  chartsInfoMap: {};
50
  
51
  displayedTimeline: string;
52
  displayedTimelineUrl: string;
53
  displayedGraph: string;
54
  displayedGraphUrl: string;
55
  displayedProjectChart: string;
56
  displayedProjectChartUrl: string;
57
  displayedEntity: string;
58
  public errorCodes: ErrorCodes = new ErrorCodes();
59
  status = null;
60
  communityName = null;
61
  
62
  subs: Subscription[] = [];
63
  
64
  constructor(
65
    private route: ActivatedRoute,
66
    private _router: Router,
67
    private location: Location,
68
    private _meta: Meta,
69
    private _title: Title,
70
    private _piwikService: PiwikService,
71
    private _statisticsService: StatisticsService,
72
    private _configService: ConfigurationService,
73
    private titleCase: TitleCasePipe,
74
    private _communityService: CommunityService,
75
    private sanitizer: DomSanitizer) {
76
    
77
  }
78
  
79
  public ngOnInit() {
80
    if (this.currentMode == "showInMonitor") {
81
      var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects ";
82
      var title = "Monitor";
83
      
84
      this._title.setTitle(title);
85
      this._meta.updateTag({content: description}, "name='description'");
86
      this._meta.updateTag({content: description}, "property='og:description'");
87
      this._meta.updateTag({content: title}, "property='og:title'");
88
    }
89
    var url = properties.domain + properties.baseLink + this._router.url;
90
    this._meta.updateTag({content: url}, "property='og:url'");
91
    this.subs.push(this._communityService.getCommunityAsObservable().subscribe(
92
      community => {
93
        if (typeof document !== 'undefined') {
94
          HelperFunctions.scroll();
95
        }
96
        if(community) {
97
          this.communityId = community.communityId;
98
          if (this.currentMode == "showInMonitor" && this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
99
            this.subs.push(this._piwikService.trackView(this.properties, "Monitor " + this.communityId, PiwikHelper.siteIDs[this.communityId]).subscribe());
100
          }
101
          this.communityName = community.shortTitle;
102
          this.createChartUrlMap();
103
          this.createStatisticsObjects();
104
        }
105
      }));
106
    
107
  }
108
  
109
  public ngOnDestroy() {
110
    for (let sub of this.subs) {
111
      if (sub instanceof Subscriber) {
112
        sub.unsubscribe();
113
      }
114
    }
115
  }
116
  
117
  getCamelCaseString(inputString: string) {
118
    return this.titleCase.transform(inputString);
119
  }
120
  
121
  createStatisticsObjects() {
122
    // console.log(" Stats! "+ this.properties.statisticsAPIURL);
123
    this.status = this.errorCodes.LOADING;
124
    this.subs.push(this._statisticsService.getCommunityStatistics(this.properties, this.communityId).subscribe(
125
      res => {
126
        if (res) {
127
          this.statisticsSum = res;
128
          if (res["other"]) { //hack because in stats API the entity name is "other" while in admin API is "orp". This component uses also "orp" name
129
            this.statisticsSum["orp"] = res["other"];
130
          }
131
          this.getDisplayOptions();
132
        } else {
133
          console.error("Error getting community statistics for community with id: " + this.communityId);
134
          this.status = this.errorCodes.ERROR;
135
        }
136
      },
137
      error => {
138
        //console.log(error);
139
        this.handleError("Error getting community statistics for community with id: " + this.communityId, error);
140
        this.status = this.errorCodes.ERROR;
141
      }));
142
  }
143
  
144
  getDisplayOptions() {
145
    this.subs.push(this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId)
146
      .subscribe(
147
        res => {
148
          this.statisticsDisplay = res;
149
          
150
          this.getCommunityInfo();
151
          this.status = this.errorCodes.DONE;
152
          
153
        },
154
        error => {
155
          //console.log(error);
156
          this.handleError("Error getting community statistics choices by administrators for community with id: " + this.communityId, error);
157
          this.status = this.errorCodes.ERROR;
158
        }
159
      ));
160
  }
161
  
162
  
163
  getCommunityInfo() {
164
    // console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`);
165
    this.subs.push(this._configService.communityInformationState.subscribe(
166
      res => {
167
        this.communityInfo = res;
168
        /*for(let i=0; i<this.communityInfo.entities.length; i++){
169

    
170
            if (this.communityInfo.entities[i]["isEnabled"] ) {
171
                this.entitiesList.push(this.communityInfo.entities[i]['pid']);
172
            }
173
        }
174
        console.log(this.entitiesList);*/
175
        this.initializeDisplayedCharts()
176
      },
177
      error => {
178
        //console.log(error)
179
        this.handleError("Error getting community with id: " + this.communityId, error);
180
        this.initializeDisplayedCharts();
181
      }
182
    ));
183
  }
184
  
185
  initializeDisplayedCharts() {
186
    let firstEntity: string;
187
    this.entitiesList = Array.from(this.entitiesMap.keys());
188
    
189
    this.allowedChartsMode[this.currentMode] = this.allowedCharts;
190
    this.allowedEntitiesMode[this.currentMode] = this.allowedEntities;
191
    let titles = [];
192
    // console.log('this.entitiesList is',this.entitiesList);
193
    // console.log(`my current mode is: ${this.currentMode}`);
194
    for (let entity of this.entitiesList) {
195
      if (this.statisticsDisplay.entities[entity] && this.statisticsSum[entity].total && this.communityInfo.entities.filter(x => x['pid'] == entity && x['isEnabled'] === true).length) {
196
        this.allowedChartsMode.showInDashboard[entity] = [];
197
        this.allowedChartsMode.showInMonitor[entity] = [];
198
        for (let chart of this.chartCatsList) {
199
          if (this.statisticsSum[entity].total &&
200
            this.statisticsDisplay.entities[entity].charts.map[chart] &&
201
            this.statisticsDisplay.entities[entity].charts.map[chart]["showInDashboard"] &&
202
            this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) {
203
            this.allowedChartsMode.showInDashboard[entity].push(entity + this.getCamelCaseString(chart));
204
            if (titles.indexOf("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) {
205
              titles.push("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title);
206
              this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = true;
207
            } else {
208
              this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = false;
209
            }
210
            
211
            // console.log(`added ${entity} - ${chart} to allowedCharts`);
212
          }
213
          if (this.statisticsSum[entity].total &&
214
            this.statisticsDisplay.entities[entity].charts.map[chart] &&
215
            this.statisticsDisplay.entities[entity].charts.map[chart]["showInMonitor"] &&
216
            this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) {
217
            this.allowedChartsMode.showInMonitor[entity].push(entity + this.getCamelCaseString(chart));
218
            if (titles.indexOf("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) {
219
              titles.push("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title);
220
              this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = true;
221
            } else {
222
              this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = false;
223
            }
224
            // console.log(`added ${entity} - ${chart} to allowedCharts`);
225
            
226
          }
227
        }
228
        if (this.allowedChartsMode.showInMonitor[entity].length) {
229
          // console.log(`added ${entity} to allowedEntities`);
230
          this.allowedEntitiesMode.showInMonitor.push(entity);
231
          if (!firstEntity) {
232
            firstEntity = entity;
233
            this.onChangeEntity(entity);
234
          }
235
        }
236
        if (this.allowedChartsMode.showInDashboard[entity].length) {
237
          // console.log(`added ${entity} to allowedEntities`);
238
          this.allowedEntitiesMode.showInDashboard.push(entity);
239
          if (!firstEntity) {
240
            firstEntity = entity;
241
            this.onChangeEntity(entity);
242
          }
243
        }
244
      }
245
    }
246
    
247
  }
248
  
249
  createChartUrlMap() {
250
    
251
    let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer);
252
    this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties);
253
  }
254
  
255
  
256
  onChangeEntity(entity: string) {
257
    this.displayedEntity = entity;
258
    // console.log(`displayed entity is ${entity}`);
259
    // console.log(`statisticsSum[${entity}].total is ${this.statisticsSum[entity].total}`);
260
    
261
    if (this.statisticsSum[entity].total &&
262
      this.allowedEntities.filter(x => x == entity).length) {
263
      
264
      // console.log(`found ${entity} in allowedEntities`);
265
      this.displayedTimeline = `${entity}Timeline`;
266
      this.displayedTimelineUrl = this.chartsInfoMap[this.displayedTimeline].url;
267
      // console.log(`displayed Timeline is: ${this.displayedTimeline}`);
268
      this.displayedGraph = `${entity}Graph`;
269
      this.displayedGraphUrl = this.chartsInfoMap[this.displayedGraph].url;
270
      // console.log(`displayed Graph is: ${this.displayedGraph}`);
271
      if (this.allowedCharts[entity]) {
272
        let firstProjectChart = this.allowedCharts[entity].filter(x => x.includes(entity + 'Project'));
273
        if (firstProjectChart[0]) {
274
          this.changeDisplayedProjectChart(firstProjectChart[0]);
275
        } else {
276
          this.displayedProjectChart = '';
277
          this.displayedProjectChartUrl = '';
278
          // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`);
279
        }
280
      }
281
    } else {
282
      this.displayedTimeline = '';
283
      this.displayedTimelineUrl = '';
284
      // console.log(`displayed Timeline is: ${this.displayedTimeline}`);
285
      this.displayedGraph = '';
286
      this.displayedGraphUrl = '';
287
      // console.log(`displayed Graph is: ${this.displayedGraph}`);
288
    }
289
  }
290
  
291
  changeDisplayedProjectChart(chartName: string) {
292
    this.displayedProjectChart = chartName;
293
    this.displayedProjectChartUrl = this.chartsInfoMap[this.displayedProjectChart].url;
294
    // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`);
295
  }
296
  
297
  private handleError(message: string, error) {
298
    console.error("Statistics (Monitor) Page: " + message, error);
299
  }
300
}
301

    
302

    
303
@Component({
304
  selector: 'statistics-for-dashboard',
305
  template: ''
306
  // templateUrl: 'statistics-for-dashboard.component.html',
307
})
308

    
309
export class StatisticsForDashboardComponent extends StatisticsComponent {
310
  ngOnInit() {
311
    super.ngOnInit();
312
  }
313
  
314
}
(3-3/4)