Project

General

Profile

1
import {
2
  ChartHelper,
3
  Indicator,
4
  IndicatorPath, IndicatorPathType,
5
  SourceType,
6
  Stakeholder,
7
  SubCategory,
8
  Topic
9
} from "../openaireLibrary/monitor/entities/stakeholder";
10
import {AbstractControl, ValidatorFn, Validators} from "@angular/forms";
11
import {Option} from "../openaireLibrary/dashboard/sharedComponents/input/input.component";
12

    
13
export class StakeholderUtils {
14
  defaultProfiles = {"funder":{
15
      index_id:"ec__________::EC",
16
  index_name:     "European Commission", index_shortName:"EC"}};
17
  
18
  
19
  types: Option[] = [
20
    {value: 'funder', label: 'Funder'},
21
    {value: 'ri', label: 'Reasearch Initiative'},
22
    {value: 'project', label: 'Project'},
23
    {value: 'organization', label: 'Organization'}
24
  ];
25

    
26
  isPublic: Option[] = [
27
    {icon: 'public', value: true, label: 'Public'},
28
    {icon: 'lock', value: false, label: 'Private'},
29
  ];
30

    
31
  isActive: Option[] = [
32
    {icon: 'brightness_1', iconClass: '', value: true, label: 'Active'},
33
    {icon: 'brightness_1', value: false, label: 'Inactive'},
34
  ];
35

    
36
  isPublicIcon: Map<boolean, string> = new Map([
37
    [true, 'public'],
38
    [false, 'lock']
39
  ]);
40

    
41
  isActiveIcon: string = 'brightness_1';
42

    
43
  public createFunderFromDefaultProfile(funder: Stakeholder, defaultTopics: Topic[]): Stakeholder {
44
    funder.topics = defaultTopics;
45
    for (let topic of funder.topics) {
46
      // console.log('id:' + topic._id);
47
      topic.defaultId = topic._id;
48
      topic._id = null;
49
      // console.log('defaultId:' + topic.defaultId);
50
      for (let category of topic.categories) {
51
        category.defaultId = category._id;
52
        category._id = null;
53
        let subTokeep: SubCategory[] = [];
54
        for (let subCategory of category.subCategories) {
55
          subCategory.defaultId = subCategory._id;
56
          subCategory._id = null;
57
          if (!subCategory.recommendedFor || subCategory.recommendedFor.length == 0 || subCategory.recommendedFor.indexOf(funder.index_id) != -1) {
58
            subTokeep.push(subCategory);
59
          }
60
          for (let section of subCategory.charts) {
61
            let chartsTokeep: Indicator[] = [];
62
            section.defaultId = section._id;
63
            section.stakeholderAlias = funder.alias;
64
            section._id = null;
65
            for (let indicator of section.indicators) {
66
              indicator.defaultId = indicator._id;
67
              indicator._id = null;
68
              if (!indicator.recommendedFor || indicator.recommendedFor.length == 0 || indicator.recommendedFor.indexOf(funder.index_id) != -1) {
69
                chartsTokeep.push(indicator);
70
              }
71
              for (let indicatorPath of indicator.indicatorPaths) {
72
                if (indicatorPath.parameters) {
73
                  Object.keys(indicatorPath.parameters).forEach(key => {
74
                    //TODO check before delete
75
                    /*if (indicatorPath.parameters[key].indexOf("_funder_name_") != -1) {
76
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_name_", funder.index_name);
77
                    } else if (indicatorPath.parameters[key].indexOf("_funder_id_") != -1) {
78
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].replace("_funder_id_", funder.index_id);
79
                    } else if (indicatorPath.parameters[key].indexOf("_fsn_") != -1) {
80
                      indicatorPath.parameters[key] = indicatorPath.parameters[key].toString().replace("_fsn_", funder.index_shortName.toLowerCase());
81
                    }*/
82
                    if (key == "index_name") {
83
                      indicatorPath.parameters[key] = funder.index_name;
84
                    } else if (key == "index_id" ) {
85
                      indicatorPath.parameters[key] = funder.index_id;
86
                    }  else if (key == "index_shortName" ) {
87
                      indicatorPath.parameters[key] =  funder.index_shortName.toLowerCase();
88
                    }
89
                  });
90
                }
91
              }
92
            }
93
            section.indicators = chartsTokeep;
94
          }
95
          for (let section of subCategory.numbers) {
96
            section.defaultId = section._id;
97
            section.stakeholderAlias = funder.alias;
98
            section._id = null;
99
            for(let indicator of section.indicators) {
100
              indicator.defaultId = indicator._id;
101
              indicator._id = null;
102
              for (let indicatorPath of indicator.indicatorPaths) {
103
                indicatorPath.url = indicatorPath.url.replace("index_id", encodeURIComponent(funder.index_id));
104
                indicatorPath.url = indicatorPath.url.replace("index_name",  encodeURIComponent(funder.index_name));
105
                indicatorPath.url = indicatorPath.url.replace("index_shortName", encodeURIComponent(funder.index_shortName));
106
                // if(indicatorPath.parameters) {
107
                //   indicatorPath.parameters.forEach((value: string, key: string) => {
108
                //     if (value.indexOf("_funder_name_")!=-1) {
109
                //       indicatorPath.parameters.set(key,value.toString().replace("_funder_name_", funder.index_name));
110
                //     }else if (value.indexOf("_fsn_")!=-1) {
111
                //       indicatorPath.parameters.set(key,value.toString().replace("_fsn_", funder.index_shortName.toLowerCase()));
112
                //     }
113
                //   });
114
                // }
115
              }
116
            }
117
          }
118

    
119
        }
120
        category.subCategories = subTokeep;
121
      }
122
    }
123
    console.log (funder)
124
    return funder;
125
  }
126

    
127
  aliasValidator(elements: any[]): ValidatorFn {
128
    return (control: AbstractControl): { [key: string]: boolean } | null => {
129
      if (control.value && elements.find(element =>
130
        element.alias === control.value
131
      )) {
132
        return {'alias': true};
133
      }
134
      return null;
135
    }
136
  }
137

    
138
  // TODO need to be fixed
139
  generateAlias(name: string): string {
140
    let alias = name.toLowerCase();
141
    while (alias.includes(' / ') || alias.includes(' ')) {
142
      alias = alias.replace(' / ', '-');
143
      alias = alias.replace(' ', '-');
144
    }
145
    return alias;
146
  }
147
}
148

    
149
export class IndicatorUtils {
150

    
151
  allChartTypes: Option[] = [
152
    {value: 'pie', label: 'Pie'},
153
    {value: 'table', label: 'Table'},
154
    {value: 'line', label: 'Line'},
155
    {value: 'column', label: 'Column'},
156
    {value: 'bar', label: 'Bar'},
157
    {value: 'other', label: 'Other'}
158
  ];
159
  basicChartTypes:IndicatorPathType[] =["pie", "line", "column", "bar"];
160
  defaultChartType:IndicatorPathType = "other";
161
  indicatorSizes: Option[] = [
162
    {value: 'small', label: 'Small'},
163
    {value: 'medium', label: 'Medium'},
164
    {value: 'large', label: 'Large'}
165
  ];
166

    
167
  sourceTypes: Option[]  = [
168
    {value: 'search', label: 'Search'},
169
    {value: 'statistics', label: 'Statistics'},
170
    {value: 'metrics', label: 'Metrics'}
171
  ]
172
  
173
  isPublic: Option[] = [
174
    {icon: 'public', value: true, label: 'Public'},
175
    {icon: 'lock', value: false, label: 'Private'},
176
  ];
177

    
178
  isActive: Option[] = [
179
    {icon: 'brightness_1', iconClass: '', value: true, label: 'Active'},
180
    {icon: 'brightness_1', value: false, label: 'Inactive'},
181
  ];
182

    
183
  chartTypesIcons: Map<string, string> = new Map([
184
    ['pie', 'pie_chart'],
185
    ['table', 'table_chart'],
186
    ['line', 'show_chart'],
187
    ['column', 'bar_chart'],
188
    ['bar', 'notes'],
189
    ['other', 'perm_media']
190
  ]);
191
  getChartTypes(initialType){
192
    let types: Option[]= [];
193
    if(this.basicChartTypes.indexOf(initialType) != -1){
194
      (this.allChartTypes).forEach(option => {
195
        if(this.basicChartTypes.indexOf(option.value)!=-1){
196
          types.push(option);
197
        }
198
      });
199
      return types;
200
    }else if(initialType == "table") {
201
      (this.allChartTypes).forEach(option => {
202
        if (initialType == option.value) {
203
          types.push(option);
204
        }
205
      });
206
      return types;
207
    }else {
208
      return this.allChartTypes;
209
    }
210
  }
211
  isPublicIcon: Map<boolean, string> = new Map([
212
    [true, 'public'],
213
    [false, 'lock']
214
  ]);
215

    
216
  isActiveIcon: string = 'brightness_1';
217

    
218
  ignoredParameters = ['index_name','index_id','index_shortName'];
219

    
220
  parametersValidators: Map<string, any> = new Map<string, any>([
221
    ['start_year', [Validators.required, Validators.pattern('^\\d+$')]],
222
    ['end_year', [Validators.required, Validators.pattern('^\\d+$')]]
223
  ]);
224

    
225
  public getFullUrl(stakeholder:Stakeholder, indicatorPath: IndicatorPath, fundingL0: string = null, startDate: string = null, endDate: string = null): string {
226
    let replacedUrl = indicatorPath.chartObject;
227
    if (indicatorPath.parameters) {
228
      Object.keys(indicatorPath.parameters).forEach(key => {
229
        let replacedValue = indicatorPath.parameters[key];
230
        if (startDate && key == "start_year" && indicatorPath.filters["start_year"]) {
231
          replacedValue = (replacedValue < startDate) ? startDate : replacedValue;
232
        }
233
        if (endDate && key == "end_year" && indicatorPath.filters["end_year"]) {
234
          replacedValue = (replacedValue > endDate) ? endDate : replacedValue;
235
        }
236
        if (key == "index_id") {
237
          replacedValue = stakeholder.index_id;
238
        }
239
        if (key == "index_name") {
240
          replacedValue = stakeholder.index_name;
241
        }
242
        if (key == "index_shortName") {
243
          replacedValue = stakeholder.index_shortName.toLowerCase();
244
        }
245

    
246
        replacedUrl = replacedUrl.split(ChartHelper.prefix + key + ChartHelper.suffix).join(replacedValue)
247
      });
248
    }
249
    if (indicatorPath.chartObject) {
250
      if (fundingL0 && indicatorPath.filters["fundingL0"]) {
251
        let newJsonObject = JSON.parse(replacedUrl);
252
        for (let queries of newJsonObject[this.getDescriptionObjectName(newJsonObject)]["queries"]) {
253
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
254
            queries["query"]["filters"] = [];
255
          }
256
          //TODO check how it works if the query already has a filter
257
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["fundingL0"].replace(ChartHelper.prefix + "fundingL0" + ChartHelper.suffix, fundingL0)));
258
        }
259
        replacedUrl = JSON.stringify(newJsonObject);
260
      }
