Commit ed50105a authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

Add tslint

parent 09231d29
......@@ -7,7 +7,8 @@
"prestart": "tsc",
"start": "node build/src/boot",
"pretest": "tsc",
"test": "mocha"
"test": "mocha",
"lint": "tslint -s node_modules/tslint-stylish -t stylish src/**/*.ts test/**/*.ts"
},
"repository": {
"type": "git",
......@@ -40,6 +41,8 @@
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.0.2"
"mocha": "^3.0.2",
"tslint": "^3.14.0",
"tslint-stylish": "^2.1.0-beta"
}
}
......@@ -22,13 +22,13 @@ import * as express from "express";
export class CollectCtrl {
static write(req: express.Request, res: express.Response, next: express.NextFunction) {
if ('_id' in req.body) {
if ("_id" in req.body) {
res.status(400)
.json({ message: 'Property named \'_id\' is protected.' });
.json({ message: "Property named \"_id\" is protected." });
return;
}
res.status(500)
.json({ message: 'Error while writing to the database.' });
.json({ message: "Error while writing to the database." });
}
}
......@@ -22,10 +22,10 @@ import * as express from "express";
export class DataCtrl {
static read(req: express.Request, res: express.Response, next: express.NextFunction) {
let metrics = req.query.metrics.split(',');
let dimensions = req.query.dimensions.split(',');
let metrics = req.query.metrics.split(",");
let dimensions = req.query.dimensions.split(",");
res.status(500).json({ message: 'Query execution failed ' +
'because of an unknown error.' });
res.status(500).json({ message: "Query execution failed " +
"because of an unknown error." });
}
}
......@@ -18,13 +18,13 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
const osprey = require('osprey');
const osprey = require("osprey");
// import controllers
import { DataCtrl } from './controllers/data';
import { CollectCtrl } from './controllers/collect';
import { DataCtrl } from "./controllers/data";
import { CollectCtrl } from "./controllers/collect";
export const router = osprey.Router();
router.get('/data', DataCtrl.read);
router.post('/collect/{class}', CollectCtrl.write);
router.get("/data", DataCtrl.read);
router.post("/collect/{class}", CollectCtrl.write);
......@@ -20,21 +20,21 @@
*/
// external libraries
import express = require('express');
import path = require('path');
const osprey = require('osprey');
const ramlParser = require('raml-parser');
import express = require("express");
import path = require("path");
const osprey = require("osprey");
const ramlParser = require("raml-parser");
// load router
import { router } from './api/router-v1';
import { router } from "./api/router-v1";
// create a new express app
const app = module.exports = express();
// parse the RAML spec and load osprey middleware
ramlParser.loadFile('specs/blendb-api-v1.raml')
ramlParser.loadFile("specs/blendb-api-v1.raml")
.then((raml: any) => {
app.use('/v1',
app.use("/v1",
osprey.security(raml),
osprey.server(raml),
router);
......@@ -43,8 +43,8 @@ ramlParser.loadFile('specs/blendb-api-v1.raml')
let port = process.env.PORT || 3000;
app.listen(port);
if (app.get('env') === 'development') {
console.log('Server listening on port ' + port + '.');
if (app.get("env") === "development") {
console.log("Server listening on port " + port + ".");
}
}
else {
......@@ -53,6 +53,6 @@ ramlParser.loadFile('specs/blendb-api-v1.raml')
}
},
(err: any) => {
console.error('RAML Parsing Error: ' + err.message);
console.error("RAML Parsing Error: " + err.message);
process.exit(1);
});
......@@ -18,23 +18,23 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
import { expect } from 'chai';
import { expect } from "chai";
import { Aggregate } from './aggregate';
import { Aggregate } from "./aggregate";
describe('aggregate class', () => {
it('should be instantiated with an array metrics and one of dimensions', () => {
let aggr = new Aggregate(['met:one'], ['dim:one', 'dim:two']);
expect(aggr).to.be.an('object');
describe("aggregate class", () => {
it("should be instantiated with an array metrics and one of dimensions", () => {
let aggr = new Aggregate(["met:one"], ["dim:one", "dim:two"]);
expect(aggr).to.be.an("object");
});
it('should not be instantiated with an empty array of metrics', () => {
let aggr = new Aggregate([], ['dim:one', 'dim:two']);
expect(aggr).to.be.an('object');
it("should not be instantiated with an empty array of metrics", () => {
let aggr = new Aggregate([], ["dim:one", "dim:two"]);
expect(aggr).to.be.an("object");
});
it('should not be instantiated with an empty array of dimensions', () => {
let aggr = new Aggregate(['met:one'], []);
expect(aggr).to.be.an('object');
it("should not be instantiated with an empty array of dimensions", () => {
let aggr = new Aggregate(["met:one"], []);
expect(aggr).to.be.an("object");
});
});
......@@ -19,9 +19,9 @@
*/
export class Aggregate {
metrics: string[];
dimensions: string[];
data: any[];
public metrics: string[];
public dimensions: string[];
private data: any[];
constructor(metrics: string[], dimensions: string[], options?: any) {
this.metrics = metrics;
......@@ -30,11 +30,11 @@ export class Aggregate {
this.data = [];
}
push(data: any) {
public push(data: any) {
this.data.push(data);
}
truncate() {
public truncate() {
this.data = [];
}
}
......@@ -18,18 +18,16 @@
* along with blend. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
import { Hash } from "../util/hash";
import { Hash } from '../util/hash';
import { Source } from './source';
import { Transformer } from './transformer';
import { Aggregate } from './aggregate';
import { Source } from "./source";
import { Transformer } from "./transformer";
import { Aggregate } from "./aggregate";
export class Server {
sources: Map<string,Source>;
transformers: Map<string,Transformer>;
aggregates: Map<string,Aggregate>;
private sources: Map<string, Source>;
private transformers: Map<string, Transformer>;
private aggregates: Map<string, Aggregate>;
constructor() {
this.sources = new Map();
......@@ -37,7 +35,7 @@ export class Server {
this.aggregates = new Map();
}
source(name: string, options?: any) {
public source(name: string, options?: any) {
if (this.sources.has(name)) {
return this.sources.get(name);
}
......@@ -48,7 +46,7 @@ export class Server {
}
}
transformer(name: string, options?: any) {
public transformer(name: string, options?: any) {
if (this.transformers.has(name)) {
return this.transformers.get(name);
}
......@@ -59,7 +57,7 @@ export class Server {
}
}
aggregate(metrics: string[], dimensions: string[], options?: any) {
public aggregate(metrics: string[], dimensions: string[], options?: any) {
const id = Hash.sha1(metrics.sort(), dimensions.sort());
if (this.aggregates.has(id)) {
......@@ -72,7 +70,7 @@ export class Server {
}
}
process() {
public process() {
this.transformers.forEach((transformer: Transformer) => {
const source = this.source(transformer.source);
const aggr = this.aggregate(transformer.metrics,
......@@ -90,7 +88,5 @@ export class Server {
// .pipe(transformer.stream());
// .pipe(aggregate.stream());
});
console.log(this.aggregates);
}
}
......@@ -18,11 +18,9 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
export class Source {
name: string;
data: any[];
public name: string;
private data: any[];
constructor(name: string, options: any) {
this.name = name;
......@@ -30,11 +28,11 @@ export class Source {
this.data = [];
}
push(doc: any) {
public push(doc: any) {
this.data.push(doc);
}
forEach(callback: Function) {
public forEach(callback: Function) {
this.data.forEach((value: any, index: number, array: any[]) => {
callback(value);
});
......
......@@ -18,13 +18,11 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
export class Transformer {
source: string;
metrics: string[];
dimensions: string[];
extractors: any;
public source: string;
public metrics: string[];
public dimensions: string[];
private extractors: any;
constructor(name: string, options: any) {
this.source = options.source || null;
......@@ -36,11 +34,11 @@ export class Transformer {
};
}
extractMetrics(doc: any) {
public extractMetrics(doc: any) {
return this.extractors.metrics(doc);
}
extractDimensions(doc: any) {
public extractDimensions(doc: any) {
return this.extractors.dimensions(doc);
}
}
......@@ -18,52 +18,52 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
import { expect } from 'chai';
import { expect } from "chai";
import { Hash } from './hash';
import { Hash } from "./hash";
describe('hash utility library', () => {
it('should generate a sha1 hash for a collection of objects', () => {
let h = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
describe("hash utility library", () => {
it("should generate a sha1 hash for a collection of objects", () => {
let h = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
expect(h).to.be.a('string');
expect(h).to.be.a("string");
expect(h).to.not.be.empty;
});
it('should generate the same hash for the same input', () => {
let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
let h2 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
it("should generate the same hash for the same input", () => {
let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
let h2 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
expect(h1).to.be.a('string');
expect(h2).to.be.a('string');
expect(h1).to.be.a("string");
expect(h2).to.be.a("string");
expect(h1).to.be.equal(h2);
});
it('should generate the same hash for different order of input', () => {
let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
let h2 = Hash.sha1('test', ['list', 'of', 'things'], { obj: 'test' });
it("should generate the same hash for different order of input", () => {
let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
let h2 = Hash.sha1("test", ["list", "of", "things"], { obj: "test" });
expect(h1).to.be.a('string');
expect(h2).to.be.a('string');
expect(h1).to.be.a("string");
expect(h2).to.be.a("string");
expect(h1).to.be.equal(h2);
});
it('should not generate the same hash for distinct input', () => {
let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
let h2 = Hash.sha1('test', { obj: 'test', x: true },
['list', 'of', 'things']);
it("should not generate the same hash for distinct input", () => {
let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
let h2 = Hash.sha1("test", { obj: "test", x: true },
["list", "of", "things"]);
expect(h1).to.be.a('string');
expect(h2).to.be.a('string');
expect(h1).to.be.a("string");
expect(h2).to.be.a("string");
expect(h1).to.not.be.equal(h2);
});
it('should not generate the same hash for different order in deep lists', () => {
let h1 = Hash.sha1('test', { obj: 'test' }, ['list', 'of', 'things']);
let h2 = Hash.sha1('test', { obj: 'test' }, ['of', 'list', 'things']);
it("should not generate the same hash for different order in deep lists", () => {
let h1 = Hash.sha1("test", { obj: "test" }, ["list", "of", "things"]);
let h2 = Hash.sha1("test", { obj: "test" }, ["of", "list", "things"]);
expect(h1).to.be.a('string');
expect(h2).to.be.a('string');
expect(h1).to.be.a("string");
expect(h2).to.be.a("string");
expect(h1).to.not.be.equal(h2);
});
});
......@@ -18,24 +18,22 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
'use strict';
import crypto = require('crypto');
import crypto = require("crypto");
export class Hash {
static sha1(...objects: any[]): string {
let hash = crypto.createHash('sha1');
public static sha1(...objects: any[]): string {
let hash = crypto.createHash("sha1");
objects
.map((obj) => {
switch (typeof obj) {
case 'string':
case "string":
return obj;
case 'object':
case "object":
return JSON.stringify(obj);
default:
throw new TypeError(typeof obj +
' cannot be hashed');
" cannot be hashed");
}
})
.sort()
......@@ -43,6 +41,6 @@ export class Hash {
hash.update(objStr);
});
return hash.digest('hex');
return hash.digest("hex");
}
}
......@@ -18,4 +18,4 @@
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
process.env.NODE_ENV = 'test';
process.env.NODE_ENV = "test";
{
"extends": "tslint:recommended",
"rules": {
"no-var-requires": false,
"object-literal-sort-keys": false,
"one-line": false,
"trailing-comma": false
}
}
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