Copilot commented on code in PR #12900: URL: https://github.com/apache/gravitino/pull/12900#discussion_r4255360773
########## web-v2/web/src/app/catalogs/rightContent/CreateSemanticModelDialog.js: ########## @@ -0,0 +1,279 @@ +/* + * 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 React, { useEffect, useRef, useState } from 'react' +import { Form, Input, Modal, Spin, Typography } from 'antd' +import { useScrolling } from 'react-use' +import RenderPropertiesFormItem from '@/components/EntityPropertiesFormItem' +import { validateMessages, dialogContentMaxHeigth, mismatchName } from '@/config' +import { nameRegex } from '@/lib/utils/regex' +import { useResetFormOnCloseModal } from '@/lib/hooks/use-reset' +import { cn } from '@/lib/utils/tailwind' +import { useAppDispatch } from '@/lib/hooks/useStore' +import { + formatSemanticModelDefinition, + genSemanticModelUpdates, + parseSemanticModelDefinition +} from '@/lib/utils/semanticModel' +import { createSemanticModel, updateSemanticModel, getSemanticModelDetails } from '@/lib/store/metalakes' + +const { Paragraph } = Typography +const { TextArea } = Input + +const definitionPlaceholder = `{ + "datasets": [ + { + "name": "orders", + "source": { "namespace": ["sales", "mart"], "name": "orders" }, + "fields": [] + } + ] +}` + +const defaultValues = { + name: '', + comment: '', + definition: '', + properties: [] +} + +export default function CreateSemanticModelDialog({ ...props }) { + const { open, setOpen, metalake, catalog, schema, editSemanticModel, onSaved } = props + const [confirmLoading, setConfirmLoading] = useState(false) + const [isLoading, setIsLoading] = useState(false) + const [cacheData, setCacheData] = useState() + const scrollRef = useRef(null) + const loadedRef = useRef(false) + const scrolling = useScrolling(scrollRef) + const [bottomShadow, setBottomShadow] = useState(false) + const [topShadow, setTopShadow] = useState(false) + const [form] = Form.useForm() + const dispatch = useAppDispatch() + + useResetFormOnCloseModal({ + form, + open + }) + + const handScroll = () => { + if (scrollRef.current) { + const { scrollTop, scrollHeight, clientHeight } = scrollRef.current + if (scrollHeight > clientHeight + scrollTop) { + setTopShadow(true) + setBottomShadow(scrollTop > 0) + } else if (scrollHeight === clientHeight + scrollTop) { + setTopShadow(false) + setBottomShadow(scrollTop > 0) + } + } + } + + useEffect(() => { + scrollRef.current && handScroll() + }, [scrolling]) + + useEffect(() => { + if (open && editSemanticModel && !loadedRef.current) { + loadedRef.current = true + + const initLoad = async () => { + setIsLoading(true) + try { + const { payload } = await dispatch( + getSemanticModelDetails({ metalake, catalog, schema, semanticModel: editSemanticModel }) + ) + const loaded = payload?.semanticModel + setCacheData(loaded) + form.setFieldValue('name', loaded?.name) + form.setFieldValue('comment', loaded?.comment) + form.setFieldValue('definition', formatSemanticModelDefinition(loaded?.definition)) + let index = 0 + Object.entries(loaded?.properties || {}).forEach(([key, value]) => { + form.setFieldValue(['properties', index, 'key'], key) + form.setFieldValue(['properties', index, 'value'], value) + index++ + }) + setIsLoading(false) + } catch (e) { + setIsLoading(false) + } + } + initLoad() + } + + // Reset loadedRef when dialog closes + if (!open) { + loadedRef.current = false + } + }, [open, editSemanticModel, metalake, catalog, schema]) + + const validateDefinition = (rule, value) => { + const { errors } = parseSemanticModelDefinition(value) + + return errors.length + ? Promise.reject( + <ul className='list-disc pl-4'> + {errors.map(error => ( + <li key={error}>{error}</li> + ))} + </ul> + ) + : Promise.resolve() + } + + const handleSubmit = e => { + e.preventDefault() + form + .validateFields() + .then(async values => { + const { definition } = parseSemanticModelDefinition(values.definition) + + setConfirmLoading(true) + + const submitData = { + name: values.name.trim(), + comment: values.comment, + definition, + properties: Object.fromEntries((values.properties || []).map(({ key, value }) => [key, value])) + } + + let result + if (editSemanticModel) { + const updates = genSemanticModelUpdates(cacheData, submitData) + if (updates.length) { + result = await dispatch( + updateSemanticModel({ + metalake, + catalog, + schema, + semanticModel: editSemanticModel, + data: { updates } + }) + ) + } + } else { + result = await dispatch(createSemanticModel({ data: submitData, metalake, catalog, schema })) + } + + if (!result?.payload?.err && !result?.error) { + setOpen(false) + if (result) await onSaved?.(result.payload) + } + }) + .catch(info => { + if (info?.errorFields?.length) { + form.scrollToField(info.errorFields[0].name) + } else { + console.error(info) + } + }) + .finally(() => setConfirmLoading(false)) + } + + const handleCancel = () => { + setOpen(false) + } + + return ( + <> + <Modal + title={!editSemanticModel ? 'Create Semantic Model' : 'Edit Semantic Model'} + open={open} + onOk={handleSubmit} + okText='Submit' + okButtonProps={{ + 'data-refer': 'handle-submit-semantic-model', + disabled: isLoading || (!!editSemanticModel && !cacheData) + }} + maskClosable={false} + keyboard={false} + width={800} + confirmLoading={confirmLoading} + onCancel={handleCancel} + > + <Paragraph type='secondary'> + {!editSemanticModel + ? 'Create a new semantic model.' + : `Edit the semantic model ${editSemanticModel}. The definition is replaced as a whole.`} + </Paragraph> + <div + className={cn('relative', { + 'after:absolute after:-bottom-10 after:left-0 after:right-0 after:h-10 after:shadow-[0px_-10px_8px_-8px_rgba(5,5,5,0.1)]': + topShadow, + 'before:absolute before:-top-10 before:left-0 before:right-0 before:h-10 before:z-10 before:shadow-[0px_10px_8px_-8px_rgba(5,5,5,0.1)]': + bottomShadow + })} + > + <div className='overflow-auto' style={{ maxHeight: `${dialogContentMaxHeigth}px` }} ref={scrollRef}> + <Spin spinning={isLoading}> + <Form + form={form} + initialValues={defaultValues} + layout='vertical' + name='semanticModelForm' + validateMessages={validateMessages} + > + <Form.Item + name='name' + label='Semantic Model Name' + rules={[{ required: true }, { type: 'string', max: 64 }, { pattern: new RegExp(nameRegex) }]} Review Comment: The literal model name `ossie` passes this rule, but `/semantic-models/ossie` is reserved by `SemanticModelOperations` for OSSIE import and shadows the native `/{semanticModel}` CRUD route. Creating or renaming to this value leaves the model listed but not loadable through this UI. Reject `ossie` in this shared create/edit name rule. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelDetailsPage.js: ########## @@ -0,0 +1,575 @@ +/* + * 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, Popover, Space, Spin, Table, Tabs, Tag, Tooltip, Typography, message } from 'antd' +import { useAntdColumnResize } from 'react-antd-column-resize' +import useResizeObserver from 'use-resize-observer' +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 SemanticModelAIContext from './SemanticModelAIContext' +import SemanticModelExport from './SemanticModelExport' +import SemanticModelExtensions from './SemanticModelExtensions' +import CreateSemanticModelDialog from '../CreateSemanticModelDialog' + +const { Title, Paragraph } = Typography + +const sourceFullName = source => { + if (!source) { + return '-' + } + + return [...(source.namespace || []), source.name].filter(Boolean).join('.') +} + +const renderExpression = expression => { + const dialects = expression?.dialects || [] + + if (!dialects.length) { + return <span>-</span> + } + + return ( + <Space direction='vertical' size={2}> + {dialects.map((dialect, index) => ( + <span key={`${dialect?.dialect}-${index}`}> + <Tag>{dialect?.dialect}</Tag> + <span className='font-mono text-xs'>{dialect?.expression}</span> + </span> + ))} + </Space> + ) +} + +const renderColumns = columns => <span className='font-mono text-xs'>{(columns || []).join(', ') || '-'}</span> + +const hasMetadata = entity => !!entity.aiContext || entity.customExtensions?.length > 0 + +const renderMetadata = (entity, label) => ( + <> + <SemanticModelAIContext context={entity?.aiContext} label={`${label} AI Context`} /> + <SemanticModelExtensions extensions={entity?.customExtensions} label={`${label} Vendor Extensions`} /> + </> +) + +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 { ref, width } = useResizeObserver() + + 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 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> + } + ], + [] + ) + + 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> + ) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Time Dimension', + dataIndex: 'dimension', + key: 'dimension', + width: 140, + render: dimension => <span>{dimension?.isTime ? 'Yes' : 'No'}</span> + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + 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 + } + ], + [] + ) + + 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: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + } + ], + [] + ) + + const tabOptions = [ + { 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: datasetColumns, minWidth: 100 } + }, [datasetColumns]) + + const { + resizableColumns: relationshipResizableColumns, + components: relationshipComponents, + tableWidth: relationshipTableWidth + } = useAntdColumnResize(() => { + return { columns: relationshipColumns, minWidth: 100 } + }, [relationshipColumns]) + + const { + resizableColumns: metricResizableColumns, + components: metricComponents, + tableWidth: metricTableWidth + } = useAntdColumnResize(() => { + return { columns: metricColumns, minWidth: 100 } + }, [metricColumns]) + + const expandedFieldsRow = dataset => ( + <div> + {renderMetadata(dataset, dataset.name)} + <Table + data-refer='semantic-model-fields-grid' + size='small' + rowKey={field => field.name} + dataSource={dataset.fields || []} + columns={fieldColumns} + pagination={false} + expandable={{ + expandedRowRender: field => renderMetadata(field, field.name), + rowExpandable: hasMetadata + }} + /> + </div> + ) + + return ( + <> + <Spin spinning={store.activatedDetailsLoading}> + <Flex className='mb-2' gap='small' align='flex-start' ref={ref}> + <div className='size-8'> + <Icons.iconify icon='mdi:graph-outline' className='my-icon-large' /> + </div> + <div className='grow-1 relative bottom-1'> + <Title level={3} style={{ marginBottom: '0.125rem' }}> + <Space> + <span + title={semanticModelName} + className='min-w-10 truncate' + style={{ maxWidth: `calc(${width}px - 90px)`, display: 'inherit' }} + > + {decodeURIComponent(semanticModelName || '')} Review Comment: `useSearchParams().get()` already returns the decoded query value, so decoding it again corrupts names containing percent escapes (for example, a literal `sales%20model` is displayed as `sales model`) and a valid imported name containing a lone `%` throws `URIError`, crashing the detail page. Render `semanticModelName` directly. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelDetailsPage.js: ########## @@ -0,0 +1,575 @@ +/* + * 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, Popover, Space, Spin, Table, Tabs, Tag, Tooltip, Typography, message } from 'antd' +import { useAntdColumnResize } from 'react-antd-column-resize' +import useResizeObserver from 'use-resize-observer' +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 SemanticModelAIContext from './SemanticModelAIContext' +import SemanticModelExport from './SemanticModelExport' +import SemanticModelExtensions from './SemanticModelExtensions' +import CreateSemanticModelDialog from '../CreateSemanticModelDialog' + +const { Title, Paragraph } = Typography + +const sourceFullName = source => { + if (!source) { + return '-' + } + + return [...(source.namespace || []), source.name].filter(Boolean).join('.') +} + +const renderExpression = expression => { + const dialects = expression?.dialects || [] + + if (!dialects.length) { + return <span>-</span> + } + + return ( + <Space direction='vertical' size={2}> + {dialects.map((dialect, index) => ( + <span key={`${dialect?.dialect}-${index}`}> + <Tag>{dialect?.dialect}</Tag> + <span className='font-mono text-xs'>{dialect?.expression}</span> + </span> + ))} + </Space> + ) +} + +const renderColumns = columns => <span className='font-mono text-xs'>{(columns || []).join(', ') || '-'}</span> + +const hasMetadata = entity => !!entity.aiContext || entity.customExtensions?.length > 0 + +const renderMetadata = (entity, label) => ( + <> + <SemanticModelAIContext context={entity?.aiContext} label={`${label} AI Context`} /> + <SemanticModelExtensions extensions={entity?.customExtensions} label={`${label} Vendor Extensions`} /> + </> +) + +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 { ref, width } = useResizeObserver() + + 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 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> + } + ], + [] + ) + + 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> + ) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Time Dimension', + dataIndex: 'dimension', + key: 'dimension', + width: 140, + render: dimension => <span>{dimension?.isTime ? 'Yes' : 'No'}</span> + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + 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 + } + ], + [] + ) + + 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: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + } + ], + [] + ) + + const tabOptions = [ + { 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: datasetColumns, minWidth: 100 } + }, [datasetColumns]) + + const { + resizableColumns: relationshipResizableColumns, + components: relationshipComponents, + tableWidth: relationshipTableWidth + } = useAntdColumnResize(() => { + return { columns: relationshipColumns, minWidth: 100 } + }, [relationshipColumns]) + + const { + resizableColumns: metricResizableColumns, + components: metricComponents, + tableWidth: metricTableWidth + } = useAntdColumnResize(() => { + return { columns: metricColumns, minWidth: 100 } + }, [metricColumns]) + + const expandedFieldsRow = dataset => ( + <div> + {renderMetadata(dataset, dataset.name)} + <Table + data-refer='semantic-model-fields-grid' + size='small' + rowKey={field => field.name} + dataSource={dataset.fields || []} + columns={fieldColumns} + pagination={false} + expandable={{ + expandedRowRender: field => renderMetadata(field, field.name), + rowExpandable: hasMetadata + }} + /> + </div> + ) + + return ( + <> + <Spin spinning={store.activatedDetailsLoading}> + <Flex className='mb-2' gap='small' align='flex-start' ref={ref}> + <div className='size-8'> + <Icons.iconify icon='mdi:graph-outline' className='my-icon-large' /> + </div> + <div className='grow-1 relative bottom-1'> + <Title level={3} style={{ marginBottom: '0.125rem' }}> + <Space> + <span + title={semanticModelName} + className='min-w-10 truncate' + style={{ maxWidth: `calc(${width}px - 90px)`, display: 'inherit' }} + > + {decodeURIComponent(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> + </Space> + </Title> + <Paragraph + type='secondary' + className='truncate' + title={semanticModel?.comment} + style={{ marginBottom: 0, maxWidth: `calc(${width}px - 56px)` }} + > + {semanticModel?.comment} + </Paragraph> + </div> + </Flex> + <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> + </Spin> + <SemanticModelExport + key={metadataObjectFullName} + metalake={metalake} + catalog={catalog} + schema={schema} + semanticModel={semanticModelName} + disabled={store.activatedDetailsLoading || !definition} + /> + {renderMetadata(definition, 'Model')} + <Tabs data-refer='details-tabs' activeKey={tabKey} onChange={setTabKey} items={tabOptions} /> + <Spin spinning={store.activatedDetailsLoading}> + {tabKey === 'Datasets' && ( + <Table + data-refer='semantic-model-datasets-grid' + size='small' + style={{ maxHeight: 'calc(100vh - 30rem)' }} + scroll={{ x: datasetTableWidth, y: 'calc(100vh - 37rem)' }} + dataSource={datasets} + columns={datasetResizableColumns} + components={datasetComponents} + expandable={{ + expandedRowRender: expandedFieldsRow, + rowExpandable: dataset => (dataset.fields || []).length > 0 || hasMetadata(dataset) + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + )} + {tabKey === 'Relationships' && ( + <Table + data-refer='semantic-model-relationships-grid' + size='small' + style={{ maxHeight: 'calc(100vh - 30rem)' }} + scroll={{ x: relationshipTableWidth, y: 'calc(100vh - 37rem)' }} + dataSource={relationships} + columns={relationshipResizableColumns} + components={relationshipComponents} + expandable={{ + expandedRowRender: relationship => renderMetadata(relationship, relationship.name), + rowExpandable: hasMetadata + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + )} + {tabKey === 'Metrics' && ( + <Table + data-refer='semantic-model-metrics-grid' + size='small' + style={{ maxHeight: 'calc(100vh - 30rem)' }} + scroll={{ x: metricTableWidth, y: 'calc(100vh - 37rem)' }} + dataSource={metrics} + columns={metricResizableColumns} + components={metricComponents} + expandable={{ + expandedRowRender: metric => renderMetadata(metric, metric.name), + rowExpandable: hasMetadata + }} + pagination={{ position: ['bottomCenter'], showSizeChanger: true }} + /> + )} + {tabKey === 'Definition' && + (definitionText ? ( + <div className='relative rounded border border-borderColor p-3'> + <Tooltip title='Copy definition' placement='top'> + <button + type='button' + 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} + > Review Comment: This icon-only button has no accessible name; the tooltip text is not a reliable button label for assistive technology. Add an `aria-label` matching the action. ########## web-v2/web/src/lib/utils/semanticModel.js: ########## @@ -0,0 +1,283 @@ +/* + * 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 { isEqual, isPlainObject } from 'lodash-es' + +const SOURCE_NAMESPACE_LENGTH = 2 +const DEFINITION_INDENT = 2 + +const isNonBlankString = value => typeof value === 'string' && value.trim() !== '' + +const duplicatesOf = names => { + const seen = new Set() + const duplicates = new Set() + + names.forEach(name => { + if (seen.has(name)) { + duplicates.add(name) + } + seen.add(name) + }) + + return [...duplicates] +} + +const validateSource = (dataset, errors) => { + const { name, source } = dataset + + if (!isPlainObject(source)) { + errors.push(`Dataset "${name}" must have a source with a namespace and a name.`) + + return + } + + const { namespace } = source + + if (!Array.isArray(namespace) || namespace.length !== SOURCE_NAMESPACE_LENGTH || !namespace.every(isNonBlankString)) { + errors.push(`Dataset "${name}" must have a source namespace of exactly [catalog, schema].`) + } + + if (!isNonBlankString(source.name)) { + errors.push(`Dataset "${name}" must have a source with a non-empty name.`) + } +} + +const validateDatasets = (datasets, errors) => { + datasets.forEach((dataset, index) => { + if (!isPlainObject(dataset) || !isNonBlankString(dataset.name)) { + errors.push(`datasets[${index}] must have a non-empty name.`) + + return + } + + validateSource(dataset, errors) + + const fields = dataset.fields + if (fields !== undefined && !Array.isArray(fields)) { + errors.push(`Dataset "${dataset.name}" must declare fields as an array.`) + + return + } + + ;(fields || []).forEach((field, fieldIndex) => { + if (!isPlainObject(field) || !isNonBlankString(field.name)) { + errors.push(`datasets[${index}].fields[${fieldIndex}] must have a non-empty name.`) + } + }) + + const duplicateFields = duplicatesOf((fields || []).map(field => field?.name).filter(isNonBlankString)) + if (duplicateFields.length) { + errors.push(`Field names must be unique within dataset "${dataset.name}": ${duplicateFields.join(', ')}.`) + } + }) + + const duplicateDatasets = duplicatesOf(datasets.map(dataset => dataset?.name).filter(isNonBlankString)) + if (duplicateDatasets.length) { + errors.push(`Dataset names must be unique within the semantic model: ${duplicateDatasets.join(', ')}.`) + } +} + +const validateRelationships = (relationships, datasetNames, errors) => { + relationships.forEach((relationship, index) => { + if (!isPlainObject(relationship) || !isNonBlankString(relationship.name)) { + errors.push(`relationships[${index}] must have a non-empty name.`) + + return + } + + const { name, from, to, fromColumns, toColumns } = relationship + + ;[from, to].forEach(endpoint => { + if (!datasetNames.has(endpoint)) { + errors.push(`Relationship "${name}" references an unknown dataset "${endpoint}".`) + } + }) + + if (!Array.isArray(fromColumns) || !Array.isArray(toColumns) || !fromColumns.length || !toColumns.length) { + errors.push(`Relationship "${name}" must have non-empty fromColumns and toColumns.`) + + return + } + + if (fromColumns.length !== toColumns.length) { + errors.push(`Relationship "${name}" must have the same number of fromColumns and toColumns.`) + } + }) + + const duplicates = duplicatesOf(relationships.map(relationship => relationship?.name).filter(isNonBlankString)) + if (duplicates.length) { + errors.push(`Relationship names must be unique within the semantic model: ${duplicates.join(', ')}.`) + } +} + +const validateMetrics = (metrics, errors) => { + metrics.forEach((metric, index) => { + if (!isPlainObject(metric) || !isNonBlankString(metric.name)) { + errors.push(`metrics[${index}] must have a non-empty name.`) + } + }) + + const duplicates = duplicatesOf(metrics.map(metric => metric?.name).filter(isNonBlankString)) + if (duplicates.length) { + errors.push(`Metric names must be unique within the semantic model: ${duplicates.join(', ')}.`) + } +} + +/** + * Checks the structural rules the server also enforces, so obvious mistakes are reported before the + * request is sent. The server stays the authority: it additionally resolves every dataset source + * and its columns against the catalog, which the browser cannot do. + * + * @param {unknown} definition The parsed semantic model definition. + * @returns {string[]} The problems found, empty when the definition looks well formed. + */ +export const validateSemanticModelDefinition = definition => { Review Comment: This validator still accepts nested definitions the native API is guaranteed to reject—for example, fields or metrics without the required non-empty `expression.dialects`, blank relationship-column entries, and duplicate dialect names. Since the editor gates submission on this function and source resolution is documented as the server-only extra check, mirror the nested invariants from `SemanticModelDefinitionValidator` so these structural errors are reported before sending. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SemanticModelDetailsPage.js: ########## @@ -0,0 +1,575 @@ +/* + * 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, Popover, Space, Spin, Table, Tabs, Tag, Tooltip, Typography, message } from 'antd' +import { useAntdColumnResize } from 'react-antd-column-resize' +import useResizeObserver from 'use-resize-observer' +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 SemanticModelAIContext from './SemanticModelAIContext' +import SemanticModelExport from './SemanticModelExport' +import SemanticModelExtensions from './SemanticModelExtensions' +import CreateSemanticModelDialog from '../CreateSemanticModelDialog' + +const { Title, Paragraph } = Typography + +const sourceFullName = source => { + if (!source) { + return '-' + } + + return [...(source.namespace || []), source.name].filter(Boolean).join('.') +} + +const renderExpression = expression => { + const dialects = expression?.dialects || [] + + if (!dialects.length) { + return <span>-</span> + } + + return ( + <Space direction='vertical' size={2}> + {dialects.map((dialect, index) => ( + <span key={`${dialect?.dialect}-${index}`}> + <Tag>{dialect?.dialect}</Tag> + <span className='font-mono text-xs'>{dialect?.expression}</span> + </span> + ))} + </Space> + ) +} + +const renderColumns = columns => <span className='font-mono text-xs'>{(columns || []).join(', ') || '-'}</span> + +const hasMetadata = entity => !!entity.aiContext || entity.customExtensions?.length > 0 + +const renderMetadata = (entity, label) => ( + <> + <SemanticModelAIContext context={entity?.aiContext} label={`${label} AI Context`} /> + <SemanticModelExtensions extensions={entity?.customExtensions} label={`${label} Vendor Extensions`} /> + </> +) + +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 { ref, width } = useResizeObserver() + + 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 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> + } + ], + [] + ) + + 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> + ) + }, + { + title: 'Data Type', + dataIndex: 'datatype', + key: 'datatype', + width: 140, + render: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Time Dimension', + dataIndex: 'dimension', + key: 'dimension', + width: 140, + render: dimension => <span>{dimension?.isTime ? 'Yes' : 'No'}</span> + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + 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 + } + ], + [] + ) + + 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: datatype => (datatype ? <Tag>{datatype}</Tag> : <span>-</span>) + }, + { + title: 'Expression', + dataIndex: 'expression', + key: 'expression', + render: renderExpression + }, + { + title: 'Description', + dataIndex: 'description', + key: 'description', + ellipsis: true, + render: description => <span>{description || '-'}</span> + } + ], + [] + ) + + const tabOptions = [ + { label: 'Datasets', key: 'Datasets' }, + { label: 'Relationships', key: 'Relationships' }, + { label: 'Metrics', key: 'Metrics' }, + { label: 'Definition', key: 'Definition' } + ] Review Comment: The PR description promises separate detail tabs for datasets, fields, relationships, metrics, and the JSON definition, but this tab list omits Fields; fields are only available by expanding individual dataset rows. Add the advertised Fields tab, or update the PR description if the nested-only presentation is intentional. ########## web-v2/web/src/app/catalogs/rightContent/entitiesContent/SchemaDetailsPage.js: ########## @@ -631,6 +657,77 @@ export default function SchemaDetailsPage() { [currentMetalake, catalogType, catalog, schema, catalogData?.provider] ) + const semanticModelColumns = useMemo( + () => [ + { + title: 'Semantic Model Name', + dataIndex: 'name', + key: 'name', + width: 300, + ellipsis: true, + sorter: (a, b) => a?.name.toLowerCase().localeCompare(b?.name.toLowerCase()), + render: name => ( + <Link + data-refer={`semantic-model-link-${name}`} + href={`/catalogs?metalake=${encodeURIComponent(currentMetalake)}&catalogType=${catalogType}&catalog=${encodeURIComponent(catalog)}&schema=${encodeURIComponent(schema)}&semanticModel=${encodeURIComponent(name)}`} + > + {name} + </Link> + ) + }, + { + title: 'Tags', + dataIndex: 'tags', + key: 'tags', + render: (_, record) => ( + <Tags metadataObjectType={'semantic_model'} metadataObjectFullName={`${catalog}.${schema}.${record.name}`} /> + ) + }, + { + title: 'Actions', + dataIndex: 'action', + key: 'action', + width: 100, + render: (_, record) => ( + <Space size={4}> + <a data-refer={`edit-semantic-model-${record.name}`}> + <Tooltip title='Edit'> + <Icons.Pencil + className='size-4 hover:cursor-pointer hover:text-defaultPrimary' + onClick={() => { + setEditSemanticModel(record.name) + setOpenSemanticModel(true) + }} + /> + </Tooltip> + </a> + <a data-refer={`delete-semantic-model-${record.name}`}> + <Tooltip title='Delete'> + <Icons.Trash2Icon + className='size-4 hover:cursor-pointer hover:text-defaultPrimary' + onClick={() => showDeleteConfirm(record, 'semanticModel')} + /> + </Tooltip> + </a> + {anthEnable && ( Review Comment: The Edit and Delete controls are click handlers on unfocusable SVGs inside anchors without `href`, so keyboard and screen-reader users cannot operate or identify them. Use icon `Button`s with `aria-label`s and put each `onClick` on the button, as the adjacent Set Owner action does. ########## web-v2/web/src/lib/store/metalakes/index.js: ########## @@ -2333,6 +2376,142 @@ export const deleteView = createAsyncThunk( } ) +export const fetchSemanticModels = createAsyncThunk( + 'appMetalakes/fetchSemanticModels', + async ({ init, metalake, catalog, schema }, { getState, dispatch }) => { + const [err, res] = await to(getSemanticModelsApi({ metalake, catalog, schema }, { errorMessageMode: 'none' })) + + if (err) { + // The catalog doesn't support semantic models (405), or the server predates the endpoint and + // has no route for it (404). Both mean "this schema has none" - return empty silently rather + // than failing every schema page against an older server. + if ([404, 405].includes(err?.response?.status)) { + return { semanticModels: [], init } + } + throw err instanceof Error ? err : new Error(err?.message ?? String(err)) + } + + if (!res) { + throw new Error('Semantic Model list returned an empty response') + } + + const { identifiers = [] } = res || {} + + const semanticModels = identifiers.map(semanticModelItem => { + return { + ...semanticModelItem, + node: 'semanticModel', + id: semanticModelNodeKey({ metalake, catalog, schema, semanticModel: semanticModelItem.name }), + key: semanticModelNodeKey({ metalake, catalog, schema, semanticModel: semanticModelItem.name }), + path: `?${new URLSearchParams({ + metalake, + catalog, + catalogType: 'relational', + schema, + semanticModel: semanticModelItem.name + }).toString()}`, + name: semanticModelItem.name, + title: semanticModelItem.name, + isLeaf: true, + children: [] + } + }) + + if ( + init && + getState().metalakes.loadedNodes.includes(`{{${metalake}}}{{${catalog}}}{{${'relational'}}}{{${schema}}}`) + ) { + const schemaKey = `{{${metalake}}}{{${catalog}}}{{${'relational'}}}{{${schema}}}` + dispatch( + setIntoTreeNodes({ + key: schemaKey, + data: mergeWithSemanticModelNodes({ + tree: getState().metalakes.metalakeTree, + key: schemaKey, + entities: semanticModels + }) + }) + ) + } + + return { semanticModels, init } + } +) + +export const getSemanticModelDetails = createAsyncThunk( + 'appMetalakes/getSemanticModelDetails', + async ({ init, metalake, catalog, schema, semanticModel }) => { + const [err, res] = await to( + getSemanticModelDetailsApi({ metalake, catalog, schema, semanticModel }, { errorMessageMode: 'none' }) + ) + + if (err) { + // Catalog doesn't support semantic models (HTTP 405) - return empty result silently + if (err?.response?.status === 405) { + return { semanticModel: null, init } + } + throw err instanceof Error ? err : new Error(err?.message ?? String(err)) + } + + if (!res) { + throw new Error('Semantic Model details returned an empty response') + } + + return { semanticModel: res.semanticModel || res, init } + } +) + +export const createSemanticModel = createAsyncThunk( + 'appMetalakes/createSemanticModel', + async ({ data, metalake, catalog, schema }, { dispatch }) => { + await dispatch(setTableLoading(true)) + const [err, res] = await to(createSemanticModelApi({ data, metalake, catalog, schema })) + await dispatch(setTableLoading(false)) + + if (err || !res) { + return { err: true } + } + + await dispatch(fetchSemanticModels({ metalake, catalog, schema, init: true })) + + return res.semanticModel + } +) + +export const updateSemanticModel = createAsyncThunk( + 'appMetalakes/updateSemanticModel', + async ({ metalake, catalog, schema, semanticModel, data }, { dispatch }) => { + await dispatch(setTableLoading(true)) + const [err, res] = await to(updateSemanticModelApi({ metalake, catalog, schema, semanticModel, data })) + await dispatch(setTableLoading(false)) + + if (err || !res) { + return { err: true } + } + + await dispatch(fetchSemanticModels({ metalake, catalog, schema, init: true })) + + return res.semanticModel + } +) + +export const deleteSemanticModel = createAsyncThunk( + 'appMetalakes/deleteSemanticModel', + async ({ metalake, catalog, schema, semanticModel }, { dispatch }) => { + await dispatch(setTableLoading(true)) + const [err, res] = await to(deleteSemanticModelApi({ metalake, catalog, schema, semanticModel })) + await dispatch(setTableLoading(false)) + + if (err || !res) { + throw new Error(err) + } Review Comment: When Axios rejects with its normal response object, `new Error(err)` turns the useful server message into `[object Object]`. This makes failed Semantic Model deletions produce a non-actionable toast; preserve an existing `Error` or construct one from `err.message`, as the new list/detail thunks already do. -- 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]