261
    }
262
    return indicatorPath.url + encodeURIComponent(replacedUrl);
263
  }
264
  public static getNumberUrl(stakeholder:Stakeholder,url:string): string {
265
    let replacedUrl = url;
266

    
267
    if(url.indexOf(ChartHelper.prefix + 'index_id' + ChartHelper.suffix) !=- 1){
268
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_id' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_id))
269
    }
270
    if(url.indexOf(ChartHelper.prefix + 'index_name' + ChartHelper.suffix) !=- 1){
271
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_name' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_name))
272
    }
273
    if(url.indexOf(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix) !=- 1){
274
      replacedUrl = replacedUrl.split(ChartHelper.prefix + 'index_shortName' + ChartHelper.suffix).join(encodeURIComponent(stakeholder.index_shortName))
275
    }
276
    return replacedUrl;
277
  }
278
  generateIndicatorByForm(form: any, indicatorPaths: IndicatorPath[]): Indicator {
279
    let indicator: Indicator = new Indicator(form.name, form.description, 'chart',
280
      form.width, form.isActive, form.isPublic, indicatorPaths, form.defaultId);
281
    indicator._id = form.id;
282
    form.indicatorPaths.forEach((indicatorPath, index) => {
283
      indicator.indicatorPaths[index].type = indicatorPath.type;
284
      indicatorPath.parameters.forEach(parameter => {
285
        indicator.indicatorPaths[index].parameters[parameter.key] = parameter.value;
286
        if (parameter.key === 'type') {
287
          indicator.indicatorPaths[index].type = parameter.value;
288
        }
289
      });
290
    });
291
    return indicator;
292
  }
