@ -1,6 +1,6 @@
// Libraries
import { Observable , of , timer , merge , from } from 'rxjs' ;
import { flatten , map as lodashMap , isArray , isString } from 'lodash' ;
import { map as isArray , isString } from 'lodash' ;
import { map , catchError , takeUntil , mapTo , share , finalize , tap } from 'rxjs/operators' ;
// Utils & Services
import { backendSrv } from 'app/core/services/backend_srv' ;
@ -16,6 +16,7 @@ import {
dateMath ,
toDataFrame ,
DataFrame ,
DataTopic ,
guessFieldTypes ,
} from '@grafana/data' ;
import { toDataQueryError } from '@grafana/runtime' ;
@ -54,19 +55,33 @@ export function processResponsePacket(packet: DataQueryResponse, state: RunningQ
}
: range ;
const combinedData = flatten (
lodashMap ( packets , ( packet : DataQueryResponse ) = > {
if ( packet . error ) {
loadingState = LoadingState . Error ;
error = packet . error ;
const series : DataQueryResponseData [ ] = [ ] ;
const annotations : DataQueryResponseData [ ] = [ ] ;
for ( const key in packets ) {
const packet = packets [ key ] ;
if ( packet . error ) {
loadingState = LoadingState . Error ;
error = packet . error ;
}
if ( packet . data && packet . data . length ) {
for ( const dataItem of packet . data ) {
if ( dataItem . meta ? . dataTopic === DataTopic . Annotations ) {
annotations . push ( dataItem ) ;
continue ;
}
series . push ( dataItem ) ;
}
return packet . data ;
} )
) ;
}
}
const panelData = {
state : loadingState ,
series : combinedData ,
series ,
annotations ,
error ,
request ,
timeRange ,
@ -77,11 +92,10 @@ export function processResponsePacket(packet: DataQueryResponse, state: RunningQ
/ * *
* This function handles the excecution of requests & and processes the single or multiple response packets into
* a combined PanelData response .
* It will
* * Merge multiple responses into a single DataFrame array based on the packet key
* * Will emit a loading state if no response after 50 ms
* * Cancel any still running network requests on unsubscribe ( using request . requestId )
* a combined PanelData response . It will
* Merge multiple responses into a single DataFrame array based on the packet key
* Will emit a loading state if no response after 50 ms
* Cancel any still running network requests on unsubscribe ( using request . requestId )
* /
export function runRequest ( datasource : DataSourceApi , request : DataQueryRequest ) : Observable < PanelData > {
let state : RunningQueryState = {
@ -162,7 +176,7 @@ export function callQueryMethod(datasource: DataSourceApi, request: DataQueryReq
* This is also used by PanelChrome for snapshot support
* /
export function getProcessedDataFrames ( results? : DataQueryResponseData [ ] ) : DataFrame [ ] {
if ( ! isArray ( results ) ) {
if ( ! results || ! isArray ( results ) ) {
return [ ] ;
}
@ -185,7 +199,7 @@ export function getProcessedDataFrames(results?: DataQueryResponseData[]): DataF
}
export function preProcessPanelData ( data : PanelData , lastResult? : PanelData ) : PanelData {
const { series } = data ;
const { series , annotations } = data ;
// for loading states with no data, use last result
if ( data . state === LoadingState . Loading && series . length === 0 ) {
@ -203,11 +217,13 @@ export function preProcessPanelData(data: PanelData, lastResult?: PanelData): Pa
// Make sure the data frames are properly formatted
const STARTTIME = performance . now ( ) ;
const processedDataFrames = getProcessedDataFrames ( series ) ;
const annotationsProcessed = getProcessedDataFrames ( annotations ) ;
const STOPTIME = performance . now ( ) ;
return {
. . . data ,
series : processedDataFrames ,
annotations : annotationsProcessed ,
timings : { dataProcessingTime : STOPTIME - STARTTIME } ,
} ;
}