Skip to content

feat: expose FluxCsvParser as AnnotatedCsvParser #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features
1. [#376](https://github.com/influxdata/influxdb-client-csharp/pull/376): Add `FluxRecord.Row` which stores response data in a list
1. [#404](https://github.com/influxdata/influxdb-client-csharp/pull/404): Expose `FluxCsvParser` as `AnnotatedCsvParser`

### Dependencies
Update dependencies:
Expand Down
48 changes: 48 additions & 0 deletions Client.Core/Flux/Serialization/AnnotatedCsvParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Threading;
using InfluxDB.Client.Core.Flux.Domain;
using InfluxDB.Client.Core.Flux.Internal;

namespace InfluxDB.Client.Core.Flux.Serialization
{
public interface IAnnotatedCsvParser
{
/// <summary>
/// Parsing query results in annotated CSV format into list of <see cref="FluxTable"/>.
/// </summary>
/// <param name="annotatedCsv">Query results in annotated CSV format</param>
/// <param name="cancellationToken">To cancel the parsing</param>
/// <returns>Parsed Annotated CSV into list of <see cref="FluxTable"/></returns>
List<FluxTable> Parse(string annotatedCsv, CancellationToken cancellationToken = default);
}

/// <summary>
/// Parser for processing <see href="https://docs.influxdata.com/influxdb/cloud/reference/syntax/annotated-csv/">Annotated CSV</see>.
/// </summary>
public class AnnotatedCsvParser : IAnnotatedCsvParser
{
private readonly FluxCsvParser _parser;

/// <summary>
/// Public constructor.
/// </summary>
public AnnotatedCsvParser()
{
_parser = new FluxCsvParser();
}

/// <summary>
/// Parsing query results in annotated CSV format into list of <see cref="FluxTable"/>.
/// </summary>
/// <param name="annotatedCsv">Query results in annotated CSV format</param>
/// <param name="cancellationToken">To cancel the parsing</param>
/// <returns>Parsed Annotated CSV into list of <see cref="FluxTable"/></returns>
public List<FluxTable> Parse(string annotatedCsv, CancellationToken cancellationToken = default)
{
var consumer = new FluxCsvParser.FluxResponseConsumerTable();
_parser.ParseFluxResponse(annotatedCsv, cancellationToken, consumer);

return consumer.Tables;
}
}
}
18 changes: 18 additions & 0 deletions Client.Legacy.Test/FluxCsvParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using InfluxDB.Client.Core.Flux.Domain;
using InfluxDB.Client.Core.Flux.Exceptions;
using InfluxDB.Client.Core.Flux.Internal;
using InfluxDB.Client.Core.Flux.Serialization;
using NodaTime;
using NodaTime.Text;
using NUnit.Framework;
Expand Down Expand Up @@ -793,6 +794,23 @@ public void ParseDuplicateColumnNames()
Assert.AreEqual(25.3, tables[0].Records[0].Row[7]);
}

[Test]
public void AnnotatedCsvParser()
{
const string data =
"#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,string,string,string,string,long,long,string\n"
+ "#group,false,false,true,true,true,true,true,true,false,false,false\n"
+ "#default,_result,,,,,,,,,,\n"
+ ",result,table,_start,_stop,_field,_measurement,host,region,_value2,value1,value_str\n"
+ ",,0,1677-09-21T00:12:43.145224192Z,2018-07-16T11:21:02.547596934Z,free,mem,A,west,121,11,test\n"
+ ",,1,1677-09-21T00:12:43.145224192Z,2018-07-16T11:21:02.547596934Z,free,mem,B,west,484,22,test\n"
+ ",,2,1677-09-21T00:12:43.145224192Z,2018-07-16T11:21:02.547596934Z,usage_system,cpu,A,west,1444,38,test\n"
+ ",,3,1677-09-21T00:12:43.145224192Z,2018-07-16T11:21:02.547596934Z,user_usage,cpu,A,west,2401,49,test";

var tables = new AnnotatedCsvParser().Parse(data);
Assert.AreEqual(4, tables.Count);
}

private List<FluxTable> ParseFluxResponse(string data)
{
var consumer = new FluxCsvParser.FluxResponseConsumerTable();
Expand Down