createUdf.vue 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
L
ligang 已提交
17
<template>
B
break60 已提交
18
  <m-popup style="width:800px" :ok-text="item ? $t('Edit') : $t('Submit')" :nameText="item ? $t('Edit UDF Function') : $t('Create UDF Function')" @ok="_ok" ref="popup">
L
ligang 已提交
19 20 21
    <template slot="content">
      <div class="udf-create-model">
        <m-list-box-f>
22
          <template slot="name"><strong>*</strong>{{$t('type')}}</template>
L
ligang 已提交
23 24 25 26 27 28 29 30
          <template slot="content">
            <x-radio-group v-model="type">
              <x-radio :label="'HIVE'">HIVE UDF</x-radio>
              <!--<v-radio :label="'SPARK'">SPARK UDF</v-radio>-->
            </x-radio-group>
          </template>
        </m-list-box-f>
        <m-list-box-f>
31
          <template slot="name"><strong>*</strong>{{$t('UDF Function Name')}}</template>
L
ligang 已提交
32 33 34
          <template slot="content">
            <x-input
                    type="input"
35
                    maxlength="40"
L
ligang 已提交
36
                    v-model="funcName"
G
i18n  
gongzijian 已提交
37
                    :placeholder="$t('Please enter a function name')">
L
ligang 已提交
38 39 40 41
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
42
          <template slot="name"><strong>*</strong>{{$t('Package Name')}}</template>
L
ligang 已提交
43 44 45
          <template slot="content">
            <x-input
                    type="input"
46
                    maxlength="100"
L
ligang 已提交
47
                    v-model="className"
G
i18n  
gongzijian 已提交
48
                    :placeholder="$t('Please enter a Package name')">
L
ligang 已提交
49 50 51
            </x-input>
          </template>
        </m-list-box-f>
B
bao liang 已提交
52
        <!-- <m-list-box-f>
G
i18n  
gongzijian 已提交
53
          <template slot="name">{{$t('Parameter')}}</template>
L
ligang 已提交
54 55 56 57
          <template slot="content">
            <x-input
                    type="input"
                    v-model="argTypes"
G
i18n  
gongzijian 已提交
58
                    :placeholder="$t('Please enter a parameter')">
L
ligang 已提交
59 60 61 62
            </x-input>
          </template>
        </m-list-box-f>
        <m-list-box-f>
G
i18n  
gongzijian 已提交
63
          <template slot="name">{{$t('Database Name')}}</template>
L
ligang 已提交
64 65 66 67
          <template slot="content">
            <x-input
                    type="input"
                    v-model="database"
G
i18n  
gongzijian 已提交
68
                    :placeholder="$t('Please enter database name')">
L
ligang 已提交
69 70
            </x-input>
          </template>
B
bao liang 已提交
71
        </m-list-box-f> -->
L
ligang 已提交
72
        <m-list-box-f>
73
          <template slot="name"><strong>*</strong>{{$t('UDF Resources')}}</template>
L
ligang 已提交
74
          <template slot="content">
B
break60 已提交
75 76 77
            <treeselect style="width:535px;float:left;" v-model="resourceId" :disable-branch-nodes="true" :options="udfResourceList" :disabled="isUpdate" :normalizer="normalizer" :placeholder="$t('Please select UDF resources directory')">
              <div slot="value-label" slot-scope="{ node }">{{ node.raw.fullName }}</div>
            </treeselect>
G
i18n  
gongzijian 已提交
78
            <x-button type="primary" @click="_toggleUpdate" :disabled="upDisabled">{{$t('Upload Resources')}}</x-button>
L
ligang 已提交
79 80
          </template>
        </m-list-box-f>
B
break60 已提交
81 82 83 84 85 86 87 88
        <m-list-box-f v-if="isUpdate">
          <template slot="name"><strong>*</strong>{{$t('UDF resources directory')}}</template>
          <template slot="content">
            <treeselect style="width:535px;float:left;" v-model="pid" @select="selTree" :options="udfResourceDirList" :normalizer="normalizer" :placeholder="$t('Please select UDF resources directory')">
              <div slot="value-label" slot-scope="{ node }">{{ node.raw.fullName }}</div>
            </treeselect>
          </template>
        </m-list-box-f>
