# All files / transforms mosaic.ts

7.59% Statements 6/79
0% Branches 0/26
0% Functions 0/8
8% Lines 6/75

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
    1871x

    1x
    1x
    1x
    1x

    1x

     import {newTable} from '../utils/newTable'
    import {MosaicLayerSpec, Table} from '../types'
    import {DISPLAY_NAME, FILL, SERIES, X_MAX, X_MIN} from '../constants/columnKeys'
    import {createGroupIDColumn} from './'
    import {resolveDomain} from '../utils/resolveDomain'
    import {getNominalColorScale} from './'

    export const mosaicTransform = (
      inputTable: Table,
      xColumnKey: string,
      yColumnKeys: string[],
      yLabelColumns: string[],
      yLabelColumnSeparator: string,
      xDomain: number[],
      fillColKeys: string[],
      colors: string[]
    ): MosaicLayerSpec => {
      const [fillColumn, fillColumnMap] = createGroupIDColumn(
        inputTable,
        fillColKeys
      )
      const xInputCol = inputTable.getColumn(xColumnKey, 'number') || []

      let labelColumns = Array.isArray(yLabelColumns)
        ? yLabelColumns.filter(labelColumn => yColumnKeys.includes(labelColumn))
        : []
      if (labelColumns.length === 0 && Array.isArray(yColumnKeys)) {
        labelColumns = yColumnKeys
      }
      const yColumnsName = labelColumns.reduce(
        (combinedName, columnName) =>
          combinedName
            ? `${combinedName}${yLabelColumnSeparator}${columnName}`
            : columnName,
        ''
      )
      const yInputCols = {}
      if (Array.isArray(yColumnKeys)) {
        yColumnKeys.forEach(columnKey => {
          const column = inputTable.getColumn(columnKey, 'string')
          yInputCols[columnKey] = column
        })
      }

      // Mosaic can only have one column as the fill value,
      //   always the first fill column key
      const valueKey = fillColumnMap.columnKeys[0]

      const timeStampMap = {}

      for (let i = 0; i < inputTable.length; i++) {
        const yColumnTick = Array.isArray(yColumnKeys)
          ? yColumnKeys.reduce((combinedValue, key) => {
              let value = ''
              if (yInputCols[key]) {
                value = yInputCols[key][i]
              }
              return `${combinedValue}${value}`
            }, '')
          : ''

        const yTickLabel = labelColumns.reduce((combinedValue, key) => {
          let value = ''
          if (yInputCols[key]) {
            value = yInputCols[key][i]
          }
          return combinedValue
            ? `${combinedValue}${yLabelColumnSeparator}${value}`
            : value
        }, '')

        const currentX = xInputCol[i]
        const currentFillValue = fillColumnMap.mappings[fillColumn[i]][valueKey]

        if (!timeStampMap[currentX]) {
          timeStampMap[currentX] = []
        }
        timeStampMap[currentX].push({
          yTickLabel,
          yColumnTick,
          fill: currentFillValue,
        })
      }

      const sortedTimeStamps = Object.keys(timeStampMap).sort()
      let tableLength = 0

      /*
        This is the structure used to create and update the data map below:

        dataMap[series] = {
          xMin: NumericColumnData,
          xMax: NumericColumnData,
          fill: NumericColumnData,
          series: string[],
          displayedColumns: string[],
          yTickLabel: string,
        }
      */
      const dataMap = {}

      sortedTimeStamps.forEach(timeStamp => {
        timeStampMap[timeStamp].forEach(data => {
          if (!dataMap[data.yColumnTick]) {
            dataMap[data.yColumnTick] = {
              xMin: [Number(timeStamp)],
              xMax: [Number(timeStamp)],
              fill: [data.fill],
              series: [data.yColumnTick],
              displayedColumns: [data.yTickLabel],
              yTickLabel: data.yTickLabel,
            }
            tableLength += 1
          } else {
            const prevMaxIndex = dataMap[data.yColumnTick].xMax.length - 1
            const prevFill =
              dataMap[data.yColumnTick].fill[
                dataMap[data.yColumnTick].fill.length - 1
              ]

            dataMap[data.yColumnTick].xMax[prevMaxIndex] = Number(timeStamp)
            if (prevFill !== data.fill) {
              dataMap[data.yColumnTick].xMin.push(Number(timeStamp))
              dataMap[data.yColumnTick].xMax.push(Number(timeStamp))
              dataMap[data.yColumnTick].fill.push(data.fill)
              dataMap[data.yColumnTick].series.push(data.yColumnTick)
              dataMap[data.yColumnTick].displayedColumns.push(data.yTickLabel)
              tableLength += 1
            }
          }
        })
      })

      let xMinData = []
      let xMaxData = []
      let fillData = []
      let seriesData = []
      let displayedColumnsData = []
      const yTicks = []
      const ySeries = []

      for (const key in dataMap) {
        //combine all series into the proper shape
        xMinData = xMinData.concat(dataMap[key].xMin)
        xMaxData = xMaxData.concat(dataMap[key].xMax)
        fillData = fillData.concat(dataMap[key].fill)
        seriesData = seriesData.concat(dataMap[key].series)
        displayedColumnsData = displayedColumnsData.concat(
          dataMap[key].displayedColumns
        )
        ySeries.push(key)
        yTicks.push(dataMap[key].yTickLabel)
      }
      /*
        xMin (start time) | xMax (end time) | Value Category | host | cpu
        -------------------------------------------------------------------
            1554308748000  |   1554308758000 |     'eenie'    | "a"  |  1
            1554308748000  |   1554308758000 |       'mo'     | "b"  |  2
      */
      const table = newTable(tableLength)
        .addColumn(X_MIN, 'system', 'number', xMinData)
        .addColumn(X_MAX, 'system', 'number', xMaxData)
        .addColumn(FILL, 'string', 'string', fillData)
        .addColumn(SERIES, 'string', 'string', seriesData)
        .addColumn(DISPLAY_NAME, 'string', 'string', displayedColumnsData)

      const resolvedXDomain = resolveDomain(xInputCol, xDomain)
      const resolvedYDomain = [0, yTicks.length]
      const fillScale = getNominalColorScale(fillColumnMap, colors)

      return {
        type: 'mosaic',
        inputTable,
        table,
        xDomain: resolvedXDomain,
        xColumnKey,
        xColumnType: inputTable.getColumnType(xColumnKey),
        yDomain: resolvedYDomain,
        yColumnType: 'string',
        yColumnsName,
        ySeries,
        yTicks,
        scales: {fill: fillScale},
        columnGroupMaps: {fill: fillColumnMap},
      }
    }