293
  parseNumberIndicatortUrl(source: SourceType, url: string, stakeholder:Stakeholder): IndicatorPath {
294
    let indicatorPath = new IndicatorPath(null, source, url, null, []);
295
    try {
296
      if (url.indexOf(encodeURIComponent(stakeholder.index_id)) !== -1) {
297
        url = url.split(encodeURIComponent(stakeholder.index_id)).join(ChartHelper.prefix + "index_id" + ChartHelper.suffix);
298
      }
299
      if (url.indexOf(encodeURIComponent(stakeholder.index_name)) !== -1) {
300
        url = url.split(encodeURIComponent(stakeholder.index_name)).join(ChartHelper.prefix + "index_name" + ChartHelper.suffix);
301
      }
302
      if (url.indexOf(encodeURIComponent(stakeholder.index_shortName)) !== -1) {
303
        url = url.split(encodeURIComponent(stakeholder.index_shortName)).join(ChartHelper.prefix + "index_shortName" + ChartHelper.suffix);
304
      }
305
      indicatorPath.url = url;
306
    }
307
    catch(e){
308
      console.error(e);
309
    }
310
    return indicatorPath;
311
  }
312
  generateIndicatorByChartUrl(source: SourceType, url: string, type: IndicatorPathType = null, stakeholder:Stakeholder): IndicatorPath {
313
    let indicatorPath = new IndicatorPath(type, source, "", "", []);
314
    try {
315
      if (source === 'stats-tool') {
316
        indicatorPath.url = url.split("json=")[0] + "json=";
317
        indicatorPath.url = indicatorPath.url.split("/")[indicatorPath.url.split("/").length - 1];
318
        indicatorPath.chartObject = decodeURIComponent(url.split("json=")[1]);
319
        let chart = JSON.parse(indicatorPath.chartObject);
320
        // console.debug(indicatorPath);
321
        if (indicatorPath.url == "chart?json=") {
322

    
323
          if (chart["library"] && (chart["library"] == "HighCharts" || chart["library"] == "eCharts" || chart["library"] == "HighMaps" )) {
324
            indicatorPath.type = this.extractType(chart, indicatorPath);
325
          } else {
326
            indicatorPath.type = this.defaultChartType;
327
          }
328

    
329
          this.extractTitle(chart, indicatorPath);
330
          this.extractSubTitle(chart, indicatorPath);
331
          this.extractXTitle(chart, indicatorPath);
332
          this.extractYTitle(chart, indicatorPath);
333
        }else if(indicatorPath.url == "table?json="){
334
          indicatorPath.type = "table";
335
        }
336
        if (indicatorPath.url == "chart?json=" || indicatorPath.url == "table?json=") {
337
          // common for tables and other chart types
338
          this.extractDataTitle(chart, indicatorPath);
339
          this.parameterizeDefaultQuery(chart, indicatorPath, stakeholder);
340
          this.extractStakeHolders(chart, indicatorPath, stakeholder);
341
          this.extractStartYear(chart, indicatorPath);
342
          this.extractEndYear(chart, indicatorPath);
343
          indicatorPath.chartObject = JSON.stringify(chart);
344
          this.addResultFilters(chart, indicatorPath);
345
        }
346
      } else if (source === 'old') {
347
        indicatorPath.url = url.split("data=")[0].split("/stats/")[1] + "data=";
348
        indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1].split("&")[0]);
349
        indicatorPath.type = type;
350
        let chart = JSON.parse(indicatorPath.chartObject);
351
        this.extractOldToolTitle(chart, indicatorPath);
352
        this.extractOldToolXTitle(chart, indicatorPath);
353
        this.extractOldToolYTitle(chart, indicatorPath);
354
        indicatorPath.chartObject = JSON.stringify(chart);
355
      } else {
356
        indicatorPath.url = url;
357
        indicatorPath.type = type;
358
      }
