Skip to content

Commit

Permalink
Added methods to query and post strongly typed points using attributes (
Browse files Browse the repository at this point in the history
#99)

* Added methods to query and post strongly typed points using attributes

* Extend readme with further property types

Co-authored-by: Titusz Ban <[email protected]>
  • Loading branch information
mvadu and tituszban authored Aug 26, 2020
2 parents bc17f5a + d31324a commit 779b171
Show file tree
Hide file tree
Showing 16 changed files with 645 additions and 29 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.21.0 [08/25/2020]

### Release Notes
This version introduces methods to query and post strongly typed points using attributes. Thanks to @tituszban for adding the attribute support. Refer to [Readme.md](https://github.com/AdysTech/InfluxDB.Client.Net/blob/master/README.md) for examples.


## v0.20.0 [08/11/2020]

### Release Notes
Expand Down Expand Up @@ -268,4 +274,4 @@ Added the functionality to query for existing data from InfluxDB. Also unknown l

### Bugfixes

- [#3](https://github.com/AdysTech/InfluxDB.Client.Net/pull/3): Double to str conversion fix, Thanks to @spamik
- [#3](https://github.com/AdysTech/InfluxDB.Client.Net/pull/3): Double to str conversion fix, Thanks to @spamik
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,62 @@ var r = await client.PostPointAsync(dbName, valMixed);

A collection of points can be posted using `await client.PostPointsAsync (dbName, points)`, where `points` can be collection of different types of `InfluxDatapoint`

#### Writing strongly typed data points to DB

```Csharp
class YourPoint
{
[InfluxDBMeasurementName]
public string Measurement { get; set; }

[InfluxDBTime]
public DateTime Time { get; set; }

[InfluxDBPrecision]
public TimePrecision Precision { get; set; }

[InfluxDBRetentionPolicy]
public InfluxRetentionPolicy Retention { get; set; }

[InfluxDBField("StringFieldName")]
public string StringFieldProperty { get; set; }

[InfluxDBField("IntFieldName")]
public int IntFieldProperty { get; set; }

[InfluxDBField("BoolFieldName")]
public bool BoolFieldProperty { get; set; }

[InfluxDBField("DoubleFieldName")]
public double DoubleFieldProperty { get; set; }

[InfluxDBTag("TagName")]
public string TagProperty { get; set; }

}

var point = new YourPoint
{
Time = DateTime.UtcNow,
Measurement = measurementName,
Precision = TimePrecision.Seconds,
StringFieldProperty = "FieldValue",
IntFieldProperty = 42,
BoolFieldProperty = true,
DoubleFieldProperty = 3.1415,
TagProperty = "TagValue",
Retention = new InfluxRetentionPolicy() { Name = "Test2" };
};

var r = await client.PostPointAsync(dbName, point);
```

This supports all types `InfluxValueField` supports. Additionally it supports tags other than strings, as long as they can be converted to string.

The parameter that has `InfluxDBRetentionPolicy` applied to it can be an `IInfluxRetentionPolicy` alternatively it will be treated as a string and will be used as the name of the retention policy.

A collection of points can be posted using `await client.PostPointsAsync<T>(dbName, points)`, where `points` can be collection of arbitrary type `T` with appropriate attributes

#### Query for data points

```Csharp
Expand All @@ -107,6 +163,18 @@ Second example above will provide multiple series objects, and allows to get dat

The last example above makes InfluxDB to split the selected points (100 limited by `limit` clause) to multiple series, each having 10 points as given by `chunk` size.


#### Query for strongly typed data points

```Csharp
var r = await client.QueryMultiSeriesAsync<YourPoint>(dbName, $"select * from {measurementName}");
```

`QueryMultiSeriesAsync<T>` method returns `List<InfluxSeries<T>>`, `InfluxSeries<T>` behaves similar to `InfluxSeries` except Entries are not dynamic but strongly typed as T. It uses the same attributes as used for writing the points to match the fields and tags to the matching properties. If a matching property can't be found, the field is discarded. If a property doesn't have a matching field, it's left empty.

It supports multiple chuncks similarly to `QueryMultiSeriesAsync`.


#### Retention Policies

This library uses a cutsom .Net object to represent the Influx Retention Policy. The `Duration` concept is nicely wraped in `TimeSpan`, so it can be easily manipulated using .Net code. It also supports `ShardDuration` concept introduced in recent versions of InfluxDB.
Expand Down
6 changes: 3 additions & 3 deletions src/AdysTech.InfluxDB.Client.Net.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Product>AdysTech.InfluxDB.Client.Net</Product>
<Company>AdysTech</Company>
<Authors>AdysTech;mvadu</Authors>
<Version>0.20.0.0</Version>
<Version>0.21.0.0</Version>
<PackageId>AdysTech.InfluxDB.Client.Net.Core</PackageId>
<Copyright>© AdysTech 2016-2020</Copyright>
<PackageProjectUrl>https://github.com/AdysTech/InfluxDB.Client.Net</PackageProjectUrl>
Expand All @@ -31,8 +31,8 @@
11. Drop databases, measurements or points
12. Get series count or points count for a measurement
</PackageReleaseNotes>
<AssemblyVersion>0.20.0.0</AssemblyVersion>
<FileVersion>0.20.0.0</FileVersion>
<AssemblyVersion>0.21.0.0</AssemblyVersion>
<FileVersion>0.21.0.0</FileVersion>
<PackageTags>InfluxDB Influx TSDB TimeSeries InfluxData Chunking retention RetentionPolicy</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/Attributes/InfluxDBField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBField : Attribute
{
public readonly string Name;
public InfluxDBField(string name)
{
Name = name;
}
}
}
7 changes: 7 additions & 0 deletions src/Attributes/InfluxDBMeasurementName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBMeasurementName : Attribute { }
}
7 changes: 7 additions & 0 deletions src/Attributes/InfluxDBPrecision.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBPrecision : Attribute { }
}
7 changes: 7 additions & 0 deletions src/Attributes/InfluxDBRetentionPolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBRetentionPolicy : Attribute { }
}
14 changes: 14 additions & 0 deletions src/Attributes/InfluxDBTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBTag : Attribute
{
public readonly string Name;
public InfluxDBTag(string name)
{
Name = name;
}
}
}
7 changes: 7 additions & 0 deletions src/Attributes/InfluxDBTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace AdysTech.InfluxDB.Client.Net
{
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class InfluxDBTime : Attribute { }
}
Loading

0 comments on commit 779b171

Please sign in to comment.