@ -4,7 +4,8 @@ import { AnalyticService } from '../services/analytic_service'
import {
import {
AnalyticUnitId , AnalyticUnit ,
AnalyticUnitId , AnalyticUnit ,
AnalyticUnitsSet , AnalyticSegment , AnalyticSegmentsSearcher , AnalyticSegmentPair
AnalyticUnitsSet , AnalyticSegment , AnalyticSegmentsSearcher , AnalyticSegmentPair ,
LabelingMode
} from '../models/analytic_unit' ;
} from '../models/analytic_unit' ;
import { MetricExpanded } from '../models/metric' ;
import { MetricExpanded } from '../models/metric' ;
import { DatasourceRequest } from '../models/datasource' ;
import { DatasourceRequest } from '../models/datasource' ;
@ -13,30 +14,29 @@ import { SegmentsSet } from '../models/segment_set';
import { SegmentArray } from '../models/segment_array' ;
import { SegmentArray } from '../models/segment_array' ;
import { ServerInfo } from '../models/info' ;
import { ServerInfo } from '../models/info' ;
import { Threshold , Condition } from '../models/threshold' ;
import { Threshold , Condition } from '../models/threshold' ;
import text from '../partials/help_section.html' ;
import { ANALYTIC_UNIT_COLORS } from '../colors' ;
import {
ANALYTIC_UNIT_COLORS ,
LABELED_SEGMENT_BORDER_COLOR ,
DELETED_SEGMENT_BORDER_COLOR ,
SEGMENT_FILL_ALPHA ,
SEGMENT_STROKE_ALPHA ,
LABELING_MODE_ALPHA
} from '../colors' ;
import { Emitter } from 'grafana/app/core/utils/emitter' ;
import { Emitter } from 'grafana/app/core/utils/emitter' ;
import _ from 'lodash' ;
import _ from 'lodash' ;
import * as tinycolor from 'tinycolor2' ;
import * as tinycolor from 'tinycolor2' ;
export const REGION_FILL_ALPHA = 0.7 ;
export const REGION_STROKE_ALPHA = 0.9 ;
export const REGION_DELETE_COLOR_LIGHT = '#d1d1d1' ;
export const REGION_DELETE_COLOR_DARK = 'white' ;
const LABELED_SEGMENT_BORDER_COLOR = 'black' ;
const DELETED_SEGMENT_FILL_COLOR = 'black' ;
const DELETED_SEGMENT_BORDER_COLOR = 'black' ;
export class AnalyticController {
export class AnalyticController {
private _analyticUnitsSet : AnalyticUnitsSet ;
private _analyticUnitsSet : AnalyticUnitsSet ;
private _selectedAnalyticUnitId : AnalyticUnitId = null ;
private _selectedAnalyticUnitId : AnalyticUnitId = null ;
private _labelingDataAddedSegments : SegmentsSet < AnalyticSegment > ;
private _labelingDataAddedSegments : SegmentsSet < AnalyticSegment > ;
private _labelingDataDelet edSegments : SegmentsSet < AnalyticSegment > ;
private _labelingDataRemovedSegments : SegmentsSet < AnalyticSegment > ;
private _newAnalyticUnit : AnalyticUnit = null ;
private _newAnalyticUnit : AnalyticUnit = null ;
private _creatingNewAnalyticType : boolean = false ;
private _creatingNewAnalyticType : boolean = false ;
private _savingNewAnalyticUnit : boolean = false ;
private _savingNewAnalyticUnit : boolean = false ;
@ -53,7 +53,7 @@ export class AnalyticController {
_panelObject . analyticUnits = _panelObject . anomalyTypes || [ ] ;
_panelObject . analyticUnits = _panelObject . anomalyTypes || [ ] ;
}
}
this . _labelingDataAddedSegments = new SegmentArray < AnalyticSegment > ( ) ;
this . _labelingDataAddedSegments = new SegmentArray < AnalyticSegment > ( ) ;
this . _labelingDataDelet edSegments = new SegmentArray < AnalyticSegment > ( ) ;
this . _labelingDataRemov edSegments = new SegmentArray < AnalyticSegment > ( ) ;
this . _analyticUnitsSet = new AnalyticUnitsSet ( this . _panelObject . analyticUnits ) ;
this . _analyticUnitsSet = new AnalyticUnitsSet ( this . _panelObject . analyticUnits ) ;
this . _thresholds = [ ] ;
this . _thresholds = [ ] ;
this . updateThresholds ( ) ;
this . updateThresholds ( ) ;
@ -61,6 +61,8 @@ export class AnalyticController {
// this.analyticUnits.forEach(a => this.runEnabledWaiter(a));
// this.analyticUnits.forEach(a => this.runEnabledWaiter(a));
}
}
get helpSectionText() { return text ; }
getSegmentsSearcher ( ) : AnalyticSegmentsSearcher {
getSegmentsSearcher ( ) : AnalyticSegmentsSearcher {
return this . _segmentsSearcher . bind ( this ) ;
return this . _segmentsSearcher . bind ( this ) ;
}
}
@ -78,6 +80,7 @@ export class AnalyticController {
async sendThresholdParamsToServer ( id : AnalyticUnitId ) {
async sendThresholdParamsToServer ( id : AnalyticUnitId ) {
await this . saveThreshold ( id ) ;
await this . saveThreshold ( id ) ;
await this . _analyticService . runDetect ( id ) ;
await this . _runStatusWaiter ( this . _analyticUnitsSet . byId ( id ) ) ;
await this . _runStatusWaiter ( this . _analyticUnitsSet . byId ( id ) ) ;
}
}
@ -86,11 +89,11 @@ export class AnalyticController {
this . _creatingNewAnalyticType = true ;
this . _creatingNewAnalyticType = true ;
this . _savingNewAnalyticUnit = false ;
this . _savingNewAnalyticUnit = false ;
if ( this . analyticUnits . length === 0 ) {
if ( this . analyticUnits . length === 0 ) {
this . _newAnalyticUnit . c olor = ANALYTIC_UNIT_COLORS [ 0 ] ;
this . _newAnalyticUnit . labeledC olor = ANALYTIC_UNIT_COLORS [ 0 ] ;
} else {
} else {
let colorIndex = ANALYTIC_UNIT_COLORS . indexOf ( _ . last ( this . analyticUnits ) . c olor) + 1 ;
let colorIndex = ANALYTIC_UNIT_COLORS . indexOf ( _ . last ( this . analyticUnits ) . labeledC olor) + 1 ;
colorIndex %= ANALYTIC_UNIT_COLORS . length ;
colorIndex %= ANALYTIC_UNIT_COLORS . length ;
this . _newAnalyticUnit . c olor = ANALYTIC_UNIT_COLORS [ colorIndex ] ;
this . _newAnalyticUnit . labeledC olor = ANALYTIC_UNIT_COLORS [ colorIndex ] ;
}
}
}
}
@ -105,9 +108,10 @@ export class AnalyticController {
this . _analyticUnitsSet . addItem ( this . _newAnalyticUnit ) ;
this . _analyticUnitsSet . addItem ( this . _newAnalyticUnit ) ;
this . _creatingNewAnalyticType = false ;
this . _creatingNewAnalyticType = false ;
this . _savingNewAnalyticUnit = false ;
this . _savingNewAnalyticUnit = false ;
// this.runEnabledWaiter(this._newAnalyticUnit);
if ( this . _newAnalyticUnit . detectorType !== 'threshold' ) {
this . _runStatusWaiter ( this . _newAnalyticUnit ) ;
this . _runStatusWaiter ( this . _newAnalyticUnit ) ;
}
}
}
get creatingNew() { return this . _creatingNewAnalyticType ; }
get creatingNew() { return this . _creatingNewAnalyticType ; }
get saving() { return this . _savingNewAnalyticUnit ; }
get saving() { return this . _savingNewAnalyticUnit ; }
@ -136,6 +140,7 @@ export class AnalyticController {
await this . disableLabeling ( ) ;
await this . disableLabeling ( ) ;
this . _selectedAnalyticUnitId = id ;
this . _selectedAnalyticUnitId = id ;
this . labelingUnit . selected = true ;
this . labelingUnit . selected = true ;
this . toggleLabelingMode ( LabelingMode . LABELING ) ;
this . toggleVisibility ( id , true ) ;
this . toggleVisibility ( id , true ) ;
}
}
@ -160,33 +165,37 @@ export class AnalyticController {
this . _labelingDataAddedSegments . getSegments ( ) . forEach ( s = > {
this . _labelingDataAddedSegments . getSegments ( ) . forEach ( s = > {
this . labelingUnit . segments . remove ( s . id ) ;
this . labelingUnit . segments . remove ( s . id ) ;
} ) ;
} ) ;
this . _labelingDataDelet edSegments . getSegments ( ) . forEach ( s = > {
this . _labelingDataRemov edSegments . getSegments ( ) . forEach ( s = > {
s . deleted = false ;
this . labelingUnit . segments . addSegment ( s ) ;
} ) ;
} ) ;
this . dropLabeling ( ) ;
this . dropLabeling ( ) ;
}
}
dropLabeling() {
dropLabeling() {
this . _labelingDataAddedSegments . clear ( ) ;
this . _labelingDataAddedSegments . clear ( ) ;
this . _labelingDataDelet edSegments . clear ( ) ;
this . _labelingDataRemov edSegments . clear ( ) ;
this . labelingUnit . selected = false ;
this . labelingUnit . selected = false ;
this . _selectedAnalyticUnitId = null ;
this . _selectedAnalyticUnitId = null ;
this . _tempIdCounted = - 1 ;
this . _tempIdCounted = - 1 ;
}
}
get l abelingMode( ) : boolean {
get inL abelingMode( ) : boolean {
return this . _selectedAnalyticUnitId !== null ;
return this . _selectedAnalyticUnitId !== null ;
}
}
get labelingDeleteMode ( ) : boolean {
get labelingMode ( ) : LabelingMode {
if ( ! this . labelingMode ) {
if ( ! this . inLabelingMode ) {
return false ;
return LabelingMode . NOT_IN_LABELING_MODE ;
}
return this . labelingUnit . labelingMode ;
}
}
return this . labelingUnit . deleteMode ;
set labelingMode ( labelingMode : LabelingMode ) {
this . labelingUnit . labelingMode = labelingMode ;
}
}
addLabelSegment ( segment : Segment , deleted? : boolean ) {
addLabelSegment ( segment : Segment , deleted = false ) {
var asegment = this . labelingUnit . addLabeledSegment ( segment , deleted ) ;
const asegment = this . labelingUnit . addLabeledSegment ( segment , deleted ) ;
this . _labelingDataAddedSegments . addSegment ( asegment ) ;
this . _labelingDataAddedSegments . addSegment ( asegment ) ;
}
}
@ -194,11 +203,15 @@ export class AnalyticController {
return this . _analyticUnitsSet . items ;
return this . _analyticUnitsSet . items ;
}
}
onAnalyticUnitColorChange ( id : AnalyticUnitId , value : string ) {
onAnalyticUnitColorChange ( id : AnalyticUnitId , value : string , deleted : boolean ) {
if ( id === undefined ) {
if ( id === undefined ) {
throw new Error ( 'id is undefined' ) ;
throw new Error ( 'id is undefined' ) ;
}
}
this . _analyticUnitsSet . byId ( id ) . color = value ;
if ( deleted ) {
this . _analyticUnitsSet . byId ( id ) . deletedColor = value ;
} else {
this . _analyticUnitsSet . byId ( id ) . labeledColor = value ;
}
}
}
fetchAnalyticUnitsStatuses() {
fetchAnalyticUnitsStatuses() {
@ -227,7 +240,7 @@ export class AnalyticController {
var allSegmentsSet = new SegmentArray ( allSegmentsList ) ;
var allSegmentsSet = new SegmentArray ( allSegmentsList ) ;
if ( analyticUnit . selected ) {
if ( analyticUnit . selected ) {
this . _labelingDataAddedSegments . getSegments ( ) . forEach ( s = > allSegmentsSet . addSegment ( s ) ) ;
this . _labelingDataAddedSegments . getSegments ( ) . forEach ( s = > allSegmentsSet . addSegment ( s ) ) ;
this . _labelingDataDelet edSegments . getSegments ( ) . forEach ( s = > allSegmentsSet . remove ( s . id ) ) ;
this . _labelingDataRemov edSegments . getSegments ( ) . forEach ( s = > allSegmentsSet . remove ( s . id ) ) ;
}
}
analyticUnit . segments = allSegmentsSet ;
analyticUnit . segments = allSegmentsSet ;
}
}
@ -240,72 +253,68 @@ export class AnalyticController {
if (
if (
this . _labelingDataAddedSegments . length === 0 &&
this . _labelingDataAddedSegments . length === 0 &&
this . _labelingDataDelet edSegments . length === 0
this . _labelingDataRemov edSegments . length === 0
) {
) {
return [ ] ;
return [ ] ;
}
}
await this . _analyticService . updateMetric ( unit . id , this . _currentMetric , this . _currentDatasource ) ;
await this . _analyticService . updateMetric ( unit . id , this . _currentMetric , this . _currentDatasource ) ;
return this . _analyticService . updateSegments (
const newIds = await this . _analyticService . updateSegments (
unit . id , this . _labelingDataAddedSegments , this . _labelingDataDelet edSegments
unit . id , this . _labelingDataAddedSegments , this . _labelingDataRemov edSegments
) ;
) ;
if ( unit . labelingMode !== LabelingMode . UNLABELING ) {
await this . _analyticService . runDetect ( unit . id ) ;
}
return newIds ;
}
}
// TODO: move to renderer
// TODO: move to renderer
updateFlotEvents ( isEditMode , options ) {
updateFlotEvents ( isEditMode : boolean , options : any ) {
if ( options . grid . markings === undefined ) {
if ( options . grid . markings === undefined ) {
options . markings = [ ] ;
options . markings = [ ] ;
}
}
for ( var i = 0 ; i < this . analyticUnits . length ; i ++ ) {
for ( var i = 0 ; i < this . analyticUnits . length ; i ++ ) {
var analyticUnit = this . analyticUnits [ i ] ;
const analyticUnit = this . analyticUnits [ i ] ;
var borderColor = addAlphaToRGB ( analyticUnit . color , REGION_STROKE_ALPHA ) ;
var fillColor = addAlphaToRGB ( analyticUnit . color , REGION_FILL_ALPHA ) ;
var segments = analyticUnit . segments . getSegments ( ) ;
if ( ! analyticUnit . visible ) {
if ( ! analyticUnit . visible ) {
continue ;
continue ;
}
}
if ( isEditMode && this . labelingMode ) {
if ( analyticUnit . selected ) {
borderColor = addAlphaToRGB ( borderColor , 0.7 ) ;
fillColor = addAlphaToRGB ( borderColor , 0.7 ) ;
} else {
continue ;
}
}
var rangeDist = + options . xaxis . max - + options . xaxis . min ;
let defaultBorderColor = addAlphaToRGB ( analyticUnit . labeledColor , SEGMENT_STROKE_ALPHA ) ;
let defaultFillColor = addAlphaToRGB ( analyticUnit . labeledColor , SEGMENT_FILL_ALPHA ) ;
let labeledSegmentBorderColor = tinycolor ( LABELED_SEGMENT_BORDER_COLOR ) . toRgbString ( ) ;
let labeledSegmentBorderColor = tinycolor ( LABELED_SEGMENT_BORDER_COLOR ) . toRgbString ( ) ;
labeledSegmentBorderColor = addAlphaToRGB ( labeledSegmentBorderColor , REGION_STROKE_ALPHA ) ;
labeledSegmentBorderColor = addAlphaToRGB ( labeledSegmentBorderColor , SEGMENT_STROKE_ALPHA ) ;
let deletedSegmentFillColor = tinycolor ( DELETED_SEGMENT_FILL_COLOR ) . toRgbString ( ) ;
let deletedSegmentFillColor = tinycolor ( analyticUnit . deletedColor ) . toRgbString ( ) ;
deletedSegmentFillColor = addAlphaToRGB ( deletedSegmentFillColor , REGION_STROKE _ALPHA) ;
deletedSegmentFillColor = addAlphaToRGB ( deletedSegmentFillColor , SEGMENT_FILL_ALPHA ) ;
let deletedSegmentBorderColor = tinycolor ( DELETED_SEGMENT_BORDER_COLOR ) . toRgbString ( ) ;
let deletedSegmentBorderColor = tinycolor ( DELETED_SEGMENT_BORDER_COLOR ) . toRgbString ( ) ;
deletedSegmentBorderColor = addAlphaToRGB ( deletedSegmentBorderColor , REGION_STROKE_ALPHA ) ;
deletedSegmentBorderColor = addAlphaToRGB ( deletedSegmentBorderColor , SEGMENT_STROKE_ALPHA ) ;
if ( isEditMode && this . inLabelingMode && analyticUnit . selected ) {
defaultBorderColor = addAlphaToRGB ( defaultBorderColor , LABELING_MODE_ALPHA ) ;
defaultFillColor = addAlphaToRGB ( defaultFillColor , LABELING_MODE_ALPHA ) ;
labeledSegmentBorderColor = addAlphaToRGB ( labeledSegmentBorderColor , LABELING_MODE_ALPHA ) ;
deletedSegmentFillColor = addAlphaToRGB ( deletedSegmentFillColor , LABELING_MODE_ALPHA ) ;
deletedSegmentBorderColor = addAlphaToRGB ( deletedSegmentBorderColor , LABELING_MODE_ALPHA ) ;
}
const segments = analyticUnit . segments . getSegments ( ) ;
const rangeDist = + options . xaxis . max - + options . xaxis . min ;
segments . forEach ( s = > {
segments . forEach ( s = > {
let segmentBorderColor ;
let segmentBorderColor = defaultBorderColor ;
let segmentFillColor = fillColor ;
let segmentFillColor = de faultF illColor;
if ( this . labelingDeleteMode ) {
if ( s . deleted ) {
if ( s . deleted ) {
segmentBorderColor = deletedSegmentBorderColor ;
segmentBorderColor = deletedSegmentBorderColor ;
segmentFillColor = deletedSegmentFillColor ;
segmentFillColor = deletedSegmentFillColor ;
}
} else {
} else {
if ( s . deleted ) {
return ;
}
}
if ( s . labeled ) {
if ( s . labeled ) {
segmentBorderColor = labeledSegmentBorderColor ;
segmentBorderColor = labeledSegmentBorderColor ;
} else {
}
segmentBorderColor = borderColor ;
}
}
var expanded = s . expandDist ( rangeDist , 0.01 ) ;
const expanded = s . expandDist ( rangeDist , 0.01 ) ;
options . grid . markings . push ( {
options . grid . markings . push ( {
xaxis : { from : expanded . from , to : expanded.to } ,
xaxis : { from : expanded . from , to : expanded.to } ,
color : segmentFillColor
color : segmentFillColor
@ -324,20 +333,24 @@ export class AnalyticController {
}
}
deleteLabelingAnalyticUnitSegmentsInRange ( from : number , to : number ) {
deleteLabelingAnalyticUnitSegmentsInRange ( from : number , to : number ) {
var allRemovedSegs = this . labelingUnit . removeSegmentsInRange ( from , to ) ;
const allRemovedSegs = this . labelingUnit . removeSegmentsInRange ( from , to ) ;
allRemovedSegs . forEach ( s = > {
allRemovedSegs . forEach ( s = > {
if ( ! this . _labelingDataAddedSegments . has ( s . id ) ) {
if ( ! this . _labelingDataAddedSegments . has ( s . id ) ) {
this . _labelingDataDelet edSegments . addSegment ( s ) ;
this . _labelingDataRemov edSegments . addSegment ( s ) ;
}
}
} ) ;
} ) ;
this . _labelingDataAddedSegments . removeInRange ( from , to ) ;
this . _labelingDataAddedSegments . removeInRange ( from , to ) ;
}
}
toggleDeleteMode() {
toggleLabelingMode ( labelingMode : LabelingMode ) {
if ( ! this . labelingMode ) {
if ( ! this . inLabelingMode ) {
throw new Error ( 'Cant enter delete mode is labeling mode disabled' ) ;
throw new Error ( ` Can't enter ${ labelingMode } mode when labeling mode is disabled ` ) ;
}
if ( this . labelingUnit . labelingMode === labelingMode ) {
this . labelingUnit . labelingMode = LabelingMode . LABELING ;
} else {
this . labelingUnit . labelingMode = labelingMode ;
}
}
this . labelingUnit . deleteMode = ! this . labelingUnit . deleteMode ;
}
}
async removeAnalyticUnit ( id : AnalyticUnitId , silent : boolean = false ) {
async removeAnalyticUnit ( id : AnalyticUnitId , silent : boolean = false ) {