import { PrometheusConnectionOptions , PrometheusDriver , QueryResult } from "prometheus-query" ; import { ChartDatasourcePrometheusPluginOptionsTimeRange , PrometheusQuery , PrometheusTimeRangeAbsolute } from "./options" ; const PROMETHEUS_QUERY_RANGE_MIN_STEP = 1 ; export default { param { Date } start param { Date } end param { number } chartWidth getPrometheusStepAuto : ( start , end , chartWidth ) : number => { const secondDuration = ( end . getTime ( ) - start . getTime ( ) ) / 1000 ; const step = Math . floor ( secondDuration / chartWidth ) * 25 ; return step < PROMETHEUS_QUERY_RANGE_MIN_STEP ? PROMETHEUS_QUERY_RANGE_MIN_STEP : step ; } , param { object } timeRange getStartAndEndDates ( timeRange ) { timeRange . type = ! ! timeRange . type ? timeRange . type : 'absolute' ; if ( timeRange . type === 'absolute' ) { return { type : 'absolute' , start : timeRange [ 'start' ] , end : timeRange [ 'end' ] , } ; } else if ( timeRange . type === 'relative' ) { return { type : 'absolute' , start : new Date ( new Date ( ) . getTime ( ) + timeRange . start ) , end : new Date ( new Date ( ) . getTime ( ) + timeRange . end ) , } ; } throw new Error ( 'Unexpected options.timeRange value.' ) ; } , executeQueries : ( prometheus , queries , start , end , step ) : Promise < QueryResult > [ ] => { const haveDirectPrometheusRequests = queries . find ( ( q ) => typeof q === 'string' ) != null ; const p = ! ! prometheus && haveDirectPrometheusRequests ? new PrometheusDriver ( prometheus ) : null ; return queries . map ( ( query ) => { if ( typeof query === 'string' ) return p . rangeQuery ( query , start , end , step ) ; return query ( start , end , step ) . then ( ( data ) => QueryResult . fromJSON ( data ) ) ; } ) ; } } ;