All files / libs bus.js

33.71% Statements 59/175
23.42% Branches 26/111
20% Functions 3/15
33.91% Lines 59/174

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301    2x 2x 2x 2x   2x 2x 2x 2x         2x                                       2x 2x 2x 2x     2x 2x 2x                 4x 4x 4x                           12x 12x 10x   2x   12x 12x                     4x 4x 4x 4x 4x 4x 4x 4x 1x 3x 1x 2x 1x   1x   4x                                                                                                                                                                                                                                                                                                   5x 5x 5x 5x 5x 3x   5x 5x 5x           5x 5x 5x 5x 5x 3x   5x 5x 5x                                   2x                    
import Vue from 'vue'
import moment from 'moment-timezone'
Date.prototype.setStart = function () {
  this.setHours(0)
  this.setMinutes(0)
  this.setSeconds(0)
}
Date.prototype.setEnd = function () {
  this.setHours(23)
  this.setMinutes(59)
  this.setSeconds(59)
}
 
export default new Vue({
  data () {
    return {
      selectDate: [], // 选中的时间段
      emailReg: /^[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,5}\.[a-zA-Z0-9]{1,5}$/,
      // 创建策略信息
      buildRuleInfo: {
        triggers: [],
        actions: []
      },
      backtoRulelist: '', // 返回策略列表页信息
      // role: md5(1),
      role: 1,
      // 创建图表信息
      chartAddInfo: {
        metricTarget: []
      }
    }
  },
  methods: {
    // 获取初始化时间,默认最近一周
    getDefaultDate () {
      let start = this.getDays(-7)
      let end = this.getDays(0)
      start.setStart()
      end.setEnd()
      // let start = this.getHoursTime(-1);
      // let end = this.getHoursTime(0);
      start = this.timeFormate(start, localStorage.getItem('nz-default-dateFormat') ? localStorage.getItem('nz-default-dateFormat') : 'YYYY-MM-DD HH:mm:ss')
      end = this.timeFormate(end, localStorage.getItem('nz-default-dateFormat') ? localStorage.getItem('nz-default-dateFormat') : 'YYYY-MM-DD HH:mm:ss')
      this.selectDate = [start, end]
    },
    getHoursTime (hours) {
      const today = new Date().getTime()
      const date = new Date(today + (hours * 60 * 60 * 1000))
      return date
    },
    // 初始化日期
    getDays (days) {
      const today = new Date().getTime()
      const date = new Date(today + (days * 24 * 60 * 60 * 1000))
      return date
    },
    formatDate (date, type) {
      const yy = date.getFullYear()
      const dateM = date.getMonth() + 1
      const mm = dateM > 9 ? dateM : `0${dateM}`
      const dateD = date.getDate()
      const dd = dateD > 9 ? dateD : `0${dateD}`
      if (type) {
        return `${yy}${type}${mm}${type}${dd}`
      }
      return `${yy}${mm}${dd}`
    },
    timeFormate (date, fmt = localStorage.getItem('nz-default-dateFormat') ? localStorage.getItem('nz-default-dateFormat') : 'YYYY-MM-DD HH:mm:ss') {
      let time = ''
      if (!isNaN(date)) {
        time = new Date(date)
      } else {
        time = moment(date, fmt)
      }
      const fm = fmt
      return moment(time).format(fm)
    },
    formateTimeToTime (date, fmt = localStorage.getItem('nz-default-dateFormat') ? localStorage.getItem('nz-default-dateFormat') : 'YYYY-MM-DD HH:mm:ss') {
      if (isNaN(date)) {
        const newDate = moment(date, fmt)
        return newDate
      } else {
        return date
      }
    },
    getStep (startTime, endTime) {
      const start = new Date(startTime)
      const end = new Date(endTime)
      let step = '15s'
      const numInterval = end.getTime() - start.getTime()
      const oneDay = 86400000
      const sevenDay = 604800000
      const thirtyDay = 2592000000
      if (numInterval < oneDay) { // 小于1天,step为15s
        step = '15s'
      } else if (numInterval < sevenDay) { // 小于7天,step为15s
        step = '5m'
      } else if (numInterval < thirtyDay) { // 小于30天,step为15s
        step = '10m'
      } else {
        step = '30m'
      }
      return step
    },
    isEmptyObject (obj) {
      if (obj) {
        let name = ''
        // eslint-disable-next-line
        for (name in obj) { return false; }
        return true
      }
      return true
    },
    validateEmail (rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入邮箱'))
      } else if (!this.emailReg.test(value)) {
        callback(new Error('邮箱格式不正确'))
      } else {
        callback()
      }
    },
    getNumStr (num) {
      if (num >= 1000) {
        const kbNum = num / 1000
        if (kbNum >= 1000) {
          const mbNum = kbNum / 1000
          if (mbNum > 1000) {
            const gbNum = mbNum / 1000
            if (gbNum > 1000) {
              const tbNum = gbNum / 1000
              if (tbNum > 1000) {
                const pbNum = tbNum / 1000
                return `${pbNum.toFixed(2)}PB`
              }
              return `${tbNum.toFixed(2)}TB`
            }
            return `${gbNum.toFixed(2)}GB`
          }
          return `${mbNum.toFixed(2)}MB`
        }
        return `${kbNum.toFixed(2)}KB`
      }
      return num.toFixed(2)
    },
    getSingleStatRlt (statistics, result) {
      let dataArray = []
      if (result) {
        result.forEach((item) => {
          dataArray.push(item[1])
        })
      }
      let statisticsRlt = ''
      if (dataArray.length > 0) {
        if (statistics === 'min') { // min:最小值
          statisticsRlt = dataArray.reduce(function (a, b) {
            return b < a ? b : a
          })
        } else if (statistics === 'max') { // max:最大值
          statisticsRlt = dataArray.reduce(function (a, b) {
            return b > a ? b : a
          })
        } else if (statistics === 'avg') { // avg:平均值
          let sum = 0
          dataArray.forEach((item) => {
            sum = Number(sum) + Number(item)
          })
          statisticsRlt = sum / dataArray.length
        } else if (statistics === 'total') { // total:总计
          dataArray.forEach((item) => {
            statisticsRlt = Number(statisticsRlt) + Number(item)
          })
        } else if (statistics === 'first') { // first:第一个值
          statisticsRlt = dataArray[0]
        } else if (statistics === 'last') { // last:最后一个值
          statisticsRlt = dataArray[dataArray.length - 1]
        } else if (statistics === 'range') { // range : max - min
          const min = dataArray.reduce(function (a, b) {
            return b < a ? b : a
          })
          const max = dataArray.reduce(function (a, b) {
            return b > a ? b : a
          })
          statisticsRlt = max - min
        } else if (statistics === 'different') { // different : last - first
          statisticsRlt = dataArray[dataArray.length - 1] - dataArray[0]
        }
      }
      dataArray = null
      return statisticsRlt
    },
    // 将本地时区转为系统配置的时区
    computeTimezone: function (sourceTime) {
      let offset = localStorage.getItem('nz-sys-timezone')
      offset = moment.tz(offset).format('Z')
      if (offset && offset !== 'undefined') {
        offset = Number.parseInt(offset)
        const date = new Date(sourceTime)
        const localOffset = date.getTimezoneOffset() * 60 * 1000 // 默认 一分钟显示时区偏移的结果
        const utcTime = sourceTime + localOffset
        return utcTime + (offset * 60 * 60 * 1000)
      } else {
        return sourceTime
      }
    },
    // 将本地时区转为系统配置的时区
    computeTimezoneTime: function (sourceTime) {
      let offset = localStorage.getItem('nz-sys-timezone')
      offset = moment.tz(offset).format('Z')
      if (offset && offset !== 'undefined') {
        offset = Number.parseInt(offset)
        const date = new Date(sourceTime)
        const localOffset = date.getTimezoneOffset() * 60 * 1000 // 默认 一分钟显示时区偏移的结果
        const utcTime = date.getTime() + localOffset
        return utcTime + (offset * 60 * 60 * 1000)
      } else {
        return sourceTime
      }
    },
    getTimezontDateRange: function (offset = -1) {
      return [
        new Date(new Date(this.computeTimezone(new Date().getTime())).setHours(new Date(this.computeTimezone(new Date().getTime())).getHours() + offset)),
        new Date(this.computeTimezone(new Date().getTime()))
      ]
    },
    getNewTime (time, num) {
      const date = new Date(time)
      const newDate = new Date(parseInt(date.getTime(), 10) + num)
      return this.timeFormate(newDate, localStorage.getItem('nz-default-dateFormat') ? localStorage.getItem('nz-default-dateFormat') : 'YYYY-MM-DD HH:mm:ss')
    },
    getOffsetTimezoneData (offset = 0) {
      return new Date(this.computeTimezone(new Date().getTime())).setHours(new Date(this.computeTimezone(new Date().getTime())).getHours() + offset)
    },
    debounce (fn, delay) {
      // 记录上一次的延时器
      let timer = null
      delay = delay || 200
      return function () {
        const args = arguments
        const that = this
        // 清除上一次延时器
        clearTimeout(timer)
        timer = setTimeout(function () {
          fn.apply(that, args)
        }, delay)
      }
    },
    UTCTimeToConfigTimezone (utcTime) {
      let offset = localStorage.getItem('nz-sys-timezone')
      offset = moment.tz(offset).format('Z')
      Eif (offset && offset !== 'undefined') {
        let time = utcTime
        if (typeof time === 'string' && /(\d+?-){2}\d+?\s(\d+?:)*\d+/.test(time)) {
          time = new Date(time).getTime()
        }
        offset = Number.parseInt(offset)
        time += offset * 60 * 60 * 1000
        return time
      } else {
        return utcTime
      }
    },
    configTimezoneToUTCTime: function (configTime) {
      let offset = localStorage.getItem('nz-sys-timezone')
      offset = moment.tz(offset).format('Z')
      Eif (offset && offset !== 'undefined') {
        let time = configTime
        if (typeof time === 'string' && /(\d+?-){2}\d+?\s(\d+?:)*\d+/.test(time)) {
          time = new Date(time).getTime()
        }
        offset = Number.parseInt(offset)
        time -= offset * 60 * 60 * 1000
        return time
      } else {
        return configTime
      }
    },
    countDecimals (value) {
      if ((value || value === 0) && Math.floor(value) !== value) {
        const arr = value.toString().split('.')
        if (Math.abs(arr[0] > 0) || arr.length < 2) {
          return 2
        }
        const dot = arr[1].split('0').length + 1
        return dot || 2
      }
      return 2
    }
  },
  created () {
    this.getDefaultDate()
  },
  computed: {
    /*
    isAdmin() {
      return this.role === Cookies.get('owl_role');
    },
    */
  }
})