You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
import { AnalyticUnitId } from './analytic_units'; |
|
|
|
import { uid } from "../utils/uid"; |
|
|
|
|
|
const UID_LENGTH = 16; |
|
|
|
export type AnalyticsTaskId = string; |
|
export enum AnalyticsTaskType { |
|
LEARN = 'LEARN', |
|
DETECT = 'DETECT', |
|
CANCEL = 'CANCEL', |
|
PUSH = 'PUSH', |
|
PROCESS = 'PROCESS' |
|
}; |
|
|
|
export class AnalyticsTask { |
|
|
|
constructor( |
|
public analyticUnitId: AnalyticUnitId, |
|
public type: AnalyticsTaskType, |
|
public payload?: any, |
|
private _id?: AnalyticsTaskId |
|
) { |
|
if(analyticUnitId === undefined) { |
|
throw new Error('analyticUnitId is undefined'); |
|
} |
|
if(type === undefined || type === null) { |
|
throw new Error('type is undefined or null'); |
|
} |
|
|
|
} |
|
|
|
public get id(): AnalyticsTaskId { |
|
if(this._id === undefined) { |
|
this._id = uid(UID_LENGTH); |
|
} |
|
return this._id; |
|
} |
|
|
|
public toObject() { |
|
return { |
|
_id: this.id, |
|
analyticUnitId: this.analyticUnitId, |
|
type: this.type, |
|
payload: this.payload |
|
}; |
|
} |
|
|
|
static fromObject(obj: any): AnalyticsTask { |
|
if(obj === undefined) { |
|
throw new Error('obj is undefined'); |
|
} |
|
return new AnalyticsTask( |
|
obj.analyticUnitId, |
|
obj.type as AnalyticsTaskType, |
|
obj.payload, |
|
obj._id, |
|
); |
|
} |
|
}
|
|
|