From c3ca05f1f8b9f4f3b46725839ca6dd756eb54f6b Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Thu, 25 Oct 2018 18:11:41 +0300 Subject: [PATCH] Old segments are not deleted #198 (#204) fix Old segments are not deleted #198 --- server/src/controllers/analytics_controller.ts | 4 ++++ server/src/models/segment_model.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index 17f0a4b..be7befa 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -190,6 +190,10 @@ export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { // } // } + let lastPredictedSegments = await Segment.findMany(id, { labeled: false, deleted: false }); + let segmentsToRemove = lastPredictedSegments.filter(s => payload.segments.indexOf(s) === -1); + Segment.removeSegments(segmentsToRemove.map(s => s.id)); + Segment.insertSegments(payload.segments); AnalyticUnitCache.setData(id, payload.cache); AnalyticUnit.setPredictionTime(id, payload.lastPredictionTime); diff --git a/server/src/models/segment_model.ts b/server/src/models/segment_model.ts index 9a397c3..60e93ab 100644 --- a/server/src/models/segment_model.ts +++ b/server/src/models/segment_model.ts @@ -56,6 +56,14 @@ export class Segment { obj.labeled, obj.deleted, obj._id ); } + + public equals(obj: Segment) : boolean { + return this.analyticUnitId === obj.analyticUnitId && + this.from === obj.from && + this.to === obj.to && + this.labeled === this.labeled && + this.deleted === this.deleted; + } } export type FindManyQuery = {