359
    }catch(e){
360
      console.error(e);
361
      indicatorPath.url = url;
362
      indicatorPath.type = type;
363
    }
364
    // console.debug(indicatorPath.parameters);
365
    // console.debug(indicatorPath.chartObject);
366
    if(indicatorPath.type == null){
367
      indicatorPath.type = this.defaultChartType;
368
    }
369
    return indicatorPath;
370
  }
371
  private getQueryObjectName(obj){
372
    if((obj[this.getDescriptionObjectName(obj)]).hasOwnProperty("queriesInfo")){
373
        return "queriesInfo";
374
    }else if((obj[this.getDescriptionObjectName(obj)]).hasOwnProperty("queries")) {
375
      return "queries";
376
    }
377
  }
378
  private getDescriptionObjectName(obj){
379
    if(obj.hasOwnProperty("mapDescription")){
380
      return "mapDescription";
381
    }else if(obj.hasOwnProperty("chartDescription")) {
382
      return "chartDescription";
383
    }else if(obj.hasOwnProperty("tableDescription") ){
384
      return "tableDescription";
385
    }
386
    }
387
  private extractType(obj, indicatorPath: IndicatorPath): IndicatorPathType {
388
    let type = (obj[this.getDescriptionObjectName(obj)] && obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)][0]["type"])?obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)][0]["type"]:"";
