2023-11-15 15:33:45 +05:30
import './WidgetFullView.styles.scss' ;
import { SyncOutlined } from '@ant-design/icons' ;
2023-07-18 08:55:01 +05:30
import { Button } from 'antd' ;
2024-02-20 16:21:07 +05:30
import cx from 'classnames' ;
2023-08-02 20:41:09 +05:30
import { ToggleGraphProps } from 'components/Graph/types' ;
2021-09-23 15:43:43 +05:30
import Spinner from 'components/Spinner' ;
import TimePreference from 'components/TimePreferenceDropDown' ;
2024-02-20 16:21:07 +05:30
import { PANEL_TYPES } from 'constants/queryBuilder' ;
2023-07-25 16:25:36 +03:00
import GridPanelSwitch from 'container/GridPanelSwitch' ;
2021-09-23 15:43:43 +05:30
import {
timeItems ,
timePreferance ,
} from 'container/NewWidget/RightContainer/timeItems' ;
2023-07-18 08:55:01 +05:30
import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange' ;
import { useStepInterval } from 'hooks/queryBuilder/useStepInterval' ;
2023-08-02 20:41:09 +05:30
import { useChartMutable } from 'hooks/useChartMutable' ;
2023-11-15 15:33:45 +05:30
import { useIsDarkMode } from 'hooks/useDarkMode' ;
2023-07-18 08:55:01 +05:30
import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables' ;
2023-11-21 13:12:37 +05:30
import { getUPlotChartOptions } from 'lib/uPlotLib/getUplotChartOptions' ;
import { getUPlotChartData } from 'lib/uPlotLib/utils/getUplotChartData' ;
2023-10-08 23:21:17 +05:30
import { useDashboard } from 'providers/Dashboard/Dashboard' ;
2024-01-22 16:36:03 +05:30
import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
2021-09-23 15:43:43 +05:30
import { useSelector } from 'react-redux' ;
import { AppState } from 'store/reducers' ;
2022-01-26 21:46:59 +05:30
import { GlobalReducer } from 'types/reducer/globalTime' ;
2023-11-15 15:33:45 +05:30
import uPlot from 'uplot' ;
2024-02-28 14:56:50 +05:30
import { getSortedSeriesData } from 'utils/getSortedSeriesData' ;
2023-12-14 22:56:25 +05:30
import { getTimeRange } from 'utils/getTimeRange' ;
2021-09-23 15:43:43 +05:30
2024-01-22 16:36:03 +05:30
import { getGraphVisibilityStateOnDataChange } from '../utils' ;
2023-08-02 20:41:09 +05:30
import { PANEL_TYPES_VS_FULL_VIEW_TABLE } from './contants' ;
import GraphManager from './GraphManager' ;
import { GraphContainer , TimeContainer } from './styles' ;
import { FullViewProps } from './types' ;
2021-09-23 15:43:43 +05:30
2022-03-22 12:10:31 +05:30
function FullView ( {
2021-10-20 09:24:55 +05:30
widget ,
fullViewOptions = true ,
onClickHandler ,
2021-12-10 14:28:31 +05:30
name ,
2024-01-22 16:36:03 +05:30
originalName ,
2022-03-15 15:18:33 +05:30
yAxisUnit ,
2024-01-22 16:36:03 +05:30
options ,
2023-01-17 13:30:34 +02:00
onDragSelect ,
2023-07-18 08:55:01 +05:30
isDependedDataLoaded = false ,
2023-08-02 20:41:09 +05:30
onToggleModelHandler ,
2023-10-08 23:21:17 +05:30
parentChartRef ,
2024-01-22 16:36:03 +05:30
parentGraphVisibilityState ,
2022-03-22 12:10:31 +05:30
} : FullViewProps ) : JSX . Element {
2023-07-18 08:55:01 +05:30
const { selectedTime : globalSelectedTime } = useSelector <
2022-01-26 21:46:59 +05:30
AppState ,
GlobalReducer
> ( ( state ) = > state . globalTime ) ;
2021-09-23 15:43:43 +05:30
2023-11-15 15:33:45 +05:30
const fullViewRef = useRef < HTMLDivElement > ( null ) ;
const [ chartOptions , setChartOptions ] = useState < uPlot.Options > ( ) ;
2023-11-29 00:02:51 +05:30
const { selectedDashboard , isDashboardLocked } = useDashboard ( ) ;
2023-10-08 23:21:17 +05:30
2024-01-22 16:36:03 +05:30
const { graphVisibilityStates : localStoredVisibilityStates } = useMemo (
( ) = >
getGraphVisibilityStateOnDataChange ( {
options ,
isExpandedName : false ,
name : originalName ,
} ) ,
[ options , originalName ] ,
) ;
const [ graphsVisibilityStates , setGraphsVisibilityStates ] = useState (
localStoredVisibilityStates ,
) ;
2021-09-23 15:43:43 +05:30
const getSelectedTime = useCallback (
( ) = >
timeItems . find ( ( e ) = > e . enum === ( widget ? . timePreferance || 'GLOBAL_TIME' ) ) ,
[ widget ] ,
) ;
2023-11-15 15:33:45 +05:30
const fullViewChartRef = useRef < ToggleGraphProps > ( ) ;
2023-08-02 20:41:09 +05:30
2021-09-23 15:43:43 +05:30
const [ selectedTime , setSelectedTime ] = useState < timePreferance > ( {
name : getSelectedTime ( ) ? . name || '' ,
enum : widget ? . timePreferance || 'GLOBAL_TIME' ,
} ) ;
2023-07-18 08:55:01 +05:30
const updatedQuery = useStepInterval ( widget ? . query ) ;
const response = useGetQueryRange (
{
selectedTime : selectedTime.enum ,
graphType : widget.panelTypes ,
query : updatedQuery ,
globalSelectedInterval : globalSelectedTime ,
2023-10-08 23:21:17 +05:30
variables : getDashboardVariables ( selectedDashboard ? . data . variables ) ,
2023-07-18 08:55:01 +05:30
} ,
{
2023-10-08 23:21:17 +05:30
queryKey : ` FullViewGetMetricsQueryRange- ${ selectedTime . enum } - ${ globalSelectedTime } - ${ widget . id } ` ,
2024-02-20 16:21:07 +05:30
enabled : ! isDependedDataLoaded && widget . panelTypes !== PANEL_TYPES . LIST , // Internally both the list view panel has it's own query range api call, so we don't need to call it again
2023-07-18 08:55:01 +05:30
} ,
2022-05-19 23:08:27 +05:30
) ;
2023-10-08 23:21:17 +05:30
const canModifyChart = useChartMutable ( {
panelType : widget.panelTypes ,
panelTypeAndGraphManagerVisibility : PANEL_TYPES_VS_FULL_VIEW_TABLE ,
} ) ;
2024-02-28 14:56:50 +05:30
if ( response . data && widget . panelTypes === PANEL_TYPES . BAR ) {
const sortedSeriesData = getSortedSeriesData (
response . data ? . payload . data . result ,
) ;
response . data . payload . data . result = sortedSeriesData ;
}
2024-01-24 15:37:34 +05:30
const chartData = getUPlotChartData ( response ? . data ? . payload , widget . fillSpans ) ;
2023-11-15 15:33:45 +05:30
const isDarkMode = useIsDarkMode ( ) ;
2023-01-25 20:31:42 +05:30
2023-12-14 22:56:25 +05:30
const [ minTimeScale , setMinTimeScale ] = useState < number > ( ) ;
const [ maxTimeScale , setMaxTimeScale ] = useState < number > ( ) ;
const { minTime , maxTime , selectedTime : globalSelectedInterval } = useSelector <
AppState ,
GlobalReducer
> ( ( state ) = > state . globalTime ) ;
useEffect ( ( ) : void = > {
const { startTime , endTime } = getTimeRange ( response ) ;
setMinTimeScale ( startTime ) ;
setMaxTimeScale ( endTime ) ;
} , [ maxTime , minTime , globalSelectedInterval , response ] ) ;
2023-10-08 23:21:17 +05:30
useEffect ( ( ) = > {
2023-11-15 15:33:45 +05:30
if ( ! response . isFetching && fullViewRef . current ) {
const width = fullViewRef . current ? . clientWidth
? fullViewRef . current . clientWidth - 45
: 700 ;
const height = fullViewRef . current ? . clientWidth
? fullViewRef . current . clientHeight
: 300 ;
const newChartOptions = getUPlotChartOptions ( {
yAxisUnit : yAxisUnit || '' ,
apiResponse : response.data?.payload ,
dimensions : {
height ,
width ,
} ,
isDarkMode ,
onDragSelect ,
graphsVisibilityStates ,
setGraphsVisibilityStates ,
2023-11-15 19:17:06 +05:30
thresholds : widget.thresholds ,
2023-12-14 22:56:25 +05:30
minTimeScale ,
maxTimeScale ,
2024-01-10 13:12:31 +05:30
softMax : widget.softMax === undefined ? null : widget . softMax ,
softMin : widget.softMin === undefined ? null : widget . softMin ,
2024-02-28 14:56:50 +05:30
panelType : widget.panelTypes ,
2023-10-08 23:21:17 +05:30
} ) ;
2023-11-15 15:33:45 +05:30
setChartOptions ( newChartOptions ) ;
2023-10-08 23:21:17 +05:30
}
2023-11-15 15:33:45 +05:30
// eslint-disable-next-line react-hooks/exhaustive-deps
} , [ response . isFetching , graphsVisibilityStates , fullViewRef . current ] ) ;
useEffect ( ( ) = > {
graphsVisibilityStates ? . forEach ( ( e , i ) = > {
fullViewChartRef ? . current ? . toggleGraph ( i , e ) ;
} ) ;
2024-01-22 16:36:03 +05:30
parentGraphVisibilityState ( graphsVisibilityStates ) ;
} , [ graphsVisibilityStates , parentGraphVisibilityState ] ) ;
2023-08-02 20:41:09 +05:30
2024-02-20 16:21:07 +05:30
const isListView = widget . panelTypes === PANEL_TYPES . LIST ;
2023-08-02 20:41:09 +05:30
if ( response . isFetching ) {
2022-05-19 23:08:27 +05:30
return < Spinner height = "100%" size = "large" tip = "Loading..." / > ;
2021-10-20 09:24:55 +05:30
}
2021-09-23 15:43:43 +05:30
return (
2023-11-15 15:33:45 +05:30
< div className = "full-view-container" >
< div className = "full-view-header-container" >
{ fullViewOptions && (
< TimeContainer $ panelType = { widget . panelTypes } >
< TimePreference
selectedTime = { selectedTime }
setSelectedTime = { setSelectedTime }
/ >
< Button
style = { {
marginLeft : '4px' ,
} }
onClick = { ( ) : void = > {
response . refetch ( ) ;
} }
type = "primary"
icon = { < SyncOutlined / > }
/ >
< / TimeContainer >
) }
< / div >
2023-11-29 00:02:51 +05:30
< div
2024-02-20 16:21:07 +05:30
className = { cx ( 'graph-container' , {
disabled : isDashboardLocked ,
'list-graph-container' : isListView ,
} ) }
2023-11-29 00:02:51 +05:30
ref = { fullViewRef }
>
2023-11-15 15:33:45 +05:30
{ chartOptions && (
< GraphContainer
2024-02-20 16:21:07 +05:30
style = { {
height : isListView ? '100%' : '90%' ,
} }
2023-11-15 15:33:45 +05:30
isGraphLegendToggleAvailable = { canModifyChart }
2022-05-19 23:08:27 +05:30
>
2023-11-15 15:33:45 +05:30
< GridPanelSwitch
panelType = { widget . panelTypes }
data = { chartData }
options = { chartOptions }
onClickHandler = { onClickHandler }
name = { name }
yAxisUnit = { yAxisUnit }
onDragSelect = { onDragSelect }
panelData = { response . data ? . payload . data . newResult . data . result || [ ] }
query = { widget . query }
ref = { fullViewChartRef }
2023-11-15 17:14:09 +05:30
thresholds = { widget . thresholds }
2024-02-20 16:21:07 +05:30
selectedLogFields = { widget . selectedLogFields }
dataSource = { widget . query . builder . queryData [ 0 ] . dataSource }
selectedTracesFields = { widget . selectedTracesFields }
selectedTime = { selectedTime }
2023-11-15 15:33:45 +05:30
/ >
< / GraphContainer >
) }
< / div >
2023-11-29 00:02:51 +05:30
{ canModifyChart && chartOptions && ! isDashboardLocked && (
2023-08-02 20:41:09 +05:30
< GraphManager
2023-11-15 15:33:45 +05:30
data = { chartData }
2024-01-22 16:36:03 +05:30
name = { originalName }
2023-11-15 15:33:45 +05:30
options = { chartOptions }
2023-08-02 20:41:09 +05:30
yAxisUnit = { yAxisUnit }
onToggleModelHandler = { onToggleModelHandler }
2023-10-08 23:21:17 +05:30
setGraphsVisibilityStates = { setGraphsVisibilityStates }
graphsVisibilityStates = { graphsVisibilityStates }
2023-11-15 15:33:45 +05:30
lineChartRef = { fullViewChartRef }
2023-10-08 23:21:17 +05:30
parentChartRef = { parentChartRef }
2023-08-02 20:41:09 +05:30
/ >
) }
2023-11-15 15:33:45 +05:30
< / div >
2021-09-23 15:43:43 +05:30
) ;
2022-03-22 12:10:31 +05:30
}
2021-09-23 15:43:43 +05:30
2022-03-24 12:06:57 +05:30
FullView . defaultProps = {
fullViewOptions : undefined ,
onClickHandler : undefined ,
yAxisUnit : undefined ,
2023-01-17 13:30:34 +02:00
onDragSelect : undefined ,
2023-07-18 08:55:01 +05:30
isDependedDataLoaded : undefined ,
2022-03-24 12:06:57 +05:30
} ;
2023-08-02 20:41:09 +05:30
FullView . displayName = 'FullView' ;
2021-11-16 21:13:20 +05:30
export default FullView ;