Skip to content

Commit

Permalink
fix: update fromFlux to allow result column to be in any position (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
TCL735 authored Feb 7, 2022
1 parent f6e1dce commit 19eb200
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
2 changes: 1 addition & 1 deletion giraffe/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influxdata/giraffe",
"version": "2.24.0",
"version": "2.24.1",
"main": "dist/index.js",
"module": "dist/index.js",
"license": "MIT",
Expand Down
94 changes: 86 additions & 8 deletions giraffe/src/utils/fromFlux.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,13 @@ describe('fromFlux', () => {
#datatype,string,string,string,string
#default,,,,
,a,b,c,d
,,,,
#group,false,false,true,false
#datatype,string,string,string,string
#default,,,,
,a,b,c,d`
,a,b,c,d
,,,,`

const {fluxGroupKeyUnion} = fromFlux(CSV)

Expand All @@ -452,30 +454,106 @@ describe('fromFlux', () => {
const CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,strangeColumnA,,,
,a,b,c,d
,result,b,c,d
,,0,0,0
#group,false,false,true,false
#datatype,string,string,string,string
#default,strangeColumnB,,,
,a,b,c,d`
,result,b,c,d
,,0,0,0`

const {resultColumnNames} = fromFlux(CSV)

expect(resultColumnNames).toEqual(['strangeColumnA', 'strangeColumnB'])
})

it('handles blank result column names', () => {
const CSV = `#group,true,false,false,true
it('handles result column in unexpected column position', () => {
const DEFAULT_CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,,resultInUnexpectedPosA,,
,a,result,c,d
,0,,0,0
#group,false,false,true,false
#datatype,string,string,string,string
#default,,resultInUnexpectedPosB,,
,a,result,c,d
,0,,0,0`

const defaultCSV = fromFlux(DEFAULT_CSV)

expect(defaultCSV.resultColumnNames).toEqual([
'resultInUnexpectedPosA',
'resultInUnexpectedPosB',
])

const ROWS_CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,,,,
,a,b,c,d
,a,result,c,d
,0,resultInUnexpectedPosA,0,0
#group,false,false,true,false
#datatype,string,string,string,string
#default,,,,
,a,b,c,d`
,a,result,c,d
,0,resultInUnexpectedPosB,0,0`

const {resultColumnNames} = fromFlux(CSV)
const rowsCSV = fromFlux(ROWS_CSV)

expect(rowsCSV.resultColumnNames).toEqual([
'resultInUnexpectedPosA',
'resultInUnexpectedPosB',
])
})

it('handles blank result column names and empty rows', () => {
const BLANK_RESULT_COLUMN_CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,,,,
,result,b,c,d
,,0,0,0
#group,false,false,true,false
#datatype,string,string,string,string
#default,,,,
,result,b,c,d
,,0,0,0`

const blankResultColumn = fromFlux(BLANK_RESULT_COLUMN_CSV)

expect(blankResultColumn.resultColumnNames).toEqual([])

const EMPTY_ROWS_CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,strangeColumnA,,,
,result,b,c,d
#group,false,false,true,false
#datatype,string,string,string,string
#default,strangeColumnB,,,
,result,b,c,d`

const emptyRows = fromFlux(EMPTY_ROWS_CSV)

expect(emptyRows.resultColumnNames).toEqual([])
})

it('handles missing result column', () => {
const NO_RESULT_CSV = `#group,true,false,false,true
#datatype,string,string,string,string
#default,strangeColumnA,,,
,a,b,c,d
,,,,
#group,false,false,true,false
#datatype,string,string,string,string
#default,strangeColumnB,,,
,a,b,c,d
,,,,`

const {resultColumnNames} = fromFlux(NO_RESULT_CSV)

expect(resultColumnNames).toEqual([])
})
Expand Down
19 changes: 8 additions & 11 deletions giraffe/src/utils/fromFlux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import {csvParse, csvParseRows} from 'd3-dsv'
import {Table, ColumnType, FluxDataType} from '../types'
import {assert} from './assert'
import {newTable} from './newTable'

const RESULT_COLUMN_INDEX = 1
import {RESULT} from '../constants/columnKeys'
export interface FromFluxResult {
error?: Error

Expand Down Expand Up @@ -100,15 +99,6 @@ export const fromFlux = (fluxCSV: string): FromFluxResult => {
} else {
tableTexts.push(line)
}
if (line.startsWith('#default')) {
const defaults = line.split(',')
if (
defaults.length >= RESULT_COLUMN_INDEX + 1 &&
defaults[RESULT_COLUMN_INDEX].length
) {
resultColumnNames.add(defaults[RESULT_COLUMN_INDEX])
}
}
})

tableText = tableTexts.join('\n').trim()
Expand Down Expand Up @@ -151,6 +141,13 @@ export const fromFlux = (fluxCSV: string): FromFluxResult => {
columnDefault = annotationData.defaultByColumnName[columnName]

for (let i = 0; i < tableData.length; i++) {
if (columnName === RESULT) {
if (columnDefault.length) {
resultColumnNames.add(columnDefault)
} else if (tableData[i][columnName].length) {
resultColumnNames.add(tableData[i][columnName])
}
}
columns[columnKey].data[tableLength + i] = parseValue(
tableData[i][columnName] || columnDefault,
columnType
Expand Down
2 changes: 1 addition & 1 deletion stories/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influxdata/giraffe-stories",
"version": "2.24.0",
"version": "2.24.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 19eb200

Please sign in to comment.