Browse Source

Tsconfig strict mode part1 (#767)

pull/1/head
Coin de Gamma 5 years ago committed by GitHub
parent
commit
38d885b5dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .travis.yml
  2. 2
      server/package.json
  3. 11
      server/spec/segments.jest.ts
  4. 3
      server/spec/utils_for_tests/analytic_units.ts
  5. 2
      server/spec/utils_for_tests/segments.ts
  6. 2
      server/src/models/analytic_unit_cache_model.ts
  7. 2
      server/src/models/analytic_units/anomaly_analytic_unit_model.ts
  8. 2
      server/src/models/analytic_units/db.ts
  9. 2
      server/src/models/analytic_units/pattern_analytic_unit_model.ts
  10. 2
      server/src/models/analytic_units/threshold_analytic_unit_model.ts
  11. 39
      server/src/models/segment_model.ts
  12. 62
      server/src/services/data_service.ts
  13. 2
      server/src/utils/segments.ts
  14. 3
      server/tsconfig.json

3
.travis.yml

@ -19,9 +19,6 @@ matrix:
before_script:
- cd analytics
- pip install -r requirements.txt
- sudo apt-get update
- sudo apt-get install -y
apt-utils gnupg curl make g++ git
script:
- python -m unittest discover

2
server/package.json

@ -21,7 +21,7 @@
"homepage": "https://github.com/hastic/hastic-server#readme",
"dependencies": {},
"devDependencies": {
"@types/jest": "^23.1.1",
"@types/jest": "^23.3.14",
"@types/koa": "^2.0.46",
"@types/koa-bodyparser": "^4.2.0",
"@types/koa-router": "^7.0.31",

11
server/spec/segments.jest.ts

@ -1,5 +1,5 @@
import { TEST_ANALYTIC_UNIT_ID } from './utils_for_tests/analytic_units';
import { buildSegments, clearSegmentsDB, convertSegmentsToTimeRanges } from './utils_for_tests/segments';
import { TEST_ANALYTIC_UNIT_ID, createTestDB, clearTestDB } from './utils_for_tests/analytic_units';
import { buildSegments, clearSegmentsDB } from './utils_for_tests/segments';
import * as Segment from '../src/models/segment_model';
@ -7,6 +7,11 @@ import * as _ from 'lodash';
const INITIAL_SEGMENTS = buildSegments([[0, 1], [2, 3], [4, 5]]);
beforeAll(async () => {
await clearTestDB();
await createTestDB();
});
beforeEach(async () => {
await Segment.mergeAndInsertSegments(INITIAL_SEGMENTS);
});
@ -16,7 +21,7 @@ afterEach(async () => {
});
describe('mergeAndInsertSegments', function() {
it('Should be merged before insertion', async function() {
it('should be merged before insertion', async function() {
const segmentsToInsert = buildSegments([[1, 2]]);
await Segment.mergeAndInsertSegments(segmentsToInsert);

3
server/spec/utils_for_tests/analytic_units.ts

@ -1,9 +1,10 @@
import * as AnalyticUnit from '../../src/models/analytic_units';
import * as AnalyticUnitCache from '../../src/models/analytic_unit_cache_model';
import { Metric } from 'grafana-datasource-kit';
import * as _ from 'lodash';
import * as AnalyticUnitCache from '../../src/models/analytic_unit_cache_model';
export const TEST_ANALYTIC_UNIT_ID: AnalyticUnit.AnalyticUnitId = 'testid';

2
server/spec/utils_for_tests/segments.ts

@ -16,5 +16,5 @@ export function convertSegmentsToTimeRanges(segments: Segment.Segment[]): number
export async function clearSegmentsDB(): Promise<void> {
const segments = await Segment.findMany(TEST_ANALYTIC_UNIT_ID, { labeled: false, deleted: false });
await Segment.removeSegments(segments.map(s => s.id));
await Segment.removeSegments(_.compact(segments.map(s => s.id)));
}

2
server/src/models/analytic_unit_cache_model.ts

@ -54,7 +54,7 @@ export class AnalyticUnitCache {
}
}
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnitCache> {
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnitCache | null> {
let obj = await db.findOne(id);
if(obj === null) {
return null;

2
server/src/models/analytic_units/anomaly_analytic_unit_model.ts

@ -75,7 +75,7 @@ export class AnomalyAnalyticUnit extends AnalyticUnit {
static fromObject(obj: any) {
// TODO: remove duplication
let metric: Metric;
let metric: Metric | undefined = undefined;
if (obj.metric !== undefined) {
metric = Metric.fromObject(obj.metric);
}

2
server/src/models/analytic_units/db.ts

@ -10,7 +10,7 @@ import * as _ from 'lodash';
const db = makeDBQ(Collection.ANALYTIC_UNITS);
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit> {
export async function findById(id: AnalyticUnitId): Promise<AnalyticUnit | null> {
let obj = await db.findOne(id);
if (obj === null) {
return null;

2
server/src/models/analytic_units/pattern_analytic_unit_model.ts

@ -56,7 +56,7 @@ export class PatternAnalyticUnit extends AnalyticUnit {
static fromObject(obj: any) {
// TODO: remove duplication
let metric: Metric;
let metric: Metric | undefined = undefined;
if(obj.metric !== undefined) {
metric = Metric.fromObject(obj.metric);
}

2
server/src/models/analytic_units/threshold_analytic_unit_model.ts

@ -72,7 +72,7 @@ export class ThresholdAnalyticUnit extends AnalyticUnit {
static fromObject(obj: any) {
// TODO: remove duplication
let metric: Metric;
let metric: Metric | undefined = undefined;
if (obj.metric !== undefined) {
metric = Metric.fromObject(obj.metric);
}

39
server/src/models/segment_model.ts

@ -128,6 +128,9 @@ export async function findIntersectedSegments(
return findMany(analyticUnitId, query);
}
// TODO: rewrite all this horrible function
// TODO: use utils.segments.IntegerSegmentsSet
/**
* Merges an array of segments with ones existing in the DB
* Inserts resulting segments into DB
@ -142,6 +145,18 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
return { addedIds: [], removedIds: [] };
}
const analyticUnitId: AnalyticUnitId = segments[0].analyticUnitId;
const unit = await AnalyticUnit.findById(analyticUnitId);
if(unit === null) {
throw new Error('Can`t find analytic unit ' + analyticUnitId);
}
const cache = await AnalyticUnitCache.findById(analyticUnitId);
if(cache === null) {
throw new Error('Can`t find cache for analytic unit ' + analyticUnitId);
}
const detector = unit.detectorType;
let segmentIdsToRemove: SegmentId[] = [];
let segmentsToInsert: Segment[] = [];
@ -156,11 +171,7 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
}
}
let cache = await AnalyticUnitCache.findById(analyticUnitId);
let unit = await AnalyticUnit.findById(analyticUnitId);
const detector = unit.detectorType;
let intersectedSegments: Segment[];
let intersectedSegments: Segment[] = [];
if(detector === AnalyticUnit.DetectorType.PATTERN) {
intersectedSegments = await findMany(analyticUnitId, {
to: { $gte: segment.from },
@ -179,12 +190,24 @@ export async function mergeAndInsertSegments(segments: Segment[]): Promise<{
}
if(intersectedSegments.length > 0) {
let from = _.minBy(intersectedSegments.concat(segment), s => s.from).from;
let to = _.maxBy(intersectedSegments.concat(segment), s => s.to).to;
let intersectedIds = intersectedSegments.map(s => s.id);
let minFromSegment = _.minBy(intersectedSegments.concat(segment), s => s.from);
let maxToSegment = _.maxBy(intersectedSegments.concat(segment), s => s.to);
if(minFromSegment === undefined) {
throw new Error('minFromSegment is undefined');
}
if(maxToSegment === undefined) {
throw new Error('maxToSegment is undefined');
}
let from = minFromSegment.from;
let to = maxToSegment.to;
let newSegment = Segment.fromObject(segment.toObject());
newSegment.from = from;
newSegment.to = to;
segmentIdsToRemove = segmentIdsToRemove.concat(intersectedSegments.map(s => s.id));
segmentIdsToRemove = segmentIdsToRemove.concat(_.compact(intersectedIds));
segmentsToInsert.push(newSegment);
} else {
segmentsToInsert.push(segment);

62
server/src/services/data_service.ts

@ -31,16 +31,24 @@ export type DBQ = {
removeMany: (query: string[] | object) => Promise<number>
}
function nedbCollectionFromCollection(collection: Collection): nedb {
let nedbCollection = db.get(collection);
if(nedbCollection === undefined) {
throw new Error('Can`t find collection ' + collection);
}
return nedbCollection;
}
export function makeDBQ(collection: Collection): DBQ {
return {
findOne: dbFindOne.bind(null, collection),
findMany: dbFindMany.bind(null, collection),
insertOne: dbInsertOne.bind(null, collection),
insertMany: dbInsertMany.bind(null, collection),
updateOne: dbUpdateOne.bind(null, collection),
updateMany: dbUpdateMany.bind(null, collection),
removeOne: dbRemoveOne.bind(null, collection),
removeMany: dbRemoveMany.bind(null, collection)
findOne: dbFindOne.bind(null, nedbCollectionFromCollection(collection)),
findMany: dbFindMany.bind(null, nedbCollectionFromCollection(collection)),
insertOne: dbInsertOne.bind(null, nedbCollectionFromCollection(collection)),
insertMany: dbInsertMany.bind(null, nedbCollectionFromCollection(collection)),
updateOne: dbUpdateOne.bind(null, nedbCollectionFromCollection(collection)),
updateMany: dbUpdateMany.bind(null, nedbCollectionFromCollection(collection)),
removeOne: dbRemoveOne.bind(null, nedbCollectionFromCollection(collection)),
removeMany: dbRemoveMany.bind(null, nedbCollectionFromCollection(collection))
}
}
@ -58,6 +66,7 @@ function wrapIdsToQuery(query: string[] | object): any {
return query;
}
// TODO: move to utils
function isEmptyArray(obj: any): boolean {
if(!Array.isArray(obj)) {
return false;
@ -67,24 +76,25 @@ function isEmptyArray(obj: any): boolean {
const db = new Map<Collection, nedb>();
let dbInsertOne = (collection: Collection, doc: object) => {
async function dbInsertOne(nd: nedb, doc: object): Promise<string> {
return new Promise<string>((resolve, reject) => {
db.get(collection).insert(doc, (err, newDoc: any) => {
nd.insert(doc, (err, newDoc: any) => {
if(err) {
reject(err);
} else {
resolve(newDoc._id);
}
});
})
});
}
let dbInsertMany = (collection: Collection, docs: object[]) => {
async function dbInsertMany(nd: nedb, docs: object[]): Promise<string[]> {
if(docs.length === 0) {
return Promise.resolve([]);
}
return new Promise<string[]>((resolve, reject) => {
db.get(collection).insert(docs, (err, newDocs: any[]) => {
nd.insert(docs, (err, newDocs: any[]) => {
if(err) {
reject(err);
} else {
@ -94,12 +104,12 @@ let dbInsertMany = (collection: Collection, docs: object[]) => {
});
}
let dbUpdateOne = (collection: Collection, query: string | object, updateQuery: object) => {
async function dbUpdateOne(nd: nedb, query: string | object, updateQuery: object): Promise<any> {
// https://github.com/louischatriot/nedb#updating-documents
let nedbUpdateQuery = { $set: updateQuery }
query = wrapIdToQuery(query);
return new Promise<any>((resolve, reject) => {
db.get(collection).update(
nd.update(
query,
nedbUpdateQuery,
{ returnUpdatedDocs: true },
@ -114,7 +124,7 @@ let dbUpdateOne = (collection: Collection, query: string | object, updateQuery:
});
}
let dbUpdateMany = (collection: Collection, query: string[] | object, updateQuery: object) => {
async function dbUpdateMany(nd: nedb, query: string[] | object, updateQuery: object): Promise<any[]> {
// https://github.com/louischatriot/nedb#updating-documents
if(isEmptyArray(query)) {
return Promise.resolve([]);
@ -122,7 +132,7 @@ let dbUpdateMany = (collection: Collection, query: string[] | object, updateQuer
let nedbUpdateQuery = { $set: updateQuery };
query = wrapIdsToQuery(query);
return new Promise<any[]>((resolve, reject) => {
db.get(collection).update(
nd.update(
query,
nedbUpdateQuery,
{ returnUpdatedDocs: true, multi: true },
@ -137,10 +147,10 @@ let dbUpdateMany = (collection: Collection, query: string[] | object, updateQuer
});
}
let dbFindOne = (collection: Collection, query: string | object) => {
async function dbFindOne(nd: nedb, query: string | object): Promise<any> {
query = wrapIdToQuery(query);
return new Promise<any | null>((resolve, reject) => {
db.get(collection).findOne(query, (err, doc) => {
nd.findOne(query, (err, doc) => {
if(err) {
reject(err);
} else {
@ -150,13 +160,13 @@ let dbFindOne = (collection: Collection, query: string | object) => {
});
}
let dbFindMany = (collection: Collection, query: string[] | object, sortQuery: object = {}) => {
async function dbFindMany(nd: nedb, query: string[] | object, sortQuery: object = {}): Promise<any[]> {
if(isEmptyArray(query)) {
return Promise.resolve([]);
}
query = wrapIdsToQuery(query);
return new Promise<any[]>((resolve, reject) => {
db.get(collection).find(query).sort(sortQuery).exec((err, docs: any[]) => {
nd.find(query).sort(sortQuery).exec((err, docs: any[]) => {
if(err) {
reject(err);
} else {
@ -166,10 +176,10 @@ let dbFindMany = (collection: Collection, query: string[] | object, sortQuery: o
});
}
let dbRemoveOne = (collection: Collection, query: string | object) => {
async function dbRemoveOne(nd: nedb, query: string | object): Promise<boolean> {
query = wrapIdToQuery(query);
return new Promise<boolean>((resolve, reject) => {
db.get(collection).remove(query, { /* options */ }, (err, numRemoved) => {
nd.remove(query, { /* options */ }, (err, numRemoved) => {
if(err) {
reject(err);
} else {
@ -183,13 +193,13 @@ let dbRemoveOne = (collection: Collection, query: string | object) => {
});
}
let dbRemoveMany = (collection: Collection, query: string[] | object) => {
async function dbRemoveMany(nd: nedb, query: string[] | object): Promise<number> {
if(isEmptyArray(query)) {
return Promise.resolve([]);
return Promise.resolve(0);
}
query = wrapIdsToQuery(query);
return new Promise<number>((resolve, reject) => {
db.get(collection).remove(query, { multi: true }, (err, numRemoved) => {
nd.remove(query, { multi: true }, (err, numRemoved) => {
if(err) {
reject(err);
} else {

2
server/src/utils/segments.ts

@ -113,7 +113,7 @@ export class IntegerSegmentsSet {
}
return s;
}, null);
push(_.last(this._segments).to + 1, Infinity);
push(this._segments[this._segments.length - 1].to + 1, Infinity);
}
return new IntegerSegmentsSet(invertedSegments, true);
}

3
server/tsconfig.json

@ -2,6 +2,7 @@
"compilerOptions": {
"sourceMap": true,
"module": "commonjs",
"target": "es6"
"target": "es6",
// "strict": true
}
}

Loading…
Cancel
Save