import { DEFAULT_MODEL , } from '../datasource/types' ; export function extract_tooltip_feature ( target ) { if ( target . select ) { if ( target . measurement ) { return target . measurement + ": " + target . select . map ( o => _formatFeature ( o ) ) . join ( ', ' ) ; } if ( target . table ) { return target . table + ": " + target . select [ 0 ] . filter ( o => o . type === 'column' ) . map ( o => o . params . join ( ', ' ) ) . join ( '; ' ) ; } } if ( target . metric ) { return target . metric ; } if ( target . expr ) { return target . expr ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return target . metrics [ 0 ] . type + ": " + target . metrics [ 0 ] . field ; } } export function extract_group_by ( target ) { if ( target . groupBy ) { let res = _formatGroupBy ( target . groupBy ) ; if ( res == "time: $__interval" ) { return "time: " + DEFAULT_MODEL . interval ; } return res ; } if ( target . group ) { return target . group . filter ( o => o . type === 'time' ) . map ( o => [ o . type , o . params [ 0 ] ] . join ( ': ' ) ) . join ( ', ' ) ; } if ( target . aggregator && target . downsampleInterval && target . downsampleAggregator ) { return target . aggregator + ":" + target . downsampleInterval + "-" + target . downsampleAggregator + "-nan" ; } if ( target . expr ) { return target . interval || "auto" ; } if ( target . query && target . bucketAggs && target . bucketAggs . length > 0 ) { if ( target . bucketAggs [ 0 ] . settings ) { return target . bucketAggs [ 0 ] . settings . interval ; } return "auto" ; } } export function extract_fill_value ( target ) { if ( target . groupBy ) { const f = _formatFillValue ( target . groupBy ) ; if ( f == "null" ) { return null ; } if ( f == "none" ) { return null ; } return f ; } if ( target . group ) { return target . group . filter ( o => o . type === 'time' ) . map ( o => o . params [ 1 ] ) . join ( ', ' ) ; } return null ; } export function extract_format_tags ( target ) { if ( target . tags && target . tags . map ) { return target . tags . map ( o => [ o . key , o . operator , o . value ] . join ( ' ' ) ) . join ( ', ' ) ; } if ( target . currentTagKey && target . currentTagValue ) { return target . currentTagKey + " = " + target . currentTagValue ; } if ( target . tags && typeof target . tags === 'object' ) { let res = [ ] ; for ( const key in target . tags ) { if ( target . tags . hasOwnProperty ( key ) ) { res . push ( key + " = " + target . tags [ key ] ) ; } } return res . join ( ', ' ) ; } if ( target . where ) { return target . where . filter ( o => o . type === 'expression' ) . map ( o => o . params . join ( ' ' ) ) . join ( ', ' ) ; } if ( target . query ) { return target . query ; } } export function extract_is_valid ( target ) { if ( target . select ) { return true ; } if ( target . metric ) { return true ; } if ( target . expr ) { return true ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return true ; } } export function extract_model_database ( datasource ) { if ( datasource . database ) { return datasource . database ; } if ( datasource . index ) { return datasource . index ; } return datasource . name . toLowerCase ( ) . replace ( /-/g , "_" ) ; } export function extract_model_measurement ( target ) { if ( target . measurement ) { return target . measurement ; } if ( target . metric ) { return target . metric ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return target . metrics [ 0 ] . type ; } return "auto" ; } export function extract_model_select ( target , field ) { if ( target . select ) { return _formatSelect ( field ) ; } if ( target . metric && target . aggregator ) { return target . aggregator + "_" + target . metric . replace ( /\./g , "_" ) ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return target . metrics [ 0 ] . type + "_" + target . metrics [ 0 ] . field . replace ( /\./g , "_" ) ; } if ( target . expr ) { return string_to_slug ( target . expr ) ; } } export function extract_model_feature ( target , field ) { if ( target . select ) { return _get_feature ( field ) ; } if ( target . metric ) { return target . metric ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return target . metrics [ 0 ] . field ; } if ( target . expr ) { return target . expr ; } } export function extract_model_func ( target , field ) { if ( target . select ) { return _get_func ( field ) ; } if ( target . aggregator ) { return target . aggregator ; } if ( target . query && target . bucketAggs && target . metrics && target . metrics . length > 0 ) { return target . metrics [ 0 ] . type ; } return "avg" ; } export function extract_model_fill ( target ) { if ( target . groupBy ) { const f = _get_fill ( target . groupBy ) ; if ( f == "0" ) { return 0 ; } if ( f == "null" ) { return null ; } if ( f == "none" ) { return null ; } return f ; } return null ; } export function extract_model_time_format ( target ) { if ( target . groupBy ) { let res = _formatTime ( target . groupBy ) ; if ( res == "time_$__interval" ) { return DEFAULT_MODEL . interval ; } return res ; } else { return target . downsampleInterval || target . interval || "auto" ; } } export function extract_model_time ( target ) { if ( target . groupBy ) { let res = _get_time ( target . groupBy ) ; if ( res == "$__interval" ) { return DEFAULT_MODEL . interval ; } return res ; } if ( target . query && target . bucketAggs && target . bucketAggs . length > 0 && target . bucketAggs [ 0 ] . settings ) { return target . bucketAggs [ 0 ] . settings . interval ; } return target . downsampleInterval || target . interval || "20m" ; } export function extract_model_tags ( target ) { if ( target . tags && target . tags . map ) { return _formatTags ( target . tags ) ; } if ( target . currentTagKey && target . currentTagValue ) { return target . currentTagKey + "_" + target . currentTagValue ; } if ( target . tags && typeof target . tags === 'object' ) { let res = [ ] ; for ( const key in target . tags ) { if ( target . tags . hasOwnProperty ( key ) ) { res . push ( key + "_" + target . tags [ key ] ) ; } } return res . join ( '_' ) ; } return "" ; } export function extract_model_tags_map ( target ) { if ( target . tags && target . tags . map ) { return target . tags . map ( ( tag ) => ( { tag : tag . key , value : tag . value , } ) ) ; } if ( target . currentTagKey && target . currentTagValue ) { return { tag : target . currentTagKey , value : target . currentTagValue } ; } if ( target . tags && typeof target . tags === 'object' ) { let res = [ ] ; for ( const key in target . tags ) { if ( target . tags . hasOwnProperty ( key ) ) { res . push ( { tag : key , value : target . tags [ key ] } ) ; } } return res ; } return [ ] ; } function _formatFeature ( value ) { const selectField = value . filter ( o => o . type === 'field' ) ; return selectField . map ( o => o . params . join ( ', ' ) ) . join ( '; ' ) ; } function _formatGroupBy ( value ) { const groupBy = value . filter ( o => o . type === 'time' ) ; return groupBy . map ( o => [ o . type , o . params ] . join ( ': ' ) ) . join ( ', ' ) ; } function _formatFillValue ( value ) { const fill = value . filter ( o => o . type === 'fill' ) ; return fill . map ( o => [ o . type , o . params ] . join ( ': ' ) ) . join ( ', ' ) ; } function _formatSelect ( value ) { const selectFunc = value . filter ( o => o . type !== 'field' ) ; const selectField = value . filter ( o => o . type === 'field' ) ; return selectFunc . map ( o => o . type ) . join ( '_' ) + '_' + selectField . map ( o => o . params . join ( '_' ) ) . join ( '_' ) ; } function _formatTime ( value ) { const groupBy = value . filter ( o => o . type === 'time' ) ; return groupBy . map ( o => [ o . type , o . params ] . join ( '_' ) ) . join ( '_' ) ; } function _get_feature ( value ) { const field = value . filter ( o => o . type === 'field' ) ; if ( field . length === 0 ) { return "" ; } return field [ 0 ] . params [ 0 ] ; } function _get_func ( value ) { const func = value . filter ( o => o . type !== 'field' ) ; if ( func . length === 0 ) { return "" ; } return func [ 0 ] . type ; } function _get_fill ( value ) { const fill = value . filter ( o => o . type === 'fill' ) ; if ( fill . length === 0 ) { return "null" ; } return fill [ 0 ] . params [ 0 ] ; } function _get_time ( value ) { const time = value . filter ( o => o . type === 'time' ) ; if ( time . length !== 1 ) { return DEFAULT_MODEL . interval ; } return time [ 0 ] . params [ 0 ] ; } function _formatTags ( value ) { return value . map ( o => [ o . key , o . value ] . join ( '_' ) ) . join ( '_' ) ; } function string_to_slug ( str ) { str = str . replace ( /^\s+|\s+$/g , '' ) ; str = str . toLowerCase ( ) ; var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;" ; var to = "aaaaeeeeiiiioooouuuunc------" ; for ( var i = 0 , l = from . length ; i < l ; i ++ ) { str = str . replace ( new RegExp ( from . charAt ( i ) , 'g' ) , to . charAt ( i ) ) ; } str = str . replace ( /[^a-z0-9 -]/g , '' ) . replace ( /\s+/g , '-' ) . replace ( /-+/g , '_' ) ; return str ; }