Skip to content
Snippets Groups Projects
Commit ed50105a authored by Eduardo L. Buratti's avatar Eduardo L. Buratti
Browse files

Add tslint

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