Skip to content

Commit

Permalink
feat(linq): add support for Async queries (influxdata#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Sep 29, 2021
1 parent 09986be commit 533c46f
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 3.1.0 [unreleased]

### Features
1. [#239](https://github.com/influxdata/influxdb-client-csharp/pull/239): Add support for Asynchronous queries [LINQ]

## 3.0.0 [2021-09-17]

### Breaking Changes
Expand All @@ -8,6 +11,7 @@ Adds a `Type` overload for POCOs to `QueryAsync`. This will add `object ConvertT
### Features
1. [#232](https://github.com/influxdata/influxdb-client-csharp/pull/232): Add a `Type` overload for POCOs to `QueryAsync`.
1. [#233](https://github.com/influxdata/influxdb-client-csharp/pull/233): Add possibility to follow HTTP redirects
1. [#239](https://github.com/influxdata/influxdb-client-csharp/pull/239): Add support for Asynchronous queries [LINQ]

### Bug Fixes
1. [#236](https://github.com/influxdata/influxdb-client-csharp/pull/236): Mapping `long` type into Flux AST [LINQ]
Expand Down
1 change: 1 addition & 0 deletions Client.Linq.Test/Client.Linq.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
138 changes: 108 additions & 30 deletions Client.Linq.Test/ItInfluxDBQueryableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ItInfluxDBQueryableTest : AbstractTest

await _client
.GetWriteApiAsync()
.WriteRecordsAsync("my-bucket", "my-org", WritePrecision.S,
.WriteRecordsAsync("my-bucket", "my-org", WritePrecision.S,
sensor11, sensor21, sensor12, sensor22, sensor13, sensor23, sensor14, sensor24);
}

Expand All @@ -50,35 +50,35 @@ public void QueryAll()

Assert.AreEqual(8, sensors.Count);
}

[Test]
public void QueryExample()
{
var query = (from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
where s.SensorId == "id-1"
where s.Value > 12
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s)
where s.SensorId == "id-1"
where s.Value > 12
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s)
.Take(2)
.Skip(2);

var sensors = query.ToList();

Assert.AreEqual(1, sensors.Count);
}

[Test]
public void QueryExampleCount()
{
var query = (from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
where s.SensorId == "id-1"
where s.Value > 12
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s)
where s.SensorId == "id-1"
where s.Value > 12
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s)
.Count();

Assert.AreEqual(3, query);
Expand All @@ -92,14 +92,14 @@ public void QueryTake()

var sensors = query.ToList();

Assert.AreEqual(2*2, sensors.Count);
Assert.AreEqual(2 * 2, sensors.Count);
}

[Test]
public void QueryTakeMultipleTimeSeries()
{
var query = (from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync(),
new QueryableOptimizerSettings {QueryMultipleTimeSeries = true})
new QueryableOptimizerSettings { QueryMultipleTimeSeries = true })
select s).Take(2);

var sensors = query.ToList();
Expand All @@ -115,9 +115,9 @@ public void QueryTakeSkip()

var sensors = query.ToList();

Assert.AreEqual(1+1, sensors.Count);
Assert.AreEqual(1 + 1, sensors.Count);
}

[Test]
public void QueryWhereEqual()
{
Expand All @@ -133,7 +133,7 @@ public void QueryWhereEqual()
Assert.AreEqual("id-1", sensor.SensorId);
}
}

[Test]
public void QueryWhereNotEqual()
{
Expand Down Expand Up @@ -229,7 +229,7 @@ public void QueryAnd()
Assert.GreaterOrEqual(sensor.Value, 28);
}
}

[Test]
public void QueryOr()
{
Expand All @@ -241,7 +241,7 @@ public void QueryOr()

Assert.AreEqual(6, sensors.Count);
}

[Test]
public void QueryTimeRange()
{
Expand All @@ -257,7 +257,7 @@ public void QueryTimeRange()
Assert.GreaterOrEqual(sensor.Value, 89);
}
}

[Test]
public void QueryTimeGreaterEqual()
{
Expand All @@ -269,7 +269,7 @@ public void QueryTimeGreaterEqual()

Assert.AreEqual(4, sensors.Count);
}

[Test]
public void QueryTimeEqual()
{
Expand All @@ -285,7 +285,7 @@ public void QueryTimeEqual()
Assert.GreaterOrEqual(sensor.Value, 15);
}
}

[Test]
public void QueryWhereNothing()
{
Expand All @@ -297,7 +297,7 @@ public void QueryWhereNothing()

Assert.AreEqual(0, sensors.Count);
}

[Test]
public void QueryOrderBy()
{
Expand All @@ -310,19 +310,19 @@ orderby s.Value
Assert.AreEqual(12, sensors.First().Value);
Assert.AreEqual(89, sensors.Last().Value);
}

[Test]
public void QueryOrderByTime()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
orderby s.Timestamp descending
orderby s.Timestamp descending
select s;

var sensors = query.ToList();

Assert.AreEqual(new DateTime(2020, 11, 17, 8, 20, 15, DateTimeKind.Utc),
Assert.AreEqual(new DateTime(2020, 11, 17, 8, 20, 15, DateTimeKind.Utc),
sensors.First().Timestamp);
Assert.AreEqual(new DateTime(2020, 10, 15, 8, 20, 15, DateTimeKind.Utc),
Assert.AreEqual(new DateTime(2020, 10, 15, 8, 20, 15, DateTimeKind.Utc),
sensors.Last().Timestamp);
}

Expand All @@ -349,6 +349,84 @@ public void QueryCountDifferentTimeSeries()
Assert.AreEqual(8, sensors);
}

[Test]
public void SyncQueryConfiguration()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApi())
select s;

var ae = Assert.Throws<ArgumentException>(() => query.ToList());
Assert.AreEqual("The 'QueryApiSync' has to be configured for sync queries.", ae.Message);
}

[Test]
public void ASyncQueryConfiguration()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
select s;

var ae = Assert.Throws<ArgumentException>(() => query.ToInfluxQueryable().GetAsyncEnumerator());
Assert.AreEqual("The 'QueryApi' has to be configured for Async queries.", ae.Message);
}

[Test]
public async Task ASyncQuery()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApi())
select s;

var sensors = await query
.ToInfluxQueryable()
.GetAsyncEnumerator()
.ToListAsync();

Assert.AreEqual(8, sensors.Count);
}

[Test]
public async Task ASyncQueryFirst()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApi())
select s;

var sensor = await query
.ToInfluxQueryable()
.GetAsyncEnumerator()
.FirstOrDefaultAsync();

Assert.IsNotNull(sensor);
}

[Test]
public void AggregateFunction()
{
var count = (from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApiSync())
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s)
.Count();

Assert.AreEqual(8, count);
}

[Test]
public async Task AggregateFunctionAsync()
{
var query = from s in InfluxDBQueryable<Sensor>.Queryable("my-bucket", "my-org", _client.GetQueryApi())
where s.Timestamp > new DateTime(2019, 11, 16, 8, 20, 15, DateTimeKind.Utc)
where s.Timestamp < new DateTime(2021, 01, 10, 5, 10, 0, DateTimeKind.Utc)
orderby s.Timestamp
select s;

var count = await query
.ToInfluxQueryable()
.GetAsyncEnumerator()
.CountAsync();

Assert.AreEqual(8, count);
}

[TearDown]
protected void After()
{
Expand Down
1 change: 1 addition & 0 deletions Client.Linq/Client.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<AssemblyName>InfluxDB.Client.Linq</AssemblyName>
<VersionPrefix>3.1.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
<LangVersion>8</LangVersion>

<PackageId>InfluxDB.Client.Linq</PackageId>
<PackageTags>influxdata;timeseries;flux;influxdb;linq</PackageTags>
Expand Down
Loading

0 comments on commit 533c46f

Please sign in to comment.