...
 
Commits (4)
......@@ -2,15 +2,19 @@ image: node:6.12.2
services:
- postgres:latest
-
name: "dockerregistry.c3sl.ufpr.br:5000/c3sl/monetdb-ci-service:latest"
alias: "monet"
variables:
POSTGRES_DB: 'blendb_fixture'
POSTGRES_USER: 'runner'
POSTGRES_PASSWORD: ''
MONETDB_NAME: 'blendb_fixture'
PKG_VERSION: 'latest'
DOCKER_URL: 'marula.c3sl.ufpr.br:5000/c3sl/blendb:'
cache:
cache:
paths:
- node_modules
......
BLENDB_DB_USER=runner
BLENDB_DB_NAME=blendb_fixture
BLENDB_DB_PASSWORD=
BLENDB_DB_HOST=postgres
BLENDB_DB_PORT=5432
BLENDB_ADAPTER=postgres
PORT=3000
BLENDB_N_DB=2
BLENDB_DB0_USER=runner
BLENDB_DB0_NAME=blendb_fixture
BLENDB_DB0_PASSWORD=
BLENDB_DB0_HOST=postgres
BLENDB_DB0_PORT=5432
BLENDB_DB0_ADAPTER=postgres
BLENDB_DB1_USER=monetdb
BLENDB_DB1_NAME=blendb_fixture
BLENDB_DB1_PASSWORD=monetdb
BLENDB_DB1_HOST=monet
BLENDB_DB1_PORT=50000
BLENDB_DB1_ADAPTER=monet
\ No newline at end of file
......@@ -7,14 +7,12 @@ services:
# depends_on:
# - db
environment:
BLENDB_DB_USER: ${BLENDB_DB_USER}
BLENDB_DB_PASSWORD: ${BLENDB_DB_PASSWORD}
BLENDB_DB_NAME: ${BLENDB_DB_NAME}
BLENDB_DB_HOST: ${BLENDB_DB_HOST}
BLENDB_DB_PORT: ${BLENDB_DB_PORT}
BLENDB_ST_CREATE: ${BLENDB_ST_CREATE}
BLENDB_ST_INSERT: ${BLENDB_ST_INSERT}
BLENDB_ADAPTER: ${BLENDB_ADAPTER}
BLENDB_DB_USER: ${BLENDB_DB0_USER}
BLENDB_DB_PASSWORD: ${BLENDB_DB0_PASSWORD}
BLENDB_DB_NAME: ${BLENDB_DB0_NAME}
BLENDB_DB_HOST: ${BLENDB_DB0_HOST}
BLENDB_DB_PORT: ${BLENDB_DB0_PORT}
BLENDB_ADAPTER: ${BLENDB_DB0_ADAPTER}
volumes:
-
type: bind
......
This diff is collapsed.
This diff is collapsed.
......@@ -23,6 +23,7 @@ import { Request } from "../types";
import { Source, Field } from "../../core/source";
import { EnumType } from "../../core/enumType";
import { DataType } from "../../common/types";
import { forEach } from "async";
/**
* Dictionary indexed by a type name which return a
......@@ -168,7 +169,16 @@ export class CollectCtrl {
return;
}
req.adapter.insertIntoSource(source, req.body, (err: Error, result: any[]) => {
forEach(req.adapters , (adapter , callback) => {
adapter.insertIntoSource(source, req.body, (err: Error, result: any[]) => {
if (err) {
callback("Insertion failed on db : " + adapter);
}
else {
callback();
}
});
}, function(err) {
if (err) {
res.status(500).json({
message: "Insertion has failed",
......@@ -176,11 +186,10 @@ export class CollectCtrl {
});
return;
}
else{
else {
res.status(200).json({message: "Data has been successfully received and stored by the server"});
return;
}
});
}
......
This diff is collapsed.
......@@ -91,7 +91,7 @@ export class DataCtrl {
return;
}
req.adapter.getDataFromView(view, (err: Error, result: any[]) => {
req.adapters[Math.random() * req.adapters.length].getDataFromView(view, (err: Error, result: any[]) => {
if (err) {
res.status(500).json({
message: "Query execution failed " +
......
......@@ -25,12 +25,48 @@ import { MonetAdapter, MonetConfig } from "../../adapter/monet";
import { PoolConfig } from "pg";
import { Connection } from "../../util/configParser";
/**
* A switch for chosing each database
* @param config Parameter for that especifc database
* @param adapter Define what database will be used
*/
function loadDB(config: Connection, adapter: string, index: number): Adapter{
switch (adapter){
case "postgres":
return PostgresMw(config, index);
case "monet":
return MonetMw(config, index);
default:
return null;
}
}
/**
* Create a connection with all DB
* @param config List with parametes for each db
* @param adapter List with each adapter required for it's index
*/
export function connectDb(config: Connection[], adapter: string[]): Middleware {
let ndb = adapter.length;
let test: Adapter[] = [];
for (let i = 0 ; i < ndb ; i++){
test[i] = loadDB(config[i], adapter[i], i);
if (test[i] === null){
console.error("Invalid adapter. Options available are: postgres and monet");
process.exit(1);
}
}
return function(req, res, next) {
req.adapters = test;
next();
};
};
/**
* Creates a PostgreSQL adapter and middleware that
* inserts the adapter into the request objects.
* @param config - Parameters required to connect in database.
*/
export function PostgresMw(config: Connection): Middleware {
export function PostgresMw(config: Connection, index: number): Adapter {
let parsedConfig: PoolConfig = {
user: config.user,
database: config.database,
......@@ -41,10 +77,11 @@ export function PostgresMw(config: Connection): Middleware {
idleTimeoutMillis: 3000
};
let adapter: Adapter = new PostgresAdapter(parsedConfig);
return function postgresMiddleware(req, res, next) {
req.adapter = adapter;
next();
};
return adapter;
// return function postgresMiddleware(req, res, next) {
// req.adapters[index] = adapter;
// next();
// };
}
......@@ -53,7 +90,7 @@ export function PostgresMw(config: Connection): Middleware {
* inserts the adapter into the request objects.
* @param config - Parameters required to connect in database.
*/
export function MonetMw(config: Connection): Middleware {
export function MonetMw(config: Connection, index: number): Adapter {
let parsedConfig: MonetConfig = {
user: config.user,
dbname: config.database,
......@@ -62,9 +99,10 @@ export function MonetMw(config: Connection): Middleware {
port: config.port,
};
let adapter: Adapter = new MonetAdapter(parsedConfig);
return function monetMiddleware(req, res, next) {
req.adapter = adapter;
next();
};
return adapter;
// return function monetMiddleware(req, res, next) {
// req.adapters[index] = adapter;
// next();
// };
}
......@@ -33,8 +33,8 @@ import { Adapter} from "../core/adapter";
export interface Request extends express.Request {
/** A engine object. Represents the database in BlenDB perspective. */
engine: Engine;
/** A adapter object. Used to communicate with the database in use. */
adapter: Adapter;
/** A adapter object. Used to communicate with one or more database in use. */
adapters: Adapter[];
}
/**
......
......@@ -36,29 +36,18 @@ const app = module.exports = express();
// Load configuration file
import { ConfigParser } from "./util/configParser";
/** @hidden */
const configPath = (app.get("env") === "test") ? "config/test.yaml" : "config/config.yaml";
/** @hidden */
const config = ConfigParser.parse(configPath);
const config = ConfigParser.parse();
// include middlewares
import { EngineMw } from "./api/middlewares/engine";
import { PostgresMw, MonetMw } from "./api/middlewares/adapter";
import { connectDb } from "./api/middlewares/adapter";
import { ErrorMw } from "./api/middlewares/error";
app.use(EngineMw(config));
if (config.adapter === "postgres") {
app.use(PostgresMw(config.connection));
}
else if (config.adapter === "monet") {
app.use(MonetMw(config.connection));
}
else {
console.error("Invalid adapter. Options available are: postgres and monet");
process.exit(1);
}
app.use(connectDb(config.connection, config.adapter));
// parse the RAML spec and load osprey middleware
ramlParser.loadFile("specs/blendb-api-v1.raml")
......
......@@ -79,9 +79,9 @@ interface BuildView {
export interface ParsedConfig {
/** What adapter is in use. */
adapter: string;
adapter: string[];
/** Connection parameters to connect in the database. */
connection: Connection;
connection: Connection[];
/** Set of all views available. */
views: View[];
/** Set of all sources available. */
......@@ -151,25 +151,33 @@ export class ConfigParser {
* Parse the configuration file.
* @param configPath - Path to configuration file.
*/
public static parse(configPath: string): ParsedConfig {
let config: ConfigSchema = yaml.safeLoad(fs.readFileSync(configPath, {
public static parse(): ParsedConfig {
let config: ConfigSchema = yaml.safeLoad(fs.readFileSync(process.env.BLENDB_SCHEMA, {
encoding: "utf-8"
})) as ConfigSchema;
let connection: Connection = {
user: process.env.BLENDB_DB_USER,
database: process.env.BLENDB_DB_NAME,
password: process.env.BLENDB_DB_PASSWORD,
host: process.env.BLENDB_DB_HOST,
port: parseInt(process.env.BLENDB_DB_PORT, 10)
};
let connection: Connection[] = [];
// checks null and 0
let ndb: number = parseInt(process.env.BLENDB_N_DATABASES, 10) ? parseInt(process.env.BLENDB_N_DATABASES, 10) : 1;
let adapter: string[] = [];
for ( let i = 0; i < ndb ; i++ ){
adapter[i] = process.env["BLENDB_DB" + i + "_ADAPTER"];
connection[i] = {
user: process.env["BLENDB_DB" + i + "_USER"],
database: process.env["BLENDB_DB" + i + "_NAME"],
password: process.env["BLENDB_DB" + i + "_PASSWORD"],
host: process.env["BLENDB_DB" + i + "_HOST"],
port: parseInt(process.env["BLENDB_DB" + i + "_PORT"], 10)
};
}
let metricsOpts = config.metrics;
let viewsOpts = config.views;
let dimensionsOpts = config.dimensions;
let enumTypesOpts = config.enumTypes;
let sourcesOpts = config.sources;
let parsed: ParsedConfig = {
adapter: process.env.BLENDB_ADAPTER || "postgres",
adapter: adapter,
connection: connection,
views: [],
metrics: [],
......
......@@ -66,7 +66,7 @@ interface DataCtrlScenario {
clausal: Query;
}
const config = ConfigParser.parse("config/test.yaml");
const config = ConfigParser.parse();
const mets = config.metrics.sort((a, b) => {
const aValue = parseInt(a.name.split(":")[1], 10);
......