Project

General

Profile

1
import {ChartHelper, IndicatorPath, Stakeholder} from "./entities/stakeholder";
2

    
3
export class IndicatorUtils {
4

    
5
  chartTypes: Map<string, string> = new Map([
6
    ['pie', 'pie_chart'],
7
    ['table', 'table_chart'],
8
    ['line', 'show_chart'],
9
    ['column', 'bar_chart'],
10
    ['bar', 'bar_chart'],
11
    ['image', 'perm_media']
12
  ]);
13

    
14
  isPublicIcon: Map<boolean, string> = new Map([
15
    [true, 'public'],
16
    [false, 'lock']
17
  ]);
18

    
19
  isActiveIcon: string = 'brightness_1';
20

    
21
  ignoredParameters = ['funder_name'];
22

    
23
  public getFullUrl(indicatorPath: IndicatorPath, fundingL0:string = null, startDate:string = null, endDate:string = null):string{
24

    
25
    let replacedUrl = indicatorPath.chartObject;
26
    if (indicatorPath.parameters) {
27
      indicatorPath.parameters.forEach((value, key) => {
28
        let replacedValue = value.toString();
29
        if( startDate && key == "start_year" && indicatorPath.filters.get("start_year")){
30
          replacedValue = (replacedValue < startDate)?startDate:replacedValue;
31
        }
32
        if( endDate && key == "end_year" && indicatorPath.filters.get("end_year")){
33
          replacedValue = (replacedValue > endDate)?endDate:replacedValue;
34
        }
35
        replacedUrl = replacedUrl.replace(ChartHelper.prefix + key + ChartHelper.suffix, replacedValue);
36
      });
37
    }
38
    if(indicatorPath.chartObject){
39
      if(fundingL0  && indicatorPath.filters.get("fundingL0")){
40
        let newJsonObject = JSON.parse(replacedUrl);
41
        for(let queries of newJsonObject["chartDescription"]["queries"]){
42
          if(!queries["query"]["filters"] || queries["query"]["filters"].length ==0) {
43
            queries["query"]["filters"] = [];
44
          }
45
          //TODO check how it works if the query already has a filter
46
            queries["query"]["filters"].push(JSON.parse(indicatorPath.filters.get("fundingL0").replace(ChartHelper.prefix + "fundingL0" + ChartHelper.suffix,fundingL0)));
47
        }
48
        replacedUrl = JSON.stringify(newJsonObject);
49

    
50
      }
51
    }
52
    return indicatorPath.url + encodeURIComponent(replacedUrl);
53
  }
54

    
55
  generateIndicatorByChartUrl(source: string, url: string, type: string = null): IndicatorPath {
56
    let indicatorPath = new IndicatorPath("", source, "", "", []);
57
    if (source === 'stats-tool') {
58
      indicatorPath.url = url.split("json=")[0] + "json=";
59
      indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
60
      indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
61
      let chart = JSON.parse(indicatorPath.chartObject);
62
      indicatorPath.type = this.extractType(chart, indicatorPath);
63
      this.extractTitle(chart, indicatorPath);
64
      this.extractXTitle(chart, indicatorPath);
65
      this.extractYTitle(chart, indicatorPath);
66
      this.extractFunder(chart, indicatorPath);
67
      this.extractStartYear(chart, indicatorPath);
68
      this.extractEndYear(chart, indicatorPath);
69
      indicatorPath.chartObject = JSON.stringify(chart);
70
      this.addResultFilters(chart,indicatorPath);
71
    } else if (source === 'old') {
72
      indicatorPath.url = url.split("data=")[0].split("/stats/")[1] + "data=";
73
      indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1].split("&")[0]);
74
      let chart = JSON.parse(indicatorPath.chartObject);
75
      this.extractOldToolTitle(chart,indicatorPath);
76
      this.extractOldToolXTitle(chart,indicatorPath);
77
      this.extractOldToolYTitle(chart,indicatorPath);
78
      indicatorPath.chartObject = JSON.stringify(chart);
79
    } else {
80
      indicatorPath.url = url;
81
      indicatorPath.type = type;
82
    }
