|
|
@ -22,6 +22,8 @@ const SECONDS_IN_MINUTE = 60; |
|
|
|
|
|
|
|
|
|
|
|
type TaskResult = any; |
|
|
|
type TaskResult = any; |
|
|
|
type DetectionResult = any; |
|
|
|
type DetectionResult = any; |
|
|
|
|
|
|
|
// TODO: move type definitions somewhere
|
|
|
|
|
|
|
|
type TimeRange = { from: number, to: number }; |
|
|
|
export type TaskResolver = (taskResult: TaskResult) => void; |
|
|
|
export type TaskResolver = (taskResult: TaskResult) => void; |
|
|
|
|
|
|
|
|
|
|
|
const taskResolvers = new Map<AnalyticsTaskId, TaskResolver>(); |
|
|
|
const taskResolvers = new Map<AnalyticsTaskId, TaskResolver>(); |
|
|
@ -113,7 +115,7 @@ async function runTask(task: AnalyticsTask): Promise<TaskResult> { |
|
|
|
async function getQueryRange( |
|
|
|
async function getQueryRange( |
|
|
|
analyticUnitId: AnalyticUnit.AnalyticUnitId, |
|
|
|
analyticUnitId: AnalyticUnit.AnalyticUnitId, |
|
|
|
detectorType: AnalyticUnit.DetectorType |
|
|
|
detectorType: AnalyticUnit.DetectorType |
|
|
|
): Promise<{ from: number, to: number }> { |
|
|
|
): Promise<TimeRange> { |
|
|
|
if(detectorType === AnalyticUnit.DetectorType.PATTERN) { |
|
|
|
if(detectorType === AnalyticUnit.DetectorType.PATTERN) { |
|
|
|
// TODO: find labeled OR deleted segments to generate timerange
|
|
|
|
// TODO: find labeled OR deleted segments to generate timerange
|
|
|
|
const segments = await Segment.findMany(analyticUnitId, { labeled: true }); |
|
|
|
const segments = await Segment.findMany(analyticUnitId, { labeled: true }); |
|
|
@ -137,7 +139,7 @@ async function getQueryRange( |
|
|
|
|
|
|
|
|
|
|
|
async function query( |
|
|
|
async function query( |
|
|
|
analyticUnit: AnalyticUnit.AnalyticUnit, |
|
|
|
analyticUnit: AnalyticUnit.AnalyticUnit, |
|
|
|
range: { from: number, to: number } |
|
|
|
range: TimeRange |
|
|
|
) { |
|
|
|
) { |
|
|
|
console.log(`query time range: from ${new Date(range.from)} to ${new Date(range.to)}`); |
|
|
|
console.log(`query time range: from ${new Date(range.from)} to ${new Date(range.to)}`); |
|
|
|
|
|
|
|
|
|
|
@ -192,7 +194,7 @@ function getQueryRangeForLearningBySegments(segments: Segment.Segment[]) { |
|
|
|
return { from, to }; |
|
|
|
return { from, to }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runLearning(id: AnalyticUnit.AnalyticUnitId, from?: number, to?: number) { |
|
|
|
console.log('learning started...'); |
|
|
|
console.log('learning started...'); |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
@ -232,7 +234,12 @@ export async function runLearning(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const range = await getQueryRange(id, detector); |
|
|
|
let range: TimeRange; |
|
|
|
|
|
|
|
if(from !== undefined && to !== undefined) { |
|
|
|
|
|
|
|
range = { from, to }; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
range = await getQueryRange(id, detector); |
|
|
|
|
|
|
|
} |
|
|
|
taskPayload.data = await query(analyticUnit, range); |
|
|
|
taskPayload.data = await query(analyticUnit, range); |
|
|
|
|
|
|
|
|
|
|
|
let task = new AnalyticsTask( |
|
|
|
let task = new AnalyticsTask( |
|
|
@ -262,7 +269,7 @@ export async function runDetect(id: AnalyticUnit.AnalyticUnitId, from?: number, |
|
|
|
let analyticUnitType = unit.type; |
|
|
|
let analyticUnitType = unit.type; |
|
|
|
let detector = AnalyticUnit.getDetectorByType(analyticUnitType); |
|
|
|
let detector = AnalyticUnit.getDetectorByType(analyticUnitType); |
|
|
|
|
|
|
|
|
|
|
|
let range; |
|
|
|
let range: TimeRange; |
|
|
|
if(from !== undefined && to !== undefined) { |
|
|
|
if(from !== undefined && to !== undefined) { |
|
|
|
range = { from, to }; |
|
|
|
range = { from, to }; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -462,12 +469,20 @@ export async function updateSegments( |
|
|
|
return { addedIds }; |
|
|
|
return { addedIds }; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function runLearningWithDetection(id: AnalyticUnit.AnalyticUnitId) { |
|
|
|
export async function runLearningWithDetection( |
|
|
|
|
|
|
|
id: AnalyticUnit.AnalyticUnitId, |
|
|
|
|
|
|
|
from?: number, |
|
|
|
|
|
|
|
to?: number |
|
|
|
|
|
|
|
): Promise<void> { |
|
|
|
// TODO: move setting status somehow "inside" learning
|
|
|
|
// TODO: move setting status somehow "inside" learning
|
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING); |
|
|
|
await AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.PENDING); |
|
|
|
|
|
|
|
const foundSegments = await Segment.findMany(id, { labeled: false, deleted: false }); |
|
|
|
|
|
|
|
if(foundSegments !== null) { |
|
|
|
|
|
|
|
await Segment.removeSegments(foundSegments.map(segment => segment.id)); |
|
|
|
|
|
|
|
} |
|
|
|
await Detection.clearSpans(id); |
|
|
|
await Detection.clearSpans(id); |
|
|
|
runLearning(id) |
|
|
|
runLearning(id, from, to) |
|
|
|
.then(() => runDetect(id)) |
|
|
|
.then(() => runDetect(id, from, to)) |
|
|
|
.catch(err => console.error(err)); |
|
|
|
.catch(err => console.error(err)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|