389
    if (this.basicChartTypes.indexOf(type) == -1) {
390
      type = this.defaultChartType;
391
    } else {
392
      obj[this.getDescriptionObjectName(obj)]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
393
      indicatorPath.parameters['type'] = type;
394
    }
395
    return type;
396
  }
397
  private extractStakeHolders(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
398
    this.extractFunder(obj, indicatorPath, stakeholder);
399
    this.extractRI(obj, indicatorPath, stakeholder);
400
    this.extractOrganization(obj, indicatorPath, stakeholder);
401
  }
402
  private extractFunder(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
403
    if(stakeholder.type != "funder"){
404
      return;
405
    }
406
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
407
      if (!query["query"]["filters"]) {
408
        return;
409
      }
410
      for (let filter of query["query"]["filters"]) {
411
        if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
412
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
413
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
414
        }else  if (filter["groupFilters"][0]["field"].indexOf(".funder.id") != -1) {
415
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
416
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
417
        }
418
      }
419
    }
420
  }
421

    
422
  private extractRI(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
423
    if(stakeholder.type != "ri"){
424
      return;
425
    }
426
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
427
      if (!query["query"]["filters"]) {
428
        return;
429
      }
430
      for (let filter of query["query"]["filters"]) {
431
        if (filter["groupFilters"][0]["field"].indexOf(".context.name") != -1) {
432
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
433
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
434
        }else  if (filter["groupFilters"][0]["field"].indexOf(".context.id") != -1) {
435
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
436
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
437
        }
438
      }
439
    }
440
  }
441

    
442
  private extractOrganization(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
443
    // works for publication.project.organization.name
444
    // and publication.organization.name
445
    if(stakeholder.type != "organization"){
446
      return;
447
    }
448
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
449
      if (!query["query"]["filters"]) {
450
        return;
451
      }
452
      for (let filter of query["query"]["filters"]) {
453
        if (filter["groupFilters"][0]["field"].indexOf(".organization.name") != -1) {
454
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
455
          indicatorPath.parameters["index_name"] = stakeholder.index_name;
456
        }else  if (filter["groupFilters"][0]["field"].indexOf(".organization.id") != -1) {
457
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
458
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName;
459
        }
460
      }
461
    }
462
  }
463
  private extractStartYear(obj, indicatorPath: IndicatorPath) {
464
    let start_year;
465
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
466
      if (!query["query"]["filters"]) {
467
        return;
468
      }
469
      for (let filter of query["query"]["filters"]) {
470
        for (let gfilter of filter["groupFilters"]) {
471
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
472
            start_year = gfilter["values"][0];
473
            gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
474
            indicatorPath.parameters["start_year"] = start_year;
475
          }
476
        }
477
      }
478
    }
479
  }
480

    
481
  private extractEndYear(obj, indicatorPath: IndicatorPath) {
482
    let end_year;
483
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
484
      if (!query["query"]["filters"]) {
485
        return;
486
      }
487
      for (let filter of query["query"]["filters"]) {
488
        for (let gfilter of filter["groupFilters"]) {
489
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
490
            end_year = gfilter["values"][0];
491
            gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
492
            indicatorPath.parameters["end_year"] = end_year;
493
          }
494
        }
495
      }
496
    }
497
  }
