diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index ad3db56..ae65a91 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,5 +1,6 @@ +import { Task } from '../models/task_model'; import * as SegmentsController from '../models/segment_model'; -import * as AnalyticUnit from '../models/analytic_unit_model' +import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service'; @@ -49,7 +50,7 @@ export function terminate() { analyticsService.close(); } -async function runTask(task: any): Promise { +async function runTask(task: Task): Promise { // let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId); // task.metric = { // datasource: anomaly.metric.datasource, diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index 7115803..26b0278 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -56,7 +56,7 @@ export class AnalyticUnit { Metric.fromObject(obj.metric), obj.status, obj.lastPredictionTime, - obj.id || obj._id, + obj._id, obj.error, ); } diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index 4abb5f2..bc46b9c 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -9,21 +9,27 @@ type SegmentId = string; export class Segment { constructor( - public auId: AnalyticUnitId, + public analyticUnitId: AnalyticUnitId, public from: number, public to: number, public labeled: boolean, public id?: SegmentId ) { - if(auId === undefined) { + if(analyticUnitId === undefined) { throw new Error('AnalyticUnitId is undefined'); } if(from === undefined) { throw new Error('from is undefined'); } + if(isNaN(from)) { + throw new Error('from is NaN'); + } if(to === undefined) { throw new Error('to is undefined'); } + if(isNaN(to)) { + throw new Error('to is NaN'); + } if(labeled === undefined) { throw new Error('labeled is undefined'); } @@ -32,7 +38,7 @@ export class Segment { public toObject() { return { _id: this.id, - auId: this.auId, + analyticUnitId: this.analyticUnitId, from: this.from, to: this.to, labeled: this.labeled @@ -44,8 +50,9 @@ export class Segment { throw new Error('obj is undefined'); } return new Segment( - obj.auId, +obj.from, +obj.to, - obj.labeled, obj.id || obj._id + obj.analyticUnitId, + +obj.from, +obj.to, + obj.labeled, obj._id ); } } diff --git a/server/src/models/task_model.ts b/server/src/models/task_model.ts index 880519c..c19e3d4 100644 --- a/server/src/models/task_model.ts +++ b/server/src/models/task_model.ts @@ -1,3 +1,32 @@ +import { AnalyticUnitId } from "./analytic_unit_model"; + + +export type TaskId = string; + export class Task { - + constructor( + public analyticUnitId: AnalyticUnitId, + public id?: TaskId + ) { + if(analyticUnitId === undefined) { + throw new Error('analyticUnitId is undefined'); + } + } + + public toObject() { + return { + _id: this.id, + analyticUnitId: this.analyticUnitId + }; + } + + static fromObject(obj: any): Task { + if(obj === undefined) { + throw new Error('obj is undefined'); + } + return new Task( + obj.analyticUnitId, + obj._id, + ); + } } diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 64ff66a..a03d8c3 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -2,11 +2,7 @@ import * as Router from 'koa-router'; import { AnalyticUnitId } from '../models/analytic_unit_model'; -import { - findMany, - insertSegments, - removeSegments, -} from '../models/segment_model'; +import * as SegmentModel from '../models/segment_model'; import { runLearning } from '../controllers/analytics_controller'; @@ -25,9 +21,14 @@ async function getSegments(ctx: Router.IRouterContext) { async function updateSegments(ctx: Router.IRouterContext) { try { - let segmentsUpdate = ctx.request.body; - let id = segmentsUpdate.id; - let addedIds = insertSegments(segmentsUpdate.addedSegments); + + let { addedSegments, id } = ctx.request.body as { addedSegments: any[], id: AnalyticUnitId }; + + let segmentsToInsert: SegmentModel.Segment[] = addedSegments.map( + s => SegmentModel.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s }) + ); + + let addedIds = await SegmentModel.insertSegments(segmentsToInsert); // removeSegments(id, segmentsUpdate.removedSegments); ctx.response.body = { addedIds }; runLearning(id); diff --git a/server/src/services/data_service.ts b/server/src/services/data_service.ts index 725a69e..e892c99 100644 --- a/server/src/services/data_service.ts +++ b/server/src/services/data_service.ts @@ -63,6 +63,9 @@ let dbInsertOne = (collection: Collection, doc: object) => { } let dbInsertMany = (collection: Collection, docs: object[]) => { + if(docs.length === 0) { + return Promise.resolve([]); + } return new Promise((resolve, reject) => { db.get(collection).insert(docs, (err, newDocs: any[]) => { if(err) { diff --git a/server/tsconfig.json b/server/tsconfig.json index f51b103..d5cef27 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -1,9 +1,7 @@ { "compilerOptions": { "sourceMap": true, - "noImplicitAny": true, "module": "commonjs", - "target": "es6", - "allowJs": true, + "target": "es6" } }