Browse Source

Task definition + some refactorings

pull/1/head
Coin de Gamma 6 years ago
parent
commit
b2c48242c9
  1. 42
      server/src/controllers/analytics_controller.ts
  2. 11
      server/src/models/analytic_unit_model.ts
  3. 6
      server/src/models/segment_model.ts
  4. 6
      server/src/models/task_model.ts
  5. 4
      server/src/services/analytics_service.ts

42
server/src/controllers/analytics_controller.ts

@ -1,5 +1,5 @@
import { Task } from '../models/task_model';
import * as SegmentsController from '../models/segment_model';
import * as Segments from '../models/segment_model';
import * as AnalyticUnit from '../models/analytic_unit_model';
import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service';
@ -66,26 +66,26 @@ async function runTask(task: Task): Promise<any> {
}
export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
// let segments = getLabeledSegments(id);
// AnalyticUnit.setStatus(id, 'LEARNING');
// let unit = await AnalyticUnit.findById(id);
// let pattern = unit.type;
// let task = {
// analyticUnitId: id,
// type: 'LEARN',
// pattern,
// segments: segments
// };
// let result = await runTask(task);
// if (result.status === 'SUCCESS') {
// AnalyticUnit.setStatus(id, 'READY');
// insertSegments(id, result.segments, false);
// AnalyticUnit.setPredictionTime(id, result.lastPredictionTime);
// } else {
// AnalyticUnit.setStatus(id, 'FAILED', result.error);
// }
let segments = Segments.findMany(id, { labeled: true });
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING);
let unit = await AnalyticUnit.findById(id);
let pattern = unit.type;
let task = {
analyticUnitId: id,
type: 'LEARN',
pattern,
segments: segments
};
let result = await runTask(task);
if (result.status === 'SUCCESS') {
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY);
insertSegments(id, result.segments, false);
AnalyticUnit.setPredictionTime(id, result.lastPredictionTime);
} else {
AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, result.error);
}
}
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) {

11
server/src/models/analytic_unit_model.ts

@ -5,7 +5,14 @@ import { Collection, makeDBQ } from '../services/data_service';
let db = makeDBQ(Collection.ANALYTIC_UNITS);
export type AnalyticUnitId = string;
export enum AnalyticUnitStatus {
LEARNING = 'LEARNING',
SUCCESS = 'SUCCESS',
READY = 'READY',
FAILED = 'FAILED'
}
export class AnalyticUnit {
constructor(
@ -15,7 +22,7 @@ export class AnalyticUnit {
public metric: Metric,
public id?: AnalyticUnitId,
public lastPredictionTime?: number,
public status?: string,
public status?: AnalyticUnitStatus,
public error?: string,
) {
if(name === undefined) {
@ -54,7 +61,7 @@ export class AnalyticUnit {
obj.panelUrl,
obj.type,
Metric.fromObject(obj.metric),
obj.status,
obj.status as AnalyticUnitStatus,
obj.lastPredictionTime,
obj._id,
obj.error,

6
server/src/models/segment_model.ts

@ -60,7 +60,8 @@ export class Segment {
export type FindManyQuery = {
timeFromGTE?: number,
timeToLTE?: number,
intexGT?: number
intexGT?: number,
labeled?: boolean
}
export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promise<Segment[]> {
@ -71,6 +72,9 @@ export async function findMany(id: AnalyticUnitId, query: FindManyQuery): Promis
if(query.timeToLTE !== undefined) {
dbQuery.to = { $lte: query.timeToLTE };
}
if(query.labeled !== undefined) {
dbQuery.labeled = query.labeled;
}
let segs = await db.findMany(dbQuery);
if(segs === null) {
return [];

6
server/src/models/task_model.ts

@ -2,15 +2,20 @@ import { AnalyticUnitId } from "./analytic_unit_model";
export type TaskId = string;
export enum TaskType { LEARN = 'LEARN' };
export class Task {
constructor(
public analyticUnitId: AnalyticUnitId,
public type: TaskType,
public id?: TaskId
) {
if(analyticUnitId === undefined) {
throw new Error('analyticUnitId is undefined');
}
if(type === undefined || type === null) {
throw new Error('type is undefined or null');
}
}
public toObject() {
@ -26,6 +31,7 @@ export class Task {
}
return new Task(
obj.analyticUnitId,
obj.type as TaskType,
obj._id,
);
}

4
server/src/services/analytics_service.ts

@ -20,9 +20,6 @@ export class AnalyticsMessage {
}
}
function analyticsMessageFromJson(obj: any): AnalyticsMessage {
return new AnalyticsMessage(obj);
}
export class AnalyticsService {
@ -194,7 +191,6 @@ export class AnalyticsService {
return;
}
let text = data.toString();
let response;
try {

Loading…
Cancel
Save