83
    return indicatorPath;
84
  }
85

    
86
  private extractType(obj, indicatorPath: IndicatorPath): string {
87
    let defaultTypes = ["column", "bar", "pie"];
88
    let type = obj["chartDescription"]["queries"][0]["type"];
89
    if (defaultTypes.indexOf(type) == -1) {
90
      type = defaultTypes [0];
91
    } else {
92
      obj["chartDescription"]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
93
      indicatorPath.parameters.set("type", type);
94
    }
95
    return type;
96
  }
97

    
98
  private extractFunder(obj, indicatorPath: IndicatorPath) {
99
    let funderName;
100
    for (let query of obj["chartDescription"]["queries"]) {
101
      if(!query["query"]["filters"]){
102
        return;
103
      }
104
      for (let filter of query["query"]["filters"]) {
105
        if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
106
          funderName = filter["groupFilters"][0]["values"][0];
107
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "funder_name" + ChartHelper.suffix;
108
          indicatorPath.parameters.set("funder_name", funderName);
109
        }
110
      }
111
    }
112
  }
113

    
114
  private extractStartYear(obj, indicatorPath: IndicatorPath) {
115
    let start_year;
116
    for (let query of obj["chartDescription"]["queries"]) {
117
      if(!query["query"]["filters"]){
118
        return;
119
      }
120
      for (let filter of query["query"]["filters"]) {
121
        for (let gfilter of filter["groupFilters"]) {
122
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
123
            start_year = gfilter["values"][0];
124
            gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
125
            indicatorPath.parameters.set("start_year", start_year);
126
          }
127
        }
128
      }
129
    }
130
  }
131

    
132
  private extractEndYear(obj, indicatorPath: IndicatorPath) {
133
    let end_year;
134
    for (let query of obj["chartDescription"]["queries"]) {
135
      if(!query["query"]["filters"]){
136
        return;
137
      }
138
      for (let filter of query["query"]["filters"]) {
139
        for (let gfilter of filter["groupFilters"]) {
140
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
141
            end_year = gfilter["values"][0];
142
            gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
143
            indicatorPath.parameters.set("end_year", end_year);
144
          }
145
        }
146
      }
147
    }
148
  }
149

    
150
  private extractTitle(obj, indicatorPath: IndicatorPath) {
151
    let title = "";
152
    if (obj["chartDescription"]["title"]) {
153
      title = obj["chartDescription"]["title"]["text"];
154
      obj["chartDescription"]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
155
      indicatorPath.parameters.set("title", title);
156

    
157
    }
158
  }
159

    
160
  private extractXTitle(obj, indicatorPath: IndicatorPath) {
161
    let title = "";
162
    if (obj["chartDescription"]["xAxis"]["title"]) {
163
      title = obj["chartDescription"]["xAxis"]["title"]["text"];
164
      obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
165
      indicatorPath.parameters.set("xAxisTitle", title);
166
    }
167
  }
168

    
169
  private extractYTitle(obj, indicatorPath: IndicatorPath) {
170
    let title = "";
171
    if (obj["chartDescription"]["yAxis"]["title"]) {
172
      title = obj["chartDescription"]["yAxis"]["title"]["text"];
173
      obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
174
      indicatorPath.parameters.set("yAxisTitle", title);
175
    }
176
  }
177
  private addResultFilters(obj, indicatorPath: IndicatorPath) {
178
  let resultTypes  = ["publication","software","dataset","other"];
179
  let index = -1;
180
    for (let query of obj["chartDescription"]["queries"]) {
181
      if (!query["query"]["select"]) {
182
        return;
183
      }
184
      for (let select of query["query"]["select"]) {
185
        for (var i=0; i < resultTypes.length; i++) {
186
          if(select.field.startsWith(resultTypes[i]) ) {
187
            index = i;
188
          }
189
        }
190
      }
191

    
192
    }
193
    if(index != -1){
194
      indicatorPath.filters = IndicatorPath.createResultFilters(resultTypes[index]);
195
    }
196
  }
