imbajin commented on code in PR #891: URL: https://github.com/apache/hugegraph-toolchain/pull/891#discussion_r4446256966
########## hugegraph-hubble/hubble-fe/src/modules/algorithm/GraphResult/OlapPreview/index.js: ########## @@ -0,0 +1,227 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +import {useEffect, useMemo, useRef, useState} from 'react'; +import {Alert, Button, Empty, Segmented, Spin, Table} from 'antd'; +import {useTranslation} from 'react-i18next'; +import * as api from '../../../../api'; +import {Link} from 'react-router-dom'; +import {GraphCanvas} from '../../../../visualization/react'; +import {adaptHubbleGraph} from '../../../../visualization/hubble/adapter'; +import c from './index.module.scss'; + +const PAGE_SIZE = 30; +const MAX_POINTS = 300; +const PREVIEW_LAYOUT = {type: 'grid', options: {nodeSize: 54, nodeSpacing: 36}}; +const valueText = value => typeof value === 'string' ? value : JSON.stringify(value ?? null); +const compact = (value, max) => value.length > max ? `${value.slice(0, max - 1)}…` : value; + +// A result is an ID/value pair, not a graph vertex or an edge. Keep this view +// read-only and do not feed synthetic elements into the editable graph canvas. +const OlapPreview = ({graphSpace, graph, taskId}) => { + const {t} = useTranslation(); + const [state, setState] = useState({phase: 'waiting'}); + const [mode, setMode] = useState('graph'); + const [canvasController, setCanvasController] = useState(null); + const canvasHost = useRef(null); + const [cursor, setCursor] = useState(null); + const [requestPage, setRequestPage] = useState(0); + const [statusAttempt, setStatusAttempt] = useState(0); + const accumulated = useRef([]); + const accumulatedEdges = useRef(new Map()); + const edgePages = useRef(new Map()); + + useEffect(() => { + let active = true; + let timer; + let misses = 0; + let seen = false; + const load = async () => { + try { + const response = await api.analysis.fetchManageTaskById(graphSpace, graph, taskId); + if (!active) return; + if (response?.status !== 200 || !Array.isArray(response.data)) throw new Error(); + const task = response.data.find(item => String(item.id) === String(taskId)); + if (!task) { + misses += 1; + if (seen || misses >= 12) { setState({phase: 'not_found'}); return; } + timer = setTimeout(load, 5000); + return; + } + seen = true; + if (['queued', 'running', 'pending'].includes(task.task_status)) { + timer = setTimeout(load, 5000); + return; + } + if (task.task_status !== 'success') { + setState({phase: 'failed'}); + return; + } + setState({phase: 'loading'}); + setCursor(0); + setRequestPage(page => page + 1); + } catch { + if (active) setState({phase: 'status_error'}); + } + }; + load(); + return () => { active = false; clearTimeout(timer); }; + }, [graphSpace, graph, statusAttempt, taskId]); + + useEffect(() => { + if (!requestPage || cursor === null) return undefined; + let active = true; + const load = async () => { + try { + const response = await api.analysis.fetchOlapPreviewEdges(graphSpace, graph, taskId, cursor, PAGE_SIZE); + if (!active) return; + if (response?.status === 200 && response.data?.result_status === 'expired') { + setState(previous => previous.vertices ? {...previous, phase: 'ready', resultExpired: true, + hasMore: false} : {phase: 'expired'}); + return; + } + if (response?.status !== 200 || !Array.isArray(response.data?.vertices) || + response.data.vertices.length > PAGE_SIZE || + (response.data.has_more === true && + (!Number.isSafeInteger(response.data.cursor) || response.data.cursor <= cursor))) { + throw new Error(); + } + const pageVertices = response.data.vertices; + const vertices = [...(cursor === 0 ? [] : accumulated.current), ...pageVertices]; + accumulated.current = vertices; + if (cursor === 0) { accumulatedEdges.current = new Map(); edgePages.current = new Map(); } + const ids = new Set(pageVertices.map(item => String(item.ID))); + const edges = response.data.edges; + let edgeStatus = 'unavailable'; + if (response.data.edges_status === 'available' && Array.isArray(edges) && edges.length <= 30 && + edges.every(edge => typeof edge.source === 'string' && typeof edge.target === 'string' && + ids.has(edge.source) && ids.has(edge.target))) { + for (const edge of edges) { + const key = JSON.stringify([edge.source, edge.target]); + accumulatedEdges.current.set(key, {id: key, source: edge.source, target: edge.target}); + } + edgeStatus = 'partial'; + } + if (pageVertices.length) { + edgePages.current.set(cursor, {failed: edgeStatus !== 'partial', count: pageVertices.length, + truncated: response.data.truncated === true}); + } + const missing = [...edgePages.current.entries()].filter(([, page]) => page.failed) + .map(([start, page]) => `${start + 1}–${start + page.count}`).join(', '); + setState({phase: 'ready', vertices, edges: [...accumulatedEdges.current.values()], + edgeStatus, missingEdgePages: missing, + next: response.data.cursor, hasMore: response.data.has_more === true}); + } catch { + if (active) setState(previous => previous.vertices + ? {...previous, phase: 'ready', pageError: true} + : {phase: 'error'}); + } + }; + load(); + return () => { active = false; }; + }, [cursor, graph, graphSpace, requestPage, taskId]); + + // GraphCanvas preserves user zoom on ordinary resizes. This bounded preview + // needs to fit again when its host changes width, including sidebar changes. + useEffect(() => { + const host = canvasHost.current; + if (mode !== 'graph' || !host || !canvasController || typeof ResizeObserver === 'undefined') return undefined; + let timer; + let previous = ''; + const observer = new ResizeObserver(entries => { + const rect = entries[0]?.contentRect; + if (!rect || rect.width <= 0 || rect.height <= 0) return; + const size = `${Math.round(rect.width)}:${Math.round(rect.height)}`; + if (size === previous) return; + previous = size; + clearTimeout(timer); + timer = setTimeout(() => { void canvasController.setLayout(PREVIEW_LAYOUT).catch(() => {}); }, 150); + }); + observer.observe(host); + return () => { clearTimeout(timer); observer.disconnect(); }; + }, [canvasController, mode, state.phase]); + + const vertices = useMemo(() => state.vertices || [], [state.vertices]); + const graphData = useMemo(() => { + const adapted = adaptHubbleGraph({vertices: vertices.map(item => ({id: String(item.ID), + properties: {result: valueText(item.Value)}})), edges: state.edges || []}); + return {...adapted, nodes: adapted.nodes.map((node, index) => ({...node, + style: {...node.style, size: 42, labelFontSize: 12, + labelText: `${compact(String(vertices[index].ID), 10)}\n${compact(valueText(vertices[index].Value), 10)}`}, + })), edges: adapted.edges.map(edge => ({...edge, style: {...edge.style, labelText: ''}}))}; + }, [vertices, state.edges]); + const columns = [{title: t('analysis.algorithm.preview_id'), dataIndex: 'ID', render: value => String(value)}, + {title: t('analysis.algorithm.preview_value'), dataIndex: 'Value', render: valueText}]; + return <div className={c.preview}> + <div className={c.header}> + <strong>{t('analysis.algorithm.preview_title')}</strong> + <Link to={`/asyncTasks/${encodeURIComponent(graphSpace)}/${encodeURIComponent(graph)}` + + `?taskId=${encodeURIComponent(taskId)}&submitted=1`}> + {t('analysis.algorithm.preview_task_detail')} + </Link> + </div> + {state.phase === 'waiting' && <Spin description={t('analysis.algorithm.preview_running')} />} + {state.phase === 'loading' && <Spin description={t('analysis.algorithm.preview_loading')} />} + {['status_error', 'not_found'].includes(state.phase) && <Alert type='warning' showIcon + title={t(state.phase === 'not_found' ? 'analysis.algorithm.preview_not_found' : 'analysis.algorithm.preview_error')} + action={<Button onClick={() => { setState({phase: 'waiting'}); setStatusAttempt(value => value + 1); }}> + {t('analysis.async_task.retry_result')} + </Button>} />} + {state.phase === 'failed' && <Alert type='error' showIcon title={t('analysis.algorithm.preview_failed')} />} + {state.phase === 'expired' && <Alert type='warning' showIcon title={t('analysis.algorithm.preview_expired')} />} + {state.phase === 'error' && <Alert type='error' showIcon title={t('analysis.algorithm.preview_error')} + action={<Button onClick={() => { setState({phase: 'loading'}); setRequestPage(page => page + 1); }}> + {t('analysis.async_task.retry_result')} + </Button>} />} + {state.phase === 'ready' && <> + {state.resultExpired && <Alert type='warning' showIcon title={t('analysis.algorithm.preview_expired')} />} + {state.pageError && <Alert type='error' showIcon title={t('analysis.algorithm.preview_error')} + action={<Button onClick={() => setRequestPage(page => page + 1)}> + {t('analysis.async_task.retry_result')} + </Button>} />} + <Alert type='info' showIcon title={t('analysis.algorithm.preview_sample', {count: vertices.length})} + description={t('analysis.algorithm.preview_no_edges')} /> + {state.edgeStatus === 'loading' && <Spin description={t('analysis.algorithm.preview_edges_loading')} />} + {state.edgeStatus === 'unavailable' && !state.missingEdgePages && <Alert type='warning' showIcon + title={t('analysis.algorithm.preview_edges_unavailable')} />} + {state.edgeStatus === 'partial' && <Alert type='info' showIcon + title={t('analysis.algorithm.preview_edges_partial', {count: state.edges.length})} />} Review Comment: [P3] Keep the provenance warning visible for retained edges If page 1 returns edges and page 2 falls back to node-only results, the accumulated edges remain visible but edgeStatus becomes unavailable. This condition then hides the warning that relationships come from the current snapshot (not necessarily the task snapshot) and lack original labels/properties. Please keep that qualification visible while accumulated edges or a successful edge page remain, and add the successful-first-page / unavailable-next-page regression case. This is an independently cross-checked static rendering-path finding, not a browser reproduction or evidence of incorrect graph writes. Reviewed source: https://github.com/apache/hugegraph-toolchain/blob/7bcc9af90b0b8be3f9150b644106e78441f10e80/hugegraph-hubble/hubble-fe/src/modules/algorithm/GraphResult/OlapPreview/index.js#L201-L202 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
