@ -50,12 +50,15 @@ export interface AiDetailValue {
export interface AiDetailSeries {
key : string ;
points : number ;
min : number ;
max : number ;
last : number ;
min? : number ;
max? : number ;
last? : number ;
lastText? : string ;
changes? : number ;
from : number ;
to : number ;
path : string ;
dot : number ;
}
export interface AiDetailFact {
@ -68,6 +71,9 @@ export interface AiDetailFact {
secret? : boolean ;
images ? : { current? : string ; value? : string } ;
chip ? : { icon? : string ; image? : string } ;
change? : boolean ;
was? : string ;
long? : boolean ;
}
export interface AiDetailCommand {
@ -85,8 +91,8 @@ export interface AiDetailLink {
export type AiDetailBlock =
| { type : 'records' ; label? : AiDetailText ; rows : AiDetailRecord [ ] ; total? : number }
| { type : 'hub' ; rows : AiDetailHubItem [ ] }
| { type : 'values' ; label? : AiDetailText ; rows : AiDetailValue [ ] }
| { type : 'series' ; rows : AiDetailSeries [ ] }
| { type : 'values' ; label? : AiDetailText ; entity? : AiDetailEntity ; rows : AiDetailValue [ ] }
| { type : 'series' ; entity? : AiDetailEntity ; rows : AiDetailSeries [ ] }
| { type : 'keys' ; label? : AiDetailText ; keys : string [ ] }
| { type : 'facts' ; rows : AiDetailFact [ ] }
| { type : 'list' ; label? : AiDetailText ; rows : Array < string | AiDetailText > }
@ -95,6 +101,7 @@ export type AiDetailBlock =
| { type : 'markdown' ; text : string }
| { type : 'code' ; label : string ; text : string ; lang? : string ; title? : string }
| { type : 'note' ; text : AiDetailText }
| { type : 'found' ; text : AiDetailText }
| { type : 'text' ; text : string }
| { type : 'json' ; label : string ; text : string }
| { type : 'images' ; rows : AiDetailImage [ ] }
@ -121,8 +128,10 @@ export interface AiStepView {
const ASKED_FOR : Record < string , string > = {
'search-named' : 'named' , 'search-typed' : 'typed' , 'search-text' : 'text' , 'search-related-from' : 'related-from' ,
'search-related-to' : 'related-to' , 'search-alarms-of' : 'alarms-of' , 'read-attributes-scope' : 'attributes-scope' ,
'search-alarms' : 'alarms' , 'search-alarms-on' : 'alarms-on' , 'search-alarms-filter-on' : 'alarms-filter-on' , 'search-alarms-window' : 'alarms-window' ,
'search-alarms-filter-window' : 'alarms-filter-window' , 'search-alarms-on-window' : 'alarms-on-window' , 'search-alarms-filter-on-window' : 'alarms-filter-on-window' ,
'read-history' : 'history' , 'read-history-keys' : 'history-keys' , 'read-latest-keys' : 'latest-keys' , 'iot-hub-query' : 'query' ,
'search-web' : 'query'
'search-web' : 'query' , 'read-widget-parts' : 'widget'
} ;
export function aiStepView ( action : AiAction , names : AiDetailNames ) : AiStepView {
@ -144,6 +153,8 @@ export function aiStepView(action: AiAction, names: AiDetailNames): AiStepView {
const K = 'ai-assistant.detail.' ;
const ALARM_FILTER_WORDS = new Set ( [ 'ACTIVE' , 'CLEARED' , 'ACK' , 'UNACK' , 'CRITICAL' , 'MAJOR' , 'MINOR' , 'WARNING' , 'INDETERMINATE' ] ) ;
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i ;
const OK_ID = /^OK:\s*([0-9a-z_-]+)/i ;
@ -414,38 +425,64 @@ function timeOf(ts: unknown): number | undefined {
return typeof value === 'number' && Number . isFinite ( value ) && value > 1 e11 ? value : undefined ;
}
function sparkPath ( values : number [ ] , times : number [ ] ) : { path : string ; dot : number } {
const width = 300 ;
const height = 60 ;
const pad = 6 ;
const from = times [ 0 ] ;
const span = times [ times . length - 1 ] - from || 1 ;
const min = Math . min ( . . . values ) ;
const range = Math . max ( . . . values ) - min || 1 ;
const y = ( value : number ) = > height - pad - ( value - min ) / range * ( height - 2 * pad ) ;
const path = values . map ( ( value , index ) = >
` ${ index ? 'L' : 'M' } ${ ( ( times [ index ] - from ) / span * width ) . toFixed ( 1 ) } ${ y ( value ) . toFixed ( 1 ) } ` ) . join ( '' ) ;
return { path , dot : Math.round ( y ( values [ values . length - 1 ] ) / height * 100 ) } ;
}
function series ( key : string , points : Array < [ number , number ] > ) : AiDetailSeries {
const sorted = [ . . . points ] . sort ( ( a , b ) = > a [ 0 ] - b [ 0 ] ) ;
const values = sorted . map ( point = > point [ 1 ] ) ;
const min = Math . min ( . . . values ) ;
const max = Math . max ( . . . values ) ;
const from = sorted [ 0 ] [ 0 ] ;
const to = sorted [ sorted . length - 1 ] [ 0 ] ;
const width = 96 ;
const height = 20 ;
const span = to - from || 1 ;
const range = max - min || 1 ;
const path = sorted . map ( ( [ ts , value ] , index ) = >
` ${ index ? 'L' : 'M' } ${ ( ( ts - from ) / span * width ) . toFixed ( 1 ) } ${ ( height - 1 - ( value - min ) / range * ( height - 2 ) ) . toFixed ( 1 ) } ` ) . join ( '' ) ;
return { key , points : sorted.length , min , max , last : sorted [ sorted . length - 1 ] [ 1 ] , from , to , path } ;
}
function telemetry ( result : Values ) : AiDetailBlock [ ] {
const times = sorted . map ( point = > point [ 0 ] ) ;
return { key , points : sorted.length , min : Math.min ( . . . values ) , max : Math.max ( . . . values ) , last : values [ values . length - 1 ] ,
from : times [ 0 ] , to : times [ times . length - 1 ] , . . . sparkPath ( values , times ) } ;
}
function changesSeries ( key : string , points : Array < [ number , unknown ] > ) : AiDetailSeries {
const sorted = [ . . . points ] . sort ( ( a , b ) = > a [ 0 ] - b [ 0 ] ) ;
const words = sorted . map ( point = > aiValueText ( point [ 1 ] ) ) ;
const levels = [ . . . new Set ( words ) ] ;
const changes = words . filter ( ( word , index ) = > index && word !== words [ index - 1 ] ) . length ;
const times = sorted . map ( point = > point [ 0 ] ) ;
return { key , points : sorted.length , lastText : words [ words . length - 1 ] , changes , from : times [ 0 ] , to : times [ times . length - 1 ] ,
. . . sparkPath ( words . map ( word = > levels . indexOf ( word ) ) , times ) } ;
}
function telemetry ( result : Values , entityOf ? : ( id : string ) = > AiDetailEntity ) : AiDetailBlock [ ] {
const ids = Object . keys ( result ) ;
if ( entityOf && ids . length && ids . every ( id = > UUID . test ( id ) && isObject ( result [ id ] ) ) ) {
return ids . flatMap ( id = > telemetry ( result [ id ] as Values ) . map ( block = >
block . type === 'values' || block . type === 'series' ? { . . . block , entity : entityOf ( id ) } : block ) ) ;
}
const latest : AiDetailValue [ ] = [ ] ;
const history : AiDetailSeries [ ] = [ ] ;
const other : AiDetailValue [ ] = [ ] ;
const add = ( key : string , points : Array < [ number , unknown ] > ) = > {
const numbers = points . map ( ( [ ts , value ] ) = > [ ts , typeof value === 'boolean' ? NaN : Number ( value ) ] as [ number , number ] ) ;
if ( points . length > 1 && numbers . every ( ( [ , value ] ) = > Number . isFinite ( value ) ) ) {
history . push ( series ( key , numbers ) ) ;
} else if ( points . length > 1 ) {
history . push ( changesSeries ( key , points ) ) ;
} else if ( points . length ) {
latest . push ( { key , value : aiValueText ( points [ 0 ] [ 1 ] ) , time : points [ 0 ] [ 0 ] } ) ;
}
} ;
for ( const [ key , data ] of Object . entries ( result ) ) {
if ( isObject ( data ) && 'value' in data ) {
latest . push ( { key , value : aiValueText ( data . value ) , time : timeOf ( data . ts ) } ) ;
continue ;
}
if ( Array . isArray ( data ) && data . every ( point = > isObject ( point ) && 'ts' in point ) ) {
const numbers = data . map ( point = > [ timeOf ( point . ts ) , Number ( point . value ) ] as [ number , number ] ) . filter ( ( [ ts , value ] ) = > ts && Number . isFinite ( value ) ) ;
if ( numbers . length > 1 ) {
history . push ( series ( key , numbers ) ) ;
} else if ( data . length ) {
latest . push ( { key , value : aiValueText ( data [ 0 ] . value ) , time : timeOf ( data [ 0 ] . ts ) } ) ;
}
add ( key , data . map ( point = > [ timeOf ( point . ts ) , point . value ] as [ number , unknown ] ) . filter ( ( [ ts ] ) = > ! ! ts ) ) ;
continue ;
}
if ( ! isObject ( data ) ) {
@ -453,12 +490,8 @@ function telemetry(result: Values): AiDetailBlock[] {
continue ;
}
const entries = Object . entries ( data ) . filter ( ( [ ts ] ) = > timeOf ( ts ) ) ;
const numbers = entries . map ( ( [ ts , value ] ) = > [ timeOf ( ts ) , Number ( value ) ] as [ number , number ] ) . filter ( ( [ , value ] ) = > Number . isFinite ( value ) ) ;
if ( entries . length > 1 && numbers . length === entries . length ) {
history . push ( series ( key , numbers ) ) ;
} else if ( entries . length ) {
const [ ts , value ] = entries . sort ( ( a , b ) = > Number ( b [ 0 ] ) - Number ( a [ 0 ] ) ) [ 0 ] ;
latest . push ( { key , value : aiValueText ( value ) , time : timeOf ( ts ) } ) ;
if ( entries . length ) {
add ( key , entries . map ( ( [ ts , value ] ) = > [ timeOf ( ts ) , value ] as [ number , unknown ] ) ) ;
} else {
other . push ( { key , value : aiValueText ( data ) } ) ;
}
@ -513,6 +546,16 @@ function recordOf(row: Values, entityType: string): AiDetailRecord | null {
return { entityType : entityType === 'ALARM_RULE' ? 'ALARM_RULE' : entityType , id : idOf ( row . id ) , name , meta : meta || undefined } ;
}
function withoutCommonMeta ( rows : AiDetailRecord [ ] ) : AiDetailRecord [ ] {
if ( rows . length < 2 ) {
return rows ;
}
const counts = new Map < string , number > ( ) ;
rows . forEach ( row = > row . meta && counts . set ( row . meta , ( counts . get ( row . meta ) ? ? 0 ) + 1 ) ) ;
const common = [ . . . counts ] . find ( ( [ , count ] ) = > count > rows . length / 2 ) ? . [ 0 ] ;
return common ? rows . map ( row = > row . meta === common ? { . . . row , meta : undefined } : row ) : rows ;
}
function searchEntities ( values : Values , result : unknown , names : AiDetailNames ) : AiDetailView {
const filter = isObject ( values . entityFilter ) ? values . entityFilter : { } ;
const type = searchType ( values ) ;
@ -534,9 +577,18 @@ function searchEntities(values: Values, result: unknown, names: AiDetailNames):
} else if ( str ( values . pageLink ? . textSearch ) ) {
key = 'search-text' ;
params . text = values . pageLink . textSearch ;
} else if ( values . filterType === 'alarm' && Array . isArray ( filter . originatorIds ) && filter . originatorIds . length === 1 ) {
key = 'search-alarms-of' ;
params . entity = entity ( filter . originatorIds [ 0 ] , filter . originatorType , names ) ;
} else if ( values . filterType === 'alarm' ) {
const words = [ . . . ( Array . isArray ( filter . severityList ) ? filter . severityList : [ ] ) , . . . ( Array . isArray ( filter . statusList ) ? filter . statusList : [ ] ) ]
. map ( word = > str ( word ) ? . toUpperCase ( ) ) . filter ( word = > ! ! word && ALARM_FILTER_WORDS . has ( word ) ) . map ( word = > K + 'alarm-filter.' + word ) ;
const one = Array . isArray ( filter . originatorIds ) && filter . originatorIds . length === 1 ;
const window = timeOf ( filter . startTs ) ? ` ${ dateText ( timeOf ( filter . startTs ) ) } – ${ endText ( filter . endTs ) } ` : undefined ;
const parts = [ words . length ? 'filter' : '' , one ? 'on' : '' , window ? 'window' : '' ] . filter ( Boolean ) ;
if ( parts . length ) {
key = parts . length === 1 && parts [ 0 ] === 'filter' ? 'search-alarms' : 'search-alarms-' + parts . join ( '-' ) ;
params . filter = { words , commas : true } ;
params . entity = one ? entity ( filter . originatorIds [ 0 ] , filter . originatorType , names ) : '' ;
params . window = window ? ? '' ;
}
}
const sections = searchSections ( result ) ;
if ( ! sections . length ) {
@ -548,13 +600,13 @@ function searchEntities(values: Values, result: unknown, names: AiDetailNames):
const data : unknown [ ] = Array . isArray ( section . page . data ) ? section . page . data : [ ] ;
if ( ! data . length ) {
if ( sections . length === 1 ) {
blocks . push ( { type : 'note ' , text : text ( 'nothing-found' ) } ) ;
blocks . push ( { type : 'found ' , text : text ( entityType === 'ALARM' ? 'no-alarms' : 'nothing-found' ) } ) ;
}
continue ;
}
const groupType = entityType === 'ENTITY_GROUP' ? str ( filter . groupType ) ? . toUpperCase ( ) : undefined ;
const rows = data . filter ( isObject ) . map ( row = > recordOf ( row , entityType ) ) . filter ( Boolean ) . slice ( 0 , MAX_ROWS )
. map ( row = > groupType ? { . . . row , groupType } : row ) ;
const rows = withoutCommonMeta ( data . filter ( isObject ) . map ( row = > recordOf ( row , entityType ) ) . filter ( Boolean ) . slice ( 0 , MAX_ROWS )
. map ( row = > groupType ? { . . . row , groupType } : row ) ) ;
const total = typeof section . page . totalElements === 'number' ? section.page.totalElements : data.length ;
blocks . push ( rows . length
? { type : 'records' , label : sections.length > 1 ? text ( 'what-found.' + ( SEARCHED [ entityType ] ? ? 'entities' ) ) : undefined , rows , total }
@ -1160,10 +1212,14 @@ function describe(tool: string, values: Values, result: unknown, names: AiDetail
return { hood : text ( 'read-relations' , { entity : target } ) , blocks : relations ( result ) } ;
case 'read_timeseries' : {
const keys = Array . isArray ( values . keys ) ? values . keys . join ( ', ' ) : '' ;
const ids = Array . isArray ( values . entityIds ) ? values . entityIds . map ( idOf ) . filter ( Boolean ) : [ ] ;
const subject : AiDetailParam = ids . length > 1
? { words : ids.map ( id = > names . get ( id ) ? . name ) . every ( Boolean ) ? ids . map ( id = > names . get ( id ) . name ) : [ 'ai-assistant.detail.several' ] , commas : true }
: ids . length ? of ( ids [ 0 ] , values . entityType ) : target ;
const hood = values . mode === 'HISTORY' || values . startTime
? text ( keys ? 'read-history-keys' : 'read-history' , { entity : target , keys , from : dateText ( values . startTime ) , to : endText ( values . endTime ) } )
: text ( keys ? 'read-latest-keys' : 'read-latest' , { entity : target , keys } ) ;
const blocks = isObject ( result ) ? telemetry ( result ) : generic ( result , 'result' ) ;
? text ( keys ? 'read-history-keys' : 'read-history' , { entity : subjec t, keys , from : dateText ( values . startTime ) , to : endText ( values . endTime ) } )
: text ( keys ? 'read-latest-keys' : 'read-latest' , { entity : subjec t, keys } ) ;
const blocks = isObject ( result ) ? telemetry ( result , id = > of ( id , values . entityType ) ) : generic ( result , 'result' ) ;
return { hood , blocks : isObject ( result ) && ! Object . keys ( result ) . length ? [ { type : 'note' , text : text ( 'no-data' ) } ] : blocks } ;
}
case 'read_timeseries_keys' : {
@ -1788,7 +1844,7 @@ function describe(tool: string, values: Values, result: unknown, names: AiDetail
return { hood , blocks : generic ( raw , 'result' ) } ;
}
const warnings = Array . isArray ( raw . warnings ) ? raw . warnings . map ( item = > String ( item ) ) : [ ] ;
const rows = facts ( raw , [ 'typeParameters' , 'warnings' , 'note' , 'next' ] ) ;
const rows = facts ( raw , [ 'typeParameters' , 'warnings' , 'note' , 'next' , 'id' , 'name' , 'fqn' , 'type' ] ) ;
return { hood , blocks : [ . . . ( rows . length ? [ { type : 'facts' as const , rows } ] : [ ] ) ,
. . . ( warnings . length ? [ { type : 'list' as const , label : text ( 'widget-warnings' ) , rows : warnings.slice ( 0 , MAX_ROWS ) } ] : [ ] ) ] } ;
}