2021-04-05 00:13:34 -05:00
|
|
|
import {
|
|
|
|
compareArrayValues,
|
|
|
|
compareDataFrameStructures,
|
|
|
|
DataFrame,
|
|
|
|
DataQueryError,
|
|
|
|
LoadingState,
|
|
|
|
PanelData,
|
|
|
|
} from '@grafana/data';
|
2020-04-22 12:21:48 -05:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
import { PanelModel } from '../../state';
|
|
|
|
import { Unsubscribable } from 'rxjs';
|
2020-11-26 11:12:02 -06:00
|
|
|
import { GetDataOptions } from '../../../query/state/PanelQueryRunner';
|
2020-04-22 12:21:48 -05:00
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
interface UsePanelLatestData {
|
|
|
|
data?: PanelData;
|
|
|
|
error?: DataQueryError;
|
|
|
|
isLoading: boolean;
|
|
|
|
hasSeries: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribes and returns latest panel data from PanelQueryRunner
|
|
|
|
*/
|
2021-04-05 00:13:34 -05:00
|
|
|
export const usePanelLatestData = (
|
|
|
|
panel: PanelModel,
|
|
|
|
options: GetDataOptions,
|
|
|
|
checkSchema?: boolean
|
|
|
|
): UsePanelLatestData => {
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
const querySubscription = useRef<Unsubscribable>();
|
2020-06-18 07:31:30 -05:00
|
|
|
const [latestData, setLatestData] = useState<PanelData>();
|
2020-04-22 12:21:48 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-04-05 00:13:34 -05:00
|
|
|
let last: DataFrame[] = [];
|
|
|
|
let lastUpdate = 0;
|
|
|
|
|
2020-04-22 12:21:48 -05:00
|
|
|
querySubscription.current = panel
|
|
|
|
.getQueryRunner()
|
2020-05-25 07:05:43 -05:00
|
|
|
.getData(options)
|
2020-04-22 12:21:48 -05:00
|
|
|
.subscribe({
|
2021-04-05 00:13:34 -05:00
|
|
|
next: (data) => {
|
|
|
|
if (checkSchema) {
|
|
|
|
const sameStructure = compareArrayValues(last, data.series, compareDataFrameStructures);
|
|
|
|
if (sameStructure) {
|
|
|
|
const now = Date.now();
|
|
|
|
const elapsed = now - lastUpdate;
|
|
|
|
if (elapsed < 10000) {
|
|
|
|
return; // avoid updates if the schema has not changed for 10s
|
|
|
|
}
|
|
|
|
lastUpdate = now;
|
|
|
|
}
|
|
|
|
last = data.series;
|
|
|
|
}
|
|
|
|
setLatestData(data);
|
|
|
|
},
|
2020-04-22 12:21:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (querySubscription.current) {
|
|
|
|
querySubscription.current.unsubscribe();
|
|
|
|
}
|
|
|
|
};
|
2020-06-18 07:31:30 -05:00
|
|
|
/**
|
|
|
|
* Adding separate options to dependencies array to avoid additional hook for comparing previous options with current.
|
2021-04-01 10:42:57 -05:00
|
|
|
* Otherwise, passing different references to the same object might cause troubles.
|
2020-06-18 07:31:30 -05:00
|
|
|
*/
|
2021-03-25 06:42:14 -05:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2020-06-18 07:31:30 -05:00
|
|
|
}, [panel, options.withFieldConfig, options.withTransforms]);
|
2020-04-22 12:21:48 -05:00
|
|
|
|
2020-06-18 07:31:30 -05:00
|
|
|
return {
|
|
|
|
data: latestData,
|
|
|
|
error: latestData && latestData.error,
|
|
|
|
isLoading: latestData ? latestData.state === LoadingState.Loading : true,
|
|
|
|
hasSeries: latestData ? !!latestData.series : false,
|
|
|
|
};
|
2020-04-22 12:21:48 -05:00
|
|
|
};
|