Commit 0211f805 authored by Lucas Fernandes de Oliveira's avatar Lucas Fernandes de Oliveira

Merge branch 'issue/72' into 'develop'

Issue #72: Remove legacy files

See merge request !58
parents da5a3c17 381fb202
Pipeline #15106 passed with stages
in 59 seconds
{
"watch": ["src", "index.js"],
"ignore": ["*.spec.ts"],
"ext": "ts"
}
/*
* Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blendb.
*
* blendb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blendb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
// import { expect } from "chai";
//
// import { Server } from "./server";
/*describe("server class", () => {
it("should be able to create and retrieve sources", () => {
const server = new Server();
// create two sources
const source1 = server.source("source1");
const source2 = server.source("source2");
// retrieve the first one
const retrieved = server.source("source1");
// check if sources were actually created/retrieved
expect(source1).to.be.an("object");
expect(source2).to.be.an("object");
expect(retrieved).to.be.an("object");
// check if the two created sources are different
expect(source1).to.not.be.equal(source2);
// check if the retrieved source is the same as the created one
expect(source1).to.be.equal(retrieved);
});
it("should be able to create and retrieve aggregates", () => {
const server = new Server();
// create two aggregates
const aggr1 = server.aggregate(["met:one"], ["dim:one", "dim:two"]);
const aggr2 = server.aggregate(["met:two"], ["dim:one", "dim:two"]);
// retrieve the first one
const retrieved = server.aggregate(["met:one"], ["dim:two", "dim:one"]);
// check if aggregates were actually created/retrieved
expect(aggr1).to.be.an("object");
expect(aggr2).to.be.an("object");
expect(retrieved).to.be.an("object");
// check if the two created aggregates are different
expect(aggr1).to.not.be.equal(aggr2);
// check if the retrieved aggregate is the same as the created one
expect(aggr1).to.be.equal(retrieved);
});
it("should be able to create and retrieve transformers", () => {
const server = new Server();
const source1 = server.source("source1");
const aggr1 = server.aggregate(["met:one"], ["dim:one", "dim:two"]);
const aggr2 = server.aggregate(["met:one", "met:two"], ["dim:one"]);
// create two transformers
const transformer1 = server.transformer("transformer1", {
source: source1,
destination: aggr1,
functions: {
map: (doc: any, emit: Function) => { return; },
reduce: (dimensions: any, metrics: any) => { return {}; }
}
});
const transformer2 = server.transformer("transformer2", {
source: source1,
destination: aggr2,
functions: {
map: (doc: any, emit: Function) => { return; },
reduce: (dimensions: any, metrics: any) => { return {}; }
}
});
// retrieve the first one
const retrieved = server.transformer("transformer1");
// check if transformers were actually created/retrieved
expect(transformer1).to.be.an("object");
expect(transformer2).to.be.an("object");
expect(retrieved).to.be.an("object");
// check if the two created transformers are different
expect(transformer1).to.not.be.equal(transformer2);
// check if the retrieved transformer is the same as the created one
expect(transformer1).to.be.equal(retrieved);
});
it("should fail to create two transformer with name collision", () => {
const server = new Server();
const source1 = server.source("source1");
const aggr1 = server.aggregate(["met:one"], ["dim:one", "dim:two"]);
const aggr2 = server.aggregate(["met:one", "met:two"], ["dim:one"]);
server.transformer("transformer1", {
source: source1,
destination: aggr1,
functions: {
map: (doc: any, emit: Function) => { return; },
reduce: (dimensions: any, metrics: any) => { return {}; }
}
});
expect(() => {
server.transformer("transformer1", {
source: source1,
destination: aggr2,
functions: {
map: (doc: any, emit: Function) => { return; },
reduce: (dimensions: any, metrics: any) => { return {}; }
}
});
}).to.throw(Error);
});
it("should fail to retrieve a transformer that doesn't exist", () => {
const server = new Server();
expect(() => {
server.transformer("transformerX");
}).to.throw(Error);
});
});*/
/*
* Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blend.
*
* blend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blend. If not, see <http://www.gnu.org/licenses/>.
*/
import { Hash } from "../util/hash";
import { Source } from "./source";
import { Transformer, ITransformerOptions } from "./transformer";
import { Aggregate } from "./aggregate";
export class Server {
private sources: Map<string, Source>;
private transformers: Map<string, Transformer>;
private aggregates: Map<string, Aggregate>;
constructor() {
this.sources = new Map();
this.transformers = new Map();
this.aggregates = new Map();
}
/*public source(name: string, options?: any) {
if (this.sources.has(name)) {
return this.sources.get(name);
}
else {
const source = new Source(name, options);
this.sources.set(name, source);
return source;
}
}*/
public aggregate(metrics: string[], dimensions: string[], options?: any) {
const id = Hash.sha1(metrics.sort(), dimensions.sort());
if (this.aggregates.has(id)) {
return this.aggregates.get(id);
}
else {
const aggregate = new Aggregate(metrics, dimensions, options);
this.aggregates.set(id, aggregate);
return aggregate;
}
}
public transformer(name: string, options?: ITransformerOptions) {
if (typeof options !== "undefined") {
if (this.transformers.has(name)) {
throw new Error("A transformer named '" + name + "' already exists");
}
const transformer = new Transformer(name, options);
this.transformers.set(name, transformer);
return transformer;
}
else {
if (!this.transformers.has(name)) {
throw new Error("A transformer named '" + name + "' does not exist");
}
return this.transformers.get(name);
}
}
}
/*
* Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blendb.
*
* blendb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blendb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
// import { expect } from "chai";
//
// import { Hash } from "../util/hash";
//
// import { Transformer } from "./transformer";
// import { Source } from "./source";
// import { Aggregate } from "./aggregate";
/*describe("transformer class", () => {
const source = new Source("testSource");
const aggregate = new Aggregate(["met:one"], ["dim:one", "dim:two"]);
it("should be able to aggregate data", () => {
source.truncate();
for (let i = 0; i < 1000; i++) {
source.push({
id: i % 10,
seed: Math.random()
});
}
source.push({ id: 10, seed: 5 });
source.push({ id: 10, seed: 5 });
source.push({ id: 10, seed: 5 });
source.push({ id: 10, seed: 5 });
source.push({ id: 10, seed: 5 });
const transformer = new Transformer("processTransformer", {
source: source,
destination: aggregate,
functions: {
map: ((doc: any, emit: Function) => {
emit({
"dim:one": doc.id,
"dim:two": Hash.sha1(doc.seed)
}, {
"met:one": Math.floor(doc.seed * 100000) / 100
});
}),
reduce: ((dimensions: any, metrics: any) => {
let tmp = 0;
metrics.forEach((met: any) => {
tmp += met["met:one"];
});
return {
"met:one": tmp
};
})
}
});
transformer.apply();
let result = aggregate.find({
"dim:one": 10,
"dim:two": Hash.sha1(5)
});
expect(result).to.have.length(1);
expect(result[0]).to.have.property("metrics");
expect(result[0].metrics).to.have.property("met:one");
expect(result[0].metrics["met:one"]).to.be.equal(25000);
});
});
*/
/*
* Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blendb.
*
* blendb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blendb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
import { Hash } from "../util/hash";
import { Source } from "./source";
import { Aggregate } from "./aggregate";
export interface ITransformerOptions {
source: Source;
destination: Aggregate;
functions: {
map: (doc: any, emit: Function) => void;
reduce: (dimensions: any, metrics: any) => any;
};
}
export class Transformer {
public name: string;
public source: Source;
public destination: Aggregate;
private functions: any;
constructor(name: string, options: ITransformerOptions) {
this.name = name;
this.source = options.source;
this.destination = options.destination;
this.functions = {
map: options.functions.map,
reduce: options.functions.reduce
};
}
public apply() {
let temp = new Map();
this.destination.truncate();
this.source.forEach((doc: any) => {
let emit = (dimensions: any, metrics: any) => {
let key = Hash.sha1(dimensions);
let current = temp.get(key) || { dimensions, metrics: [] };
temp.set(key, {
dimensions,
metrics: current.metrics.concat([metrics])
});
};
this.functions.map(doc, emit);
});
temp.forEach((value, key) => {
let dimensions = value.dimensions;
let metrics = value.metrics;
if (metrics.length > 1) {
this.destination.push({
dimensions: dimensions,
metrics: this.functions.reduce(dimensions, metrics)
});
}
else {
this.destination.push({
dimensions: dimensions,
metrics: metrics
});
}
});
}
}
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