Commit 3b425558 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch 'issue/91' into 'develop'

Issue #91: Refactor engine

See merge request !79
parents 773a7101 9143c05e
Pipeline #18761 passed with stages
in 1 minute and 7 seconds
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
import { Engine } from "../src/core/engine"; import { Engine } from "../src/core/engine";
import { PostgresAdapter } from "../src/adapter/postgres"; import { PostgresAdapter } from "../src/adapter/postgres";
import { ConfigParser } from "../src/util/configParser"; import { ConfigParser } from "../src/util/configParser";
import { QueryOpts, Query } from "../src/common/query";
import * as path from "path"; import * as path from "path";
import * as fs from "fs"; import * as fs from "fs";
...@@ -37,13 +38,11 @@ const configFile = process.argv[2]; ...@@ -37,13 +38,11 @@ const configFile = process.argv[2];
const schemaFile = process.argv[3]; const schemaFile = process.argv[3];
const config = ConfigParser.parse(configFile); const config = ConfigParser.parse(configFile);
const referencePath = path.dirname(configFile); const referencePath = path.dirname(configFile);
const engine = new Engine();
const adapter = new PostgresAdapter(config.connection);
let schema = ""; const engine = new Engine(config);
const adapter = new PostgresAdapter(config.connection);
config.metrics.forEach ((met) => engine.addMetric(met)); let schema = "";
config.dimensions.forEach ((dim) => engine.addDimension(dim));
for (let i = 0; i < config.buildViews.length; ++i) { for (let i = 0; i < config.buildViews.length; ++i) {
const view = config.buildViews[i].view; const view = config.buildViews[i].view;
...@@ -77,11 +76,9 @@ for (let i = 0; i < config.buildViews.length; ++i) { ...@@ -77,11 +76,9 @@ for (let i = 0; i < config.buildViews.length; ++i) {
const view = config.buildViews[i].view; const view = config.buildViews[i].view;
const alias = config.buildViews[i].alias; const alias = config.buildViews[i].alias;
if (!view.origin) { if (!view.origin) {
const materializedView = engine.query({ const qOpt: QueryOpts = { metrics: view.metrics,dimensions: view.dimensions,
metrics: view.metrics, clauses: view.clauses };
dimensions: view.dimensions, const materializedView = engine.query(new Query(qOpt));
clauses: view.clauses
});
const table = adapter.getQueryFromView(materializedView); const table = adapter.getQueryFromView(materializedView);
let query = "-- View: " + alias + "\n"; let query = "-- View: " + alias + "\n";
......
...@@ -24,41 +24,36 @@ import { MonetAdapter, MonetConfig } from "./monet"; ...@@ -24,41 +24,36 @@ import { MonetAdapter, MonetConfig } from "./monet";
import { Adapter } from "../core/adapter"; import { Adapter } from "../core/adapter";
import { Fixture as FixPostgres } from "../../test/postgres/fixture"; import { Fixture as FixPostgres } from "../../test/postgres/fixture";
import { Fixture as FixMonet } from "../../test/monet/fixture"; import { Fixture as FixMonet } from "../../test/monet/fixture";
import { ConfigParser } from "../util/configParser";
import { adapterScenario } from "../../test/scenario"; import { adapterScenario } from "../../test/scenario";
describe("Sql adapter", () => { describe("Sql adapter", () => {
// Initializing
let config: any;
let adapter: Adapter; let adapter: Adapter;
let fixture; let fixture;
before(function (done): void { before(function (done): void {
// Arrow function not used to get acces to this and skip the test
const configPath = process.env.BLENDB_SCHEMA_FILE; if (adapterScenario.config.adapter === "postgres") {
config = ConfigParser.parse(configPath); fixture = new FixPostgres(adapterScenario.config.connection);
if (config.adapter === "postgres") { fixture.load(adapterScenario.config.loadViews, (err) => {
fixture = new FixPostgres(config.connection);
fixture.load(config.loadViews, (err) => {
if (err) { if (err) {
throw err; throw err;
} }
adapter = new PostgresAdapter(config.connection); adapter = new PostgresAdapter(adapterScenario.config.connection);
done(); done();
}); });
} }
else if (config.adapter === "monet") { else if (adapterScenario.config.adapter === "monet") {
fixture = new FixMonet(config.connection); fixture = new FixMonet(adapterScenario.config.connection);
fixture.load(config.loadViews, (err) => { fixture.load(adapterScenario.config.loadViews, (err) => {
if (err) { if (err) {
throw err; throw err;
} }
let parsedConfig: MonetConfig = { let parsedConfig: MonetConfig = {
user: config.connection.user, user: adapterScenario.config.connection.user,
dbname: config.connection.database, dbname: adapterScenario.config.connection.database,
password: config.connection.password, password: adapterScenario.config.connection.password,
host: config.connection.host, host: adapterScenario.config.connection.host,
port: config.connection.port port: adapterScenario.config.connection.port
}; };
adapter = new MonetAdapter(parsedConfig); adapter = new MonetAdapter(parsedConfig);
done(); done();
......
...@@ -28,13 +28,7 @@ import { Middleware } from "../types"; ...@@ -28,13 +28,7 @@ import { Middleware } from "../types";
* @param config - Parsed database schema. * @param config - Parsed database schema.
*/ */
export function EngineMw (config: ParsedConfig): Middleware { export function EngineMw (config: ParsedConfig): Middleware {
let engine: Engine = new Engine(); let engine: Engine = new Engine(config);
config.metrics.forEach ((met) => engine.addMetric(met));
config.dimensions.forEach ((dim) => engine.addDimension(dim));
config.views.forEach ((view) => engine.addView(view));
config.enumTypes.forEach ((enumt) => engine.addEnumType(enumt));
config.sources.forEach ((sourc) => engine.addSource(sourc));
return function engineMiddleware(req, res, next) { return function engineMiddleware(req, res, next) {
req.engine = engine; req.engine = engine;
......
...@@ -25,28 +25,10 @@ import { View } from "./view"; ...@@ -25,28 +25,10 @@ import { View } from "./view";
import { engineScenario } from "../../test/scenario"; import { engineScenario } from "../../test/scenario";
describe("engine class", () => { describe("engine class", () => {
const engine = new Engine();
const met = engineScenario.metrics; const engine = new Engine(engineScenario.config);
const dim = engineScenario.dimensions;
const subdim = engineScenario.subDimensions; const subdim = engineScenario.subDimensions;
const views = engineScenario.views;
for (let key in met){
if (met[key]){
engine.addMetric(met[key]);
}
}
for (let key in dim){
if (dim[key]){
engine.addDimension(dim[key]);
}
}
for (let key in views){
if (views[key]){
engine.addView(views[key]);
}
}
for (let key in subdim){ for (let key in subdim){
if (subdim[key]){ if (subdim[key]){
engine.addDimension(subdim[key]); engine.addDimension(subdim[key]);
...@@ -166,7 +148,7 @@ describe("engine class", () => { ...@@ -166,7 +148,7 @@ describe("engine class", () => {
expect(clause.filters[0]).to.have.property("target"); expect(clause.filters[0]).to.have.property("target");
expect(clause.filters[0]).to.have.property("value"); expect(clause.filters[0]).to.have.property("value");
expect(clause.filters[0]).to.have.property("operator"); expect(clause.filters[0]).to.have.property("operator");
expect(clause.filters[0].target).to.be.equal(dim["dim:client:name"]); expect(clause.filters[0].target).to.be.equal(engine.getDimensionByName("dim:client:name"));
expect(clause.filters[0].value).to.be.equal("Laci"); expect(clause.filters[0].value).to.be.equal("Laci");
expect(clause.filters[0].operator).to.be.equal(FilterOperator.EQUAL); expect(clause.filters[0].operator).to.be.equal(FilterOperator.EQUAL);
}); });
...@@ -183,7 +165,7 @@ describe("engine class", () => { ...@@ -183,7 +165,7 @@ describe("engine class", () => {
expect(clause.filters[0]).to.have.property("target"); expect(clause.filters[0]).to.have.property("target");
expect(clause.filters[0]).to.have.property("operator"); expect(clause.filters[0]).to.have.property("operator");
expect(clause.filters[0]).to.have.property("value"); expect(clause.filters[0]).to.have.property("value");
expect(clause.filters[0].target).to.be.equal(met["met:product:avg:pricein"]); expect(clause.filters[0].target).to.be.equal(engine.getMetricByName("met:product:avg:pricein"));
expect(clause.filters[0].value).to.be.equal("0"); expect(clause.filters[0].value).to.be.equal("0");
expect(clause.filters[0].operator).to.be.equal(FilterOperator.NOTEQUAL); expect(clause.filters[0].operator).to.be.equal(FilterOperator.NOTEQUAL);
}); });
...@@ -284,7 +266,7 @@ describe("engine class", () => { ...@@ -284,7 +266,7 @@ describe("engine class", () => {
expect(clause.filters[0]).to.have.property("target"); expect(clause.filters[0]).to.have.property("target");
expect(clause.filters[0]).to.have.property("operator"); expect(clause.filters[0]).to.have.property("operator");
expect(clause.filters[0]).to.have.property("value"); expect(clause.filters[0]).to.have.property("value");
expect(clause.filters[0].target).to.be.equal(dim["dim:sell:datein"]); expect(clause.filters[0].target).to.be.equal(engine.getDimensionByName("dim:sell:datein"));
expect(clause.filters[0].value).to.be.equal("2018-02-17"); expect(clause.filters[0].value).to.be.equal("2018-02-17");
expect(clause.filters[0].operator).to.be.equal(operators[op]); expect(clause.filters[0].operator).to.be.equal(operators[op]);
} }
...@@ -301,7 +283,7 @@ describe("engine class", () => { ...@@ -301,7 +283,7 @@ describe("engine class", () => {
expect(clause.filters[0]).to.have.property("target"); expect(clause.filters[0]).to.have.property("target");
expect(clause.filters[0]).to.have.property("operator"); expect(clause.filters[0]).to.have.property("operator");
expect(clause.filters[0]).to.have.property("value"); expect(clause.filters[0]).to.have.property("value");
expect(clause.filters[0].target).to.be.equal(dim["dim:seller:id"]); expect(clause.filters[0].target).to.be.equal(engine.getDimensionByName("dim:seller:id"));
expect(clause.filters[0].value).to.be.equal("0"); expect(clause.filters[0].value).to.be.equal("0");
expect(clause.filters[0].operator).to.be.equal(operators[op]); expect(clause.filters[0].operator).to.be.equal(operators[op]);
} }
......
...@@ -28,6 +28,7 @@ import { Graph } from "../util/graph"; ...@@ -28,6 +28,7 @@ import { Graph } from "../util/graph";
import { EnumType, EnumTypeOptions} from "./enumType"; import { EnumType, EnumTypeOptions} from "./enumType";
import { Source , SourceStrOptions } from "./source"; import { Source , SourceStrOptions } from "./source";
import { ViewHandler } from "../util/viewHandler"; import { ViewHandler } from "../util/viewHandler";
import { ParsedConfig } from "../util/configParser";
/** /**
* Represents the database schema from the BlenDB perspective. * Represents the database schema from the BlenDB perspective.
...@@ -39,26 +40,35 @@ import { ViewHandler } from "../util/viewHandler"; ...@@ -39,26 +40,35 @@ import { ViewHandler } from "../util/viewHandler";
*/ */
export class Engine { export class Engine {
/** Set of views available in the database */ /** Set of views available in the database */
private views: View[] = []; private views: View[];
/** Set of metrics available in the database. */ /** Set of metrics available in the database. */
private metrics: Metric[] = []; private metrics: Metric[];
/** Set of enumerable types available in the database. */ /** Set of enumerable types available in the database. */
private enumTypes: EnumType[] = []; private enumTypes: EnumType[];
/** Set of dimensions available in the database. */ /** Set of dimensions available in the database. */
private dimensions: Dimension[] = []; private dimensions: Dimension[];
/** Set of sources available in the database. */ /** Set of sources available in the database. */
private sources: Source[] = []; private sources: Source[];
/** Graph which represents the database schema. */ /** Graph which represents the database schema. */
private graph: Graph; private graph: Graph;
/** Constructs an empty database schema. */ /** Constructs an empty database schema. */
constructor () { constructor (config: ParsedConfig) {
this.graph = new Graph();
this.enumTypes = []; this.enumTypes = [];
this.views = []; this.views = [];
this.metrics = []; this.metrics = [];
this.dimensions = []; this.dimensions = [];
this.sources = []; this.sources = [];
this.graph = new Graph();
config.metrics.forEach ((met) => this.addMetric(met));
config.dimensions.forEach ((dim) => this.addDimension(dim));
config.views.forEach ((view) => this.addView(view));
config.enumTypes.forEach ((enumt) => this.addEnumType(enumt));
config.sources.forEach ((sourc) => this.addSource(sourc));
} }
/** Gets all the available */ /** Gets all the available */
......
...@@ -22,16 +22,9 @@ import { View } from "../core/view"; ...@@ -22,16 +22,9 @@ import { View } from "../core/view";
import { Metric } from "../core/metric"; import { Metric } from "../core/metric";
import { Dimension } from "../core/dimension"; import { Dimension } from "../core/dimension";
import { Query } from "../common/query"; import { Query } from "../common/query";
import { ParsedConfig } from "./configParser";
export interface EngineScenario { export interface EngineScenario {
/**
* All metrics from /config/*
*/
metrics: {[key: string]: Metric};
/**
* All dimensions from /config/*
*/
dimensions: {[key: string]: Dimension};
/** /**
* subDimensions from scenario * subDimensions from scenario
*/ */
...@@ -44,10 +37,6 @@ export interface EngineScenario { ...@@ -44,10 +37,6 @@ export interface EngineScenario {
* Dimension that does not exist on /config/* * Dimension that does not exist on /config/*
*/ */
wrongDim: Dimension; wrongDim: Dimension;
/**
* All views from /config/*
*/
views: {[key: string]: View};
/** /**
* Query that have all metrics and dimensions * Query that have all metrics and dimensions
*/ */
...@@ -84,6 +73,10 @@ export interface EngineScenario { ...@@ -84,6 +73,10 @@ export interface EngineScenario {
* A query that contains a subdimentions without a parent * A query that contains a subdimentions without a parent
*/ */
queryNoParent: Query; queryNoParent: Query;
/**
* ParsedConfig that is load in scenario
*/
config: ParsedConfig;
} }
export interface AdapterScenario { export interface AdapterScenario {
...@@ -143,6 +136,10 @@ export interface AdapterScenario { ...@@ -143,6 +136,10 @@ export interface AdapterScenario {
* View that contains the seller id and how many sell's he has done * View that contains the seller id and how many sell's he has done
*/ */
JoinWithAncestors: View; JoinWithAncestors: View;
/**
* ParsedConfig that is load in scenario
*/
config: ParsedConfig;
} }
export interface DataCtrlScenario { export interface DataCtrlScenario {
......
...@@ -365,12 +365,9 @@ const viewActiveSeller = views["view:ActiveSeller"]; ...@@ -365,12 +365,9 @@ const viewActiveSeller = views["view:ActiveSeller"];
// Exports // Exports
export const engineScenario: EngineScenario = { export const engineScenario: EngineScenario = {
metrics: mets,
dimensions: dims,
wrongMet: wrongMet, wrongMet: wrongMet,
wrongDim: wrongDim, wrongDim: wrongDim,
subDimensions: subdims, subDimensions: subdims,
views: views,
queryMetsDims: queries["queryMetsDims"], queryMetsDims: queries["queryMetsDims"],
queryNoMets: queries["queryNoMets"], queryNoMets: queries["queryNoMets"],
queryNoDims: queries["queryNoDims"], queryNoDims: queries["queryNoDims"],
...@@ -379,7 +376,8 @@ export const engineScenario: EngineScenario = { ...@@ -379,7 +376,8 @@ export const engineScenario: EngineScenario = {
queryActive: queries["queryActive"], queryActive: queries["queryActive"],
viewActiveSeller: viewActiveSeller, viewActiveSeller: viewActiveSeller,
querySubDim: queries["querySubDim"], querySubDim: queries["querySubDim"],
queryNoParent: queries["queryNoParent"] queryNoParent: queries["queryNoParent"],
config: config
}; };
export const adapterScenario: AdapterScenario = { export const adapterScenario: AdapterScenario = {
...@@ -395,6 +393,7 @@ export const adapterScenario: AdapterScenario = { ...@@ -395,6 +393,7 @@ export const adapterScenario: AdapterScenario = {
joinWithNoMetrics: joinWithNoMetrics, joinWithNoMetrics: joinWithNoMetrics,
growOneView: growOneView, growOneView: growOneView,
JoinWithAncestors:JoinWithAncestors, JoinWithAncestors:JoinWithAncestors,
config: config
}; };
export const dataCtrlScenario: DataCtrlScenario = { export const dataCtrlScenario: DataCtrlScenario = {
......
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