197

    
198

    
199
  private extractOldToolTitle(obj, indicatorPath: IndicatorPath) {
200
    let title = "";
201
    if (obj["title"]) {
202
      title = obj["title"];
203
      obj["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
204
      indicatorPath.parameters.set("title", title);
205

    
206
    }
207
  }
208

    
209
  private extractOldToolXTitle(obj, indicatorPath: IndicatorPath) {
210
    let title = "";
211
    if (obj["xaxistitle"]) {
212
      title = obj["xaxistitle"];
213
      obj["xaxistitle"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
214
      indicatorPath.parameters.set("xAxisTitle", title);
215
    }
216
  }
217

    
218
  private extractOldToolYTitle(obj, indicatorPath: IndicatorPath) {
219
    let title = "";
220
    if (obj["fieldsheaders"] ) {
221
      title = obj["fieldsheaders"];
222
      obj["fieldsheaders"]  = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
223
      indicatorPath.parameters.set("yAxisTitle", title);
224
    }
225
  }
226

    
227
}
228
/*
229
custom query
230
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22queries%22%3A%5B%7B%22name%22%3A%22Publications%22%2C%22type%22%3A%22bar%22%2C%22query%22%3A%7B%22name%22%3A%22monitor.ec.publications.datasources%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22Publication%20content%20provider%22%7D%2C%22subtitle%22%3A%7B%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Content%20provider%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Atrue%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D
231

    
232
query with year filters:
233
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22queries%22%3A%5B%7B%22name%22%3A%22Publications%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22publication%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22publication.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222008%22%5D%7D%2C%7B%22field%22%3A%22publication.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222020%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%5D%2C%22entity%22%3A%22publication%22%2C%22profile%22%3A%22OpenAIRE%20All-inclusive%22%2C%22limit%22%3A%220%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22Publications%20timeline%22%7D%2C%22subtitle%22%3A%7B%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Year%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Atrue%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D
234

    
235
double query
236
http://88.197.53.71:8080/stats-api/chart?json=%7B%22library%22%3A%22HighCharts%22%2C%22chartDescription%22%3A%7B%22colors%22%3A%5B%22%2342a5f5%22%2C%22%2326a69a%22%2C%22%2390ed7d%22%2C%22%23607d8b%22%2C%22%2300838f%22%2C%22%23689f38%22%2C%22%23e4d354%22%2C%22%232b908f%22%2C%22%23546e7a%22%2C%22%2301579%22%5D%2C%22queries%22%3A%5B%7B%22name%22%3A%22Gold%22%2C%22color%22%3A%22%23f8b500%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22result%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22result.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%2C%7B%22field%22%3A%22result.project.funding%20level%200%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22H2020%22%5D%7D%2C%7B%22field%22%3A%22result.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22publication%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222014%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222019%22%5D%7D%2C%7B%22field%22%3A%22result.access%20mode%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Open%20Access%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Journal%22%5D%7D%2C%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Journal%20Aggregator%2FPublisher%22%5D%7D%5D%2C%22op%22%3A%22OR%22%7D%5D%2C%22entity%22%3A%22result%22%2C%22profile%22%3A%22OpenAIRE%20original%22%2C%22limit%22%3A%220%22%7D%7D%2C%7B%22name%22%3A%22Green%22%2C%22color%22%3A%22%23239d60%22%2C%22type%22%3A%22column%22%2C%22query%22%3A%7B%22select%22%3A%5B%7B%22field%22%3A%22result%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22result.year%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.project.funder%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22European%20Commission%22%5D%7D%2C%7B%22field%22%3A%22result.project.funding%20level%200%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22H2020%22%5D%7D%2C%7B%22field%22%3A%22result.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22publication%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3E%3D%22%2C%22values%22%3A%5B%222014%22%5D%7D%2C%7B%22field%22%3A%22result.year%22%2C%22type%22%3A%22%3C%3D%22%2C%22values%22%3A%5B%222019%22%5D%7D%2C%7B%22field%22%3A%22result.access%20mode%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Open%20Access%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%2C%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Institutional%20Repository%22%5D%7D%2C%7B%22field%22%3A%22result.datasource.type%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22Thematic%20Repository%22%5D%7D%5D%2C%22op%22%3A%22OR%22%7D%5D%2C%22entity%22%3A%22result%22%2C%22profile%22%3A%22OpenAIRE%20original%22%2C%22limit%22%3A%220%22%7D%7D%5D%2C%22chart%22%3A%7B%22backgroundColor%22%3A%22%23FFFFFFFF%22%2C%22borderColor%22%3A%22%23335cadff%22%2C%22borderRadius%22%3A0%2C%22borderWidth%22%3A0%2C%22plotBorderColor%22%3A%22%23ccccccff%22%2C%22plotBorderWidth%22%3A0%7D%2C%22title%22%3A%7B%22text%22%3A%22H2020%20green%20and%20gold%20publications%22%7D%2C%22subtitle%22%3A%7B%22text%22%3A%22over%20time%22%7D%2C%22yAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Publications%22%7D%7D%2C%22xAxis%22%3A%7B%22title%22%3A%7B%22text%22%3A%22Year%22%7D%7D%2C%22lang%22%3A%7B%22noData%22%3A%22No%20Data%20available%20for%20the%20Query%22%7D%2C%22exporting%22%3A%7B%22enabled%22%3Atrue%7D%2C%22plotOptions%22%3A%7B%22series%22%3A%7B%22dataLabels%22%3A%7B%22enabled%22%3Afalse%7D%7D%7D%2C%22legend%22%3A%7B%22enabled%22%3Afalse%2C%22align%22%3A%22center%22%2C%22verticalAlign%22%3A%22bottom%22%2C%22layout%22%3A%22horizontal%22%7D%2C%22credits%22%3A%7B%22href%22%3Anull%2C%22enabled%22%3Atrue%2C%22text%22%3A%22Created%20by%20OpenAIRE%20via%20HighCharts%22%7D%7D%7D
237

    
238

    
239
//old tool
240
https://www.openaire.eu/stats/chart.php?com=query&data={%22table%22:%22result%22,%22fields%22:[{%22fld%22:%22number%22,%22agg%22:%22count%22,%22type%22:%22bar%22,%22yaxis%22:1,%22c%22:false}],%22xaxis%22:{%22name%22:%22result_datasources-datasource-name%22,%22agg%22:%22avg%22},%22group%22:%22%22,%22color%22:%22%22,%22type%22:%22chart%22,%22size%22:%2220%22,%22sort%22:%22count-number%22,%22yaxisheaders%22:[%22%22],%22fieldsheaders%22:[%22publications%22],%22in%22:[],%22filters%22:[{%22name%22:%22result_projects-project-funding_lvl0%22,%22values%22:[%22H2020%22],%22to%22:%22-1%22},{%22name%22:%22type%22,%22values%22:[%22publication%22],%22to%22:%22-1%22},{%22name%22:%22result_datasources-datasource-type%22,%22exvalues%22:[%22Publication%20Catalogue%22]}],%22having%22:[],%22xStyle%22:{%22r%22:%22-%22,%22s%22:%22-%22,%22l%22:%22-%22,%22ft%22:%22-%22,%22wt%22:%22-%22},%22title%22:%22H2020%20Publications%20by%20datasource%20%28top%2020%29%22,%22subtitle%22:%22%22,%22xaxistitle%22:%22datasource%22,%22order%22:%22d%22}&w=90%
241
 */
(1-1/2)