Skip to content

Commit 3efde23

Browse files
authored
feat: update all versions of fromFlux (#3776)
1 parent 65592aa commit 3efde23

File tree

11 files changed

+124
-20
lines changed

11 files changed

+124
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
"@docsearch/react": "^3.0.0-alpha.37",
159159
"@influxdata/clockface": "^3.1.14",
160160
"@influxdata/flux-lsp-browser": "^0.8.0",
161-
"@influxdata/giraffe": "^2.23.1",
161+
"@influxdata/giraffe": "^2.24.1",
162162
"@influxdata/influxdb-templates": "0.9.0",
163163
"@influxdata/react-custom-scrollbars": "4.3.8",
164164
"abortcontroller-polyfill": "^1.3.0",

src/alerting/utils/history.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('history utils', () => {
3333
beforeEach(jest.clearAllMocks)
3434

3535
const fluxGroupKeyUnion = ['']
36+
const resultColumnNames = []
3637
const length = 100
3738
const cols = 20
3839
const table: Table = {
@@ -51,6 +52,7 @@ describe('history utils', () => {
5152
mocked(fromFlux).mockImplementationOnce(() => ({
5253
table: {...table, length: 0},
5354
fluxGroupKeyUnion,
55+
resultColumnNames,
5456
}))
5557

5658
const actual = await processResponse({
@@ -70,6 +72,7 @@ describe('history utils', () => {
7072
mocked(fromFlux).mockImplementationOnce(() => ({
7173
table,
7274
fluxGroupKeyUnion,
75+
resultColumnNames,
7376
}))
7477
const expected = range(length).map(i =>
7578
Object.fromEntries([

src/alerting/utils/statusEvents.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('process statuses response', () => {
2121
beforeEach(jest.clearAllMocks)
2222

2323
const fluxGroupKeyUnion = ['']
24+
const resultColumnNames = []
2425
const length = 100
2526
const cols = 20
2627
const table: Table = {
@@ -38,6 +39,7 @@ describe('process statuses response', () => {
3839
mocked(fromFlux).mockImplementationOnce(() => ({
3940
table: {...table, length: 0},
4041
fluxGroupKeyUnion,
42+
resultColumnNames,
4143
}))
4244

4345
const actual = await processStatusesResponse({
@@ -55,7 +57,11 @@ describe('process statuses response', () => {
5557
})
5658

5759
it('process single table', async () => {
58-
mocked(fromFlux).mockImplementationOnce(() => ({table, fluxGroupKeyUnion}))
60+
mocked(fromFlux).mockImplementationOnce(() => ({
61+
table,
62+
fluxGroupKeyUnion,
63+
resultColumnNames,
64+
}))
5965
const expected = range(length).map(i =>
6066
Object.fromEntries([
6167
['table', 0],

src/shared/contexts/csv.worker.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import {csvParse, csvParseRows} from 'd3-dsv'
22

3+
const RESULT_COLUMN_INDEX = 1
34
interface FromFluxResult {
45
error?: Error
56
// The single parsed `Table`
67
table: Table
78

89
// The union of unique group keys from all input Flux tables
910
fluxGroupKeyUnion: string[]
11+
12+
// All possible values of the "result" column
13+
resultColumnNames: string[]
1014
}
1115

1216
type Column =
@@ -87,6 +91,7 @@ const assert = (condition: boolean, errorMessage: string) => {
8791
const fromFlux = (fluxCSV: string): FromFluxResult => {
8892
const columns: Columns = {}
8993
const fluxGroupKeyUnion = new Set<string>()
94+
const resultColumnNames = new Set<string>()
9095
let tableLength = 0
9196

9297
try {
@@ -101,11 +106,29 @@ const fromFlux = (fluxCSV: string): FromFluxResult => {
101106
let columnDefault: any = ''
102107

103108
for (const chunk of chunks) {
104-
tableText = chunk
105-
.split('\n')
106-
.filter(line => !line.startsWith('#'))
107-
.join('\n')
108-
.trim()
109+
const splittedChunk = chunk.split('\n')
110+
111+
const tableTexts = []
112+
const annotationTexts = []
113+
114+
splittedChunk.forEach(line => {
115+
if (line.startsWith('#')) {
116+
annotationTexts.push(line)
117+
} else {
118+
tableTexts.push(line)
119+
}
120+
if (line.startsWith('#default')) {
121+
const defaults = line.split(',')
122+
if (
123+
defaults.length >= RESULT_COLUMN_INDEX + 1 &&
124+
defaults[RESULT_COLUMN_INDEX].length
125+
) {
126+
resultColumnNames.add(defaults[RESULT_COLUMN_INDEX])
127+
}
128+
}
129+
})
130+
131+
tableText = tableTexts.join('\n').trim()
109132

110133
assert(
111134
!!tableText,
@@ -114,11 +137,7 @@ const fromFlux = (fluxCSV: string): FromFluxResult => {
114137

115138
tableData = csvParse(tableText)
116139

117-
annotationText = chunk
118-
.split('\n')
119-
.filter(line => line.startsWith('#'))
120-
.join('\n')
121-
.trim()
140+
annotationText = annotationTexts.join('\n').trim()
122141

123142
assert(
124143
!!annotationText,
@@ -182,16 +201,18 @@ const fromFlux = (fluxCSV: string): FromFluxResult => {
182201
const result = {
183202
table,
184203
fluxGroupKeyUnion: Array.from(fluxGroupKeyUnion),
204+
resultColumnNames: Array.from(resultColumnNames),
185205
}
186206

187207
return result
188208
} catch (error) {
189209
return {
210+
error,
190211
table: {
191212
columns: {},
192213
},
193214
fluxGroupKeyUnion: [],
194-
error,
215+
resultColumnNames: [],
195216
}
196217
}
197218
}

src/shared/utils/fromFlux.legacy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ export default function fromFluxLegacy(csv: string): FromFluxResult {
2626
newTable(parsedFlux.table.length)
2727
),
2828
fluxGroupKeyUnion: parsedFlux.fluxGroupKeyUnion,
29+
resultColumnNames: parsedFlux.resultColumnNames,
2930
}
3031
}

src/shared/utils/fromFlux.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,66 @@ describe('fromFlux', () => {
143143
expect(fluxGroupKeyUnion).toEqual(['a', 'c', 'd'])
144144
})
145145

146+
test('returns all result column names', () => {
147+
const CSV = `#group,true,false,false,true
148+
#datatype,string,string,string,string
149+
#default,new,,,
150+
,result,b,c,d
151+
,,0,0,0
152+
153+
#group,false,false,true,false
154+
#datatype,string,string,string,string
155+
#default,old,,,
156+
,result,b,c,d
157+
,,0,0,0`
158+
159+
const flux = fromFlux(CSV)
160+
const fluxLegacy = fromFluxLegacy(CSV)
161+
162+
expect(flux.resultColumnNames).toEqual(['new', 'old'])
163+
expect(fluxLegacy.resultColumnNames).toEqual(['new', 'old'])
164+
})
165+
166+
test('handles blank result column names', () => {
167+
const CSV = `#group,true,false,false,true
168+
#datatype,string,string,string,string
169+
#default,,,,
170+
,result,b,c,d
171+
,,0,0,0
172+
173+
#group,false,false,true,false
174+
#datatype,string,string,string,string
175+
#default,,,,
176+
,result,b,c,d
177+
,,0,0,0`
178+
179+
const flux = fromFlux(CSV)
180+
const fluxLegacy = fromFluxLegacy(CSV)
181+
182+
expect(flux.resultColumnNames).toEqual([])
183+
expect(fluxLegacy.resultColumnNames).toEqual([])
184+
})
185+
186+
test('handles missing result column', () => {
187+
const CSV = `#group,true,false,false,true
188+
#datatype,string,string,string,string
189+
#default,_result,,,
190+
,a,b,c,d
191+
,,0,0,0
192+
193+
#group,false,false,true,false
194+
#datatype,string,string,string,string
195+
#default,_result,,,
196+
,a,b,c,d
197+
,,0,0,0`
198+
199+
const flux = fromFlux(CSV)
200+
const fluxLegacy = fromFluxLegacy(CSV)
201+
202+
expect(flux.resultColumnNames).toEqual([])
203+
expect(fluxLegacy.resultColumnNames).toEqual([])
204+
})
205+
146206
test('parses empty numeric values as null', () => {
147207
const CSV = `#group,false,false,true,true,false,true,true,true,true,true
148208
#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string,string

src/shared/utils/fromFlux.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export interface ParsedFlux {
135135
length: number
136136
}
137137
fluxGroupKeyUnion: string[]
138+
resultColumnNames: string[]
138139
}
139140

140141
export default function fromFlux(csv: string): ParsedFlux {
@@ -347,12 +348,20 @@ export default function fromFlux(csv: string): ParsedFlux {
347348
}
348349
})
349350

351+
const resultColumnNames = new Set<string>()
352+
output?.result?.data.forEach(columnName => {
353+
if (String(columnName).length > 0) {
354+
resultColumnNames.add(String(columnName))
355+
}
356+
})
357+
350358
return {
351359
table: {
352360
columnKeys: Object.keys(output),
353361
columns: output,
354362
length: runningTotal,
355363
},
356364
fluxGroupKeyUnion: Object.keys(groupKey),
365+
resultColumnNames: Array.from(resultColumnNames),
357366
}
358367
}

src/timeMachine/selectors/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ const getVisTableMemoized = memoizeOne(fromFlux)
115115

116116
export const getVisTable = (
117117
state: AppState
118-
): {table: Table; fluxGroupKeyUnion: string[]} => {
118+
): {table: Table; fluxGroupKeyUnion: string[]; resultColumnNames: string[]} => {
119119
const files = getActiveTimeMachine(state).queryResults.files || []
120-
const {table, fluxGroupKeyUnion} = getVisTableMemoized(files.join('\n\n'))
120+
const {table, fluxGroupKeyUnion, resultColumnNames} = getVisTableMemoized(
121+
files.join('\n\n')
122+
)
121123

122-
return {table, fluxGroupKeyUnion}
124+
return {table, fluxGroupKeyUnion, resultColumnNames}
123125
}
124126

125127
const getNumericColumnsMemoized = memoizeOne(getNumericColumnsUtil)

src/visualization/types/Map/view.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const setup = () => {
3333
result: {
3434
table,
3535
fluxGroupKeyUnion: ['', ''],
36+
resultColumnNames: [],
3637
results: {},
3738
},
3839
}

src/visualization/types/Scatter/TimeAutoToggle.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('Time Domain Auto Toggle', () => {
5151
results: {
5252
table,
5353
fluxGroupKeyUnion: ['', ''],
54+
resultColumnNames: [],
5455
...results,
5556
},
5657
update: jest.fn(),

0 commit comments

Comments
 (0)