diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 80a259e..72081ad 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -1,6 +1,6 @@ import { AnalyticsMessageMethod, AnalyticsMessage } from '../models/analytics_message_model' import { AnalyticsTask, AnalyticsTaskType, AnalyticsTaskId } from '../models/analytics_task_model'; -import * as Segments from '../models/segment_model'; +import * as Segment from '../models/segment_model'; import * as AnalyticUnit from '../models/analytic_unit_model'; import { AnalyticsService } from '../services/analytics_service'; @@ -75,7 +75,7 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { let previousLastPredictionTime: number = undefined; try { - let segments = await Segments.findMany(id, { labeled: true }); + let segments = await Segment.findMany(id, { labeled: true }); let segmentObjs = segments.map(s => s.toObject()); let analyticUnit = await AnalyticUnit.findById(id); @@ -94,7 +94,7 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { previousLastPredictionTime = analyticUnit.lastPredictionTime; await Promise.all([ - Segments.insertSegments(predictedSegments), + Segment.insertSegments(predictedSegments), AnalyticUnit.setPredictionTime(id, lastPredictionTime) ]); await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); @@ -110,7 +110,7 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { async function processLearningResult(taskResult: any): Promise<{ lastPredictionTime: number, - segments: Segments.Segment[] + segments: Segment.Segment[] }> { if(taskResult.status !== 'SUCCESS') { return Promise.reject(taskResult.error); @@ -126,7 +126,7 @@ async function processLearningResult(taskResult: any): Promise<{ return { lastPredictionTime: +taskResult.lastPredictionTime, - segments: taskResult.segments.map(Segments.Segment.fromObject) + segments: taskResult.segments.map(Segment.Segment.fromObject) }; } @@ -169,6 +169,18 @@ export function isAnalyticReady(): boolean { export async function createAnalyticUnitFromObject(obj: any): Promise { let unit: AnalyticUnit.AnalyticUnit = AnalyticUnit.AnalyticUnit.fromObject(obj); let id = await AnalyticUnit.create(unit); - // runLearning(unit); return id; +} + +export async function updateSegments( + id: AnalyticUnit.AnalyticUnitId, + segmentsToInsert: Segment.Segment[], + removedIds: Segment.SegmentId[] +) { + let [addedIds, removed] = await Promise.all([ + Segment.insertSegments(segmentsToInsert), + Segment.removeSegments(removedIds) + ]); + runLearning(id); + return { addedIds, removed } } \ No newline at end of file diff --git a/server/src/models/analytic_unit_model.ts b/server/src/models/analytic_unit_model.ts index e018de3..631ecf6 100644 --- a/server/src/models/analytic_unit_model.ts +++ b/server/src/models/analytic_unit_model.ts @@ -5,13 +5,13 @@ import { Collection, makeDBQ } from '../services/data_service'; let db = makeDBQ(Collection.ANALYTIC_UNITS); - export type AnalyticUnitId = string; export enum AnalyticUnitStatus { LEARNING = 'LEARNING', SUCCESS = 'SUCCESS', READY = 'READY', - FAILED = 'FAILED' + FAILED = 'FAILED', + PENDING = 'PENDING' } export class AnalyticUnit { @@ -72,7 +72,8 @@ export class AnalyticUnit { export async function findById(id: AnalyticUnitId): Promise { - return AnalyticUnit.fromObject(await db.findOne(id)); + let obj = await db.findOne(id); + return AnalyticUnit.fromObject(obj); } /** @@ -82,14 +83,12 @@ export async function findById(id: AnalyticUnitId): Promise { * @returns unit.id */ export async function create(unit: AnalyticUnit): Promise { - var obj = unit.toObject(); - var r = await db.insertOne(obj); - return r; + let obj = unit.toObject(); + return db.insertOne(obj); } export async function remove(id: AnalyticUnitId): Promise { await db.removeOne(id); - return; } export async function update(id: AnalyticUnitId, unit: AnalyticUnit) { diff --git a/server/src/routes/analytic_units_router.ts b/server/src/routes/analytic_units_router.ts index 37a85d8..3afd82f 100644 --- a/server/src/routes/analytic_units_router.ts +++ b/server/src/routes/analytic_units_router.ts @@ -1,9 +1,9 @@ -import * as Router from 'koa-router'; - import * as AnalyticUnit from '../models/analytic_unit_model'; import { createAnalyticUnitFromObject } from '../controllers/analytics_controller' +import * as Router from 'koa-router'; + async function getStatus(ctx: Router.IRouterContext) { try { @@ -42,8 +42,8 @@ async function getUnit(ctx: Router.IRouterContext) { async function createUnit(ctx: Router.IRouterContext) { try { - let newId = await createAnalyticUnitFromObject(ctx.request.body); - ctx.response.body = { id: newId }; + let id = await createAnalyticUnitFromObject(ctx.request.body); + ctx.response.body = { id }; } catch(e) { ctx.response.status = 500; ctx.response.body = { diff --git a/server/src/routes/segments_router.ts b/server/src/routes/segments_router.ts index 07bc1fc..1b34e78 100644 --- a/server/src/routes/segments_router.ts +++ b/server/src/routes/segments_router.ts @@ -1,9 +1,9 @@ -import * as Router from 'koa-router'; +import * as AnalyticsController from '../controllers/analytics_controller'; -import { AnalyticUnitId } from '../models/analytic_unit_model'; +import { AnalyticUnitId, AnalyticUnit, AnalyticUnitStatus } from '../models/analytic_unit_model'; +import * as Segment from '../models/segment_model'; -import * as SegmentModel from '../models/segment_model'; -import { runLearning } from '../controllers/analytics_controller'; +import * as Router from 'koa-router'; async function getSegments(ctx: Router.IRouterContext) { @@ -11,7 +11,7 @@ async function getSegments(ctx: Router.IRouterContext) { if(id === undefined || id === '') { throw new Error('analyticUnitId (id) is missing'); } - let query: SegmentModel.FindManyQuery = {}; + let query: Segment.FindManyQuery = {}; if(!isNaN(+ctx.request.query.lastSegmentId)) { query.intexGT = +ctx.request.query.lastSegmentId; @@ -23,7 +23,7 @@ async function getSegments(ctx: Router.IRouterContext) { query.timeToLTE = +ctx.request.query.to; } - let segments = await SegmentModel.findMany(id, query); + let segments = await Segment.findMany(id, query); ctx.response.body = { segments }; @@ -35,20 +35,19 @@ async function updateSegments(ctx: Router.IRouterContext) { let { addedSegments, id, removedSegments: removedIds } = ctx.request.body as { - addedSegments: any[], id: AnalyticUnitId, removedSegments: SegmentModel.SegmentId[] + addedSegments: any[], id: AnalyticUnitId, removedSegments: Segment.SegmentId[] }; - let segmentsToInsert: SegmentModel.Segment[] = addedSegments.map( - s => SegmentModel.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s }) + let segmentsToInsert: Segment.Segment[] = addedSegments.map( + s => Segment.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s }) ); - let [addedIds, removed] = await Promise.all([ - SegmentModel.insertSegments(segmentsToInsert), - SegmentModel.removeSegments(removedIds) - ]); + let { addedIds, removed } = await AnalyticsController.updateSegments( + id, segmentsToInsert, removedIds + ); - ctx.response.body = { addedIds, removed }; - runLearning(id); + ctx.response.body = { addedIds, removed, status: AnalyticUnitStatus.PENDING }; + } catch(e) { ctx.response.status = 500; ctx.response.body = {