Skip to content

Commit 49aad04

Browse files
committed
feat: CSV parser is able to parse export from UI
1 parent 2cb5b11 commit 49aad04

2 files changed

Lines changed: 37 additions & 9 deletions

File tree

client-core/src/main/java/com/influxdb/query/internal/FluxCsvParser.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import java.time.Duration;
2929
import java.time.Instant;
3030
import java.util.ArrayList;
31+
import java.util.Arrays;
3132
import java.util.Base64;
33+
import java.util.Collections;
3234
import java.util.List;
3335
import javax.annotation.Nonnull;
3436
import javax.annotation.Nullable;
@@ -53,6 +55,12 @@
5355
*/
5456
public class FluxCsvParser {
5557

58+
private static final String ANNOTATION_DATATYPE = "#datatype";
59+
private static final String ANNOTATION_GROUP = "#group";
60+
private static final String ANNOTATION_DEFAULT = "#default";
61+
private static final List<String> ANNOTATIONS = Arrays
62+
.asList(ANNOTATION_DATATYPE, ANNOTATION_GROUP, ANNOTATION_DEFAULT);
63+
5664
private enum ParsingState {
5765
NORMAL,
5866

@@ -125,6 +133,7 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,
125133
int tableId = -1;
126134
boolean startNewTable = false;
127135
FluxTable table = null;
136+
List<String> groups = Collections.emptyList();
128137
for (CSVRecord csvRecord : parser) {
129138

130139
if (cancellable.isCancelled()) {
@@ -157,10 +166,11 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,
157166

158167
String token = csvRecord.get(0);
159168
//// start new table
160-
if ("#datatype".equals(token)) {
169+
if (ANNOTATIONS.contains(token) && !startNewTable) {
161170
startNewTable = true;
162171

163172
table = new FluxTable();
173+
groups = Collections.emptyList();
164174
consumer.accept(tableIndex, cancellable, table);
165175
tableIndex++;
166176
tableId = -1;
@@ -171,18 +181,17 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,
171181
}
172182

173183
//#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string,string,string
174-
if ("#datatype".equals(token)) {
184+
if (ANNOTATION_DATATYPE.equals(token)) {
175185
addDataTypes(table, toList(csvRecord));
176186

177-
} else if ("#group".equals(token)) {
178-
addGroups(table, toList(csvRecord));
179-
180-
} else if ("#default".equals(token)) {
187+
} else if (ANNOTATION_GROUP.equals(token)) {
188+
groups = toList(csvRecord);
189+
} else if (ANNOTATION_DEFAULT.equals(token)) {
181190
addDefaultEmptyValues(table, toList(csvRecord));
182-
183191
} else {
184192
// parse column names
185193
if (startNewTable) {
194+
addGroups(table, groups);
186195
addColumnNamesAndTags(table, toList(csvRecord));
187196
startNewTable = false;
188197
continue;
@@ -297,12 +306,12 @@ private void addGroups(@Nonnull final FluxTable table, @Nonnull final List<Strin
297306
Arguments.checkNotNull(table, "table");
298307
Arguments.checkNotNull(groups, "groups");
299308

300-
for (int i = 0; i < groups.size(); i++) {
309+
for (int i = 0; i < table.getColumns().size(); i++) {
301310

302311
FluxColumn fluxColumn = getFluxColumn(i, table);
303312

304313
String group = groups.get(i);
305-
fluxColumn.setGroup(Boolean.valueOf(group));
314+
fluxColumn.setGroup(Boolean.parseBoolean(group));
306315
}
307316
}
308317

client-core/src/test/java/com/influxdb/query/internal/FluxCsvParserTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ void mappingBase64Binary() throws IOException {
270270
List<FluxTable> tables = parseFluxResponse(data);
271271

272272
byte[] value = (byte[]) tables.get(0).getRecords().get(0).getValueByKey("value");
273+
Assertions.assertThat(value).isNotNull();
273274
Assertions.assertThat(value).isNotEmpty();
274275
Assertions.assertThat(new String(value, UTF_8)).isEqualTo(binaryData);
275276

@@ -595,6 +596,24 @@ public void responseWithError() {
595596
.matches((Predicate<Throwable>) throwable -> ((FluxQueryException) throwable).reference() == 0);
596597
}
597598

599+
@Test
600+
public void parseExportFromUserInterface() throws IOException {
601+
String data = "#group,false,false,true,true,true,true,true,true,false,false\n"
602+
+ "#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,string,string,string,string,double,dateTime:RFC3339\n"
603+
+ "#default,mean,,,,,,,,,\n"
604+
+ ",result,table,_start,_stop,_field,_measurement,city,location,_value,_time\n"
605+
+ ",,0,1754-06-26T11:30:27.613654848Z,2040-10-27T12:13:46.485Z,temperatureC,weather,London,us-midwest,30,1975-09-01T16:59:54.5Z\n"
606+
+ ",,1,1754-06-26T11:30:27.613654848Z,2040-10-27T12:13:46.485Z,temperatureF,weather,London,us-midwest,86,1975-09-01T16:59:54.5Z\n";
607+
608+
List<FluxTable> tables = parseFluxResponse(data);
609+
Assertions.assertThat(tables).hasSize(2);
610+
Assertions.assertThat(tables.get(0).getRecords()).hasSize(1);
611+
Assertions.assertThat(tables.get(0).getColumns().get(0).isGroup()).isFalse();
612+
Assertions.assertThat(tables.get(0).getColumns().get(1).isGroup()).isFalse();
613+
Assertions.assertThat(tables.get(0).getColumns().get(2).isGroup()).isTrue();
614+
Assertions.assertThat(tables.get(1).getRecords()).hasSize(1);
615+
}
616+
598617
@Nonnull
599618
private List<FluxTable> parseFluxResponse(@Nonnull final String data) throws IOException {
600619

0 commit comments

Comments
 (0)