L
ligang 已提交
89 90 91 92
        <m-list-box-f v-if="isUpdate">
          <template slot="name">&nbsp;</template>
          <template slot="content">
            <m-udf-update
B
break60 已提交
93
                    ref="assignment"
L
ligang 已提交
94 95 96 97 98 99
                    @on-update-present="_onUpdatePresent"
                    @on-update="_onUpdate">
            </m-udf-update>
          </template>
        </m-list-box-f>
        <m-list-box-f>
G
i18n  
gongzijian 已提交
100
          <template slot="name">{{$t('Instructions')}}</template>
L
ligang 已提交
101 102 103
          <template slot="content">
            <x-input
                    type="textarea"
B
break60 已提交
104
                    v-model="description"
G
i18n  
gongzijian 已提交
105
                    :placeholder="$t('Please enter a instructions')">
L
ligang 已提交
106 107 108 109 110 111 112 113 114 115 116
            </x-input>
          </template>
        </m-list-box-f>
      </div>
    </template>
  </m-popup>
</template>
<script>
  import _ from 'lodash'
  import i18n from '@/module/i18n'
  import store from '@/conf/home/store'
B
break60 已提交
117 118
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
L
ligang 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
  import mPopup from '@/module/components/popup/popup'
  import mListBoxF from '@/module/components/listBoxF/listBoxF'
  import mUdfUpdate from '@/module/components/fileUpdate/udfUpdate'

  export default {
    name: 'udf-create',
    data () {
      return {
        store,
        type: 'HIVE',
        funcName: '',
        className: '',
        argTypes: '',
        database: '',
B
break60 已提交
133
        description: '',
B
break60 已提交
134 135
        resourceId: null,
        pid: null,
L
ligang 已提交
136 137
        udfResourceList: [],
        isUpdate: false,
B
break60 已提交
138 139 140 141 142 143
        upDisabled: false,
        normalizer(node) {
          return {
            label: node.name
          }
        },
L
ligang 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      }
    },
    props: {
      item: Object
    },
    methods: {
      _ok () {
        this.$refs['popup'].spinnerLoading = true
        if (this._validation()) {
          this._verifyUdfFuncName().then(res => {
            this._createUdfFunc().then()
          }).then(res => {
            setTimeout(() => {
              this.$refs['popup'].spinnerLoading = false
            }, 800)
          }).catch(e => {
            this.$refs['popup'].spinnerLoading = false
          })
        } else {
          this.$refs['popup'].spinnerLoading = false
        }
      },
      _createUdfFunc () {
        return new Promise((resolve, reject) => {
          // parameter
          let param = {
            type: this.type, // HIVE,SPARK
            funcName: this.funcName,
            className: this.className,
            argTypes: this.argTypes, // Can not pass this parameter
            database: this.database, // Can not pass this parameter
B
break60 已提交
175
            description: this.description,
176
            resourceId: this.resourceId
L
ligang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
          }

          let id = this.item && this.item.id || null

          // edit
          if (id) {
            param.id = id
          }
          // api
          this.store.dispatch(`resource/${id ? `updateUdfFunc` : `createUdfFunc`}`, param).then(res => {
            this.$emit('onUpdate', param)
            this.$message.success(res.msg)
            resolve()
          }).catch(e => {
            this.$message.error(e.msg || '')
            reject(e)
          })
        })
      },
      _onUpdatePresent () {
        // disabled submit
        this.$refs.popup.apDisabled = true
        // disabled update
        this.upDisabled = true
      },
B
break60 已提交
202 203 204 205
      // selTree
      selTree(node) {
        this.$refs.assignment.receivedValue(node.id,node.fullName)
      },
L
ligang 已提交
206 207 208 209 210 211
      /**
       * get udf resources
       */
      _getUdfList () {
        return new Promise((resolve, reject) => {
          this.store.dispatch('resource/getResourcesList', { type: 'UDF' }).then(res => {
B
break60 已提交
212 213 214 215 216 217 218 219 220
            let item = res.data
            this.filterEmptyDirectory(item)
            item = this.filterEmptyDirectory(item)
            let item1 = _.cloneDeep(res.data)
            this.diGuiTree(item)
            
            this.diGuiTree(this.filterJarFile(item1))
            this.udfResourceList = item
            this.udfResourceDirList = item1
L
ligang 已提交
221 222 223 224
            resolve()
          })
        })
      },
B
break60 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      // filterEmptyDirectory
      filterEmptyDirectory(array) {
        for (const item of array) {
          if (item.children) {
            this.filterEmptyDirectory(item.children)
          }
        }
        return array.filter(n => ((/\.jar$/.test(n.name) && n.children.length==0) || (!/\.jar$/.test(n.name) && n.children.length>0)))
      },
      // filterJarFile
      filterJarFile (array) {
        for (const item of array) {
          if (item.children) {
            item.children = this.filterJarFile(item.children)
          }
        }
        return array.filter(n => !/\.jar$/.test(n.name))
      },
      // diGuiTree
      diGuiTree(item) {  // Recursive convenience tree structure
        item.forEach(item => {
          item.children === '' || item.children === undefined || item.children === null || item.children.length === 0?        
            delete item.children : this.diGuiTree(item.children);
        })
      },
L
ligang 已提交
250 251 252 253 254 255 256 257
      /**
       * Upload udf resources
       */
      _onUpdate (o) {
        this.upDisabled = false
        this.udfResourceList.push(o)
        this.isUpdate = false
        this.$nextTick(() => {
258
          this.resourceId = o.id
L
ligang 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272
        })
        this.$refs.popup.apDisabled = false
      },
      _toggleUpdate () {
        this.isUpdate = !this.isUpdate
        if (this.isUpdate) {
          this.resourceId = null
        }
      },
      /**
       * verification
       */
      _validation () {
        if (!this.funcName) {
G
i18n  
gongzijian 已提交
273
          this.$message.warning(`${i18n.$t('Please enter a UDF function name')}`)
L
ligang 已提交
274 275 276
          return false
        }
        if (!this.className) {
G
i18n  
gongzijian 已提交
277
          this.$message.warning(`${i18n.$t('Please enter a Package name')}`)
L
ligang 已提交
278 279 280
          return false
        }
        if (!this.resourceId) {
G
i18n  
gongzijian 已提交
281
          this.$message.warning(`${i18n.$t('Select UDF Resources')}`)
L
ligang 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
          return false
        }
        return true
      },
      /**
       * Verify that the function name exists
       */
      _verifyUdfFuncName () {
        return new Promise((resolve, reject) => {
          if (this.item && this.item.funcName === this.funcName) {
            resolve()
          } else {
            this.store.dispatch('resource/verifyUdfFuncName', { name: this.funcName }).then(res => {
              resolve()
            }).catch(e => {
              this.$message.error(e.msg || '')
              reject(e)
            })
          }
        })
      }
    },
B
break60 已提交
304
    watch: {},
L
ligang 已提交
305 306 307 308 309 310 311 312 313
    created () {
      this._getUdfList().then(res => {
        // edit
        if (this.item) {
          this.type = this.item.type
          this.funcName = this.item.funcName || ''
          this.className = this.item.className || ''
          this.argTypes = this.item.argTypes || ''
          this.database = this.item.database || ''
B
break60 已提交
314
          this.description = this.item.description || ''
315
          this.resourceId = this.item.resourceId
L
ligang 已提交
316
        } else {
B
break60 已提交
317
          this.resourceId = null
L
ligang 已提交
318 319 320 321 322 323
        }
      })
    },
    mounted () {

    },
B
break60 已提交
324
    components: { mPopup, mListBoxF, mUdfUpdate, Treeselect }
L
ligang 已提交
325 326
  }
</script>
B
break60 已提交
327 328 329 330 331
<style lang="scss" rel="stylesheet/scss">
  .vue-treeselect__control {
    height: 32px;
  }
</style>