Commit 9143c05e authored by rafaelatc3sl's avatar rafaelatc3sl

Issue #91: Refactor engine

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