498

    
499
  private parameterizeDefaultQuery(obj, indicatorPath: IndicatorPath, stakeholder:Stakeholder) {
500
    let name = "";
501
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
502
      //monitor.{{stakeholderType}}.{{queryname}}
503
      //parameters: stakeholderId*, type
504
      if (query["query"]["name"]) {
505
        name = query["query"]["name"];
506
        let parameters = (query["query"]["parameters"])?query["query"]["parameters"]:[];
507
        if(name.split('.')[0] == "rcd" && parameters.length > 0  && stakeholder.type=="ri") {
508
          //rcd.{{queryname}}
509
          parameters[0] = ChartHelper.prefix + "index_id" + ChartHelper.suffix;
510
          indicatorPath.parameters["index_id"] = stakeholder.index_id;
511
        }else if(name.split('.')[0] == "monitor" && parameters.length == 0 && stakeholder.type=="funder"){
512
          // old saved queries without params
513
          //monitor.{{funder_shortName}}.{{type}}.{{queryname}}
514
          let stakeholderSN = name.split('.')[1];
515
          query["query"]["name"] = name.split('.' + stakeholderSN + ".")[0] + "." + ChartHelper.prefix + "index_shortName" + ChartHelper.suffix +"." + name.split('.' + stakeholderSN + ".")[1];
516
          indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName.toLowerCase();
517
        }else if(name.split('.')[0] == "monitor" && parameters.length > 0 && name.split('.')[1]  == stakeholder.type) {
518
          // new parameterized queries
519
          //monitor.{{type}}.{{queryname}}.{{param1 - id }}.{{param2 result-type}}.{{fl0}}
520
          if( name.split('.').length > 3 && name.split('.')[3] == "id") {
521
            parameters[0] = ChartHelper.prefix + "index_id" + ChartHelper.suffix;
522
            indicatorPath.parameters["index_id"] = stakeholder.index_id;
523
          }else if( name.split('.').length > 3 && name.split('.')[3] == "shortname") {
524
            parameters[0] = ChartHelper.prefix + "index_shortName" + ChartHelper.suffix;
525
            indicatorPath.parameters["index_shortName"] = stakeholder.index_shortName.toLowerCase();
526
          }else if( name.split('.').length > 3 && name.split('.')[3] == "name") {
527
            parameters[0] = ChartHelper.prefix + "index_name" + ChartHelper.suffix;
528
            indicatorPath.parameters["index_name"] = stakeholder.index_name;
529
          }
530
        }
531
      }
532
    }
533
  }
534
  private extractDataTitle(obj, indicatorPath: IndicatorPath) {
535
    let index = 0;
536
    if(!obj[this.getDescriptionObjectName(obj)] || !obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]){
537
      return;
538
    }
539
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
540
      if (query["name"]) {
541
        let name = query["name"];
542
        query["name"] =  ChartHelper.prefix + "data_title_"+index + ChartHelper.suffix;
543
        indicatorPath.parameters["data_title_"+index] = name;
544
      }
545
      index++;
546
    }
547
  }
