Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

10.1 KB
Raw Blame
/*
 * 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 { Alert, 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 [saveError, setSaveError] = 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 { semanticModel: loaded } = await dispatch(
            getSemanticModelDetails({ metalake, catalog, schema, semanticModel: editSemanticModel, revision: 'latest' })
          ).unwrap()
          if (!loaded?.revision) throw new Error('The latest Semantic Model revision is unavailable.')
          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++
          })
        } catch (e) {
          setCacheData(undefined)
          setSaveError(e.message || 'Unable to load the latest Semantic Model revision.')
        } finally {
          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)
        setSaveError('')

        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 > 1 && updates.some(update => update['@type'] === 'rename')) {
            setSaveError('Save the rename and other changes separately.')

            return
          }
          if (updates.length) {
            result = await dispatch(
              updateSemanticModel({
                metalake,
                catalog,
                schema,
                semanticModel: editSemanticModel,
                data: { updates, expectedRevision: cacheData.revision }
              })
            )
          }
        } else {
          result = await dispatch(createSemanticModel({ data: submitData, metalake, catalog, schema }))
        }

        if (result?.payload?.err || result?.error) {
          setSaveError(result.payload?.message || result.error?.message || 'Unable to save the Semantic Model.')
        } else {
          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>
        {saveError && (
          <Alert className='mb-3' type='error' showIcon message={saveError} description='Your edits are retained.' />
        )}
        <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) }]}
                  messageVariables={{ label: 'semantic model name' }}
                >
                  <Input data-refer='semantic-model-name-field' placeholder={mismatchName} />
                </Form.Item>
                <Form.Item name='comment' label='Comment'>
                  <TextArea data-refer='semantic-model-comment-field' />
                </Form.Item>
                <Form.Item
                  name='definition'
                  label='Definition (advanced JSON)'
                  extra='The datasets, relationships and metrics of the semantic model, as JSON. The server validates the sources.'
                  rules={[{ required: true }, { validator: validateDefinition }]}
                  messageVariables={{ label: 'definition' }}
                >
                  <TextArea
                    data-refer='semantic-model-definition-field'
                    autoSize={{ minRows: 12, maxRows: 24 }}
                    className='font-mono text-xs'
                    placeholder={definitionPlaceholder}
                  />
                </Form.Item>
                <Form.Item label='Properties'>
                  <Form.List name='properties'>
                    {(fields, subOpt) => (
                      <RenderPropertiesFormItem
                        fields={fields}
                        subOpt={subOpt}
                        form={form}
                        isEdit={!!editSemanticModel}
                        isDisable={false}
                      />
                    )}
                  </Form.List>
                </Form.Item>
              </Form>
            </Spin>
          </div>
        </div>
      </Modal>
    </>
  )
}