use-table.ts 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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 { useI18n } from 'vue-i18n'
import { h, reactive, ref } from 'vue'
import { useAsyncState } from '@vueuse/core'
import { queryRuleListPaging } from '@/service/modules/data-quality'
22
import type { RuleRes } from '@/service/modules/data-quality/types'
23 24
import TableAction from './components/table-action'
import _ from 'lodash'
25
import { format } from 'date-fns'
26
import { TableColumns } from 'naive-ui/es/data-table/src/interface'
27
import { parseTime } from '@/common/common'
28

29
export function useTable(viewRuleEntry = (unusedRuleJson: string): void => {}) {
30 31 32 33 34 35 36 37
  const { t } = useI18n()

  const variables = reactive({
    tableData: [],
    page: ref(1),
    pageSize: ref(10),
    state: ref(null),
    searchVal: ref(null),
38 39
    totalPage: ref(1),
    loadingRef: ref(false)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  })

  const columns: TableColumns<any> = [
    {
      title: t('data_quality.rule.name'),
      key: 'ruleName'
    },
    {
      title: t('data_quality.rule.type'),
      key: 'ruleTypeName'
    },
    {
      title: t('data_quality.rule.username'),
      key: 'userName'
    },
    {
      title: t('data_quality.rule.create_time'),
      key: 'createTime'
    },
    {
      title: t('data_quality.rule.update_time'),
      key: 'updateTime'
    },
    {
      title: t('data_quality.rule.actions'),
      key: 'actions',
      width: 150,
      render: (row: any) =>
        h(TableAction, {
          row,
          onViewRuleEntry: (ruleJson: string) => {
            viewRuleEntry(ruleJson)
          }
        })
    }
  ]

  const ruleTypeMapping = [
    {
      code: -1,
      label: t('data_quality.rule.all')
    },
    {
      code: 0,
      label: t('data_quality.rule.single_table')
    },
    {
      code: 1,
      label: t('data_quality.rule.custom_sql')
    },
    {
      code: 2,
      label: t('data_quality.rule.multi_table_accuracy')
    },
    {
      code: 3,
      label: t('data_quality.rule.multi_table_value_comparison')
    }
  ]

  const getTableData = (params: any) => {
101 102
    if (variables.loadingRef) return
    variables.loadingRef = true
103 104 105 106 107 108 109 110 111 112
    const data = {
      pageSize: params.pageSize,
      pageNo: params.pageNo,
      searchVal: params.searchVal,
      startDate: params.startDate,
      endDate: params.endDate
    }

    const { state } = useAsyncState(
      queryRuleListPaging(data).then((res: RuleRes) => {
113
        variables.tableData = res.totalList.map((item, unused) => {
114 115 116 117 118 119 120 121 122 123 124 125 126
          const ruleName =
            'data_quality.rule.' + item.name.substring(3, item.name.length - 1)
          const ruleNameLocale = t(ruleName)

          const ruleType = _.find(ruleTypeMapping, { code: item.type })

          let ruleTypeName = ''

          if (ruleType) {
            ruleTypeName = ruleType.label
          }

          item.createTime = format(
127
            parseTime(item.createTime),
128 129 130
            'yyyy-MM-dd HH:mm:ss'
          )
          item.updateTime = format(
131
            parseTime(item.updateTime),
132 133 134 135 136 137 138 139 140
            'yyyy-MM-dd HH:mm:ss'
          )

          return {
            ...item,
            ruleName: ruleNameLocale,
            ruleTypeName: ruleTypeName
          }
        }) as any
141 142

        variables.loadingRef = false
143 144 145 146 147 148 149 150 151
      }),
      {}
    )

    return state
  }

  return { t, variables, getTableData, columns }
}