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 {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;
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

    
90
    this.properties = properties;
91
    var url = properties.baseLink + this._router.url;
92
    this._meta.updateTag({content: url}, "property='og:url'");
93

    
94
    this.subs.push(this.route.queryParams.subscribe(
95
      communityId => {
96
        this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
97
        if(!this.communityId) {
98
          this.communityId = communityId['communityId'];
99
        }
100

    
101
        if (this.currentMode == "showInMonitor" && this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
102
          this.subs.push(this._piwikService.trackView(this.properties, "Monitor " + this.communityId, PiwikHelper.siteIDs[this.communityId]).subscribe());
103
        }
104
        this.subs.push(this._communityService.getCommunityByState(this.properties, this.properties.communityAPI + this.communityId).subscribe(
105
          community => {
106
            if (typeof document !== 'undefined') {
107
              HelperFunctions.scroll();
108
            }
109
            this.communityName = community.shortTitle;
110
            this.createChartUrlMap();
111
            this.createStatisticsObjects();
112
          }));
113
        // console.log(" Stats! "+ this.properties.statisticsAPIURL);
114

    
115
      }));
116

    
117
  }
118

    
119
  public ngOnDestroy() {
120
    for (let sub of this.subs) {
121
      sub.unsubscribe();
122
    }
123
  }
124

    
125
  getCamelCaseString(inputString: string) {
126
    return this.titleCase.transform(inputString);
127
  }
128

    
129
  createStatisticsObjects() {
130
    // console.log(" Stats! "+ this.properties.statisticsAPIURL);
131
    this.status = this.errorCodes.LOADING;
132
    this.subs.push(this._statisticsService.getCommunityStatistics(this.properties, this.communityId).subscribe(
133
      res => {
134
        if(res) {
135
          this.statisticsSum = res;
136
          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
137
            this.statisticsSum["orp"] = res["other"];
138
          }
139
          this.getDisplayOptions();
140
        }else{
141
          console.error("Error getting community statistics for community with id: " + this.communityId);
142
          this.status = this.errorCodes.ERROR;
143
        }
144
      },
145
      error => {
146
        //console.log(error);
147
        this.handleError("Error getting community statistics for community with id: " + this.communityId, error);
148
        this.status = this.errorCodes.ERROR;
149
      }));
150
  }
151

    
152
  getDisplayOptions() {
153
    this.subs.push(this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId)
154
      .subscribe(
155
        res => {
156
          this.statisticsDisplay = res;
157

    
158
          this.getCommunityInfo();
159
          this.status = this.errorCodes.DONE;
160

    
161
        },
162
        error => {
163
          //console.log(error);
164
          this.handleError("Error getting community statistics choices by administrators for community with id: " + this.communityId, error);
165
          this.status = this.errorCodes.ERROR;
166
        }
167
      ));
168
  }
169

    
170

    
171
  getCommunityInfo() {
172
    // console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`);
173
    this.subs.push(this._configService.communityInformationState.subscribe(
174
      res => {
175
        this.communityInfo = res;
176
        /*for(let i=0; i<this.communityInfo.entities.length; i++){
177

    
178
            if (this.communityInfo.entities[i]["isEnabled"] ) {
179
                this.entitiesList.push(this.communityInfo.entities[i]['pid']);
180
            }
181
        }
182
        console.log(this.entitiesList);*/
183
        this.initializeDisplayedCharts()
184
      },
185
      error => {
186
        //console.log(error)
187
        this.handleError("Error getting community with id: " + this.communityId, error);
188
        this.initializeDisplayedCharts();
189
      }
190
    ));
191
  }