548
  private extractTitle(obj, indicatorPath: IndicatorPath) {
549
    let title = "";
550
    if (obj[this.getDescriptionObjectName(obj)]["title"]) {
551
      title = obj[this.getDescriptionObjectName(obj)]["title"]["text"];
552
      obj[this.getDescriptionObjectName(obj)]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
553
    }else if (obj[this.getDescriptionObjectName(obj)]["options"] && obj[this.getDescriptionObjectName(obj)]["options"]["title"]) {
554
      title = obj[this.getDescriptionObjectName(obj)]["options"]["title"];
555
      obj[this.getDescriptionObjectName(obj)]["options"]["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
556
    }
557
    indicatorPath.parameters["title"] = title ? title : "";
558
  }
559

    
560
  private extractSubTitle(obj, indicatorPath: IndicatorPath) {
561
    let subtitle = "";
562
    if (obj[this.getDescriptionObjectName(obj)]["subtitle"]) {
563
      subtitle = obj[this.getDescriptionObjectName(obj)]["subtitle"]["text"];
564
      obj[this.getDescriptionObjectName(obj)]["subtitle"]["text"] = ChartHelper.prefix + "subtitle" + ChartHelper.suffix;
565
      indicatorPath.parameters["subtitle"] = subtitle ? subtitle : "";
566
    }else if (obj[this.getDescriptionObjectName(obj)]["title"] && obj[this.getDescriptionObjectName(obj)]["title"] && obj[this.getDescriptionObjectName(obj)]["title"]["subtext"]) {
567
      subtitle = obj[this.getDescriptionObjectName(obj)]["title"]["subtext"];
568
      obj[this.getDescriptionObjectName(obj)]["title"]["subtext"] = ChartHelper.prefix + "subtitle" + ChartHelper.suffix;
569
      indicatorPath.parameters["subtitle"] = subtitle ? subtitle : "";
570
    }
571
  }
572

    
573
  private extractXTitle(obj, indicatorPath: IndicatorPath) {
574
    let title = "";
575
    if (obj[this.getDescriptionObjectName(obj)]["xAxis"] && obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]) {
576
      title = obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]["text"];
577
      obj[this.getDescriptionObjectName(obj)]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
578
    }else if (obj[this.getDescriptionObjectName(obj)]["options"] && obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"] && obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"]) {
579
      title = obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"];
580
      obj[this.getDescriptionObjectName(obj)]["options"]["hAxis"]["title"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
581
    }else if (obj[this.getDescriptionObjectName(obj)]["xAxis"] && obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"]) {
582
      title = obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"];
583
      obj[this.getDescriptionObjectName(obj)]["xAxis"]["name"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
584
    }
585
    indicatorPath.parameters["xAxisTitle"] = title ? title : "";
586
  }
587

    
588
  private extractYTitle(obj, indicatorPath: IndicatorPath) {
589
    let title = "";
590
    if (obj[this.getDescriptionObjectName(obj)]["yAxis"] && obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"] ) {
591
      title = obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"]["text"];
592
      obj[this.getDescriptionObjectName(obj)]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
593
    }else if (obj[this.getDescriptionObjectName(obj)]["options"]&& obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"] && obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"]) {
594
      title = obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"];
595
      obj[this.getDescriptionObjectName(obj)]["options"]["vAxis"]["title"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
596
    }else if (obj[this.getDescriptionObjectName(obj)]["yAxis"] && obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"]) {
597
      title = obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"];
598
      obj[this.getDescriptionObjectName(obj)]["yAxis"]["name"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
599
    }
600
    indicatorPath.parameters["yAxisTitle"] = title ? title : "";
601
  }
602

    
603
  private addResultFilters(obj, indicatorPath: IndicatorPath) {
604
    let resultTypes = ["publication", "software", "dataset", "other"];
605
    let index = -1;
606
    for (let query of obj[this.getDescriptionObjectName(obj)][this.getQueryObjectName(obj)]) {
607
      if (!query["query"]["select"]) {
608
        return;
609
      }
610
      for (let select of query["query"]["select"]) {
611
        for (var i = 0; i < resultTypes.length; i++) {
612
          if (select.field.startsWith(resultTypes[i])) {
613
            index = i;
614
          }
615
        }
616
      }
617

    
618
    }
619
    if (index != -1) {
620
      indicatorPath.filters = IndicatorPath.createResultFilters(resultTypes[index]);
621
    }
622
  }
623

    
624

    
625
  private extractOldToolTitle(obj, indicatorPath: IndicatorPath) {
626
    let title = "";
627
    if (obj["title"]) {
628
      title = obj["title"];
629
      obj["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
630
      indicatorPath.parameters["title"] = title;
631

    
632
    }
633
  }
634

    
635
  private extractOldToolXTitle(obj, indicatorPath: IndicatorPath) {
636
    let title = "";
637
    if (obj["xaxistitle"]) {
638
      title = obj["xaxistitle"];
639
      obj["xaxistitle"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
640
      indicatorPath.parameters["xAxisTitle"] = title;
641
    }
642
  }
643

    
644
  private extractOldToolYTitle(obj, indicatorPath: IndicatorPath) {
645
    let title = "";
646
    if (obj["fieldsheaders"]) {
647
      title = Array.isArray(obj["fieldsheaders"]) ? obj["fieldsheaders"][0] : obj["fieldsheaders"];
648
      if (Array.isArray(obj["fieldsheaders"])) {
649
        obj["fieldsheaders"][0] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
650
      } else {
651
        obj["fieldsheaders"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
652
      }
653
      indicatorPath.parameters["yAxisTitle"] = title;
654
    }
655
  }
656

    
657
}
658

    
659
/*
660
custom query
661
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
662

    
663
query with year filters:
664
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
665

    
666
double query
667
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
668

    
669

    
670
//old tool
671
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%
672
 */
(2-2/3)