Browse Source

Merge branch 'NeDB-instead-of-files-for-analytics-#58'

pull/1/head
Coin de Gamma 6 years ago
parent
commit
d1ec0b13e1
  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 { 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 * as AnalyticUnit from '../models/analytic_unit_model';
import { AnalyticsService, AnalyticsMessage } from '../services/analytics_service'; 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) { export async function runLearning(id: AnalyticUnit.AnalyticUnitId) {
// let segments = getLabeledSegments(id); let segments = Segments.findMany(id, { labeled: true });
// AnalyticUnit.setStatus(id, 'LEARNING'); AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.LEARNING);
// let unit = await AnalyticUnit.findById(id); let unit = await AnalyticUnit.findById(id);
// let pattern = unit.type; let pattern = unit.type;
// let task = { let task = {
// analyticUnitId: id, analyticUnitId: id,
// type: 'LEARN', type: 'LEARN',
// pattern, pattern,
// segments: segments segments: segments
// }; };
// let result = await runTask(task); let result = await runTask(task);
// if (result.status === 'SUCCESS') { if (result.status === 'SUCCESS') {
// AnalyticUnit.setStatus(id, 'READY'); AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.READY);
// insertSegments(id, result.segments, false); insertSegments(id, result.segments, false);
// AnalyticUnit.setPredictionTime(id, result.lastPredictionTime); AnalyticUnit.setPredictionTime(id, result.lastPredictionTime);
// } else { } else {
// AnalyticUnit.setStatus(id, 'FAILED', result.error); AnalyticUnit.setStatus(id, AnalyticUnit.AnalyticUnitStatus.FAILED, result.error);
// } }
} }
export async function runPredict(id: AnalyticUnit.AnalyticUnitId) { 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); let db = makeDBQ(Collection.ANALYTIC_UNITS);
export type AnalyticUnitId = string; export type AnalyticUnitId = string;
export enum AnalyticUnitStatus {
LEARNING = 'LEARNING',
SUCCESS = 'SUCCESS',
READY = 'READY',
FAILED = 'FAILED'
}
export class AnalyticUnit { export class AnalyticUnit {
constructor( constructor(
@ -15,7 +22,7 @@ export class AnalyticUnit {
public metric: Metric, public metric: Metric,
public id?: AnalyticUnitId, public id?: AnalyticUnitId,
public lastPredictionTime?: number, public lastPredictionTime?: number,
public status?: string, public status?: AnalyticUnitStatus,
public error?: string, public error?: string,
) { ) {
if(name === undefined) { if(name === undefined) {
@ -54,7 +61,7 @@ export class AnalyticUnit {
obj.panelUrl, obj.panelUrl,
obj.type, obj.type,
Metric.fromObject(obj.metric), Metric.fromObject(obj.metric),
obj.status, obj.status as AnalyticUnitStatus,
obj.lastPredictionTime, obj.lastPredictionTime,
obj._id, obj._id,
obj.error, obj.error,

6
server/src/models/segment_model.ts

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

6
server/src/models/task_model.ts

@ -2,15 +2,20 @@ import { AnalyticUnitId } from "./analytic_unit_model";
export type TaskId = string; export type TaskId = string;
export enum TaskType { LEARN = 'LEARN' };
export class Task { export class Task {
constructor( constructor(
public analyticUnitId: AnalyticUnitId, public analyticUnitId: AnalyticUnitId,
public type: TaskType,
public id?: TaskId public id?: TaskId
) { ) {
if(analyticUnitId === undefined) { if(analyticUnitId === undefined) {
throw new Error('analyticUnitId is undefined'); throw new Error('analyticUnitId is undefined');
} }
if(type === undefined || type === null) {
throw new Error('type is undefined or null');
}
} }
public toObject() { public toObject() {
@ -26,6 +31,7 @@ export class Task {
} }
return new Task( return new Task(
obj.analyticUnitId, obj.analyticUnitId,
obj.type as TaskType,
obj._id, 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 { export class AnalyticsService {
@ -194,7 +191,6 @@ export class AnalyticsService {
return; return;
} }
let text = data.toString(); let text = data.toString();
let response; let response;
try { try {

Loading…
Cancel
Save