192

    
193
  initializeDisplayedCharts() {
194
    let firstEntity: string;
195
    this.entitiesList = Array.from(this.entitiesMap.keys());
196

    
197
    this.allowedChartsMode[this.currentMode] = this.allowedCharts;
198
    this.allowedEntitiesMode[this.currentMode] = this.allowedEntities;
199
    let titles = [];
200
    // console.log('this.entitiesList is',this.entitiesList);
201
    // console.log(`my current mode is: ${this.currentMode}`);
202
    for (let entity of this.entitiesList) {
203
      if (this.statisticsDisplay.entities[entity] && this.statisticsSum[entity].total && this.communityInfo.entities.filter(x => x['pid'] == entity && x['isEnabled'] === true).length) {
204
        this.allowedChartsMode.showInDashboard[entity] = [];
205
        this.allowedChartsMode.showInMonitor[entity] = [];
206
        for (let chart of this.chartCatsList) {
207
          if (this.statisticsSum[entity].total &&
208
            this.statisticsDisplay.entities[entity].charts.map[chart] &&
209
            this.statisticsDisplay.entities[entity].charts.map[chart]["showInDashboard"] &&
210
            this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) {
211
            this.allowedChartsMode.showInDashboard[entity].push(entity + this.getCamelCaseString(chart));
212
            if (titles.indexOf("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) {
213
              titles.push("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title);
214
              this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = true;
215
            } else {
216
              this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = false;
217
            }
218

    
219
            // console.log(`added ${entity} - ${chart} to allowedCharts`);
220
          }
221
          if (this.statisticsSum[entity].total &&
222
            this.statisticsDisplay.entities[entity].charts.map[chart] &&
223
            this.statisticsDisplay.entities[entity].charts.map[chart]["showInMonitor"] &&
224
            this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) {
225
            this.allowedChartsMode.showInMonitor[entity].push(entity + this.getCamelCaseString(chart));
226
            if (titles.indexOf("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) {
227
              titles.push("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title);
228
              this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = true;
229
            } else {
230
              this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = false;
231
            }
232
            // console.log(`added ${entity} - ${chart} to allowedCharts`);
233

    
234
          }
235
        }
236
        if (this.allowedChartsMode.showInMonitor[entity].length) {
237
          // console.log(`added ${entity} to allowedEntities`);
238
          this.allowedEntitiesMode.showInMonitor.push(entity);
239
          if (!firstEntity) {
240
            firstEntity = entity;
241
            this.onChangeEntity(entity);
242
          }
243
        }
244
        if (this.allowedChartsMode.showInDashboard[entity].length) {
245
          // console.log(`added ${entity} to allowedEntities`);
246
          this.allowedEntitiesMode.showInDashboard.push(entity);
247
          if (!firstEntity) {
248
            firstEntity = entity;
249
            this.onChangeEntity(entity);
250
          }
251
        }
252
      }
253
    }
254

    
255
  }
256

    
257
  createChartUrlMap() {
258

    
259
    let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer);
260
    this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties);
261
  }
262

    
263

    
264
  onChangeEntity(entity: string) {
265
    this.displayedEntity = entity;
266
    // console.log(`displayed entity is ${entity}`);
267
    // console.log(`statisticsSum[${entity}].total is ${this.statisticsSum[entity].total}`);
268

    
269
    if (this.statisticsSum[entity].total &&
270
      this.allowedEntities.filter(x => x == entity).length) {
271

    
272
      // console.log(`found ${entity} in allowedEntities`);
273
      this.displayedTimeline = `${entity}Timeline`;
274
      this.displayedTimelineUrl = this.chartsInfoMap[this.displayedTimeline].url;
275
      // console.log(`displayed Timeline is: ${this.displayedTimeline}`);
276
      this.displayedGraph = `${entity}Graph`;
277
      this.displayedGraphUrl = this.chartsInfoMap[this.displayedGraph].url;
278
      // console.log(`displayed Graph is: ${this.displayedGraph}`);
279
      if (this.allowedCharts[entity]) {
280
        let firstProjectChart = this.allowedCharts[entity].filter(x => x.includes(entity + 'Project'));
281
        if (firstProjectChart[0]) {
282
          this.changeDisplayedProjectChart(firstProjectChart[0]);
283
        } else {
284
          this.displayedProjectChart = '';
285
          this.displayedProjectChartUrl = '';
286
          // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`);
287
        }
288
      }
289
    } else {
290
      this.displayedTimeline = '';
291
      this.displayedTimelineUrl = '';
292
      // console.log(`displayed Timeline is: ${this.displayedTimeline}`);
293
      this.displayedGraph = '';
294
      this.displayedGraphUrl = '';
295
      // console.log(`displayed Graph is: ${this.displayedGraph}`);
296
    }
297
  }
298

    
299
  changeDisplayedProjectChart(chartName: string) {
300
    this.displayedProjectChart = chartName;
301
    this.displayedProjectChartUrl = this.chartsInfoMap[this.displayedProjectChart].url;
302
    // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`);
303
  }
304

    
305
  private handleError(message: string, error) {
306
    console.error("Statistics (Monitor) Page: " + message, error);
307
  }
308
}
309

    
310

    
311
@Component({
312
  selector: 'statistics-for-dashboard',
313
  template: ''
314
  // templateUrl: 'statistics-for-dashboard.component.html',
315
})
316

    
317
export class StatisticsForDashboardComponent extends StatisticsComponent {
318
  ngOnInit() {
319
    super.ngOnInit();
320
  }
321

    
322
}
(3-3/4)