Browse Source

Unlabeled segments hastic/hastic-grafana-app#187 (#432)

pull/1/head
rozetko 6 years ago committed by GitHub
parent
commit
45e276a9f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      server/src/controllers/analytics_controller.ts
  2. 22
      server/src/routes/analytic_units_router.ts
  3. 10
      server/src/routes/segments_router.ts

13
server/src/controllers/analytics_controller.ts

@ -106,6 +106,7 @@ async function runTask(task: AnalyticsTask): Promise<TaskResult> {
async function query(analyticUnit: AnalyticUnit.AnalyticUnit, detector: AnalyticUnit.DetectorType) {
let range;
if(detector === AnalyticUnit.DetectorType.PATTERN) {
// TODO: find labeled OR deleted segments to generate timerange
const segments = await Segment.findMany(analyticUnit.id, { labeled: true });
if(segments.length === 0) {
throw new Error('Need at least 1 labeled segment');
@ -387,14 +388,10 @@ export async function updateSegments(
segmentsToInsert: Segment.Segment[],
removedIds: Segment.SegmentId[]
) {
let [addedIds, removed] = await Promise.all([
Segment.insertSegments(segmentsToInsert),
Segment.setSegmentsDeleted(removedIds)
]);
removed = removed.map(s => s._id);
await Segment.removeSegments(removedIds);
const addedIds = await Segment.insertSegments(segmentsToInsert);
runFirstLearning(id);
return { addedIds, removed };
return { addedIds };
}
export async function updateThreshold(
@ -407,7 +404,7 @@ export async function updateThreshold(
runFirstLearning(id);
}
async function runFirstLearning(id: AnalyticUnit.AnalyticUnitId) {
export async function runFirstLearning(id: AnalyticUnit.AnalyticUnitId) {
// TODO: move setting status somehow "inside" learning
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING);
runLearning(id)

22
server/src/routes/analytic_units_router.ts

@ -206,6 +206,26 @@ async function deleteUnit(ctx: Router.IRouterContext) {
}
}
async function runDetect(ctx: Router.IRouterContext) {
try {
const { id: analyticUnitId } = ctx.request.body as { id: AnalyticUnit.AnalyticUnitId };
AnalyticsController.runFirstLearning(analyticUnitId);
ctx.response.body = {
code: 200,
message: 'Success'
};
} catch (e) {
ctx.response.status = 500;
ctx.response.body = {
code: 500,
message: `POST /analyticUnits/detect error: ${e.message}`
};
}
}
export var router = new Router();
@ -219,3 +239,5 @@ router.patch('/alert', updateAlert);
router.post('/', createUnit);
router.delete('/', deleteUnit);
router.patch('/', updateUnit);
router.post('/detect', runDetect);

10
server/src/routes/segments_router.ts

@ -40,21 +40,21 @@ async function getSegments(ctx: Router.IRouterContext) {
async function updateSegments(ctx: Router.IRouterContext) {
try {
let {
const {
addedSegments, id, removedSegments: removedIds
} = ctx.request.body as {
addedSegments: any[], id: AnalyticUnitId, removedSegments: Segment.SegmentId[]
};
let segmentsToInsert: Segment.Segment[] = addedSegments.map(
s => Segment.Segment.fromObject({ analyticUnitId: id, labeled: true, ...s })
const segmentsToInsert: Segment.Segment[] = addedSegments.map(
s => Segment.Segment.fromObject({ analyticUnitId: id, ...s })
);
let { addedIds, removed } = await AnalyticsController.updateSegments(
const { addedIds } = await AnalyticsController.updateSegments(
id, segmentsToInsert, removedIds
);
ctx.response.body = { addedIds, removed };
ctx.response.body = { addedIds };
} catch(e) {
console.error(e);

Loading…
Cancel
Save