mchades commented on code in PR #12900: URL: https://github.com/apache/gravitino/pull/12900#discussion_r4278752652
########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelDetailsPage.js: ########## @@ -0,0 +1,755 @@ +/* + * 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. + */ + +'use client' + +import { useMemo, useState } from 'react' +import { + Button, + Divider, + Flex, + Input, + Modal, + Popover, + Space, + Spin, + Table, + Tabs, + Tag, + Tooltip, + Typography, + message +} from 'antd' +import { useAntdColumnResize } from 'react-antd-column-resize' +import { useRouter, useSearchParams } from 'next/navigation' +import Tags from '@/components/CustomTags' +import GetOwner from '@/components/GetOwner' +import Icons from '@/components/Icons' +import PropertiesContent from '@/components/PropertiesContent' +import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore' +import { getSemanticModelDetails } from '@/lib/store/metalakes' +import { copyToClipboard } from '@/lib/utils' +import { formatToDateTime, isValidDate } from '@/lib/utils/date' +import { formatSemanticModelDefinition } from '@/lib/utils/semanticModel' +import { ColumnTypeColorEnum } from '@/config' +import SemanticModelExpression from './SemanticModelExpression' +import SemanticModelAIContext from './SemanticModelAIContext' +import SemanticModelExport from './SemanticModelExport' +import SemanticModelExtensions from './SemanticModelExtensions' +import CreateSemanticModelDialog from '../CreateSemanticModelDialog' +import SemanticMetadataEditor from '../SemanticMetadataEditor' + +const { Title, Paragraph } = Typography + +const sourceFullName = source => { + if (!source) { + return '-' + } + + return [...(source.namespace || []), source.name].filter(Boolean).join('.') +} + +const renderDataType = datatype => { + if (!datatype) return <span>-</span> + const type = { DateTime: 'timestamp', DateTimeTz: 'timestamp_tz' }[datatype] || datatype.toLowerCase() + + return <Tag color={ColumnTypeColorEnum[type] || undefined}>{datatype}</Tag> +} + +const topAlignedColumns = columns => + columns.map(column => ({ ...column, onCell: () => ({ style: { verticalAlign: 'top' } }) })) + +const renderColumns = columns => <span className='font-mono text-xs'>{(columns || []).join(', ') || '-'}</span> + +const hasMetadata = entity => !!entity.aiContext || entity.customExtensions?.length > 0 + +export default function SemanticModelDetailsPage() { + const searchParams = useSearchParams() + const router = useRouter() + const dispatch = useAppDispatch() + const metalake = searchParams.get('metalake') + const catalog = searchParams.get('catalog') + const schema = searchParams.get('schema') + const semanticModelName = searchParams.get('semanticModel') + const auth = useAppSelector(state => state.auth) + const { anthEnable } = auth + const store = useAppSelector(state => state.metalakes) + const semanticModel = store.activatedDetails + const [tabKey, setTabKey] = useState('Datasets') + const [openEdit, setOpenEdit] = useState(false) + const [metadataEdit, setMetadataEdit] = useState(null) + const [fieldContext, setFieldContext] = useState(null) + const [fieldSearch, setFieldSearch] = useState('') + const [metricSearch, setMetricSearch] = useState('') + const [expandedDatasets, setExpandedDatasets] = useState([]) + + const definition = semanticModel?.definition + const properties = semanticModel?.properties + const createdAt = semanticModel?.audit?.createTime + const createdAtText = !createdAt || !isValidDate(createdAt) ? '-' : formatToDateTime(createdAt) + const lastModifiedAt = semanticModel?.audit?.lastModifiedTime + const lastModifiedText = !lastModifiedAt || !isValidDate(lastModifiedAt) ? '-' : formatToDateTime(lastModifiedAt) + const metadataObjectFullName = `${catalog}.${schema}.${semanticModelName}` + + const datasets = useMemo( + () => (definition?.datasets || []).map(dataset => ({ ...dataset, key: dataset.name })), + [definition] + ) + + const relationships = useMemo( + () => (definition?.relationships || []).map(relationship => ({ ...relationship, key: relationship.name })), + [definition] + ) + + const metrics = useMemo( + () => (definition?.metrics || []).map(metric => ({ ...metric, key: metric.name })), + [definition] + ) + + const fieldQuery = fieldSearch.trim().toLowerCase() + + const filteredDatasets = useMemo( + () => + fieldQuery + ? datasets + .map(dataset => ({ + ...dataset, + fields: (dataset.fields || []).filter(field => + [field.name, field.label].some(value => value?.toLowerCase().includes(fieldQuery)) + ) + })) + .filter(dataset => dataset.fields.length) + : datasets, + [datasets, fieldQuery] + ) + + const filteredMetrics = useMemo( + () => metrics.filter(metric => metric.name.toLowerCase().includes(metricSearch.trim().toLowerCase())), + [metrics, metricSearch] + ) + + const renderMetadata = (entity, label, target) => ( + <> + <SemanticModelAIContext + context={entity?.aiContext} + label={`${label} AI Context`} + onEdit={target ? () => setMetadataEdit({ ...target, section: 'context' }) : undefined} + /> + <SemanticModelExtensions extensions={entity?.customExtensions} label={`${label} Vendor Extensions`} /> + </> + ) + + const definitionText = useMemo(() => formatSemanticModelDefinition(definition), [definition]) + + const tagContent = ( + <div> + <Tags readOnly={true} metadataObjectType={'semantic_model'} metadataObjectFullName={metadataObjectFullName} /> + </div> + ) + + const propertyContent = <PropertiesContent properties={properties} /> + + const onSaved = async updated => { + if (updated.name !== semanticModelName) { + const params = new URLSearchParams(searchParams.toString()) + params.set('semanticModel', updated.name) + router.replace(`/catalogs?${params.toString()}`) + } else { + await dispatch(getSemanticModelDetails({ metalake, catalog, schema, semanticModel: updated.name, init: true })) + } + } + + const onCopyDefinition = async () => { + try { + await copyToClipboard(definitionText) + message.success('Definition copied!') + } catch (err) { + console.error('Failed to copy definition: ', err) + message.error('Failed to copy definition') + } + } + + const datasetColumns = useMemo( + () => [ + { + title: 'Dataset Name', + dataIndex: 'name', + key: 'name', + width: 200, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { + title: 'Source', + dataIndex: 'source', + key: 'source', + width: 260, + ellipsis: true, + render: source => <span className='font-mono text-xs'>{sourceFullName(source)}</span> + }, + { + title: 'Primary Key', + dataIndex: 'primaryKey', + key: 'primaryKey', + width: 180, + ellipsis: true, + render: renderColumns + }, + { + title: 'Unique Keys', + dataIndex: 'uniqueKeys', + key: 'uniqueKeys', + width: 220, + render: keys => + keys?.length ? ( + <Space direction='vertical' size={4}> + {keys.map((columns, index) => ( + <Tag key={index}>{renderColumns(columns)}</Tag> + ))} + </Space> + ) : ( + <span>-</span> + ) + }, + { + title: 'Fields', + dataIndex: 'fields', + key: 'fields', + width: 80, + render: fields => <span>{(fields || []).length}</span> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit dataset ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'dataset', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const fieldColumns = useMemo( + () => [ + { + title: 'Field Name', + dataIndex: 'name', + key: 'name', + width: 220, + render: (name, field) => ( + <div className='break-words'> + <div>{name}</div> + {field.label && <Typography.Text type='secondary'>{field.label}</Typography.Text>} + <div> + {' '} + {field.aiContext && (typeof field.aiContext === 'string' || Object.keys(field.aiContext).length > 0) && ( + <Button + size='small' + type='link' + aria-label={`${field.name} AI Context`} + onClick={() => setFieldContext(field)} + > + AI Context + </Button> + )} + </div> + </div> + ) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: renderDataType + }, + { + title: 'Time Dimension', + dataIndex: 'dimension', + key: 'dimension', + width: 140, + render: dimension => <span>{dimension?.isTime ? 'Yes' : 'No'}</span> + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: (expression, record) => <SemanticModelExpression expression={expression} name={record.name} /> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + } + ], + [] + ) + + const relationshipColumns = useMemo( + () => [ + { + title: 'Relationship Name', + dataIndex: 'name', + key: 'name', + width: 220, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { title: 'From', dataIndex: 'from', key: 'from', width: 160, ellipsis: true }, + { + title: 'From Columns', + dataIndex: 'fromColumns', + key: 'fromColumns', + width: 200, + ellipsis: true, + render: renderColumns + }, + { title: 'To', dataIndex: 'to', key: 'to', width: 160, ellipsis: true }, + { + title: 'To Columns', + dataIndex: 'toColumns', + key: 'toColumns', + ellipsis: true, + render: renderColumns + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit relationship ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'relationship', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const metricColumns = useMemo( + () => [ + { + title: 'Metric Name', + dataIndex: 'name', + key: 'name', + width: 200, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: renderDataType + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: (expression, record) => <SemanticModelExpression expression={expression} name={record.name} /> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit metric ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'metric', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const tabOptions = [ + { label: 'Overview', key: 'Overview' }, + { label: 'Datasets', key: 'Datasets' }, + { label: 'Relationships', key: 'Relationships' }, + { label: 'Metrics', key: 'Metrics' }, + { label: 'Definition', key: 'Definition' } + ] + + const { + resizableColumns: datasetResizableColumns, + components: datasetComponents, + tableWidth: datasetTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(datasetColumns), minWidth: 100 } + }, [datasetColumns]) + + const { + resizableColumns: relationshipResizableColumns, + components: relationshipComponents, + tableWidth: relationshipTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(relationshipColumns), minWidth: 100 } + }, [relationshipColumns]) + + const { + resizableColumns: metricResizableColumns, + components: metricComponents, + tableWidth: metricTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(metricColumns), minWidth: 100 } + }, [metricColumns]) + + const expandedFieldsRow = dataset => ( + <div> + {renderMetadata(dataset, dataset.name, { kind: 'dataset', name: dataset.name })} + <Table Review Comment: UX (non-blocking): Simplify the expanded dataset layout Expanding a dataset puts metadata accordions and another wide table inside the parent table's scroll area. Please retain dataset grouping but use a lighter field section with spacing/dividers, and move lengthy AI Context and vendor details into an on-demand detail panel instead of growing the nested row. Keep entity names and Edit actions visible during horizontal scrolling. This complements the scrollbar fix: removing a border alone will not make the nested content easier to navigate. ########## web-v2/web/src/app/catalogs/rightContent/ImportSemanticModelDialog.js: ########## @@ -0,0 +1,175 @@ +/* + * 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, useRef, useState } from 'react' +import { Alert, Button, Descriptions, Modal, Space, Typography, Upload, message } from 'antd' +import { UploadOutlined } from '@ant-design/icons' +import { importSemanticModelApi } from '@/lib/api/semanticModels' +import { useAppDispatch } from '@/lib/hooks/useStore' +import { fetchSemanticModels } from '@/lib/store/metalakes' +import { parseOssiePreview } from '@/lib/utils/ossiePreview' +import { semanticModelErrorMessage } from '@/lib/utils/semanticModel' + +export default function ImportSemanticModelDialog({ open, setOpen, metalake, catalog, schema }) { + const dispatch = useAppDispatch() + const [file, setFile] = useState(null) + const [loading, setLoading] = useState(false) + const [error, setError] = useState('') + const [selection, setSelection] = useState(null) + const [reading, setReading] = useState(false) + const selectionId = useRef(0) + useEffect( + () => () => { + selectionId.current++ + }, + [] + ) + + const selectFile = async selected => { + const id = ++selectionId.current + setError('') + setSelection(null) + setFile(selected) + setReading(true) + try { + const document = await selected.text() + const format = selected.name.toLowerCase().endsWith('.json') ? 'json' : 'yaml' + const preview = parseOssiePreview(document, format) + if (id === selectionId.current) setSelection({ document, format, preview }) + } catch (err) { + if (id === selectionId.current) setError(semanticModelErrorMessage(err, 'Unable to read the OSSIE document.')) + } finally { + if (id === selectionId.current) setReading(false) + } + } + + const importDocument = async () => { + if (!selection || reading || loading) return + setLoading(true) + setError('') + try { + const { document, format } = selection + + const response = await importSemanticModelApi({ + metalake, + catalog, + schema, + document, + format + }) + if (!response?.semanticModel) throw new Error('Import returned an empty response.') + await dispatch(fetchSemanticModels({ metalake, catalog, schema, init: true })) + message.success('Semantic Model imported') + setOpen(false) + } catch (err) { + setError(semanticModelErrorMessage(err, 'Failed to import the OSSIE document.')) + } finally { + setLoading(false) + } + } + + return ( + <Modal + title='Import OSSIE document' + style={{ top: 24 }} + styles={{ body: { maxHeight: 'calc(100vh - 160px)', overflowY: 'auto' } }} + open={open} + onCancel={() => setOpen(false)} + onOk={importDocument} + okText='Import' + confirmLoading={loading} + okButtonProps={{ + disabled: !selection || reading, + 'data-refer': 'import-semantic-model-submit', + 'aria-label': 'Import' + }} + cancelButtonProps={{ disabled: loading }} + closable={!loading} + maskClosable={!loading} + keyboard={!loading} + > + <Space direction='vertical' className='w-full' size='middle'> + <Typography.Paragraph className='mb-0'> + Select a standalone OSSIE YAML or JSON document to create a Semantic Model in this schema. + </Typography.Paragraph> + <Upload + accept='.yaml,.yml,.json' + maxCount={1} + disabled={loading} + fileList={file ? [file] : []} + beforeUpload={selected => { + setError('') + if (!/\.(ya?ml|json)$/i.test(selected.name)) { + selectionId.current++ + setSelection(null) + setReading(false) + setFile(null) + setError('Select a .yaml, .yml, or .json file.') + + return Upload.LIST_IGNORE + } + selectFile(selected) + + return false + }} + onRemove={() => { + selectionId.current++ + setSelection(null) + setReading(false) + setFile(null) + setError('') + }} + > + <Button icon={<UploadOutlined />} disabled={loading}> + Select file + </Button> + </Upload> + {reading && <Typography.Text>Reading document...</Typography.Text>} + {selection && ( + <> + <Descriptions bordered size='small' column={2} title='Import preview'> + <Descriptions.Item label='Model' span={2}> + {selection.preview.name} + </Descriptions.Item> + <Descriptions.Item label='Destination' span={2}> + {metalake} / {catalog} / {schema} + </Descriptions.Item> + {['datasets', 'fields', 'relationships', 'metrics'].map(key => ( + <Descriptions.Item key={key} label={key[0].toUpperCase() + key.slice(1)}> + {selection.preview[key]} + </Descriptions.Item> + ))} + </Descriptions> + <Typography.Text type='secondary'> + The server will validate the complete document and its sources on import. + </Typography.Text> + </> + )} + {error && ( + <Alert + type='error' + showIcon + message='Import failed' Review Comment: UX: Distinguish file-reading errors from import failures This heading is also used when selectFile rejects malformed YAML/JSON or an empty document, before Import is clicked and before any request is sent. Label that state as a file/preview validation error, and reserve "Import failed" for a submitted request. Keep the specific cause and a clear correction path, such as replacing the file; preserve the existing retained-file and retry behavior. Cover both stages so users can tell whether an import was attempted. ########## web-v2/web/src/app/catalogs/rightContent/ImportSemanticModelDialog.js: ########## @@ -0,0 +1,175 @@ +/* + * 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, useRef, useState } from 'react' +import { Alert, Button, Descriptions, Modal, Space, Typography, Upload, message } from 'antd' +import { UploadOutlined } from '@ant-design/icons' +import { importSemanticModelApi } from '@/lib/api/semanticModels' +import { useAppDispatch } from '@/lib/hooks/useStore' +import { fetchSemanticModels } from '@/lib/store/metalakes' +import { parseOssiePreview } from '@/lib/utils/ossiePreview' +import { semanticModelErrorMessage } from '@/lib/utils/semanticModel' + +export default function ImportSemanticModelDialog({ open, setOpen, metalake, catalog, schema }) { + const dispatch = useAppDispatch() + const [file, setFile] = useState(null) + const [loading, setLoading] = useState(false) + const [error, setError] = useState('') + const [selection, setSelection] = useState(null) + const [reading, setReading] = useState(false) + const selectionId = useRef(0) + useEffect( + () => () => { + selectionId.current++ + }, + [] + ) + + const selectFile = async selected => { + const id = ++selectionId.current + setError('') + setSelection(null) + setFile(selected) + setReading(true) + try { + const document = await selected.text() + const format = selected.name.toLowerCase().endsWith('.json') ? 'json' : 'yaml' + const preview = parseOssiePreview(document, format) + if (id === selectionId.current) setSelection({ document, format, preview }) + } catch (err) { + if (id === selectionId.current) setError(semanticModelErrorMessage(err, 'Unable to read the OSSIE document.')) + } finally { + if (id === selectionId.current) setReading(false) + } + } + + const importDocument = async () => { + if (!selection || reading || loading) return + setLoading(true) + setError('') + try { + const { document, format } = selection + + const response = await importSemanticModelApi({ + metalake, + catalog, + schema, + document, + format + }) + if (!response?.semanticModel) throw new Error('Import returned an empty response.') + await dispatch(fetchSemanticModels({ metalake, catalog, schema, init: true })) + message.success('Semantic Model imported') + setOpen(false) Review Comment: UX (non-blocking): Offer a direct path to the imported model Success refreshes the list/tree and closes the dialog with a generic toast, so users must find the new model again to inspect it. Include the returned model name and a "View model" action in the success feedback, or select/highlight its row with an equivalent action. Use the successful response's model identity and retain the list/tree refresh so users can immediately verify the imported result. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelAIContext.js: ########## @@ -0,0 +1,107 @@ +/* + * 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 { Button, Tag, theme } from 'antd' +import SemanticMetadataSection from './SemanticMetadataSection' + +export default function SemanticModelAIContext({ context, label = 'AI Context', inline = false, onEdit }) { + const { token } = theme.useToken() + if (!context || (typeof context === 'object' && !Object.keys(context).length)) return null + + const additional = + typeof context === 'object' + ? Object.fromEntries( + Object.entries(context).filter(([key]) => !['instructions', 'synonyms', 'examples'].includes(key)) + ) + : {} + + const content = ( + <div className='max-h-64 overflow-auto whitespace-pre-wrap break-words'> Review Comment: UX (non-blocking): Avoid a second scroll area for prose in Overview At 1280x600, the sample model's AI Context has 343px of content inside this 256px scroller, while the surrounding detail panel also scrolls. Reading the guidance requires switching between the two scroll areas. Let prose flow in the Overview content area, or offer an explicit expanded reader; keep bounded scrolling scoped to standalone detail/code viewers. Preserve the existing section headings and metadata content. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelDetailsPage.js: ########## @@ -0,0 +1,755 @@ +/* + * 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. + */ + +'use client' + +import { useMemo, useState } from 'react' +import { + Button, + Divider, + Flex, + Input, + Modal, + Popover, + Space, + Spin, + Table, + Tabs, + Tag, + Tooltip, + Typography, + message +} from 'antd' +import { useAntdColumnResize } from 'react-antd-column-resize' +import { useRouter, useSearchParams } from 'next/navigation' +import Tags from '@/components/CustomTags' +import GetOwner from '@/components/GetOwner' +import Icons from '@/components/Icons' +import PropertiesContent from '@/components/PropertiesContent' +import { useAppDispatch, useAppSelector } from '@/lib/hooks/useStore' +import { getSemanticModelDetails } from '@/lib/store/metalakes' +import { copyToClipboard } from '@/lib/utils' +import { formatToDateTime, isValidDate } from '@/lib/utils/date' +import { formatSemanticModelDefinition } from '@/lib/utils/semanticModel' +import { ColumnTypeColorEnum } from '@/config' +import SemanticModelExpression from './SemanticModelExpression' +import SemanticModelAIContext from './SemanticModelAIContext' +import SemanticModelExport from './SemanticModelExport' +import SemanticModelExtensions from './SemanticModelExtensions' +import CreateSemanticModelDialog from '../CreateSemanticModelDialog' +import SemanticMetadataEditor from '../SemanticMetadataEditor' + +const { Title, Paragraph } = Typography + +const sourceFullName = source => { + if (!source) { + return '-' + } + + return [...(source.namespace || []), source.name].filter(Boolean).join('.') +} + +const renderDataType = datatype => { + if (!datatype) return <span>-</span> + const type = { DateTime: 'timestamp', DateTimeTz: 'timestamp_tz' }[datatype] || datatype.toLowerCase() + + return <Tag color={ColumnTypeColorEnum[type] || undefined}>{datatype}</Tag> +} + +const topAlignedColumns = columns => + columns.map(column => ({ ...column, onCell: () => ({ style: { verticalAlign: 'top' } }) })) + +const renderColumns = columns => <span className='font-mono text-xs'>{(columns || []).join(', ') || '-'}</span> + +const hasMetadata = entity => !!entity.aiContext || entity.customExtensions?.length > 0 + +export default function SemanticModelDetailsPage() { + const searchParams = useSearchParams() + const router = useRouter() + const dispatch = useAppDispatch() + const metalake = searchParams.get('metalake') + const catalog = searchParams.get('catalog') + const schema = searchParams.get('schema') + const semanticModelName = searchParams.get('semanticModel') + const auth = useAppSelector(state => state.auth) + const { anthEnable } = auth + const store = useAppSelector(state => state.metalakes) + const semanticModel = store.activatedDetails + const [tabKey, setTabKey] = useState('Datasets') + const [openEdit, setOpenEdit] = useState(false) + const [metadataEdit, setMetadataEdit] = useState(null) + const [fieldContext, setFieldContext] = useState(null) + const [fieldSearch, setFieldSearch] = useState('') + const [metricSearch, setMetricSearch] = useState('') + const [expandedDatasets, setExpandedDatasets] = useState([]) + + const definition = semanticModel?.definition + const properties = semanticModel?.properties + const createdAt = semanticModel?.audit?.createTime + const createdAtText = !createdAt || !isValidDate(createdAt) ? '-' : formatToDateTime(createdAt) + const lastModifiedAt = semanticModel?.audit?.lastModifiedTime + const lastModifiedText = !lastModifiedAt || !isValidDate(lastModifiedAt) ? '-' : formatToDateTime(lastModifiedAt) + const metadataObjectFullName = `${catalog}.${schema}.${semanticModelName}` + + const datasets = useMemo( + () => (definition?.datasets || []).map(dataset => ({ ...dataset, key: dataset.name })), + [definition] + ) + + const relationships = useMemo( + () => (definition?.relationships || []).map(relationship => ({ ...relationship, key: relationship.name })), + [definition] + ) + + const metrics = useMemo( + () => (definition?.metrics || []).map(metric => ({ ...metric, key: metric.name })), + [definition] + ) + + const fieldQuery = fieldSearch.trim().toLowerCase() + + const filteredDatasets = useMemo( + () => + fieldQuery + ? datasets + .map(dataset => ({ + ...dataset, + fields: (dataset.fields || []).filter(field => + [field.name, field.label].some(value => value?.toLowerCase().includes(fieldQuery)) + ) + })) + .filter(dataset => dataset.fields.length) + : datasets, + [datasets, fieldQuery] + ) + + const filteredMetrics = useMemo( + () => metrics.filter(metric => metric.name.toLowerCase().includes(metricSearch.trim().toLowerCase())), + [metrics, metricSearch] + ) + + const renderMetadata = (entity, label, target) => ( + <> + <SemanticModelAIContext + context={entity?.aiContext} + label={`${label} AI Context`} + onEdit={target ? () => setMetadataEdit({ ...target, section: 'context' }) : undefined} + /> + <SemanticModelExtensions extensions={entity?.customExtensions} label={`${label} Vendor Extensions`} /> + </> + ) + + const definitionText = useMemo(() => formatSemanticModelDefinition(definition), [definition]) + + const tagContent = ( + <div> + <Tags readOnly={true} metadataObjectType={'semantic_model'} metadataObjectFullName={metadataObjectFullName} /> + </div> + ) + + const propertyContent = <PropertiesContent properties={properties} /> + + const onSaved = async updated => { + if (updated.name !== semanticModelName) { + const params = new URLSearchParams(searchParams.toString()) + params.set('semanticModel', updated.name) + router.replace(`/catalogs?${params.toString()}`) + } else { + await dispatch(getSemanticModelDetails({ metalake, catalog, schema, semanticModel: updated.name, init: true })) + } + } + + const onCopyDefinition = async () => { + try { + await copyToClipboard(definitionText) + message.success('Definition copied!') + } catch (err) { + console.error('Failed to copy definition: ', err) + message.error('Failed to copy definition') + } + } + + const datasetColumns = useMemo( + () => [ + { + title: 'Dataset Name', + dataIndex: 'name', + key: 'name', + width: 200, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { + title: 'Source', + dataIndex: 'source', + key: 'source', + width: 260, + ellipsis: true, + render: source => <span className='font-mono text-xs'>{sourceFullName(source)}</span> + }, + { + title: 'Primary Key', + dataIndex: 'primaryKey', + key: 'primaryKey', + width: 180, + ellipsis: true, + render: renderColumns + }, + { + title: 'Unique Keys', + dataIndex: 'uniqueKeys', + key: 'uniqueKeys', + width: 220, + render: keys => + keys?.length ? ( + <Space direction='vertical' size={4}> + {keys.map((columns, index) => ( + <Tag key={index}>{renderColumns(columns)}</Tag> + ))} + </Space> + ) : ( + <span>-</span> + ) + }, + { + title: 'Fields', + dataIndex: 'fields', + key: 'fields', + width: 80, + render: fields => <span>{(fields || []).length}</span> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit dataset ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'dataset', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const fieldColumns = useMemo( + () => [ + { + title: 'Field Name', + dataIndex: 'name', + key: 'name', + width: 220, + render: (name, field) => ( + <div className='break-words'> + <div>{name}</div> + {field.label && <Typography.Text type='secondary'>{field.label}</Typography.Text>} + <div> + {' '} + {field.aiContext && (typeof field.aiContext === 'string' || Object.keys(field.aiContext).length > 0) && ( + <Button + size='small' + type='link' + aria-label={`${field.name} AI Context`} + onClick={() => setFieldContext(field)} + > + AI Context + </Button> + )} + </div> + </div> + ) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: renderDataType + }, + { + title: 'Time Dimension', + dataIndex: 'dimension', + key: 'dimension', + width: 140, + render: dimension => <span>{dimension?.isTime ? 'Yes' : 'No'}</span> + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: (expression, record) => <SemanticModelExpression expression={expression} name={record.name} /> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + } + ], + [] + ) + + const relationshipColumns = useMemo( + () => [ + { + title: 'Relationship Name', + dataIndex: 'name', + key: 'name', + width: 220, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { title: 'From', dataIndex: 'from', key: 'from', width: 160, ellipsis: true }, + { + title: 'From Columns', + dataIndex: 'fromColumns', + key: 'fromColumns', + width: 200, + ellipsis: true, + render: renderColumns + }, + { title: 'To', dataIndex: 'to', key: 'to', width: 160, ellipsis: true }, + { + title: 'To Columns', + dataIndex: 'toColumns', + key: 'toColumns', + ellipsis: true, + render: renderColumns + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit relationship ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'relationship', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const metricColumns = useMemo( + () => [ + { + title: 'Metric Name', + dataIndex: 'name', + key: 'name', + width: 200, + ellipsis: true, + sorter: (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: renderDataType + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: (expression, record) => <SemanticModelExpression expression={expression} name={record.name} /> + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + }, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, record) => ( + <Button + size='small' + aria-label={`Edit metric ${record.name}`} + onClick={() => setMetadataEdit({ kind: 'metric', name: record.name })} + > + Edit + </Button> + ) + } + ], + [] + ) + + const tabOptions = [ + { label: 'Overview', key: 'Overview' }, + { label: 'Datasets', key: 'Datasets' }, + { label: 'Relationships', key: 'Relationships' }, + { label: 'Metrics', key: 'Metrics' }, + { label: 'Definition', key: 'Definition' } + ] + + const { + resizableColumns: datasetResizableColumns, + components: datasetComponents, + tableWidth: datasetTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(datasetColumns), minWidth: 100 } + }, [datasetColumns]) + + const { + resizableColumns: relationshipResizableColumns, + components: relationshipComponents, + tableWidth: relationshipTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(relationshipColumns), minWidth: 100 } + }, [relationshipColumns]) + + const { + resizableColumns: metricResizableColumns, + components: metricComponents, + tableWidth: metricTableWidth + } = useAntdColumnResize(() => { + return { columns: topAlignedColumns(metricColumns), minWidth: 100 } + }, [metricColumns]) + + const expandedFieldsRow = dataset => ( + <div> + {renderMetadata(dataset, dataset.name, { kind: 'dataset', name: dataset.name })} + <Table + data-refer='semantic-model-fields-grid' + size='small' + rowKey={field => field.name} + dataSource={(dataset.fields || []).map(field => ({ ...field, datasetName: dataset.name }))} + tableLayout='fixed' + scroll={{ x: 1000 }} + columns={topAlignedColumns([ + ...fieldColumns, + { + title: 'Actions', + key: 'actions', + width: 90, + render: (_, field) => ( + <Space direction='vertical' size={4}> + <Button + size='small' + aria-label={`Edit field ${field.name}`} + onClick={() => setMetadataEdit({ kind: 'field', name: field.name, datasetName: dataset.name })} + > + Edit + </Button> + </Space> + ) + } + ])} + pagination={false} + expandable={{ + expandedRowRender: field => ( + <SemanticModelExtensions extensions={field.customExtensions} label={`${field.name} Vendor Extensions`} /> + ), + rowExpandable: field => field.customExtensions?.length > 0 + }} + /> + </div> + ) + + return ( + <> + <Spin spinning={store.activatedDetailsLoading}> + <Flex className='mb-2' gap='small' align='flex-start'> + <div className='size-8'> + <Icons.iconify icon='mdi:graph-outline' className='my-icon-large' /> + </div> + <div className='relative bottom-1 min-w-0 flex-1'> + <Title level={3} style={{ marginBottom: '0.125rem' }}> + <div className='flex min-w-0 items-center gap-2'> + <span title={semanticModelName} className='min-w-0 truncate'> + {semanticModelName || ''} + </span> + <Tooltip title='Edit'> + <Button + type='text' + size='small' + aria-label='Edit Semantic Model' + icon={<Icons.Pencil className='size-4' />} + disabled={store.activatedDetailsLoading || !definition} + onClick={() => setOpenEdit(true)} + /> + </Tooltip> + </div> + </Title> + <Paragraph type='secondary' className='truncate' title={semanticModel?.comment} style={{ marginBottom: 0 }}> + {semanticModel?.comment} + </Paragraph> + </div> + <div className='ml-auto shrink-0'> + <SemanticModelExport + key={metadataObjectFullName} + metalake={metalake} + catalog={catalog} + schema={schema} + semanticModel={semanticModelName} + disabled={store.activatedDetailsLoading || !definition} + /> + </div> + </Flex> + </Spin> + <Tabs data-refer='details-tabs' activeKey={tabKey} onChange={setTabKey} items={tabOptions} /> + <Spin spinning={store.activatedDetailsLoading}> + {tabKey === 'Overview' && ( + <div> + <Space split={<Divider type='vertical' />} wrap={true} className='mb-2'> + {anthEnable && ( + <Space size={4}> + <Tooltip title='Owned' placement='top'> + <Icons.User className='size-4' color='grey' /> + </Tooltip> + <GetOwner + metalake={metalake} + metadataObjectType={'semantic_model'} + metadataObjectFullName={metadataObjectFullName} + /> + </Space> + )} + {semanticModel?.audit?.creator && ( + <Space size={4}> + <Tooltip title='Creator' placement='top'> + <Icons.UserPen className='size-4' color='grey' /> + </Tooltip> + <span>{semanticModel.audit.creator}</span> + </Space> + )} + <Space size={4}> + <Tooltip title='Created' placement='top'> + <span className='inline-flex'> + <Icons.iconify icon='mdi:clock-outline' className='size-4' props={{ color: 'grey' }} /> + </span> + </Tooltip> + <span>{createdAtText}</span> + </Space> + <Space size={4}> + <Tooltip title='Tags' placement='top'> + <Icons.Tags className='size-4' color='grey' /> + </Tooltip> + {store.currentEntityTags && store.currentEntityTags?.length > 0 ? ( + <Popover placement='bottom' title={<span>Tags</span>} content={tagContent}> + <a className='text-defaultPrimary'>{store.currentEntityTags?.length}</a> + </Popover> + ) : ( + <a className='text-defaultPrimary'>0</a> + )} + </Space> + <Space size={4}> + <Tooltip title='Properties' placement='top'> + <Icons.TableProperties className='size-4' color='grey' /> + </Tooltip> + {properties && Object.keys(properties).length > 0 ? ( + <Popover placement='bottom' title={<span>Properties</span>} content={propertyContent}> + <a className='text-defaultPrimary'>{Object.keys(properties).length}</a> + </Popover> + ) : ( + <a className='text-defaultPrimary'>0</a> + )} + </Space> + {semanticModel?.audit?.lastModifier && ( + <Space size={4} data-refer='semantic-model-last-modifier'> + <Tooltip title='Last modified by'> + <Icons.UserPen className='size-4' color='grey' /> + </Tooltip> + <span>{semanticModel.audit.lastModifier}</span> + </Space> + )} + {lastModifiedAt && ( + <Space size={4} data-refer='semantic-model-last-modified-time'> + <Tooltip title='Last modified'> + <Icons.History className='size-4' color='grey' /> + </Tooltip> + <span>{lastModifiedText}</span> + </Space> + )} + </Space> + <Button + className='mb-3 block' + disabled={!definition} + onClick={() => setMetadataEdit({ kind: 'model', section: 'context' })} + > + Edit Model AI Context + </Button> + {renderMetadata(definition, 'Model')} + </div> + )} + {tabKey === 'Datasets' && ( + <> + <Space className='mb-3'> + <Input + aria-label='Search fields' + placeholder='Search field name or label' + value={fieldSearch} + onChange={event => setFieldSearch(event.target.value)} + /> + {fieldSearch && <Button onClick={() => setFieldSearch('')}>Clear field search</Button>} + </Space> + <Table + data-refer='semantic-model-datasets-grid' + size='small' + tableLayout='fixed' + scroll={{ x: datasetTableWidth, y: 'max(20rem, calc(100vh - 28rem))' }} + dataSource={filteredDatasets} + columns={datasetResizableColumns} + components={datasetComponents} + expandable={{ + expandedRowRender: expandedFieldsRow, + expandedRowKeys: fieldQuery ? filteredDatasets.map(dataset => dataset.key) : expandedDatasets, + onExpandedRowsChange: keys => { + if (!fieldQuery) setExpandedDatasets(keys) + }, + rowExpandable: dataset => (dataset.fields || []).length > 0 || hasMetadata(dataset) + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + </> + )} + {tabKey === 'Relationships' && ( + <Table + data-refer='semantic-model-relationships-grid' + size='small' + tableLayout='fixed' + scroll={{ x: relationshipTableWidth, y: 'max(20rem, calc(100vh - 28rem))' }} + dataSource={relationships} + columns={relationshipResizableColumns} + components={relationshipComponents} + expandable={{ + expandedRowRender: relationship => + renderMetadata(relationship, relationship.name, { kind: 'relationship', name: relationship.name }), + rowExpandable: hasMetadata + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + )} + {tabKey === 'Metrics' && ( + <> + <Space className='mb-3'> + <Input + aria-label='Search metrics' + placeholder='Search metric name' + value={metricSearch} + onChange={event => setMetricSearch(event.target.value)} + /> + {metricSearch && <Button onClick={() => setMetricSearch('')}>Clear metric search</Button>} + </Space> + <Table + data-refer='semantic-model-metrics-grid' + size='small' + tableLayout='fixed' + scroll={{ x: metricTableWidth, y: 'max(20rem, calc(100vh - 28rem))' }} + dataSource={filteredMetrics} + columns={metricResizableColumns} + components={metricComponents} + expandable={{ + expandedRowRender: metric => renderMetadata(metric, metric.name, { kind: 'metric', name: metric.name }), + rowExpandable: hasMetadata + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + </> + )} + {tabKey === 'Definition' && + (definitionText ? ( + <div className='relative rounded border border-borderColor p-3'> + <Button className='mb-3' onClick={() => setOpenEdit(true)}> + Edit JSON definition + </Button> + <Tooltip title='Copy definition' placement='top'> + <button + type='button' + aria-label='Copy definition' + className='absolute right-3 top-3 inline-flex cursor-pointer items-center rounded border border-borderColor bg-transparent p-1 text-textSecondary transition-colors hover:text-defaultPrimary' + onClick={onCopyDefinition} + > + <Icons.Copy className='size-4' /> + </button> + </Tooltip> + <pre + data-refer='semantic-model-definition' + className='m-0 overflow-auto whitespace-pre-wrap break-all font-mono text-sm leading-6' + style={{ maxHeight: 'calc(100vh - 34rem)' }} Review Comment: [P2] Give the Definition viewer the available panel height At 1280x600, this calculation reduces the JSON viewer to 56px, roughly two lines at its 24px line height, despite unused space below it. The populated definition is then impractical to read. Size the viewer from the detail panel's remaining space, with its toolbar outside the scrolling content, and provide an expand/fullscreen option for longer documents. Verify the short viewport as well as a normal desktop window instead of relying on another fixed viewport subtraction. -- 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]
