InfluxData.Net
InfluxData.Net copied to clipboard
Convert attribute decorated POCO's to Points
This feature adds an extension method allowing users to convert attribute decorated POCO's following certain rules into Points.
The following attributes must be present in the POCO:
Attribute name | Mandatory? | Maximum in one type |
---|---|---|
[Measurement] |
Yes | 1 |
[Timestamp] |
No | 1 |
[Tag] |
No | ∞ |
[Field] |
Yes | ∞ |
Example:
Please notice how one of the tag and field names are explicitly set using the attribute and name
argument
public class MyType
{
[Timestamp]
public DateTime Time { get; set; }
[Measurement]
public string MyMeasurement { get; set; }
[Tag]
public string Tag1 { get; set; }
[Tag(name: "newtagname")] // <- Explicitly set tag name
public string Tag2 { get; set; }
[Field]
public string Field1 { get; set; }
[Field(name: "newfieldname")] // <- Explicitly set field name
public bool Field2 { get; set; }
[Field]
public float Field2 { get; set; }
}
MyType myType = new MyType
{
Time = DateTime.Now,
Measurement = "TestMeasurement",
Tag1 = "FirstTag",
Tag2 = "SecondTag",
Field1 = "FirstField",
Field2 = True,
Field3 = 2324
};
var point = myType.ToPoint();
This will produce the following point object:
point =
{
"Timestamp" : "2017-10-17 10:18:00",
"Name" : "TestMeasurement",
"Tags" : [
"Tag1" : "FirstTag",
"newtagname`" : "SecondTag" // <- explicitly set tag name
],
"Fields" : [
"Field1" : "FirstField",
"newfieldname" : True, // <- explicitly set field name
"Field3" : 2324
]
}
The following validations are implemented and will return detailed exceptions:
-
Makes sure tags are only of type
string
-
Makes sure fields are of type
primitive
orstring
-
Makes sure there is EXACTLY one
[Measurement]
present -
Makes sure the property decorated with
[Measurement]
is of typestring
-
If used, make sure there is EXACTLY one
[Timestamp]
present -
Makes sure the property decorated with
[Timestamp]
is of typeDateTime
Lol! Dude.. this is a sick feature. Just awesome. :)
I'll try to publish it over the weekend.
Thanks!
@pootzko Glad I can help! I just added the option to explicitly set the names for tags and fields using a name argument in the attributes. Updated my original comment.
I found out at work today that this would be nice to have so figured I could make a proper implementation!
Have you had the chance to look more into this 😄 ?
Hey man, sorry for the delay. I haven't yet, I've been very busy the last 2 weeks. I'll try to look at it this weekend.