import {Component, Input} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Location, TitleCasePipe} from '@angular/common'; import {Title, Meta} from '@angular/platform-browser'; import {DomSanitizer} from '@angular/platform-browser'; import "rxjs/add/observable/zip"; import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties'; import {ErrorCodes} from '../openaireLibrary/utils/properties/errorCodes'; import {StatisticsService} from '../utils/services/statistics.service'; import {ConfigurationService} from '../openaireLibrary/utils/configuration/configuration.service'; import {PiwikService} from '../openaireLibrary/utils/piwik/piwik.service'; import {ConnectHelper} from '../openaireLibrary/connect/connectHelper'; import { availableCharts, availableEntitiesMap, StatisticsDisplay, StatisticsSummary } from "../openaireLibrary/connect/statistics/statisticsEntities"; import {PiwikHelper} from '../utils/piwikHelper'; import {CommunityCharts} from "../openaireLibrary/connect/statistics/communityCharts"; import {HelperFunctions} from "../openaireLibrary/utils/HelperFunctions.class"; import {CommunityService} from "../openaireLibrary/connect/community/community.service"; @Component({ selector: 'statistics', templateUrl: 'statistics.component.html', }) export class StatisticsComponent { public piwiksub: any; public pageTitle = "OpenAIRE"; properties: EnvProperties; @Input() communityId = null; @Input() currentMode = 'showInMonitor'; communityInfo: any = null; entitiesList: string[] = []; entitiesMap: Map = availableEntitiesMap; chartCatsList: string[] = availableCharts; allowedCharts: Map = new Map(); allowedEntities: string[] = []; allowedChartsMode = {showInMonitor: new Map(), showInDashboard: new Map()}; allowedEntitiesMode = {showInMonitor: [], showInDashboard: []}; statisticsSum: StatisticsSummary = null; statisticsDisplay: StatisticsDisplay = null; chartTitlesMode = {showInMonitor: {}, showInDashboard: {}}; chartsInfoMap: {}; displayedTimeline: string; displayedTimelineUrl: string; displayedGraph: string; displayedGraphUrl: string; displayedProjectChart: string; displayedProjectChartUrl: string; displayedEntity: string; public errorCodes: ErrorCodes = new ErrorCodes(); status = null; communityName = null; constructor( private route: ActivatedRoute, private _router: Router, private location: Location, private _meta: Meta, private _title: Title, private _piwikService: PiwikService, private _statisticsService: StatisticsService, private _configService: ConfigurationService, private titleCase: TitleCasePipe, private _communityService: CommunityService, private sanitizer: DomSanitizer) { } public ngOnInit() { if (this.currentMode == "showInMonitor") { var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects "; var title = "Monitor"; this._title.setTitle(title); this._meta.updateTag({content: description}, "name='description'"); this._meta.updateTag({content: description}, "property='og:description'"); this._meta.updateTag({content: title}, "property='og:title'"); } this.route.data .subscribe((data: { envSpecific: EnvProperties }) => { this.properties = data.envSpecific; var url = data.envSpecific.baseLink + this._router.url; this._meta.updateTag({content: url}, "property='og:url'"); this.route.queryParams.subscribe( communityId => { this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); if(!this.communityId) { this.communityId = communityId['communityId']; } if (this.currentMode == "showInMonitor" && this.properties.enablePiwikTrack && (typeof document !== 'undefined')) { this.piwiksub = this._piwikService.trackView(this.properties, "Monitor " + this.communityId, PiwikHelper.siteIDs[this.communityId]).subscribe(); } this._communityService.getCommunity(this.properties, this.properties.communityAPI + this.communityId).subscribe( community => { if (typeof document !== 'undefined') { HelperFunctions.scroll(); } this.communityName = community.shortTitle; this.createStatisticsObjects(); }); // console.log(" Stats! "+ this.properties.statisticsAPIURL); }); }); } public ngOnDestroy() { if (this.piwiksub) { this.piwiksub.unsubscribe(); } } getCamelCaseString(inputString: string) { return this.titleCase.transform(inputString); } createStatisticsObjects() { // console.log(" Stats! "+ this.properties.statisticsAPIURL); this.status = this.errorCodes.LOADING; this._statisticsService.getCommunityStatistics(this.properties, this.communityId).subscribe( res => { if(res) { this.statisticsSum = res; 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 this.statisticsSum["orp"] = res["other"]; } this.getDisplayOptions(); }else{ console.error("Error getting community statistics for community with id: " + this.communityId); this.status = this.errorCodes.ERROR; } }, error => { //console.log(error); this.handleError("Error getting community statistics for community with id: " + this.communityId, error); this.status = this.errorCodes.ERROR; }); } getDisplayOptions() { this._statisticsService.getCommunityAdminStatisticsChoices(this.properties, this.communityId) .subscribe( res => { this.statisticsDisplay = res; this.createChartUrlMap(); this.getCommunityInfo(); this.status = this.errorCodes.DONE; }, error => { //console.log(error); this.handleError("Error getting community statistics choices by administrators for community with id: " + this.communityId, error); this.status = this.errorCodes.ERROR; } ); } getCommunityInfo() { // console.log(`calling ${this.properties.adminToolsAPIURL}/communityFull/${this.communityId}`); this._configService.getCommunityInformation(this.properties, this.communityId).subscribe( res => { this.communityInfo = res; /*for(let i=0; i { //console.log(error) this.handleError("Error getting community with id: " + this.communityId, error); }, () => this.initializeDisplayedCharts() ); } initializeDisplayedCharts() { let firstEntity: string; this.entitiesList = Array.from(this.entitiesMap.keys()); this.allowedChartsMode[this.currentMode] = this.allowedCharts; this.allowedEntitiesMode[this.currentMode] = this.allowedEntities; let titles = []; // console.log('this.entitiesList is',this.entitiesList); // console.log(`my current mode is: ${this.currentMode}`); for (let entity of this.entitiesList) { if (this.statisticsDisplay.entities[entity] && this.statisticsSum[entity].total && this.communityInfo.entities.filter(x => x['pid'] == entity && x['isEnabled'] === true).length) { this.allowedChartsMode.showInDashboard[entity] = []; this.allowedChartsMode.showInMonitor[entity] = []; for (let chart of this.chartCatsList) { if (this.statisticsSum[entity].total && this.statisticsDisplay.entities[entity].charts.map[chart] && this.statisticsDisplay.entities[entity].charts.map[chart]["showInDashboard"] && this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) { this.allowedChartsMode.showInDashboard[entity].push(entity + this.getCamelCaseString(chart)); if (titles.indexOf("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) { titles.push("dashboard" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title); this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = true; } else { this.chartTitlesMode.showInDashboard[entity + this.getCamelCaseString(chart)] = false; } // console.log(`added ${entity} - ${chart} to allowedCharts`); } if (this.statisticsSum[entity].total && this.statisticsDisplay.entities[entity].charts.map[chart] && this.statisticsDisplay.entities[entity].charts.map[chart]["showInMonitor"] && this.chartsInfoMap[entity + this.getCamelCaseString(chart)].url) { this.allowedChartsMode.showInMonitor[entity].push(entity + this.getCamelCaseString(chart)); if (titles.indexOf("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title) == -1) { titles.push("monitor" + this.chartsInfoMap[entity + this.getCamelCaseString(chart)].title); this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = true; } else { this.chartTitlesMode.showInMonitor[entity + this.getCamelCaseString(chart)] = false; } // console.log(`added ${entity} - ${chart} to allowedCharts`); } } if (this.allowedChartsMode.showInMonitor[entity].length) { // console.log(`added ${entity} to allowedEntities`); this.allowedEntitiesMode.showInMonitor.push(entity); if (!firstEntity) { firstEntity = entity; this.onChangeEntity(entity); } } if (this.allowedChartsMode.showInDashboard[entity].length) { // console.log(`added ${entity} to allowedEntities`); this.allowedEntitiesMode.showInDashboard.push(entity); if (!firstEntity) { firstEntity = entity; this.onChangeEntity(entity); } } } } } createChartUrlMap() { let communityCharts: CommunityCharts = new CommunityCharts(this.sanitizer); this.chartsInfoMap = communityCharts.getChartsForCommunity(this.communityId, this.communityName, this.properties); } onChangeEntity(entity: string) { this.displayedEntity = entity; // console.log(`displayed entity is ${entity}`); // console.log(`statisticsSum[${entity}].total is ${this.statisticsSum[entity].total}`); if (this.statisticsSum[entity].total && this.allowedEntities.filter(x => x == entity).length) { // console.log(`found ${entity} in allowedEntities`); this.displayedTimeline = `${entity}Timeline`; this.displayedTimelineUrl = this.chartsInfoMap[this.displayedTimeline].url; // console.log(`displayed Timeline is: ${this.displayedTimeline}`); this.displayedGraph = `${entity}Graph`; this.displayedGraphUrl = this.chartsInfoMap[this.displayedGraph].url; // console.log(`displayed Graph is: ${this.displayedGraph}`); if (this.allowedCharts[entity]) { let firstProjectChart = this.allowedCharts[entity].filter(x => x.includes(entity + 'Project')); if (firstProjectChart[0]) { this.changeDisplayedProjectChart(firstProjectChart[0]); } else { this.displayedProjectChart = ''; this.displayedProjectChartUrl = ''; // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`); } } } else { this.displayedTimeline = ''; this.displayedTimelineUrl = ''; // console.log(`displayed Timeline is: ${this.displayedTimeline}`); this.displayedGraph = ''; this.displayedGraphUrl = ''; // console.log(`displayed Graph is: ${this.displayedGraph}`); } } changeDisplayedProjectChart(chartName: string) { this.displayedProjectChart = chartName; this.displayedProjectChartUrl = this.chartsInfoMap[this.displayedProjectChart].url; // console.log(`displayed ProjectChart is: ${this.displayedProjectChart}`); } private handleError(message: string, error) { console.error("Statistics (Monitor) Page: " + message, error); } } @Component({ selector: 'statistics-for-dashboard', template: '' // templateUrl: 'statistics-for-dashboard.component.html', }) export class StatisticsForDashboardComponent extends StatisticsComponent { ngOnInit() { super.ngOnInit(); } }