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

    
22

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

    
28
export class StatisticsComponent {
29
  public piwiksub: any;
30
  public pageTitle = "OpenAIRE";
31
  properties: EnvProperties;
32
  @Input() communityId = null;
33
  @Input() currentMode = 'showInMonitor';
34
  communityInfo: any = null;
35
  entitiesList: string[] = [];
36
  entitiesMap: Map<string, string> = availableEntitiesMap;
37
  chartCatsList: string[] = availableCharts;
38
  allowedCharts: Map<string, string[]> = new Map<string, string[]>();
39
  allowedEntities: string[] = [];
40
  allowedChartsMode = {showInMonitor: new Map<string, string[]>(), showInDashboard: new Map<string, string[]>()};
41
  allowedEntitiesMode = {showInMonitor: [], showInDashboard: []};
42

    
43
  statisticsSum: StatisticsSummary = null;
44
  statisticsDisplay: StatisticsDisplay = null;
45
  chartTitlesMode = {showInMonitor: {}, showInDashboard: {}};
46
  chartsInfoMap: {};
47

    
48
  displayedTimeline: string;
49
  displayedTimelineUrl: string;
50
  displayedGraph: string;
51
  displayedGraphUrl: string;
52
  displayedProjectChart: string;
53
  displayedProjectChartUrl: string;
54
  displayedEntity: string;
55
  public errorCodes: ErrorCodes = new ErrorCodes();
56
  status = null;
57
  communityName = null;
58

    
59
  constructor(
60
    private route: ActivatedRoute,
61
    private _router: Router,
62
    private location: Location,
63
    private _meta: Meta,
64
    private _title: Title,
65
    private _piwikService: PiwikService,
66
    private _statisticsService: StatisticsService,
67
    private _configService: ConfigurationService,
68
    private titleCase: TitleCasePipe,
69
    private _communityService: CommunityService,
70
    private sanitizer: DomSanitizer) {
71

    
72
  }
73

    
74
  public ngOnInit() {
75
    if (this.currentMode == "showInMonitor") {
76
      var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects ";
77
      var title = "Monitor";
78

    
79
      this._title.setTitle(title);
80
      this._meta.updateTag({content: description}, "name='description'");
81
      this._meta.updateTag({content: description}, "property='og:description'");
82
      this._meta.updateTag({content: title}, "property='og:title'");
83
    }
84
    this.route.data
85
      .subscribe((data: { envSpecific: EnvProperties }) => {
86
        this.properties = data.envSpecific;
87
        var url = data.envSpecific.baseLink + this._router.url;
88
        this._meta.updateTag({content: url}, "property='og:url'");
89

    
90
        this.route.queryParams.subscribe(
91
          communityId => {
92
            this.communityId  = ConnectHelper.getCommunityFromDomain(this.properties.domain);
93
            if(!this.communityId) {
94
              this.communityId = communityId['communityId'];
95
            }
96

    
97
            if (this.currentMode == "showInMonitor" && this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
98
              this.piwiksub = this._piwikService.trackView(this.properties, "Monitor " + this.communityId, PiwikHelper.siteIDs[this.communityId]).subscribe();
99
            }
100
            this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe(
101
              community => {
102
                if (typeof document !== 'undefined') {
103
                  HelperFunctions.scroll();
104
                }
105
                this.communityName = community.shortTitle;
106
                this.createStatisticsObjects();
107
              });
108
            // console.log(" Stats! "+ this.properties.statisticsAPIURL);
109

    
110
          });
111
      });
112

    
113
  }
114

    
115

    
116
  public ngOnDestroy() {
117
    if (this.piwiksub) {
118
      this.piwiksub.unsubscribe();
119
    }
120

    
121
  }
122

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

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

    
145
  getDisplayOptions() {
146
    this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId)
147
      .subscribe(
148
        res => {
149
          this.statisticsDisplay = res;
150
          this.createChartUrlMap();
151
          this.getCommunityInfo();
152
          this.status = this.errorCodes.DONE;
153

    
154
        },
155
        error => {
156
          //console.log(error);
157
          this.handleError("Error getting community statistics choices by administrators for community with id: " + this.communityId, error);
158
          this.status = this.errorCodes.ERROR;
159
        }
160
      );
161
  }
162

    
163

    
164
  getCommunityInfo() {
165
    // console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`);
166
    this._configService.getCommunityInformation(this.properties, this.communityId).subscribe(
167
      res => {
168
        this.communityInfo = res;
169
        /*for(let i=0; i<this.communityInfo.entities.length; i++){
170

    
171
            if (this.communityInfo.entities[i]["isEnabled"] ) {
172
                this.entitiesList.push(this.communityInfo.entities[i]['pid']);
173
            }
174
        }
175
        console.log(this.entitiesList);*/
176
      },
177
      error => {
178
        //console.log(error)
179
        this.handleError("Error getting community with id: " + this.communityId, error);
180
      },
181
      () => this.initializeDisplayedCharts()
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)