|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +import { useParams } from 'react-router-dom' |
| 15 | +import { useEffect, useRef, useState } from 'react' |
| 16 | +import { |
| 17 | + Alert, |
| 18 | + Box, |
| 19 | + CircularProgress, |
| 20 | + FormControl, |
| 21 | + Grid2 as Grid, |
| 22 | + InputLabel, |
| 23 | + MenuItem, |
| 24 | + Select, |
| 25 | + SelectChangeEvent, |
| 26 | +} from '@mui/material' |
| 27 | +import { type Edge, type Node, ReactFlow, useEdgesState, useNodesState } from '@xyflow/react' |
| 28 | +import { queryStatusApi, QueryStatusInfo, QueryStage } from '../api/webapp/api.ts' |
| 29 | +import { ApiResponse } from '../api/base.ts' |
| 30 | +import { Texts } from '../constant.ts' |
| 31 | +import { QueryProgressBar } from './QueryProgressBar.tsx' |
| 32 | +import { HelpMessage } from './flow/HelpMessage' |
| 33 | +import { nodeTypes, getLayoutedStagePerformanceElements } from './flow/layout' |
| 34 | +import { LayoutDirectionType } from './flow/types' |
| 35 | +import { getStagePerformanceFlowElements } from './flow/flowUtils.ts' |
| 36 | + |
| 37 | +interface IQueryStatus { |
| 38 | + info: QueryStatusInfo | null |
| 39 | + ended: boolean |
| 40 | +} |
| 41 | + |
| 42 | +export const QueryStagePerformance = () => { |
| 43 | + const { queryId } = useParams() |
| 44 | + const initialQueryStatus: IQueryStatus = { |
| 45 | + info: null, |
| 46 | + ended: false, |
| 47 | + } |
| 48 | + |
| 49 | + const [queryStatus, setQueryStatus] = useState<IQueryStatus>(initialQueryStatus) |
| 50 | + const [stagePlanIds, setStagePlanIds] = useState<string[]>([]) |
| 51 | + const [stagePlanId, setStagePlanId] = useState<string>() |
| 52 | + const [stage, setStage] = useState<QueryStage>() |
| 53 | + const [nodes, setNodes, onNodesChange] = useNodesState<Node>([]) |
| 54 | + const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]) |
| 55 | + const [layoutDirection, setLayoutDirection] = useState<LayoutDirectionType>('BT') |
| 56 | + |
| 57 | + const [loading, setLoading] = useState<boolean>(true) |
| 58 | + const [error, setError] = useState<string | null>(null) |
| 59 | + const queryStatusRef = useRef(queryStatus) |
| 60 | + const containerRef = useRef<HTMLDivElement>(null) |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + queryStatusRef.current = queryStatus |
| 64 | + }, [queryStatus]) |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + if (queryStatus.info?.stages) { |
| 68 | + const allStagePlanIds = queryStatus.info.stages.stages.map((stage) => stage.plan.id) |
| 69 | + setStagePlanIds(allStagePlanIds) |
| 70 | + setStagePlanId(allStagePlanIds[0]) |
| 71 | + } |
| 72 | + }, [queryStatus]) |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (queryStatus.ended && stagePlanId) { |
| 76 | + const stage = queryStatus.info?.stages.stages.find((stage) => stage.plan.id === stagePlanId) |
| 77 | + setStage(stage) |
| 78 | + |
| 79 | + if (stage) { |
| 80 | + const flowElements = getStagePerformanceFlowElements(stage, layoutDirection) |
| 81 | + const layoutedElements = getLayoutedStagePerformanceElements(flowElements.nodes, flowElements.edges, { |
| 82 | + direction: layoutDirection, |
| 83 | + }) |
| 84 | + |
| 85 | + setNodes(layoutedElements.nodes) |
| 86 | + setEdges(layoutedElements.edges) |
| 87 | + } |
| 88 | + } |
| 89 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 90 | + }, [queryStatus, stagePlanId, layoutDirection]) |
| 91 | + |
| 92 | + useEffect(() => { |
| 93 | + const runLoop = () => { |
| 94 | + const queryEnded = !!queryStatusRef.current.info?.finalQueryInfo |
| 95 | + if (!queryEnded) { |
| 96 | + getQueryStatus() |
| 97 | + setTimeout(runLoop, 3000) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (queryId) { |
| 102 | + queryStatusRef.current = initialQueryStatus |
| 103 | + } |
| 104 | + |
| 105 | + runLoop() |
| 106 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 107 | + }, [queryId]) |
| 108 | + |
| 109 | + const getQueryStatus = () => { |
| 110 | + if (queryId) { |
| 111 | + queryStatusApi(queryId, false).then((apiResponse: ApiResponse<QueryStatusInfo>) => { |
| 112 | + setLoading(false) |
| 113 | + if (apiResponse.status === 200 && apiResponse.data) { |
| 114 | + setQueryStatus({ |
| 115 | + info: apiResponse.data, |
| 116 | + ended: apiResponse.data.finalQueryInfo, |
| 117 | + }) |
| 118 | + setError(null) |
| 119 | + } else { |
| 120 | + setError(`${Texts.Error.Communication} ${apiResponse.status}: ${apiResponse.message}`) |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const smallFormControlSx = { |
| 127 | + fontSize: '0.8rem', |
| 128 | + } |
| 129 | + |
| 130 | + const smallDropdownMenuPropsSx = { |
| 131 | + PaperProps: { |
| 132 | + sx: { |
| 133 | + '& .MuiMenuItem-root': smallFormControlSx, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + const handleStageIdChange = (event: SelectChangeEvent) => { |
| 139 | + setStagePlanId(event.target.value as string) |
| 140 | + } |
| 141 | + |
| 142 | + return ( |
| 143 | + <> |
| 144 | + {loading && <CircularProgress />} |
| 145 | + {error && <Alert severity="error">{Texts.Error.QueryNotFound}</Alert>} |
| 146 | + |
| 147 | + {!loading && !error && queryStatus.info && ( |
| 148 | + <Grid container spacing={0}> |
| 149 | + <Grid size={{ xs: 12 }}> |
| 150 | + <Box sx={{ pt: 2 }}> |
| 151 | + <Box sx={{ width: '100%' }}> |
| 152 | + <QueryProgressBar queryInfoBase={queryStatus.info} /> |
| 153 | + </Box> |
| 154 | + |
| 155 | + {queryStatus.ended ? ( |
| 156 | + <Grid container spacing={3}> |
| 157 | + <Grid size={{ xs: 12, md: 12 }}> |
| 158 | + <Box sx={{ pt: 2 }}> |
| 159 | + <Box |
| 160 | + ref={containerRef} |
| 161 | + sx={{ width: '100%', height: '80vh', border: '1px solid #ccc' }} |
| 162 | + > |
| 163 | + {stage ? ( |
| 164 | + <ReactFlow |
| 165 | + nodes={nodes} |
| 166 | + edges={edges} |
| 167 | + onNodesChange={onNodesChange} |
| 168 | + onEdgesChange={onEdgesChange} |
| 169 | + nodeTypes={nodeTypes} |
| 170 | + minZoom={0.1} |
| 171 | + proOptions={{ hideAttribution: true }} |
| 172 | + defaultViewport={{ x: 200, y: 20, zoom: 0.8 }} |
| 173 | + > |
| 174 | + <HelpMessage |
| 175 | + layoutDirection={layoutDirection} |
| 176 | + onLayoutDirectionChange={setLayoutDirection} |
| 177 | + additionalContent={ |
| 178 | + <Box sx={{ mt: 2 }}> |
| 179 | + <FormControl size="small" sx={{ minWidth: 200 }}> |
| 180 | + <InputLabel sx={smallFormControlSx}> |
| 181 | + Stage |
| 182 | + </InputLabel> |
| 183 | + <Select |
| 184 | + label="Stage" |
| 185 | + sx={smallFormControlSx} |
| 186 | + MenuProps={smallDropdownMenuPropsSx} |
| 187 | + value={stagePlanId} |
| 188 | + onChange={handleStageIdChange} |
| 189 | + > |
| 190 | + {stagePlanIds.map((stageId) => ( |
| 191 | + <MenuItem key={stageId} value={stageId}> |
| 192 | + {stageId} |
| 193 | + </MenuItem> |
| 194 | + ))} |
| 195 | + </Select> |
| 196 | + </FormControl> |
| 197 | + </Box> |
| 198 | + } |
| 199 | + /> |
| 200 | + </ReactFlow> |
| 201 | + ) : ( |
| 202 | + <Alert severity="error">Stage not found.</Alert> |
| 203 | + )} |
| 204 | + </Box> |
| 205 | + </Box> |
| 206 | + </Grid> |
| 207 | + </Grid> |
| 208 | + ) : ( |
| 209 | + <> |
| 210 | + <Box sx={{ width: '100%', mt: 1 }}> |
| 211 | + <Alert severity="info"> |
| 212 | + Operator graph will appear automatically when query completes. |
| 213 | + </Alert> |
| 214 | + </Box> |
| 215 | + </> |
| 216 | + )} |
| 217 | + </Box> |
| 218 | + </Grid> |
| 219 | + </Grid> |
| 220 | + )} |
| 221 | + </> |
| 222 | + ) |
| 223 | +} |
0 commit comments