Commit 997d0248 authored by Felipe Wu's avatar Felipe Wu

[001] Conecta com o elasticsearch

Signed-off-by: 's avatarFelipe Wu <felipeshiwuelipeshiwu@gmail.com>
parent e60dbecd
Pipeline #20067 failed with stages
in 59 seconds
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 { Client, ConfigOptions } from "elasticsearch";
/** @hidden */
const elasticsearch = require('elasticsearch');
/**
* Adapter which connects with a Elasticsearch database.
*/
export class ElasticsearchAdapter extends Adapter {
/** Information used to connect with a PostgreSQL database. */
private client: Client;
/**
* Creates a new adapter with the database connection configuration.
* @param conf - The information required to create a connection with
* the database.
*/
constructor (conf: ConfigOptions) {
super();
this.client = new elasticsearch.Client(conf);
}
public getQueryFromView(view: View): string {
return "oi";
}
/**
* Asynchronously reads all data from given view.
* In other words perform a SELECT query.
* @param view - "Location" from all data should be read.
* @param cb - Callback function which contains the data read.
* @param cb.error - Error information when the method fails.
* @param cb.result - Data got from view.
*/
public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
//const query = this.getQueryFromView(view);
//console.log(JSON.stringify(query));
//console.log(JSON.stringify(view));
//cb(null, []);
const query = "";
this.executeQuery(query, cb);
}
/**
* Asynchronously executes a query and get its result.
* @param query - Query (SQL format) to be executed.
* @param cb - Callback function which contains the data read.
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
private executeQuery(query: string, cb: (error: Error, result?: any[]) => void): void {
this.client.search({
index: 'teste',
body: {
"size": 0,
"query": {
"bool": {
"must": [
{"match": { "TP_SEXO": 2 }}
]
}
},
"aggs": {
"group_by_estado": {
"terms": {
"field": "CO_UF_NASC.keyword",
"size": 1000,
"order": {
"average_idade": "asc"
}
},
"aggs": {
"average_idade": {
"avg": {
"field": "NU_IDADE"
}
},
"sum_idade": {
"sum": {
"field": "NU_IDADE"
}
}
}
}
}
}
}).then(function(resp) {
cb(null, [resp]);
}, function(err) {
cb(err);
});
}
/**
* Asynchronously insert one register into a given Source.
* @param source - Insertion "location".
* @param cb - Callback function which contains the query result.
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
public insertIntoSource(source: Source, data: any[], cb: (err: Error, result: any[]) => void): void {
// const query = this.getQueryFromSource(source, data);
const query = "";
this.executeQuery(query, cb);
}
/**
* Cast BlenDB data types to be used in MonetDB queries.
* @param quotedValue - SQL query attribute wrapped by quotes.
* @param dt - Attribute data type.
*/
/*protected typeCast(quotedValue: string, dt: DataType): string {
switch (dt) {
case DataType.DATE:
return "CAST(" + quotedValue + " AS TIMESTAMP)";
case DataType.INTEGER:
return "CAST(" + quotedValue + " AS INTEGER)";
case DataType.BOOLEAN:
return "CAST(" + quotedValue + " AS BOOLEAN)";
default:
return quotedValue;
}
}
/**
* Translate filter operator to be used in MonetDB queries.
* @param lSide - Operation left side operator.
* @param rSide - Operation right side operator.
* @param op - Operation to be performed.
*/
/*protected applyOperator(lSide: string, rSide: string, op: FilterOperator): string {
switch (op) {
case FilterOperator.EQUAL:
return lSide + " = " + rSide;
case FilterOperator.NOTEQUAL:
return "NOT(" + lSide + " = " + rSide + ")";
case FilterOperator.GREATER:
return lSide + " > " + rSide;
case FilterOperator.LOWER:
return lSide + " < " + rSide;
case FilterOperator.GREATEREQ:
return lSide + " >= " + rSide;
case FilterOperator.LOWEREQ:
return lSide + " <= " + rSide;
default:
return "";
}
}*/
}
......@@ -22,8 +22,10 @@ import { Middleware } from "../types";
import { Adapter } from "../../core/adapter";
import { PostgresAdapter } from "../../adapter/postgres";
import { MonetAdapter, MonetConfig } from "../../adapter/monet";
import { ElasticsearchAdapter } from "../../adapter/elasticsearch";
import { PoolConfig } from "pg";
import { Connection } from "../../util/configParser";
import { ConfigOptions } from "elasticsearch";
/**
* Creates a PostgreSQL adapter and middleware that
......@@ -68,3 +70,20 @@ export function MonetMw(config: Connection): Middleware {
};
}
/*Elastic */
export function ElasticsearchMw(config: Connection): Middleware {
let parsedConfig: ConfigOptions = {
host: config.host + ":" + config.port,
maxRetries: 5,
requestTimeout: 60000,
sniffOnStart: true,
};
let adapter: Adapter = new ElasticsearchAdapter(parsedConfig);
return function elasticsearchMiddleware(req, res, next) {
req.adapter = adapter;
next();
};
}
......@@ -52,7 +52,7 @@ const config = ConfigParser.parse(configPath);
// include middlewares
import { EngineMw } from "./api/middlewares/engine";
import { PostgresMw, MonetMw } from "./api/middlewares/adapter";
import { PostgresMw, MonetMw, ElasticsearchMw } from "./api/middlewares/adapter";
import { ErrorMw } from "./api/middlewares/error";
app.use(EngineMw(config));
......@@ -64,6 +64,10 @@ else if (config.adapters[0] === "monet") {
app.use(MonetMw(config.connections[0]));
}
else if (config.adpters[0] === "elasticsearch") {
app.use(ElasticsearchMw(config.connections[0]));
}
else {
console.error("Invalid adapter. Options available are: postgres and monet");
process.exit(1);
......
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