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(community) {
94
          this.communityId = community.communityId;
95
          if (this.currentMode == "showInMonitor" && this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
96
            this.subs.push(this._piwikService.trackView(this.properties, "Monitor " + this.communityId, PiwikHelper.siteIDs[this.communityId]).subscribe());
97
          }
98
          this.communityName = community.shortTitle;
99
          this.createChartUrlMap();
100
          this.createStatisticsObjects();
101
        }
102
      }));
103
    
104
  }
105
  
106
  public ngOnDestroy() {
107
    for (let sub of this.subs) {
108
      if (sub instanceof Subscriber) {
109
        sub.unsubscribe();
110
      }
111
    }
112
  }
113
  
114
  getCamelCaseString(inputString: string) {
115
    return this.titleCase.transform(inputString);
116
  }
117
  
118
  createStatisticsObjects() {
119
    // console.log(" Stats! "+ this.properties.statisticsAPIURL);
120
    this.status = this.errorCodes.LOADING;
121
    this.subs.push(this._statisticsService.getCommunityStatistics(this.properties, this.communityId).subscribe(
122
      res => {
123
        if (res) {
124
          this.statisticsSum = res;
125
          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
126
            this.statisticsSum["orp"] = res["other"];
127
          }
128
          this.getDisplayOptions();
129
        } else {
130
          console.error("Error getting community statistics for community with id: " + this.communityId);
131
          this.status = this.errorCodes.ERROR;
132
        }
133
      },
134
      error => {
135
        //console.log(error);
136
        this.handleError("Error getting community statistics for community with id: " + this.communityId, error);
137
        this.status = this.errorCodes.ERROR;
138
      }));
139
  }
140
  
141
  getDisplayOptions() {
142
    this.subs.push(this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId)
143
      .subscribe(
144
        res => {
145
          this.statisticsDisplay = res;
146
          
147
          this.getCommunityInfo();
148
          this.status = this.errorCodes.DONE;
149
          
150
        },
151
        error => {
152
          //console.log(error);
153
          this.handleError("Error getting community statistics choices by administrators for community with id: " + this.communityId, error);
154
          this.status = this.errorCodes.ERROR;
155
        }
156
      ));
157
  }
158
  
159
  
160
  getCommunityInfo() {
161
    // console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`);
162
    this.subs.push(this._configService.communityInformationState.subscribe(
163
      res => {
164
        this.communityInfo = res;
165
        /*for(let i=0; i<this.communityInfo.entities.length; i++){
166

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

    
299

    
300
@Component({
301
  selector: 'statistics-for-dashboard',
302
  template: ''
303
  // templateUrl: 'statistics-for-dashboard.component.html',
304
})
305

    
306
export class StatisticsForDashboardComponent extends StatisticsComponent {
307
  ngOnInit() {
308
    super.ngOnInit();
309
  }
310
  
311
}
(3-3/4)