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.

184 lines
5.3 KiB

6 years ago
import { SegmentsSet } from './segment_set';
import { SegmentArray } from './segment_array';
6 years ago
import { Segment, SegmentId } from './segment';
6 years ago
import { ANALYTIC_UNIT_COLORS } from '../colors';
6 years ago
import _ from 'lodash';
5 years ago
export enum LabelingMode {
LABELING = 'LABELING',
UNLABELING = 'UNLABELING',
DELETING = 'DELETING',
NOT_IN_LABELING_MODE = 'NOT_IN_LABELING_MODE'
};
export type AnalyticSegmentPair = { analyticUnit: AnalyticUnit, segment: AnalyticSegment };
export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => AnalyticSegmentPair[];
6 years ago
export type AnalyticUnitId = string;
6 years ago
export class AnalyticSegment extends Segment {
constructor(public labeled: boolean, id: SegmentId, from: number, to: number, public deleted = false) {
super(id, from, to);
6 years ago
if(!_.isBoolean(labeled)) {
throw new Error('labeled value is not boolean');
}
}
}
export class AnalyticUnit {
6 years ago
5 years ago
private _labelingMode: LabelingMode = LabelingMode.LABELING;
6 years ago
private _selected: boolean = false;
private _saving: boolean = false;
private _segmentSet = new SegmentArray<AnalyticSegment>();
6 years ago
private _status: string;
private _error: string;
6 years ago
constructor(private _panelObject?: any) {
6 years ago
if(_panelObject === undefined) {
this._panelObject = {};
}
_.defaults(this._panelObject, {
name: 'AnalyticUnitName',
labeledColor: ANALYTIC_UNIT_COLORS[0],
deletedColor: '#00f0ff',
detectorType: 'pattern',
type: 'GENERAL',
alert: false
6 years ago
});
}
6 years ago
get id(): AnalyticUnitId { return this._panelObject.id; }
6 years ago
set id(value: AnalyticUnitId) { this._panelObject.id = value; }
6 years ago
set name(value: string) { this._panelObject.name = value; }
get name(): string { return this._panelObject.name; }
set detectorType(value: string) { this._panelObject.detectorType = value; }
get detectorType(): string { return this._panelObject.detectorType; }
6 years ago
set type(value: string) { this._panelObject.type = value; }
get type(): string { return this._panelObject.type; }
6 years ago
set confidence(value: number) { this._panelObject.confidence = value; }
get confidence(): number { return this._panelObject.confidence; }
set labeledColor(value: string) { this._panelObject.labeledColor = value; }
get labeledColor(): string { return this._panelObject.labeledColor; }
set deletedColor(value: string) { this._panelObject.deletedColor = value; }
get deletedColor(): string { return this._panelObject.deletedColor; }
6 years ago
set alert(value: boolean) { this._panelObject.alert = value; }
get alert(): boolean { return this._panelObject.alert; }
6 years ago
get selected(): boolean { return this._selected; }
set selected(value: boolean) { this._selected = value; }
5 years ago
get labelingMode(): LabelingMode { return this._labelingMode; }
set labelingMode(value: LabelingMode) { this._labelingMode = value; }
6 years ago
get saving(): boolean { return this._saving; }
set saving(value: boolean) { this._saving = value; }
6 years ago
get visible(): boolean {
6 years ago
return (this._panelObject.visible === undefined) ? true : this._panelObject.visible
}
set visible(value: boolean) {
this._panelObject.visible = value;
}
addLabeledSegment(segment: Segment, deleted: boolean): AnalyticSegment {
5 years ago
const asegment = new AnalyticSegment(!deleted, segment.id, segment.from, segment.to, deleted);
6 years ago
this._segmentSet.addSegment(asegment);
return asegment;
}
removeSegmentsInRange(from: number, to: number): AnalyticSegment[] {
let deletedSegments = this._segmentSet.removeInRange(from, to);
return deletedSegments;
6 years ago
}
get segments(): SegmentsSet<AnalyticSegment> { return this._segmentSet; }
set segments(value: SegmentsSet<AnalyticSegment>) {
6 years ago
this._segmentSet.setSegments(value.getSegments());
}
6 years ago
6 years ago
get status() { return this._status; }
set status(value) {
if(
value !== '404' &&
value !== 'READY' &&
value !== 'LEARNING' &&
value !== 'PENDING' &&
value !== 'FAILED'
6 years ago
) {
throw new Error('Unsupported status value: ' + value);
}
this._status = value;
}
get error() { return this._error; }
set error(value) { this._error = value; }
6 years ago
get isActiveStatus() {
switch(this.status) {
case '404':
case 'READY':
case 'FAILED':
return false;
}
return true;
6 years ago
}
get panelObject() { return this._panelObject; }
}
export class AnalyticUnitsSet {
6 years ago
6 years ago
private _mapIdIndex: Map<AnalyticUnitId, number>;
private _items: AnalyticUnit[];
6 years ago
constructor(private _panelObject: any[]) {
if(_panelObject === undefined) {
throw new Error('panel object can`t be undefined');
}
6 years ago
this._mapIdIndex = new Map<AnalyticUnitId, number>();
this._items = _panelObject.map(p => new AnalyticUnit(p));
6 years ago
this._rebuildIndex();
}
get items() { return this._items; }
6 years ago
6 years ago
addItem(item: AnalyticUnit) {
this._panelObject.push(item.panelObject);
this._mapIdIndex[item.id] = this._items.length;
6 years ago
this._items.push(item);
6 years ago
}
6 years ago
removeItem(id: AnalyticUnitId) {
var index = this._mapIdIndex[id];
6 years ago
this._panelObject.splice(index, 1);
this._items.splice(index, 1);
6 years ago
this._rebuildIndex();
}
_rebuildIndex() {
this._items.forEach((a, i) => {
6 years ago
this._mapIdIndex[a.id] = i;
6 years ago
});
}
6 years ago
byId(id: AnalyticUnitId): AnalyticUnit {
return this._items[this._mapIdIndex[id]];
6 years ago
}
byIndex(index: number): AnalyticUnit {
return this._items[index];
6 years ago
}
}