2

I use the following library for queries from an InfluxDB v2.0.

https://github.com/influxdata/influxdb-client-csharp

I want to convert the result from the InfluxDB query into a List. I have solved this with the following code. It feels messy to me though, is there any way I can solve this more cleanly?

namespace InfluxDBRead
{
    class Program
    {

        static async Task Main(string[] args)
        {

            var influxDBClient = InfluxDBClientFactory.Create("http://localhost:8086", "Qlu0fF8tliDLMK-DCbZ-XwcJ9_PiGY5-Dw-LfTCjcgjy8CUA80KaashqCKR77JUAdbIY-O1A3Ar_mz4jgzZy5g==");

            var flux = "from(bucket: \"Development\")\n"
            + "|> range(start: -1d)"
            + "|> filter(fn: (r) => r[\"device\"] == \"Freezer 01\")"
            + "|> filter(fn: (r) => r[\"_field\"] == \"tempBLowAlarm\" or r[\"_field\"] == \"tempBHighAlarm\" or r[\"_field\"] == \"tempB\" or r[\"_field\"] == \"tempALowAlarm\" or r[\"_field\"] == \"tempAHighAlarm\" or r[\"_field\"] == \"tempA\" or r[\"_field\"] == \"levelLowAlarm\" or r[\"_field\"] == \"levelHighAlarm\" or r[\"_field\"] == \"level\" or r[\"_field\"] == \"fillTimeAlarm\" or r[\"_field\"] == \"bypassTimeAlarm\")";


            var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "Organisation");

            var bypassTimeAlarmRecords = fluxTables[0].Records;
            var fillTimeAlarmRecords = fluxTables[1].Records;
            var levelRecords = fluxTables[2].Records;
            var levelHighAlarmRecords = fluxTables[3].Records;
            var levelLowAlarmRecords = fluxTables[4].Records;
            var tempARecords = fluxTables[5].Records;
            var tempAHighAlarmRecords = fluxTables[6].Records;
            var tempALowAlarmRecords = fluxTables[7].Records;
            var tempBRecords = fluxTables[8].Records;
            var tempBHighAlarmRecords = fluxTables[9].Records;
            var tempBLowAlarmRecords = fluxTables[10].Records;

            var count = bypassTimeAlarmRecords.Count();

            List<Value> Values = new List<Value>();

            for(int i = 0; i < count; i++)
            {
                Value _value = new Value();
                _value.DateAndTime = Convert.ToDateTime(bypassTimeAlarmRecords[i].GetTimeInDateTime());
                _value.BypassTimeAlarm = Convert.ToBoolean(bypassTimeAlarmRecords[i].GetValue());
                _value.FillTimeAlarm = Convert.ToBoolean(fillTimeAlarmRecords[i].GetValue());
                _value.Level = Convert.ToDouble(levelRecords[i].GetValue());
                _value.LevelHighAlarm = Convert.ToBoolean(levelHighAlarmRecords[i].GetValue());
                _value.LevelLowAlarm = Convert.ToBoolean(levelLowAlarmRecords[i].GetValue());
                _value.TempA = Convert.ToDouble(tempARecords[i].GetValue());
                _value.TempAHighAlarm = Convert.ToBoolean(tempAHighAlarmRecords[i].GetValue());
                _value.TempALowAlarm = Convert.ToBoolean(tempALowAlarmRecords[i].GetValue());
                _value.TempB = Convert.ToDouble(tempBRecords[i].GetValue());
                _value.TempBHighAlarm = Convert.ToBoolean(tempBHighAlarmRecords[i].GetValue());
                _value.TempBLowAlarm = Convert.ToBoolean(tempBLowAlarmRecords[i].GetValue());
                Values.Add(_value);
            }

            influxDBClient.Dispose();
           
        }

    }


    public class Value
    {
        public DateTime DateAndTime { get; set; }
        public double TempA { get; set; } 
        public double TempB { get; set; } 
        public double Level { get; set; } 
        public bool TempAHighAlarm { get; set; }
        public bool TempBHighAlarm { get; set; } 
        public bool LevelHighAlarm { get; set; }
        public bool TempALowAlarm { get; set; }
        public bool TempBLowAlarm { get; set; } 
        public bool LevelLowAlarm { get; set; }
        public bool FillTimeAlarm { get; set; } 
        public bool BypassTimeAlarm { get; set; } 
    }
}
patrickgc
  • 49
  • 7

1 Answers1

0

You need to decorate your model class using the InfluxDB.Client.Core attributes:

using InfluxDB.Client.Core;

[Measurement(<YOUR_MEASUREMENT_NAME>)]
public class Value
{
    [Column("tempBHighAlarm")]
    public bool TempBHighAlarm { get; set; } 
    
    [Column("tempA")]
    public double TempA { get; set; } 

    ...
}

And when you query the data, you need to specify the type of model class you want the result to be converted to (QueryAsync<T>):

 var fluxTables = await influxDBClient.GetQueryApi().QueryAsync<Value>(flux, "Organisation");

More examples here.

Nata
  • 986
  • 9
  • 4