diff --git a/src/core/server.spec.ts b/src/core/server.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..f817c727dbfcfb5da533595c9d57bcb5fcb32aa3 --- /dev/null +++ b/src/core/server.spec.ts @@ -0,0 +1,103 @@ +/* + * 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", () => { + const server = new Server(); + + it("should be able to create and retrieve sources", () => { + // 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 transformers", () => { + // create two transformers + const transformer1 = server.transformer("transformer1", { + source: "source1", + metrics: ["met:one"], + dimensions: ["dim:one"], + extractors: { + metrics: ((doc: any) => null), + dimensions: ((doc: any) => null), + } + }); + const transformer2 = server.transformer("transformer2", { + source: "source2", + metrics: ["met:one"], + dimensions: ["dim:one"], + extractors: { + metrics: ((doc: any) => null), + dimensions: ((doc: any) => null), + } + }); + + // 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 be able to create and retrieve aggregates", () => { + // 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:one", "dim:two"]); + + // 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); + }); +}); diff --git a/src/core/server.ts b/src/core/server.ts index 8445d178b9693d249ff5f686aa4f0c9b5443c66b..bf110d060d0cb3be798786df149b73f8d2f072db 100644 --- a/src/core/server.ts +++ b/src/core/server.ts @@ -21,7 +21,7 @@ import { Hash } from "../util/hash"; import { Source } from "./source"; -import { Transformer } from "./transformer"; +import { Transformer, ITransformerOptions } from "./transformer"; import { Aggregate } from "./aggregate"; export class Server { @@ -46,15 +46,23 @@ export class Server { } } - public transformer(name: string, options?: any) { - if (this.transformers.has(name)) { - return this.transformers.get(name); - } - else { + 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); + } } public aggregate(metrics: string[], dimensions: string[], options?: any) { diff --git a/src/core/transformer.ts b/src/core/transformer.ts index 31471ffdf98189f5cc31cd0d79dd3aa8601e13e7..cc62f6b7ece5a95c52dafc332175a58a6955d449 100644 --- a/src/core/transformer.ts +++ b/src/core/transformer.ts @@ -18,19 +18,32 @@ * along with blendb. If not, see <http://www.gnu.org/licenses/>. */ +export interface ITransformerOptions { + source: string; + metrics: string[]; + dimensions: string[]; + extractors: { + metrics: (doc: any) => any; + dimensions: (doc: any) => any; + }; +} + export class Transformer { + public name: string; public source: string; public metrics: string[]; public dimensions: string[]; private extractors: any; - constructor(name: string, options: any) { - this.source = options.source || null; - this.metrics = options.metrics || []; - this.dimensions = options.dimensions || []; + constructor(name: string, options: ITransformerOptions) { + this.name = name; + + this.source = options.source; + this.metrics = options.metrics; + this.dimensions = options.dimensions; this.extractors = { - metrics: options.extractors.metrics || ((doc: any): any => null), - dimensions: options.extractors.dimensions || ((doc: any): any => null) + metrics: options.extractors.metrics, + dimensions: options.extractors.dimensions }; }