You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

import * as Router from 'koa-router';
import { AnalyticUnitId } from '../models/analytic_unit_model';
6 years ago
import * as SegmentModel from '../models/segment_model';
import { runLearning } from '../controllers/analytics_controller';
6 years ago
async function getSegments(ctx: Router.IRouterContext) {
6 years ago
// let id: AnalyticUnitId = ctx.request.query.id;
6 years ago
6 years ago
// let segments = await findMany(id, {
// intexGT: ctx.request.query.lastSegmentId,
// timeFromGTE: ctx.request.query.from,
// timeToLTE: ctx.request.query.to
// });
6 years ago
ctx.response.body = { segments: [] };
6 years ago
}
async function updateSegments(ctx: Router.IRouterContext) {
6 years ago
try {
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);
6 years ago
ctx.response.body = { addedIds };
runLearning(id);
6 years ago
} catch(e) {
ctx.response.status = 500;
ctx.response.body = {
6 years ago
code: 500,
message: `Learning error: ${e.message}`
};
6 years ago
}
}
export const router = new Router();
6 years ago
router.get('/', getSegments);
6 years ago
router.patch('/', updateSegments);