Browse Source

mv updateSegments to controller and pending status

pull/1/head
Coin de Gamma 6 years ago
parent
commit
12fdb9d179
  1. 24
      server/src/controllers/analytics_controller.ts
  2. 13
      server/src/models/analytic_unit_model.ts
  3. 8
      server/src/routes/analytic_units_router.ts
  4. 29
      server/src/routes/segments_router.ts

24
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<AnalyticUnit.AnalyticUnitId> {
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 }
}

13
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<AnalyticUnit> {
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<AnalyticUnit> {
* @returns unit.id
*/
export async function create(unit: AnalyticUnit): Promise<AnalyticUnitId> {
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<void> {
await db.removeOne(id);
return;
}
export async function update(id: AnalyticUnitId, unit: AnalyticUnit) {

8
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 = {

29
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 AnalyticsController.updateSegments(
id, segmentsToInsert, removedIds
);
let [addedIds, removed] = await Promise.all([
SegmentModel.insertSegments(segmentsToInsert),
SegmentModel.removeSegments(removedIds)
]);
ctx.response.body = { addedIds, removed, status: AnalyticUnitStatus.PENDING };
ctx.response.body = { addedIds, removed };
runLearning(id);
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {

Loading…
Cancel
Save