From 7f8597636d964c8d7ade45cc5b6d016795a0126c Mon Sep 17 00:00:00 2001 From: Evgeny Smyshlyaev Date: Wed, 10 Jul 2019 17:44:50 +0300 Subject: [PATCH] Data kit error status code 400 #300 (#711) --- server/src/controllers/analytics_controller.ts | 11 +---------- server/src/routes/detections_router.ts | 4 ++++ server/src/services/data_puller.ts | 12 ++++++++++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/server/src/controllers/analytics_controller.ts b/server/src/controllers/analytics_controller.ts index c35e660..ee3c482 100644 --- a/server/src/controllers/analytics_controller.ts +++ b/server/src/controllers/analytics_controller.ts @@ -75,15 +75,6 @@ async function onPushDetect(detectionResult: DetectionResult) { } catch(err) { console.error(`error while sending webhook: ${err.message}`); } - } else { - let reasons = []; - if(!analyticUnit.alert) { - reasons.push('alerting disabled'); - } - if(_.isEmpty(detectionResult.segments)) { - reasons.push('segments empty'); - } - console.log(`skip sending webhook for ${analyticUnit.id}, ${reasons.join(', ')}`); } await onDetect(detectionResult); } @@ -377,7 +368,7 @@ export async function runDetect(id: AnalyticUnit.AnalyticUnitId, from?: number, await Promise.all([ Segment.insertSegments(payload.segments), AnalyticUnitCache.setData(id, payload.cache), - AnalyticUnit.setDetectionTime(id, payload.lastDetectionTime), + AnalyticUnit.setDetectionTime(id, range.to - intersection), ]); await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); await Detection.insertSpan( diff --git a/server/src/routes/detections_router.ts b/server/src/routes/detections_router.ts index 47566bf..05480a0 100644 --- a/server/src/routes/detections_router.ts +++ b/server/src/routes/detections_router.ts @@ -24,6 +24,10 @@ export async function getDetectionSpans(ctx: Router.IRouterContext) { throw new Error(`to is missing or corrupted (got ${ctx.request.query.to})`); } + if(from >= to) { + throw new Error(`'from' timestamp ${from} must be less than 'to' timestamp ${to}`); + } + let response: DetectionSpansResponse = { spans: [] }; // TODO: invalidate response.spans = await AnalyticsController.getDetectionSpans(id, from, to); diff --git a/server/src/services/data_puller.ts b/server/src/services/data_puller.ts index 8889755..e096383 100644 --- a/server/src/services/data_puller.ts +++ b/server/src/services/data_puller.ts @@ -94,8 +94,7 @@ export class DataPuller { private async _runAnalyticUnitPuller(analyticUnit: AnalyticUnit.AnalyticUnit) { console.log(`run data puller for analytic unit ${analyticUnit.id}`); - // TODO: lastDetectionTime can be in ns - const time = analyticUnit.lastDetectionTime + 1 || Date.now(); + const time = Date.now(); this._unitTimes[analyticUnit.id] = time; const dataGenerator = this.getDataGenerator( @@ -150,6 +149,15 @@ export class DataPuller { throw new Error(`Analytic unit ${analyticUnit.id} is deleted from puller`); } const now = Date.now(); + + if(time >= now) { + // TODO: probably we should have ability to set PULL_PERIOD_MS or get it from metric as time step between points + return { + columns: [], + values: [] + }; + } + const res = await this.pullData(analyticUnit, time, now); this._grafanaAvailableConsoleReporter(true); this.alertService.sendGrafanaAvailableWebhook();