Skip to content

Commit 779b171

Browse files
mvaduTitusz Ban
andauthored
Added methods to query and post strongly typed points using attributes (#99)
* Added methods to query and post strongly typed points using attributes * Extend readme with further property types Co-authored-by: Titusz Ban <titusz.ban@trayport.com>
2 parents bc17f5a + d31324a commit 779b171

16 files changed

Lines changed: 645 additions & 29 deletions

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v0.21.0 [08/25/2020]
2+
3+
### Release Notes
4+
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.
5+
6+
17
## v0.20.0 [08/11/2020]
28

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

269275
### Bugfixes
270276

271-
- [#3](https://github.com/AdysTech/InfluxDB.Client.Net/pull/3): Double to str conversion fix, Thanks to @spamik
277+
- [#3](https://github.com/AdysTech/InfluxDB.Client.Net/pull/3): Double to str conversion fix, Thanks to @spamik

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,62 @@ var r = await client.PostPointAsync(dbName, valMixed);
9393

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

96+
#### Writing strongly typed data points to DB
97+
98+
```Csharp
99+
class YourPoint
100+
{
101+
[InfluxDBMeasurementName]
102+
public string Measurement { get; set; }
103+
104+
[InfluxDBTime]
105+
public DateTime Time { get; set; }
106+
107+
[InfluxDBPrecision]
108+
public TimePrecision Precision { get; set; }
109+
110+
[InfluxDBRetentionPolicy]
111+
public InfluxRetentionPolicy Retention { get; set; }
112+
113+
[InfluxDBField("StringFieldName")]
114+
public string StringFieldProperty { get; set; }
115+
116+
[InfluxDBField("IntFieldName")]
117+
public int IntFieldProperty { get; set; }
118+
119+
[InfluxDBField("BoolFieldName")]
120+
public bool BoolFieldProperty { get; set; }
121+
122+
[InfluxDBField("DoubleFieldName")]
123+
public double DoubleFieldProperty { get; set; }
124+
125+
[InfluxDBTag("TagName")]
126+
public string TagProperty { get; set; }
127+
128+
}
129+
130+
var point = new YourPoint
131+
{
132+
Time = DateTime.UtcNow,
133+
Measurement = measurementName,
134+
Precision = TimePrecision.Seconds,
135+
StringFieldProperty = "FieldValue",
136+
IntFieldProperty = 42,
137+
BoolFieldProperty = true,
138+
DoubleFieldProperty = 3.1415,
139+
TagProperty = "TagValue",
140+
Retention = new InfluxRetentionPolicy() { Name = "Test2" };
141+
};
142+
143+
var r = await client.PostPointAsync(dbName, point);
144+
```
145+
146+
This supports all types `InfluxValueField` supports. Additionally it supports tags other than strings, as long as they can be converted to string.
147+
148+
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.
149+
150+
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
151+
96152
#### Query for data points
97153

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

108164
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.
109165

166+
167+
#### Query for strongly typed data points
168+
169+
```Csharp
170+
var r = await client.QueryMultiSeriesAsync<YourPoint>(dbName, $"select * from {measurementName}");
171+
```
172+
173+
`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.
174+
175+
It supports multiple chuncks similarly to `QueryMultiSeriesAsync`.
176+
177+
110178
#### Retention Policies
111179

112180
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.

src/AdysTech.InfluxDB.Client.Net.Core.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Product>AdysTech.InfluxDB.Client.Net</Product>
77
<Company>AdysTech</Company>
88
<Authors>AdysTech;mvadu</Authors>
9-
<Version>0.20.0.0</Version>
9+
<Version>0.21.0.0</Version>
1010
<PackageId>AdysTech.InfluxDB.Client.Net.Core</PackageId>
1111
<Copyright>© AdysTech 2016-2020</Copyright>
1212
<PackageProjectUrl>https://github.com/AdysTech/InfluxDB.Client.Net</PackageProjectUrl>
@@ -31,8 +31,8 @@
3131
11. Drop databases, measurements or points
3232
12. Get series count or points count for a measurement
3333
</PackageReleaseNotes>
34-
<AssemblyVersion>0.20.0.0</AssemblyVersion>
35-
<FileVersion>0.20.0.0</FileVersion>
34+
<AssemblyVersion>0.21.0.0</AssemblyVersion>
35+
<FileVersion>0.21.0.0</FileVersion>
3636
<PackageTags>InfluxDB Influx TSDB TimeSeries InfluxData Chunking retention RetentionPolicy</PackageTags>
3737
<PackageLicenseExpression>MIT</PackageLicenseExpression>
3838
</PropertyGroup>

src/Attributes/InfluxDBField.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBField : Attribute
7+
{
8+
public readonly string Name;
9+
public InfluxDBField(string name)
10+
{
11+
Name = name;
12+
}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBMeasurementName : Attribute { }
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBPrecision : Attribute { }
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBRetentionPolicy : Attribute { }
7+
}

src/Attributes/InfluxDBTag.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBTag : Attribute
7+
{
8+
public readonly string Name;
9+
public InfluxDBTag(string name)
10+
{
11+
Name = name;
12+
}
13+
}
14+
}

src/Attributes/InfluxDBTime.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System;
2+
3+
namespace AdysTech.InfluxDB.Client.Net
4+
{
5+
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
6+
public class InfluxDBTime : Attribute { }
7+
}

0 commit comments

Comments
 (0)