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
        if(res) {
133
          this.statisticsSum = res;
134
          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
135
            this.statisticsSum["orp"] = res["other"];
136
          }
137
          this.getDisplayOptions();
138
        }else{
139
          console.error("Error getting community statistics for community with id: " + this.communityId);
140
          this.status = this.errorCodes.ERROR;
141
        }
142
      },
143
      error => {
144
        //console.log(error);
145
        this.handleError("Error getting community statistics for community with id: " + this.communityId, error);
146
        this.status = this.errorCodes.ERROR;
147
      });
148
  }
149

    
150
  getDisplayOptions() {
151
    this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId)
152
      .subscribe(
153
        res => {
154
          this.statisticsDisplay = res;
155
          this.createChartUrlMap();
156
          this.getCommunityInfo();
157
          this.status = this.errorCodes.DONE;
158

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

    
168

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

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

    
190
  initializeDisplayedCharts() {
191
    let firstEntity: string;
192
    this.entitiesList = Array.from(this.entitiesMap.keys());
193

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

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

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

    
252
  }
253

    
254
  createChartUrlMap() {
255

    
256
    let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer);
257
    this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties);
258
  }
259

    
260

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

    
266
    if (this.statisticsSum[entity].total &&
267
      this.allowedEntities.filter(x => x == entity).length) {
268

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

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

    
302
  private handleError(message: string, error) {
303
    console.error("Statistics (Monitor) Page: " + message, error);
304
  }
305
}
306

    
307

    
308
@Component({
309
  selector: 'statistics-for-dashboard',
310
  template: ''
311
  // templateUrl: 'statistics-for-dashboard.component.html',
312
})
313

    
314
export class StatisticsForDashboardComponent extends StatisticsComponent {
315
  ngOnInit() {
316
    super.ngOnInit();
317
  }
318

    
319
}
(3-3/4)