|
|
@ -88,9 +88,6 @@ function getQueryRangeForLearningBySegments(segments: Segment.Segment[]) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
|
|
|
|
|
|
|
|
let previousLastPredictionTime: number = undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
let analyticUnit = await AnalyticUnit.findById(id); |
|
|
|
let analyticUnit = await AnalyticUnit.findById(id); |
|
|
@ -117,94 +114,101 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
oldCache = oldCache.data; |
|
|
|
oldCache = oldCache.data; |
|
|
|
} |
|
|
|
} |
|
|
|
let task = new AnalyticsTask( |
|
|
|
let task = new AnalyticsTask( |
|
|
|
id, AnalyticsTaskType.LEARN,
|
|
|
|
id, AnalyticsTaskType.LEARN, { pattern, segments: segmentObjs, data, cache: oldCache } |
|
|
|
{ pattern, segments: segmentObjs, data, cache: oldCache } |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); |
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING); |
|
|
|
let result = await runTask(task); |
|
|
|
let result = await runTask(task); |
|
|
|
let { lastPredictionTime, segments: predictedSegments, cache: newCache } = await processLearningResult(result); |
|
|
|
if(result.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) { |
|
|
|
AnalyticUnitCache.setData(id, newCache); |
|
|
|
throw new Error(result.error) |
|
|
|
previousLastPredictionTime = analyticUnit.lastPredictionTime; |
|
|
|
} |
|
|
|
|
|
|
|
AnalyticUnitCache.setData(id, result.payload.cache); |
|
|
|
await Promise.all([ |
|
|
|
|
|
|
|
Segment.insertSegments(predictedSegments), |
|
|
|
|
|
|
|
AnalyticUnit.setPredictionTime(id, lastPredictionTime) |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); |
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
let message = err.message || JSON.stringify(err); |
|
|
|
let message = err.message || JSON.stringify(err); |
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); |
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); |
|
|
|
if(previousLastPredictionTime !== undefined) { |
|
|
|
|
|
|
|
await AnalyticUnit.setPredictionTime(id, previousLastPredictionTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function processLearningResult(taskResult: any): Promise<{ |
|
|
|
function processPredictionResult(analyticUnitId: AnalyticUnit.AnalyticUnitId, taskResult: any): { |
|
|
|
lastPredictionTime: number, |
|
|
|
lastPredictionTime: number, |
|
|
|
segments: Segment.Segment[], |
|
|
|
segments: Segment.Segment[], |
|
|
|
cache: any |
|
|
|
cache: any |
|
|
|
}> { |
|
|
|
} { |
|
|
|
if(taskResult.status !== AnalyticUnit.AnalyticUnitStatus.SUCCESS) { |
|
|
|
let payload = taskResult.payload; |
|
|
|
return Promise.reject(taskResult.error); |
|
|
|
if(payload === undefined) { |
|
|
|
|
|
|
|
throw new Error(`Missing payload in result: ${taskResult}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if(payload.segments === undefined || !Array.isArray(payload.segments)) { |
|
|
|
|
|
|
|
throw new Error(`Missing segments in result or it is corrupted: ${JSON.stringify(payload)}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if(payload.lastPredictionTime === undefined || isNaN(+payload.lastPredictionTime)) { |
|
|
|
|
|
|
|
throw new Error( |
|
|
|
|
|
|
|
`Missing lastPredictionTime is result or it is corrupted: ${JSON.stringify(payload)}` |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
console.log(taskResult) |
|
|
|
|
|
|
|
// if(taskResult.segments === undefined || !Array.isArray(taskResult.segments)) {
|
|
|
|
let segments = payload.segments.map(segment => new Segment.Segment(analyticUnitId, segment.from, segment.to, false)); |
|
|
|
// throw new Error('Missing segments in result or it is corrupted: ' + taskResult);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if(taskResult.lastPredictionTime === undefined || isNaN(+taskResult.lastPredictionTime)) {
|
|
|
|
|
|
|
|
// throw new Error(
|
|
|
|
|
|
|
|
// 'Missing lastPredictionTime is result or it is corrupted: ' + taskResult.lastPredictionTime
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
lastPredictionTime: 0, |
|
|
|
lastPredictionTime: payload.lastPredictionTime, |
|
|
|
segments: [], |
|
|
|
segments: segments, |
|
|
|
cache: {} |
|
|
|
cache: {} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
let unit = await AnalyticUnit.findById(id); |
|
|
|
let previousLastPredictionTime: number = undefined; |
|
|
|
let pattern = unit.type; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let segments = await Segment.findMany(id, { labeled: true }); |
|
|
|
try { |
|
|
|
if (segments.length < 2) { |
|
|
|
let unit = await AnalyticUnit.findById(id); |
|
|
|
throw new Error('Need at least 2 labeled segments'); |
|
|
|
previousLastPredictionTime = unit.lastPredictionTime; |
|
|
|
} |
|
|
|
let pattern = unit.type; |
|
|
|
|
|
|
|
|
|
|
|
let { from, to } = getQueryRangeForLearningBySegments(segments); |
|
|
|
let segments = await Segment.findMany(id, { labeled: true }); |
|
|
|
let data = await queryByMetric(unit.metric, unit.panelUrl, from, to); |
|
|
|
if (segments.length < 2) { |
|
|
|
if (data.length === 0) { |
|
|
|
throw new Error('Need at least 2 labeled segments'); |
|
|
|
throw new Error('Empty data to predict on'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let task = new AnalyticsTask( |
|
|
|
let { from, to } = getQueryRangeForLearningBySegments(segments); |
|
|
|
id, |
|
|
|
let data = await queryByMetric(unit.metric, unit.panelUrl, from, to); |
|
|
|
AnalyticsTaskType.PREDICT, |
|
|
|
if (data.length === 0) { |
|
|
|
{ pattern, lastPredictionTime: unit.lastPredictionTime, data } |
|
|
|
throw new Error('Empty data to predict on'); |
|
|
|
); |
|
|
|
|
|
|
|
let result = await runTask(task); |
|
|
|
|
|
|
|
if(result.status === AnalyticUnit.AnalyticUnitStatus.FAILED) { |
|
|
|
|
|
|
|
return []; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Merging segments
|
|
|
|
|
|
|
|
if(segments.length > 0 && result.segments.length > 0) { |
|
|
|
|
|
|
|
let lastOldSegment = segments[segments.length - 1]; |
|
|
|
|
|
|
|
let firstNewSegment = result.segments[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(firstNewSegment.from <= lastOldSegment.to) { |
|
|
|
|
|
|
|
result.segments[0].from = lastOldSegment.from; |
|
|
|
|
|
|
|
Segment.removeSegments([lastOldSegment.id]) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Segment.insertSegments(result.segments); |
|
|
|
let task = new AnalyticsTask( |
|
|
|
AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); |
|
|
|
id, |
|
|
|
return result.segments; |
|
|
|
AnalyticsTaskType.PREDICT, |
|
|
|
|
|
|
|
{ pattern, lastPredictionTime: unit.lastPredictionTime, data, cache: {} } |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
let result = await runTask(task); |
|
|
|
|
|
|
|
if(result.status === AnalyticUnit.AnalyticUnitStatus.FAILED) { |
|
|
|
|
|
|
|
return []; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let payload = processPredictionResult(id, result); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Merging segments
|
|
|
|
|
|
|
|
if(segments.length > 0 && payload.segments.length > 0) { |
|
|
|
|
|
|
|
let lastOldSegment = segments[segments.length - 1]; |
|
|
|
|
|
|
|
let firstNewSegment = payload.segments[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(firstNewSegment.from <= lastOldSegment.to) { |
|
|
|
|
|
|
|
payload.segments[0].from = lastOldSegment.from; |
|
|
|
|
|
|
|
Segment.removeSegments([lastOldSegment.id]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Segment.insertSegments(payload.segments); |
|
|
|
|
|
|
|
AnalyticUnit.setPredictionTime(id, payload.lastPredictionTime); |
|
|
|
|
|
|
|
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY); |
|
|
|
|
|
|
|
} catch(err) { |
|
|
|
|
|
|
|
let message = err.message || JSON.stringify(err); |
|
|
|
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, message); |
|
|
|
|
|
|
|
if(previousLastPredictionTime !== undefined) { |
|
|
|
|
|
|
|
await AnalyticUnit.setPredictionTime(id, previousLastPredictionTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function isAnalyticReady(): boolean { |
|
|
|
export function isAnalyticReady(): boolean { |
|
|
|