|
|
@ -3,23 +3,24 @@ import { queryByMetric } from 'grafana-datasource-kit'; |
|
|
|
jest.mock('grafana-datasource-kit', () => ( |
|
|
|
jest.mock('grafana-datasource-kit', () => ( |
|
|
|
{ |
|
|
|
{ |
|
|
|
...(jest.requireActual('grafana-datasource-kit')), |
|
|
|
...(jest.requireActual('grafana-datasource-kit')), |
|
|
|
queryByMetric: jest.fn((metric, url, from, to, apiKey) => {}) |
|
|
|
queryByMetric: jest.fn((metric, url, from, to, apiKey) => { |
|
|
|
|
|
|
|
return { values:[], columns:[] } |
|
|
|
|
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
)); |
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
import { saveAnalyticUnitFromObject, runDetect, onDetect } from '../src/controllers/analytics_controller'; |
|
|
|
import { saveAnalyticUnitFromObject, runDetect, onDetect, getHSR } from '../src/controllers/analytics_controller'; |
|
|
|
import * as AnalyticUnit from '../src/models/analytic_units'; |
|
|
|
import * as AnalyticUnit from '../src/models/analytic_units'; |
|
|
|
import * as AnalyticUnitCache from '../src/models/analytic_unit_cache_model'; |
|
|
|
import * as AnalyticUnitCache from '../src/models/analytic_unit_cache_model'; |
|
|
|
import * as Segment from '../src/models/segment_model'; |
|
|
|
import * as Segment from '../src/models/segment_model'; |
|
|
|
import { TEST_ANALYTIC_UNIT_ID } from './utils_for_tests/analytic_units'; |
|
|
|
import { TEST_ANALYTIC_UNIT_ID } from './utils_for_tests/analytic_units'; |
|
|
|
import { buildSegments, clearSegmentsDB, convertSegmentsToTimeRanges } from './utils_for_tests/segments'; |
|
|
|
import { buildSegments, clearSegmentsDB, convertSegmentsToTimeRanges } from './utils_for_tests/segments'; |
|
|
|
|
|
|
|
|
|
|
|
import { HASTIC_API_KEY } from '../src/config'; |
|
|
|
import { HASTIC_API_KEY } from '../src/config'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
describe('Check detection range', function() { |
|
|
|
|
|
|
|
const analyticUnitObj = { |
|
|
|
const DEFAULT_ANALYTIC_UNIT_OBJECT = { |
|
|
|
_id: 'test', |
|
|
|
|
|
|
|
name: "test", |
|
|
|
name: "test", |
|
|
|
grafanaUrl: "http://127.0.0.1:3000", |
|
|
|
grafanaUrl: "http://127.0.0.1:3000", |
|
|
|
panelId: "ZLc0KfNZk/2", |
|
|
|
panelId: "ZLc0KfNZk/2", |
|
|
@ -65,30 +66,31 @@ describe('Check detection range', function() { |
|
|
|
collapsed: false, |
|
|
|
collapsed: false, |
|
|
|
createdAt: {"$$date":1564476040880}, |
|
|
|
createdAt: {"$$date":1564476040880}, |
|
|
|
updatedAt: {"$$date":1564476040880} |
|
|
|
updatedAt: {"$$date":1564476040880} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const WINDOW_SIZE = 10; |
|
|
|
const WINDOW_SIZE = 10; |
|
|
|
const TIME_STEP = 1000; |
|
|
|
const TIME_STEP = 1000; |
|
|
|
|
|
|
|
|
|
|
|
async function addTestUnitToDB(): Promise<string> { |
|
|
|
async function addTestUnitToDB(analyticUnitObj: any): Promise<string> { |
|
|
|
const analyticUnitId = await saveAnalyticUnitFromObject(analyticUnitObj); |
|
|
|
const analyticUnitId = await saveAnalyticUnitFromObject(analyticUnitObj); |
|
|
|
await AnalyticUnit.update(analyticUnitId, {lastDetectionTime: 1000}); |
|
|
|
await AnalyticUnit.update(analyticUnitId, { lastDetectionTime: 1000 }); |
|
|
|
await AnalyticUnitCache.create(analyticUnitId); |
|
|
|
await AnalyticUnitCache.create(analyticUnitId); |
|
|
|
await AnalyticUnitCache.setData(analyticUnitId, { |
|
|
|
await AnalyticUnitCache.setData(analyticUnitId, { |
|
|
|
windowSize: WINDOW_SIZE, |
|
|
|
windowSize: WINDOW_SIZE, |
|
|
|
timeStep: TIME_STEP |
|
|
|
timeStep: TIME_STEP |
|
|
|
}); |
|
|
|
}); |
|
|
|
return analyticUnitId; |
|
|
|
return analyticUnitId; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('Check detection range', function() { |
|
|
|
it('check range >= 2 * window size * timeStep', async () => { |
|
|
|
it('check range >= 2 * window size * timeStep', async () => { |
|
|
|
const from = 1500000000000; |
|
|
|
const from = 1500000000000; |
|
|
|
const to = 1500000000001; |
|
|
|
const to = 1500000000001; |
|
|
|
const expectedFrom = to - WINDOW_SIZE * TIME_STEP * 2; |
|
|
|
const expectedFrom = to - WINDOW_SIZE * TIME_STEP * 2; |
|
|
|
|
|
|
|
|
|
|
|
const id = await addTestUnitToDB(); |
|
|
|
const id = await addTestUnitToDB(DEFAULT_ANALYTIC_UNIT_OBJECT); |
|
|
|
await runDetect(id, from, to); |
|
|
|
await runDetect(id, from, to); |
|
|
|
expect(queryByMetric).toBeCalledWith(analyticUnitObj.metric, undefined, expectedFrom, to, HASTIC_API_KEY); |
|
|
|
expect(queryByMetric).toBeCalledWith(DEFAULT_ANALYTIC_UNIT_OBJECT.metric, undefined, expectedFrom, to, HASTIC_API_KEY); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -136,3 +138,15 @@ describe('onDetect', () => { |
|
|
|
expect(detectedRanges).toEqual([[7, 8]]); |
|
|
|
expect(detectedRanges).toEqual([[7, 8]]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('getHSR', function() { |
|
|
|
|
|
|
|
it('should return nothing if unit state is LEARNING', async () => { |
|
|
|
|
|
|
|
let unitObj = _.clone(DEFAULT_ANALYTIC_UNIT_OBJECT); |
|
|
|
|
|
|
|
unitObj.detectorType = 'anomaly'; |
|
|
|
|
|
|
|
const analyticUnitId = await addTestUnitToDB(unitObj); |
|
|
|
|
|
|
|
await AnalyticUnitCache.remove(analyticUnitId); |
|
|
|
|
|
|
|
const unit = await AnalyticUnit.findById(analyticUnitId); |
|
|
|
|
|
|
|
const result = await getHSR(unit, 9000, 100000); |
|
|
|
|
|
|
|
expect(result).toEqual({"hsr": {"columns": [], "values": []}}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|