Commit 640217ea authored by Felipe Wu's avatar Felipe Wu

[003] update functions

Signed-off-by: 's avatarFelipe Wu <felipeshiwu@gmail.com>
parent c070f7f8
Pipeline #20090 failed with stages
in 26 seconds
......@@ -2,7 +2,7 @@ import { View } from "../core/view";
import { Source } from "../core/source";
import { FilterOperator } from "../core/filter";
import { Adapter } from "../core/adapter";
import { DataType } from "../common/types";
import { DataType, AggregationType } from "../common/types";
import { Client, ConfigOptions } from "elasticsearch";
/** @hidden */
......@@ -40,34 +40,40 @@ export class ElasticsearchAdapter extends Adapter {
}
};
/** Query is used to define the filters */
let filter = {"match": { "TP_SEXO": "2" }};
let filter = {"match": { [view.clauses[0].filters[0].target.name]: view.clauses[0].filters[0].value }};
let match = [];
match.push(filter);
/** Query is used to define the filters */
for(let i=1;i<view.clauses[0].filters.length; i++) {
filter = {"match": { [view.clauses[0].filters[i].target.name]: view.clauses[0].filters[i].value }};
match.push(filter);
}
Object.assign(query.query.bool, {must: match});
//console.log(JSON.stringify(initial));
console.log(JSON.stringify(view, null, 2));
/** Aggregation is used to define the dimention and the aggregation */
const func = this.getAggregateFunction(view.metrics[0].aggregation, view.origin);
let aggrName = func + "_" + view.metrics[0].name.toLowerCase();
let aggregation = {
group_by_estado: {
group_by: {
terms: {
field: "CO_UF_NASC.keyword",
field: view.dimensions[0].name + ".keyword",
size: 10000,
order: {
average_idade: "asc"
[aggrName]: "asc"
}
},
aggs: {
average_idade: {avg: {field: "NU_IDADE"}},
sum_idade: {sum: {field: "NU_IDADE" }}
}
}
},
aggs: {}
}
};
Object.assign(aggregation.group_by.aggs, {[aggrName]: {[func]: {field: view.metrics[0].name}}});
Object.assign(query, {aggs: aggregation});
console.log(JSON.stringify(query, null, 2));
return query;
}
......@@ -152,4 +158,22 @@ export class ElasticsearchAdapter extends Adapter {
*/
public insertIntoSource(source: Source, data: any[], cb: (err: Error, result?: any[]) => void): void {
}
private getAggregateFunction(aggrType: AggregationType, origin: boolean): string {
switch (aggrType) {
case AggregationType.SUM:
return "sum";
case AggregationType.AVG:
return "avg";
case AggregationType.COUNT:
return "value_count";
case AggregationType.MAX:
return "max";
case AggregationType.MIN:
return "min";
default:
return "";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment