# All files / utils randomTable.ts

46.15% Statements 42/91
17.5% Branches 7/40
60% Functions 3/5
46.43% Lines 39/84

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
    1771x
    1x
    1x

    1x

    20x

    20x

    1x
    1x
    1x

    1x
    1x

    1x

    1x

    1x
    1x

    1x
    1x
    1x
    1x

    1x

    1x
    20x
    20x
    20x

    1x

    1x

    1x
    1x
    3x
    3x
    3x
    60x

    3x
    3x

    1x

    1x

    3x

    1x

    1x

     import {newTable} from './newTable'
    import {LayerTypes} from '../types'
    import memoizeOne from 'memoize-one'

    const getRandomNumber = (
      max: number,
      decimalPlaces: number,
      include_negative?: boolean
    ) => {
      const result = include_negative
        ? Number((Math.random() * (2 * max + 1) - max).toFixed(decimalPlaces))
        : Number((Math.random() * max).toFixed(decimalPlaces))

      return result === -0 ? 0 : result // eslint-disable-line no-compare-neg-zero
    }

    export const COLUMN_KEY = 'cpu'
    export const POINT_KEY = 'disk'
    export const HOST_KEY = 'host'

    const defaultNumberOfRecords = 80
    const defaultRecordsPerLine = 20

    export const makeColumnName = (length: number) => {
      let result = ''
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
      const charactersLength = characters.length
      for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength))
      }
      return result
    }

    type ColumnsArg = number | Array<string>

    interface RandomFillColumns {
      columnNames: Array<string>
      columnValues: Array<Array<string>>
    }

    export const getRandomTable = memoizeOne(
      (
        maxValue: number,
        includeNegative: boolean,
        InumberOfRecords: number = defaultNumberOfRecords,
        IrecordsPerLine: number = defaultRecordsPerLine,
        fillColumns?: ColumnsArg,
        fillColumnNameLength?: number
      ) => {
        const timeColumn = []
        const valueColumn = []
        const cpuColumn = []
        const randomFillColumns = {
          columnNames: [],
          columnValues: [],
        } as RandomFillColumns
        const now = Date.now()

        for (let i = 0; i < numberOfRecords; i += 1) {
          valueColumn.push(getRandomNumber(maxValue, 2, includeNegative))
          cpuColumn.push(`cpu${Math.floor(i / recordsPerLine)}`)
          timeColumn.push(now + (i % recordsPerLine) * 1000 * 60)
        }

        Iif (!fillColumns) {
          randomFillColumns.columnNames.push('cpu')
          randomFillColumns.columnValues.push(cpuColumn)
        }

        Iif (typeof fillColumns === 'number') {
          const nameLength =
            typeof fillColumnNameLength === 'number' &&
            fillColumnNameLength === fillColumnNameLength
              ? fillColumnNameLength
              : 4
          for (let j = 0; j < fillColumns; j += 1) {
            const randomFillColumn = []
            const randomFillColumnName = makeColumnName(nameLength)
            for (let k = 0; k < numberOfRecords; k += 1) {
              randomFillColumn.push(
                `${randomFillColumnName}${Math.floor(k / recordsPerLine)}`
              )
            }
            randomFillColumns.columnNames.push(randomFillColumnName)
            randomFillColumns.columnValues.push(randomFillColumn)
          }
        }

        Eif (Array.isArray(fillColumns)) {
          for (let j = 0; j < fillColumns.length; j += 1) {
            const randomFillColumn = []
            const fillColumnName = fillColumns[j]
            for (let k = 0; k < numberOfRecords; k += 1) {
              randomFillColumn.push(
                `${fillColumnName}${Math.floor(k / recordsPerLine)}`
              )
            }
            randomFillColumns.columnNames.push(fillColumnName)
            randomFillColumns.columnValues.push(randomFillColumn)
          }
        }

        let table = newTable(numberOfRecords)
          .addColumn('_time', 'dateTime:RFC3339', 'time', timeColumn)
          .addColumn('_value', 'system', 'number', valueColumn)

        randomFillColumns.columnNames.forEach(
          (name, index) =>
            (table = table.addColumn(
              name,
              'string',
              'string',
              randomFillColumns.columnValues[index]
            ))
        )
        return table
      }
    )

    export interface SampleTableOptions {
      include_negative?: boolean
      all_negative?: boolean
      decimalPlaces?: number
      maxValue?: number
      numberOfRecords?: number
      recordsPerLine?: number
      plotType?: string
    }

    export const createSampleTable = (options: SampleTableOptions) => {
      const {
        include_negative = false,
        all_negative = false,
        decimalPlaces = 2,
        maxValue = 100,
        numberOfRecords = 20,
        recordsPerLine = 5,
        plotType = 'line',
      } = options

      const now = Date.now()
      const TIME_COL = []
      const VALUE_COL = []
      const CPU_COL = []
      const SYMBOL_COL = []
      const DISK_COL = []
      const HOST_COL = []

      for (let i = 0; i < numberOfRecords; i += 1) {
        let num = getRandomNumber(maxValue, decimalPlaces)
        if (include_negative) {
          num = all_negative
            ? Math.abs(num) * -1
            : getRandomNumber(maxValue, decimalPlaces, true)
        }
        VALUE_COL.push(num)
        CPU_COL.push(`${COLUMN_KEY}${Math.floor(i / recordsPerLine)}`)
        TIME_COL.push(now + (i % recordsPerLine) * 1000 * 60)
        if (plotType === LayerTypes.Scatter) {
          SYMBOL_COL.push(i % 2)
          DISK_COL.push(`disk-${i % recordsPerLine}`)
          HOST_COL.push(`host-${i % 2}`)
        }
      }
      const table = newTable(numberOfRecords)
        .addColumn('_time', 'dateTime:RFC3339', 'time', TIME_COL)
        .addColumn('_value', 'system', 'number', VALUE_COL)

      if (plotType === LayerTypes.Scatter) {
        return table
          .addColumn(POINT_KEY, 'string', 'string', DISK_COL)
          .addColumn('__symbol', 'string', 'string', SYMBOL_COL)
          .addColumn(HOST_KEY, 'string', 'string', HOST_COL)
      }
      return table.addColumn(COLUMN_KEY, 'string', 'string', CPU_COL)
    }