InfluxData.Net icon indicating copy to clipboard operation
InfluxData.Net copied to clipboard

Convert attribute decorated POCO's to Points

Open joakimhew opened this issue 7 years ago • 4 comments

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:

joakimhew avatar Oct 17 '17 20:10 joakimhew

Lol! Dude.. this is a sick feature. Just awesome. :)

I'll try to publish it over the weekend.

Thanks!

tihomir-kit avatar Oct 17 '17 21:10 tihomir-kit

@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!

joakimhew avatar Oct 17 '17 21:10 joakimhew

Have you had the chance to look more into this 😄 ?

joakimhew avatar Nov 07 '17 16:11 joakimhew

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.

tihomir-kit avatar Nov 08 '17 10:11 tihomir-kit