|
|
|
@ -22,6 +22,8 @@ const SECONDS_IN_MINUTE = 60;
|
|
|
|
|
|
|
|
|
|
type TaskResult = any; |
|
|
|
|
type DetectionResult = any; |
|
|
|
|
// TODO: move type definitions somewhere
|
|
|
|
|
type TimeRange = { from: number, to: number }; |
|
|
|
|
export type TaskResolver = (taskResult: TaskResult) => void; |
|
|
|
|
|
|
|
|
|
const taskResolvers = new Map<AnalyticsTaskId, TaskResolver>(); |
|
|
|
@ -113,7 +115,7 @@ async function runTask(task: AnalyticsTask): Promise<TaskResult> {
|
|
|
|
|
async function getQueryRange( |
|
|
|
|
analyticUnitId: AnalyticUnit.AnalyticUnitId, |
|
|
|
|
detectorType: AnalyticUnit.DetectorType |
|
|
|
|
): Promise<{ from: number, to: number }> { |
|
|
|
|
): Promise<TimeRange> { |
|
|
|
|
if(detectorType === AnalyticUnit.DetectorType.PATTERN) { |
|
|
|
|
// TODO: find labeled OR deleted segments to generate timerange
|
|
|
|
|
const segments = await Segment.findMany(analyticUnitId, { labeled: true }); |
|
|
|
@ -137,7 +139,7 @@ async function getQueryRange(
|
|
|
|
|
|
|
|
|
|
async function query( |
|
|
|
|
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)}`); |
|
|
|
|
|
|
|
|
@ -192,7 +194,7 @@ function getQueryRangeForLearningBySegments(segments: Segment.Segment[]) {
|
|
|
|
|
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...'); |
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
let task = new AnalyticsTask( |
|
|
|
@ -262,7 +269,7 @@ export async function runDetect(id: AnalyticUnit.AnalyticUnitId, from?: number,
|
|
|
|
|
let analyticUnitType = unit.type; |
|
|
|
|
let detector = AnalyticUnit.getDetectorByType(analyticUnitType); |
|
|
|
|
|
|
|
|
|
let range; |
|
|
|
|
let range: TimeRange; |
|
|
|
|
if(from !== undefined && to !== undefined) { |
|
|
|
|
range = { from, to }; |
|
|
|
|
} else { |
|
|
|
@ -462,12 +469,20 @@ export async function updateSegments(
|
|
|
|
|
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
|
|
|
|
|
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); |
|
|
|
|
runLearning(id) |
|
|
|
|
.then(() => runDetect(id)) |
|
|
|
|
runLearning(id, from, to) |
|
|
|
|
.then(() => runDetect(id, from, to)) |
|
|
|
|
.catch(err => console.error(err)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|