# All files / components/geo/processing tableProcessing.ts

59.18% Statements 29/49
60% Branches 24/40
50% Functions 4/8
59.57% Lines 28/47

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

    1x
    1x

    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x

    3x
    3x
    2x

    1x

    1x
    3x
    2x

    1x

    1x

    1x

    1x

    1x

    1x
    8x

    1x

     // Types
    import {GeoTable} from './GeoTable'
    import {PivotedGeoTable} from './PivotedGeoTable'
    import {NativeGeoTable} from './NativeGeoTable'
    import {Table} from '../../../types'
    import {LatLonColumns} from '../../../types/geo'

    // Constants
    export const FIELD_COLUMN = '_field'
    export const VALUE_COLUMN = '_value'
    export const TABLE_COLUMN = 'table'
    export const QUERY_META_COLUMNS = [
      FIELD_COLUMN,
      VALUE_COLUMN,
      TABLE_COLUMN,
      '_start',
      '_stop',
    ]

    export const TIME_COLUMN = '_time'
    export const START_COLUMN = '_start'
    export const STOP_COLUMN = '_stop'
    export const LON_COLUMN = 'lon'
    export const LAT_COLUMN = 'lat'
    export const GEO_HASH_COLUMN = 's2_cell_id'

    export const preprocessData = (
      table: Table,
      rowLimit: number,
      isS2Present: boolean,
      latLonColumns: LatLonColumns,
      s2Column: string
    ): GeoTable => {
      const isLatLonAsTags = latLonAsTags(isS2Present, latLonColumns)
      if (isS2Present || isLatLonAsTags) {
        return new NativeGeoTable(table, rowLimit, latLonColumns, s2Column)
      }
      return new PivotedGeoTable(table, rowLimit, latLonColumns)
    }

    const latLonAsTags = (useS2CellID, latLonColumns) => {
      if (typeof useS2CellID !== 'undefined' && useS2CellID === false) {
        if (
          latLonColumns?.lat?.key === 'tag' &&
          latLonColumns?.lon?.key === 'tag'
        ) {
          return true
        }
        return false
      }
      return false
    }

    export const getColumnNames = (
      table: Table,
      autoPivoting: boolean
    ): string[] => {
      if (autoPivoting) {
        return getFieldColumnValues(table)
      }
      return getNumericColumns(table)
    }

    const getFieldColumnValues = (table: Table): string[] => {
      const fieldColumn = table.getColumn(FIELD_COLUMN, 'string')
      const valueColumn = table.getColumn(VALUE_COLUMN)
      if (!fieldColumn) {
        return []
      }
      const entriesCount = fieldColumn.length
      const fieldNames = {}
      for (let i = 0; i < entriesCount; i++) {
        const fieldName = fieldColumn[i]
        if (typeof valueColumn[i] === 'number') {
          fieldNames[fieldName] = true
        }
      }
      return filterMetaColumns(Object.keys(fieldNames))
    }

    export const filterMetaColumns = (fieldNames: string[]) => {
      return fieldNames.filter(name => QUERY_META_COLUMNS.indexOf(name) < 0)
    }

    const getNumericColumns = (table: Table): string[] => {
      return table.columnKeys.filter(k => {
        if (k === 'result' || k === 'table') {
          return false
        }

        const columnType = table.getColumnType(k)

        return columnType === 'time' || columnType === 'number'
      })
    }