Project

General

Profile

« Previous | Next » 

Revision 57673

[Monitor Dashboard]: Complete indicator save and reorder.

View differences:

indicator-utils.ts
1
import {ChartHelper, IndicatorPath, Stakeholder} from "./entities/stakeholder";
1
import {ChartHelper, Indicator, IndicatorPath} from "./entities/stakeholder";
2
import {Validators} from "@angular/forms";
2 3

  
4
export interface Option {
5
  icon?: string,
6
  iconClass?: string,
7
  value: any,
8
  label: string
9
}
10

  
3 11
export class IndicatorUtils {
4 12

  
5
  chartTypes: Map<string, string> = new Map([
13
  chartTypes: Option[] = [
14
    {value: 'pie', label: 'Pie'},
15
    {value: 'table', label: 'Table'},
16
    {value: 'line', label: 'Line'},
17
    {value: 'column', label: 'Column'},
18
    {value: 'bar', label: 'Bar'},
19
    {value: 'other', label: 'Other'}
20
  ];
21

  
22
  chartSizes: Option[] = [
23
    {value: 'small', label: 'Small'},
24
    {value: 'medium', label: 'Medium'},
25
    {value: 'large', label: 'Large'}
26
  ];
27

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

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

  
38
  chartTypesIcons: Map<string, string> = new Map([
6 39
    ['pie', 'pie_chart'],
7 40
    ['table', 'table_chart'],
8 41
    ['line', 'show_chart'],
9 42
    ['column', 'bar_chart'],
10 43
    ['bar', 'bar_chart'],
11
    ['image', 'perm_media']
44
    ['other', 'perm_media']
12 45
  ]);
13 46

  
14 47
  isPublicIcon: Map<boolean, string> = new Map([
......
20 53

  
21 54
  ignoredParameters = ['funder_name'];
22 55

  
23
  public getFullUrl(indicatorPath: IndicatorPath, fundingL0:string = null, startDate:string = null, endDate:string = null):string{
56
  parametersValidators: Map<string, any> = new Map<string, any>([
57
    ['start_year', [Validators.required, Validators.pattern('^\\d+$')]],
58
    ['end_year', [Validators.required, Validators.pattern('^\\d+$')]]
59
  ]);
24 60

  
61
  public getFullUrl(indicatorPath: IndicatorPath, fundingL0: string = null, startDate: string = null, endDate: string = null): string {
62

  
25 63
    let replacedUrl = indicatorPath.chartObject;
26 64
    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;
65
      Object.keys(indicatorPath.parameters).forEach(key => {
66
        let replacedValue = indicatorPath.parameters[key];
67
        if (startDate && key == "start_year" && indicatorPath.filters["start_year"]) {
68
          replacedValue = (replacedValue < startDate) ? startDate : replacedValue;
31 69
        }
32
        if( endDate && key == "end_year" && indicatorPath.filters.get("end_year")){
33
          replacedValue = (replacedValue > endDate)?endDate:replacedValue;
70
        if (endDate && key == "end_year" && indicatorPath.filters["end_year"]) {
71
          replacedValue = (replacedValue > endDate) ? endDate : replacedValue;
34 72
        }
35 73
        replacedUrl = replacedUrl.replace(ChartHelper.prefix + key + ChartHelper.suffix, replacedValue);
36 74
      });
37 75
    }
38
    if(indicatorPath.chartObject){
39
      if(fundingL0  && indicatorPath.filters.get("fundingL0")){
76
    if (indicatorPath.chartObject) {
77
      if (fundingL0 && indicatorPath.filters["fundingL0"]) {
40 78
        let newJsonObject = JSON.parse(replacedUrl);
41
        for(let queries of newJsonObject["chartDescription"]["queries"]){
42
          if(!queries["query"]["filters"] || queries["query"]["filters"].length ==0) {
79
        for (let queries of newJsonObject["chartDescription"]["queries"]) {
80
          if (!queries["query"]["filters"] || queries["query"]["filters"].length == 0) {
43 81
            queries["query"]["filters"] = [];
44 82
          }
45 83
          //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)));
84
          queries["query"]["filters"].push(JSON.parse(indicatorPath.filters["fundingL0"].
85
          replace(ChartHelper.prefix + "fundingL0" + ChartHelper.suffix, fundingL0)));
47 86
        }
48 87
        replacedUrl = JSON.stringify(newJsonObject);
49

  
50 88
      }
51 89
    }
52 90
    return indicatorPath.url + encodeURIComponent(replacedUrl);
53 91
  }
54 92

  
93
  generateIndicatorByForm(form: any, indicatorPaths: IndicatorPath[]): Indicator {
94
    let indicator: Indicator = new Indicator(form.name, form.description, 'chart',
95
      form.width, form.isActive, form.isPublic, indicatorPaths);
96
    indicator._id = form.id;
97
    form.indicatorPaths.forEach((indicatorPath, index) => {
98
      indicatorPath.parameters.forEach(parameter => {
99
        indicator.indicatorPaths[index].parameters[parameter.key] = parameter.value;
100
        if(parameter.key === 'type') {
101
          indicator.indicatorPaths[index].type = parameter.value;
102
        }
103
      });
104
    });
105
    return indicator;
106
  }
107

  
55 108
  generateIndicatorByChartUrl(source: string, url: string, type: string = null): IndicatorPath {
56 109
    let indicatorPath = new IndicatorPath("", source, "", "", []);
57 110
    if (source === 'stats-tool') {
......
67 120
      this.extractStartYear(chart, indicatorPath);
68 121
      this.extractEndYear(chart, indicatorPath);
69 122
      indicatorPath.chartObject = JSON.stringify(chart);
70
      this.addResultFilters(chart,indicatorPath);
123
      this.addResultFilters(chart, indicatorPath);
71 124
    } else if (source === 'old') {
72 125
      indicatorPath.url = url.split("data=")[0].split("/stats/")[1] + "data=";
73 126
      indicatorPath.chartObject = decodeURIComponent(url.split("data=")[1].split("&")[0]);
74 127
      let chart = JSON.parse(indicatorPath.chartObject);
75
      this.extractOldToolTitle(chart,indicatorPath);
76
      this.extractOldToolXTitle(chart,indicatorPath);
77
      this.extractOldToolYTitle(chart,indicatorPath);
128
      this.extractOldToolTitle(chart, indicatorPath);
129
      this.extractOldToolXTitle(chart, indicatorPath);
130
      this.extractOldToolYTitle(chart, indicatorPath);
78 131
      indicatorPath.chartObject = JSON.stringify(chart);
79 132
    } else {
80 133
      indicatorPath.url = url;
......
90 143
      type = defaultTypes [0];
91 144
    } else {
92 145
      obj["chartDescription"]["queries"][0]["type"] = ChartHelper.prefix + "type" + ChartHelper.suffix;
93
      indicatorPath.parameters.set("type", type);
146
      indicatorPath.parameters['type'] = type;
94 147
    }
95 148
    return type;
96 149
  }
......
98 151
  private extractFunder(obj, indicatorPath: IndicatorPath) {
99 152
    let funderName;
100 153
    for (let query of obj["chartDescription"]["queries"]) {
101
      if(!query["query"]["filters"]){
154
      if (!query["query"]["filters"]) {
102 155
        return;
103 156
      }
104 157
      for (let filter of query["query"]["filters"]) {
105 158
        if (filter["groupFilters"][0]["field"].indexOf(".funder") != -1) {
106 159
          funderName = filter["groupFilters"][0]["values"][0];
107 160
          filter["groupFilters"][0]["values"][0] = ChartHelper.prefix + "funder_name" + ChartHelper.suffix;
108
          indicatorPath.parameters.set("funder_name", funderName);
161
          indicatorPath.parameters["funder_name"]  = funderName;
109 162
        }
110 163
      }
111 164
    }
......
114 167
  private extractStartYear(obj, indicatorPath: IndicatorPath) {
115 168
    let start_year;
116 169
    for (let query of obj["chartDescription"]["queries"]) {
117
      if(!query["query"]["filters"]){
170
      if (!query["query"]["filters"]) {
118 171
        return;
119 172
      }
120 173
      for (let filter of query["query"]["filters"]) {
......
122 175
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf(">") != -1) {
123 176
            start_year = gfilter["values"][0];
124 177
            gfilter["values"][0] = ChartHelper.prefix + "start_year" + ChartHelper.suffix;
125
            indicatorPath.parameters.set("start_year", start_year);
178
            indicatorPath.parameters["start_year"] =  start_year;
126 179
          }
127 180
        }
128 181
      }
......
132 185
  private extractEndYear(obj, indicatorPath: IndicatorPath) {
133 186
    let end_year;
134 187
    for (let query of obj["chartDescription"]["queries"]) {
135
      if(!query["query"]["filters"]){
188
      if (!query["query"]["filters"]) {
136 189
        return;
137 190
      }
138 191
      for (let filter of query["query"]["filters"]) {
......
140 193
          if (gfilter["field"].indexOf(".year") != -1 && gfilter["type"].indexOf("<") != -1) {
141 194
            end_year = gfilter["values"][0];
142 195
            gfilter["values"][0] = ChartHelper.prefix + "end_year" + ChartHelper.suffix;
143
            indicatorPath.parameters.set("end_year", end_year);
196
            indicatorPath.parameters["end_year"] =  end_year;
144 197
          }
145 198
        }
146 199
      }
......
152 205
    if (obj["chartDescription"]["title"]) {
153 206
      title = obj["chartDescription"]["title"]["text"];
154 207
      obj["chartDescription"]["title"]["text"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
155
      indicatorPath.parameters.set("title", title);
208
      indicatorPath.parameters["title"] =  title;
156 209

  
157 210
    }
158 211
  }
......
162 215
    if (obj["chartDescription"]["xAxis"]["title"]) {
163 216
      title = obj["chartDescription"]["xAxis"]["title"]["text"];
164 217
      obj["chartDescription"]["xAxis"]["title"]["text"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
165
      indicatorPath.parameters.set("xAxisTitle", title);
218
      indicatorPath.parameters["xAxisTitle"] = title;
166 219
    }
167 220
  }
168 221

  
......
171 224
    if (obj["chartDescription"]["yAxis"]["title"]) {
172 225
      title = obj["chartDescription"]["yAxis"]["title"]["text"];
173 226
      obj["chartDescription"]["yAxis"]["title"]["text"] = ChartHelper.prefix + "yAxisTitle" + ChartHelper.suffix;
174
      indicatorPath.parameters.set("yAxisTitle", title);
227
      indicatorPath.parameters["yAxisTitle"] = title;
175 228
    }
176 229
  }
230

  
177 231
  private addResultFilters(obj, indicatorPath: IndicatorPath) {
178
  let resultTypes  = ["publication","software","dataset","other"];
179
  let index = -1;
232
    let resultTypes = ["publication", "software", "dataset", "other"];
233
    let index = -1;
180 234
    for (let query of obj["chartDescription"]["queries"]) {
181 235
      if (!query["query"]["select"]) {
182 236
        return;
183 237
      }
184 238
      for (let select of query["query"]["select"]) {
185
        for (var i=0; i < resultTypes.length; i++) {
186
          if(select.field.startsWith(resultTypes[i]) ) {
239
        for (var i = 0; i < resultTypes.length; i++) {
240
          if (select.field.startsWith(resultTypes[i])) {
187 241
            index = i;
188 242
          }
189 243
        }
190 244
      }
191 245

  
192 246
    }
193
    if(index != -1){
247
    if (index != -1) {
194 248
      indicatorPath.filters = IndicatorPath.createResultFilters(resultTypes[index]);
195 249
    }
196 250
  }
......
201 255
    if (obj["title"]) {
202 256
      title = obj["title"];
203 257
      obj["title"] = ChartHelper.prefix + "title" + ChartHelper.suffix;
204
      indicatorPath.parameters.set("title", title);
258
      indicatorPath.parameters["title"] = title;
205 259

  
206 260
    }
207 261
  }
......
211 265
    if (obj["xaxistitle"]) {
212 266
      title = obj["xaxistitle"];
213 267
      obj["xaxistitle"] = ChartHelper.prefix + "xAxisTitle" + ChartHelper.suffix;
214
      indicatorPath.parameters.set("xAxisTitle", title);
268
      indicatorPath.parameters["xAxisTitle"] = title;
215 269
    }
216 270
  }
217 271

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

  
227 281
}
282

  
228 283
/*
229 284
custom query
230 285
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

Also available in: Unified diff