Browse Source

task model + tsconfig checksfix + segments parse++

pull/1/head
Coin de Gamma 6 years ago
parent
commit
de68788c8d
  1. 5
      server/src/controllers/analytics_controller.ts
  2. 2
      server/src/models/analytic_unit_model.ts
  3. 17
      server/src/models/segment_model.ts
  4. 29
      server/src/models/task_model.ts
  5. 17
      server/src/routes/segments_router.ts
  6. 3
      server/src/services/data_service.ts
  7. 4
      server/tsconfig.json

5
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 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'; import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service';
@ -49,7 +50,7 @@ export function terminate() {
analyticsService.close(); analyticsService.close();
} }
async function runTask(task: any): Promise<any> { async function runTask(task: Task): Promise<any> {
// let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId); // let anomaly: AnalyticUnit.AnalyticUnit = await AnalyticUnit.findById(task.analyticUnitId);
// task.metric = { // task.metric = {
// datasource: anomaly.metric.datasource, // datasource: anomaly.metric.datasource,

2
server/src/models/analytic_unit_model.ts

@ -56,7 +56,7 @@ export class AnalyticUnit {
Metric.fromObject(obj.metric), Metric.fromObject(obj.metric),
obj.status, obj.status,
obj.lastPredictionTime, obj.lastPredictionTime,
obj.id || obj._id, obj._id,
obj.error, obj.error,
); );
} }

17
server/src/models/segment_model.ts

@ -9,21 +9,27 @@ type SegmentId = string;
export class Segment { export class Segment {
constructor( constructor(
public auId: AnalyticUnitId, public analyticUnitId: AnalyticUnitId,
public from: number, public from: number,
public to: number, public to: number,
public labeled: boolean, public labeled: boolean,
public id?: SegmentId public id?: SegmentId
) { ) {
if(auId === undefined) { if(analyticUnitId === undefined) {
throw new Error('AnalyticUnitId is undefined'); throw new Error('AnalyticUnitId is undefined');
} }
if(from === undefined) { if(from === undefined) {
throw new Error('from is undefined'); throw new Error('from is undefined');
} }
if(isNaN(from)) {
throw new Error('from is NaN');
}
if(to === undefined) { if(to === undefined) {
throw new Error('to is undefined'); throw new Error('to is undefined');
} }
if(isNaN(to)) {
throw new Error('to is NaN');
}
if(labeled === undefined) { if(labeled === undefined) {
throw new Error('labeled is undefined'); throw new Error('labeled is undefined');
} }
@ -32,7 +38,7 @@ export class Segment {
public toObject() { public toObject() {
return { return {
_id: this.id, _id: this.id,
auId: this.auId, analyticUnitId: this.analyticUnitId,
from: this.from, from: this.from,
to: this.to, to: this.to,
labeled: this.labeled labeled: this.labeled
@ -44,8 +50,9 @@ export class Segment {
throw new Error('obj is undefined'); throw new Error('obj is undefined');
} }
return new Segment( return new Segment(
obj.auId, +obj.from, +obj.to, obj.analyticUnitId,
obj.labeled, obj.id || obj._id +obj.from, +obj.to,
obj.labeled, obj._id
); );
} }
} }

29
server/src/models/task_model.ts

@ -1,3 +1,32 @@
import { AnalyticUnitId } from "./analytic_unit_model";
export type TaskId = string;
export class Task { 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,
);
}
} }

17
server/src/routes/segments_router.ts

@ -2,11 +2,7 @@ import * as Router from 'koa-router';
import { AnalyticUnitId } from '../models/analytic_unit_model'; import { AnalyticUnitId } from '../models/analytic_unit_model';
import { import * as SegmentModel from '../models/segment_model';
findMany,
insertSegments,
removeSegments,
} from '../models/segment_model';
import { runLearning } from '../controllers/analytics_controller'; import { runLearning } from '../controllers/analytics_controller';
@ -25,9 +21,14 @@ async function getSegments(ctx: Router.IRouterContext) {
async function updateSegments(ctx: Router.IRouterContext) { async function updateSegments(ctx: Router.IRouterContext) {
try { try {
let segmentsUpdate = ctx.request.body;
let id = segmentsUpdate.id; let { addedSegments, id } = ctx.request.body as { addedSegments: any[], id: AnalyticUnitId };
let addedIds = insertSegments(segmentsUpdate.addedSegments);
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); // removeSegments(id, segmentsUpdate.removedSegments);
ctx.response.body = { addedIds }; ctx.response.body = { addedIds };
runLearning(id); runLearning(id);

3
server/src/services/data_service.ts

@ -63,6 +63,9 @@ let dbInsertOne = (collection: Collection, doc: object) => {
} }
let dbInsertMany = (collection: Collection, docs: object[]) => { let dbInsertMany = (collection: Collection, docs: object[]) => {
if(docs.length === 0) {
return Promise.resolve([]);
}
return new Promise<string[]>((resolve, reject) => { return new Promise<string[]>((resolve, reject) => {
db.get(collection).insert(docs, (err, newDocs: any[]) => { db.get(collection).insert(docs, (err, newDocs: any[]) => {
if(err) { if(err) {

4
server/tsconfig.json

@ -1,9 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"sourceMap": true, "sourceMap": true,
"noImplicitAny": true,
"module": "commonjs", "module": "commonjs",
"target": "es6", "target": "es6"
"allowJs": true,
} }
} }

Loading…
Cancel
Save