Git Product home page Git Product logo

influxdb-client-csharp's Introduction

influxdb-client-csharp

CircleCI codecov Nuget License GitHub issues GitHub pull requests Slack Status

This repository contains the C# client library for use with InfluxDB 2.x and Flux. InfluxDB 3.x users should instead use the lightweight v3 client library. InfluxDB 1.x users should use the v1 client library.

For ease of migration and a consistent query and write experience, v2 users should consider using InfluxQL and the v1 client library.

Documentation

This section contains links to the client library documentation.

Client Description Documentation Compatibility
Client The reference C# client that allows query, write and InfluxDB 2.x management. readme 2.x
Client.Linq The library supports to use a LINQ expression to query the InfluxDB. readme 2.x
Client.Legacy The reference C# client that allows you to perform Flux queries against InfluxDB 1.7+. readme 1.7+

Features

  • Supports querying using the Flux language over the InfluxDB 1.7+ REST API (/api/v2/query endpoint)
  • InfluxDB 2.x client
    • Querying data using the Flux language
    • Writing data using
    • InfluxDB 2.x Management API client for managing
      • sources, buckets
      • tasks
      • authorizations
      • health check
      • ...

How To Use

Writes and Queries in InfluxDB 2.x

The following example demonstrates how to write data to InfluxDB 2.x and read them back using the Flux language:

Installation

Use the latest version:

.Net CLI
dotnet add package InfluxDB.Client
Or when using Package Manager
Install-Package InfluxDB.Client
using System;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;
using InfluxDB.Client.Writes;
using Task = System.Threading.Tasks.Task;

namespace Examples
{
    public static class QueriesWritesExample
    {
        private static readonly char[] Token = "".ToCharArray();

        public static async Task Main()
        {
            using var client = new InfluxDBClient("http://localhost:8086", Token);

            //
            // Write Data
            //
            using (var writeApi = client.GetWriteApi())
            {
                //
                // Write by Point
                //
                var point = PointData.Measurement("temperature")
                    .Tag("location", "west")
                    .Field("value", 55D)
                    .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
                
                writeApi.WritePoint(point, "bucket_name", "org_id");
                
                //
                // Write by LineProtocol
                //
                writeApi.WriteRecord("temperature,location=north value=60.0", WritePrecision.Ns, "bucket_name", "org_id");
                
                //
                // Write by POCO
                //
                var temperature = new Temperature {Location = "south", Value = 62D, Time = DateTime.UtcNow};
                writeApi.WriteMeasurement(temperature, WritePrecision.Ns, "bucket_name", "org_id");
            }
            
            //
            // Query data
            //
            var flux = "from(bucket:\"temperature-sensors\") |> range(start: 0)";

            var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, "org_id");
            fluxTables.ForEach(fluxTable =>
            {
                var fluxRecords = fluxTable.Records;
                fluxRecords.ForEach(fluxRecord =>
                {
                    Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
                });
            });
        }
        
        [Measurement("temperature")]
        private class Temperature
        {
            [Column("location", IsTag = true)] public string? Location { get; set; }

            [Column("value")] public double Value { get; set; }

            [Column(IsTimestamp = true)] public DateTime Time { get; set; }
        }
    }
}

Use Management API to create a new Bucket in InfluxDB 2.x

The following example demonstrates how to use a InfluxDB 2.x Management API. For further information see client documentation.

Installation

Use the latest version:

.Net CLI
dotnet add package InfluxDB.Client
Or when using Package Manager
Install-Package InfluxDB.Client
using System;
using System.Collections.Generic;
using System.Linq;
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using Task = System.Threading.Tasks.Task;

namespace Examples
{
    public static class ManagementExample
    {
        public static async Task Main()
        {
            const string url = "http://localhost:8086";
            const string token = "my-token";
            const string org = "my-org";
            
            using var client = new InfluxDBClient(url, token);

            // Find ID of Organization with specified name (PermissionAPI requires ID of Organization).
            var orgId = (await client.GetOrganizationsApi().FindOrganizationsAsync(org: org)).First().Id;

            //
            // Create bucket "iot_bucket" with data retention set to 3,600 seconds
            //
            var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);

            var bucket = await client.GetBucketsApi().CreateBucketAsync("iot_bucket", retention, orgId);

            //
            // Create access token to "iot_bucket"
            //
            var resource = new PermissionResource(PermissionResource.TypeEnum.Buckets, bucket.Id, null,
                orgId);

            // Read permission
            var read = new Permission(Permission.ActionEnum.Read, resource);

            // Write permission
            var write = new Permission(Permission.ActionEnum.Write, resource);

            var authorization = await client.GetAuthorizationsApi()
                .CreateAuthorizationAsync(orgId, new List<Permission> { read, write });

            //
            // Created token that can be use for writes to "iot_bucket"
            //
            Console.WriteLine($"Authorized token to write into iot_bucket: {authorization.Token}");
        }
    }
}

InfluxDB 1.8 API compatibility

InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.

The following forward compatible APIs are available:

API Endpoint Description
QueryApi.cs /api/v2/query Query data in InfluxDB 1.8.0+ using the InfluxDB 2.x API and Flux (endpoint should be enabled by flux-enabled option)
WriteApi.cs /api/v2/write Write data to InfluxDB 1.8.0+ using the InfluxDB 2.x API
PingAsync /ping Checks the status of InfluxDB instance and version of InfluxDB.

For detail info see InfluxDB 1.8 example.

Flux queries in InfluxDB 1.7+

The following example demonstrates querying using the Flux language.

Installation

Use the latest version:

.Net CLI
dotnet add package InfluxDB.Client.Flux
Or when using Package Manager
Install-Package InfluxDB.Client.Flux
using System;
using InfluxDB.Client.Flux;

namespace Examples
{
    public static class FluxExample
    {
        public static void Run()
        {
            using var client = new FluxClient("http://localhost:8086/");

            var fluxQuery = "from(bucket: \"telegraf\")\n"
                               + " |> filter(fn: (r) => (r[\"_measurement\"] == \"cpu\" AND r[\"_field\"] == \"usage_system\"))"
                               + " |> range(start: -1d)"
                               + " |> sample(n: 5, pos: 1)";

            client.QueryAsync(fluxQuery, record =>
                            {
                                // process the flux query records
                                Console.WriteLine(record.GetTime() + ": " + record.GetValue());
                            },
                            (error) =>
                            {
                                // error handling while processing result
                                Console.WriteLine(error.ToString());

                            }, () =>
                            {
                                // on complete
                                Console.WriteLine("Query completed");
                            }).GetAwaiter().GetResult();
        }
    }
}

Contributing

If you would like to contribute code you can do through GitHub by forking the repository and sending a pull request into the master branch.

License

The InfluxDB 2.x Clients are released under the MIT License.

influxdb-client-csharp's People

Contributors

alespour avatar bednar avatar bnayae avatar deinok avatar dependabot[bot] avatar flq avatar janstadt avatar jdstrand avatar kelseiv avatar knollsen avatar kvakulo avatar ldematte avatar maxreb avatar michaelahojna avatar michhartmann avatar pgodwin avatar powersj avatar rhajek avatar ritchiecarroll avatar rolincova avatar rs-blade avatar sbhenderson avatar sciator avatar sspaink avatar tangbao0002 avatar tysonkamp avatar vesuvian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

influxdb-client-csharp's Issues

Contribution offer

I would like to contribute the following functionality.

  • PointData.Tags: will accept IReadOnlyDictionary
  • Immutable PointData builder
    currently PointData cannot be reuse because it Mutable.
    Therefore the following scenario will have side effect:

// may be register with DI as initial state var pointBase = PointData .Measurement("xyz") .Tag("company", "best-one") .Tag("feature", "a");

var point = pointBase.Field("count", 10); using (var writeApi = _client.GetWriteApi()) { writeApi.WritePoint(BUCKET, TENANT, point); }

var point = pointBase.Tag("path", "/api") .Field("count", 10); using (var writeApi = _client.GetWriteApi()) { writeApi.WritePoint(BUCKET, TENANT, point); }

What's your opinion?

naming of classes (task, point)

it's unfortunate that influxdb reuses some very common class names in the c# ecosystem. especially the name Task has some bad side effects. after using the namespace InfluxDB.Client.Api.Domain the common async/await pattern (System.Threading.Tasks.Task) is in conflict with InfluxDB. the same with the point class. Maybe different classnames could come a long way in improving the usability of this library.

Entity Framework Core provider

Now that Entity Framework Core has taken off, it would be nice to see an InfluxDB provider for EF Core. This would help ppl currently targeting a traditional DB like SQL Server to migrate over to InfluxDB.
https://docs.microsoft.com/en-us/ef/core/providers/

Additional LINQ operators could then be added to be able to write Flux queries as LINQ queries.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-add-custom-methods-for-linq-queries

For example, the Flux query on the bottom of the README.md would then be able to be rewritten as:

var fluxQuery = db.telegraf
			.Where(b => b.measurement == β€œcpu”
				     && b.field == β€œusage_system”)
			.RangeStart(TimeSpan.FromDays(1))
			.Sample(n: 5, pos: 1);

This would allow compile time checking of the query, and autocomplete when writing the query.

Add async/await Write API without batching

Useful for simple use case of client:

  • simple IOT device

Related to:

API:

public class WriteApiAsyncAwait
{
    /// <summary>
    /// Write Line Protocol record into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="record">
    ///     specifies the record in InfluxDB Line Protocol.
    ///     The <see cref="record" /> is considered as one batch unit.
    /// </param>
    public async Task WriteRecordAsync(WritePrecision precision, string record)
    {
    }

    /// <summary>
    /// Write Line Protocol record into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="record">
    ///     specifies the record in InfluxDB Line Protocol.
    ///     The <see cref="record" /> is considered as one batch unit.
    /// </param>
    public async Task WriteRecordAsync(string bucket, string org, WritePrecision precision, string record)
    {
    }

    /// <summary>
    /// Write Line Protocol records into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="records">specifies the record in InfluxDB Line Protocol</param>
    public async Task WriteRecordsAsync(WritePrecision precision, List<string> records)
    {
    }

    /// <summary>
    /// Write Line Protocol records into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="records">specifies the record in InfluxDB Line Protocol</param>
    public async Task WriteRecordsAsync(string bucket, string org, WritePrecision precision, List<string> records)
    {
    }

    /// <summary>
    /// Write Line Protocol records into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="records">specifies the record in InfluxDB Line Protocol</param>
    public async Task WriteRecordsAsync(WritePrecision precision, params string[] records)
    {
    }

    /// <summary>
    /// Write Line Protocol records into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="records">specifies the record in InfluxDB Line Protocol</param>
    public async Task WriteRecordsAsync(string bucket, string org, WritePrecision precision, params string[] records)
    {
    }

    /// <summary>
    /// Write a Data point into specified bucket.
    /// </summary>
    /// <param name="point">specifies the Data point to write into bucket</param>
    public async Task WritePointAsync(PointData point)
    {
    }

    /// <summary>
    /// Write a Data point into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="point">specifies the Data point to write into bucket</param>
    public async Task WritePointAsync(string bucket, string org, PointData point)
    {
    }

    /// <summary>
    /// Write Data points into specified bucket.
    /// </summary>
    /// <param name="points">specifies the Data points to write into bucket</param>
    public async Task WritePointsAsync(List<PointData> points)
    {
    }


    /// <summary>
    /// Write Data points into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="points">specifies the Data points to write into bucket</param>
    public async Task WritePointsAsync(string bucket, string org, List<PointData> points)
    {
    }

    /// <summary>
    /// Write Data points into specified bucket.
    /// </summary>
    /// <param name="points">specifies the Data points to write into bucket</param>
    public async Task WritePointsAsync(params PointData[] points)
    {
    }

    /// <summary>
    /// Write Data points into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="points">specifies the Data points to write into bucket</param>
    public async Task WritePointsAsync(string bucket, string org, params PointData[] points)
    {
    }

    /// <summary>
    /// Write a Measurement into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurement">specifies the Measurement to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementAsync<TM>(WritePrecision precision, TM measurement)
    {
    }

    /// <summary>
    /// Write a Measurement into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurement">specifies the Measurement to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementAsync<TM>(string bucket, string org, WritePrecision precision, TM measurement)
    {
    }

    /// <summary>
    /// Write Measurements into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurements">specifies Measurements to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementsAsync<TM>(WritePrecision precision, List<TM> measurements)
    {
    }

    /// <summary>
    /// Write Measurements into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurements">specifies Measurements to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementsAsync<TM>(string bucket, string org, WritePrecision precision,
        List<TM> measurements)
    {
    }

    /// <summary>
    /// Write Measurements into specified bucket.
    /// </summary>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurements">specifies Measurements to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementsAsync<TM>(WritePrecision precision, params TM[] measurements)
    {
    }

    /// <summary>
    /// Write Measurements into specified bucket.
    /// </summary>
    /// <param name="bucket">specifies the destination bucket for writes</param>
    /// <param name="org">specifies the destination organization for writes</param>
    /// <param name="precision">specifies the precision for the unix timestamps within the body line-protocol</param>
    /// <param name="measurements">specifies Measurements to write into bucket</param>
    /// <typeparam name="TM">measurement type</typeparam>
    public async Task WriteMeasurementsAsync<TM>(string bucket, string org, WritePrecision precision,
        params TM[] measurements)
    {
    }
}

consecutive deletes

seems like I cannot execute a consecutive series of delete requests before dispose of the influx client. Only the first or last request actually deletes something.

Authentication with username and password not working

Hi, connectiong to influxdb with username and pw like this:
var client_options = new InfluxDBClientOptions.Builder()
.Url("http://199.199.199.199:8086")
.Authenticate("username", "password".ToCharArray())
.Build();

results in an InfluxDB.Client.Core.Exceptions.HttpException saying 'unable to parse authentication credentials' when writing data via 'WriteRecord'.
But when trying via curl - everything is OK:
_curl -G http://199.199.199.199:8086/query -u username:password --data-urlencode "q=SHOW DATABASES"
{"results":[{"statement_id":0,"series":[{"name":"databases","columns":["name"],"values":[["internal"]]}]}]}

The user (username) has full access to all databases.

Any hints?
Cheers

Authentication for Client.Legacy is only URL-Based

Unfortunatelly, URL-based authentication mechanizm is not recomended by InfluxDB documentation.
Moreover, it is unsecure because URLs are visible in every log of network device.
Is there any possibility to add HTTP Basic Auth scheme to the list of possible options?

Referenced assembly does not have a strong name

Hi,
I am trying to use the influxdb client in my .Net Framework project. When I compile I get the following error message:
error

the assembly I am building is signed using a strong name key, therefore it can not reference an unsigned assembly and that is the cause of the issue.

InfluxDBClient should dispose the child clients

The InfluxDBClient should dispose all child clients (api) when InfluxDBClient.Dispose is invoked.

The first dispose should be useles:

_influxDbClient = InfluxDBClientFactory.Create(MockServerUrl, "token".ToCharArray());
_writeApi = _influxDbClient.GetWriteApi();
            
_writeApi.WriteRecord("my-bycjet", "my-org", WritePrecision.Ns, "h2o_feet,location=coyote_creek level\\ description=\"feet 1\",water_level=1.0 1");
            
_writeApi.Dispose();
_influxDbClient.Dispose();

JsonReaderException

I'm following the example in a dotnet core 3.1 worker service.

But I keep getting this error:

InfluxDB Error: 0 : The error occurred during writing of data: Error reading JObject from JsonReader. Current JsonReader item is not an object: Integer. Path '', line 1, position 3.
The batch item wasn't processed successfully because: Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: Integer. Path '', line 1, position 3.
   at InfluxDB.Client.WriteApi.<>c__DisplayClass6_2.<.ctor>b__18(Exception e)
   at System.Reactive.Linq.ObservableImpl.SelectMany`2.ObservableSelector._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\SelectMany.cs:line 865

var point = PointData.Measurement("Event")
                        .Tag("field1", "test")
                        .Tag("field2", "test")
                        .Tag("field3", "test")
                        .Tag("field4", "test")
                        .Field("value", "test")
                        .Timestamp(DateTime.UtcNow, WritePrecision.Ns);

                    _dbWriteApi.WritePoint("Events", "org", point);

Keep getting the same error, also when I use WriteMeasurement or any other way.

Any idea what this is or what I am doing wrong?

Maybe I found a bug?

Version:influxed 1.8,and client 1.6 support 1.8

when i use

using (WriteApi writer = client.GetWriteApi())
{
writer.WriteMeasurement(WritePrecision.Ns, new Temperature() { ItemName = "item", Value = 1.1, Time = DateTime.Now });
 }

program not responding

so I read source code debug,and I see
in client WriteApi.cs
WriteRecord method
public void WriteRecord(WritePrecision precision, string record)
millis is 30000

while (!condition())
{
    Thread.Sleep(25);
    if (DateTime.Now.Millisecond - start > millis)
        {
            Trace.TraceError($"The WriteApi can't be gracefully dispose! - {millis}ms elapsed.");
            break;
        }
}

The Task offset has to be a part of Flux query

The Task offset has to be a part of Flux query not a separate JSON attribute:

{
	"flux": "option task = {   
                name: \"it task1551170782349-IT\",  
                every: 1h,
                from(bucket:\"my-bucket\") |> range(start: 0) |> last()
         },
	"links": {},
	"name": "it task1551170782349-IT",
	"offset": "30m",
	"orgID": "037632398eeb6000",
	"status": "active"
   }

has to be

{
	"flux": "option task = {   
                name: \"it task1551170782349-IT\",  
                every: 1h,
                offset: 30m
                from(bucket:\"my-bucket\") |> range(start: 0) |> last()
         },
	"links": {},
	"name": "it task1551170782349-IT",
	"orgID": "037632398eeb6000",
	"status": "active"
   }

Set Client ReadWriteTimeout

My Write/QueryAsync functions seem to have a default fixed timeout at 10 seconds and I can't figure out how to override it. The InfluxDBClientFactory.Create(...) has an overload that takes InfluxDBClientOptions which looks to have the settings I want, but I can't instantiate the InfluxDBClientOptions object directly (no public constructor?), only the sealed Builder class within it, which the InfluxDBClientFactory.Create(...) method won't accept. Am I missing something?

async calls should be named as such

Right now, in the V2 api, it is not possible to know what methods are async and which ones are not, from the method names.

It would be nice to append the 'async' suffix to method names, when needed, since it's pretty much a common C# convention.

LogLevel Header should contains query parameters:

If I set Client.SetLogLevel(LogLevel.Headers); then the tracing output should contains query parameters:

--> POST /api/v2/write
--> Header: Content-Encoding Value: identity
--> Header: Content-Type Value: text/plain; charset=utf-8
--> Header: Accept Value: application/json
--> Header: Authorization Value: Token ugwN9Vd0tejE-Nac-r29U0tUx6uKPM10afySQ-_1mrb3gQGZSRSk7TMDPhjtN8i2VrE5dxLzP4dgYiHqu2ZW4w==
--> END

<-- 204
<-- Header: Date Value: Thu, 09 Jan 2020 12:44:19 GMT
<-- END

Auto-configure client from configuration file

InfluxDBClient loads configuration form the App.config file automatically.
All you have to do is to add configuration to your App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="influx2" type="InfluxDB.Client.Configurations.Influx2, InfluxDB.Client" />
    </configSections>

    <influx2 url="http://localhost:9999"
             org="my-org"
             bucket="my-bucket"
             token="my-token"
             logLevel="BODY"
             readWriteTimeout="5s"
             timeout="10s">
    </influx2>
</configuration>

Default tags

Sometimes is useful to store same information in every measurement e.g. hostname, location, customer:

The C# client will be able to use static value, app settings or env variable as a tag value.

Configuration
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="influx2" type="InfluxDB.Client.Configurations.Influx2, InfluxDB.Client" />
    </configSections>
    <appSettings>
        <add key="SensorVersion" value="v1.00"/>
    </appSettings>
    <influx2 url="http://localhost:9999"
             org="my-org"
             bucket="my-bucket"
             token="my-token"
             logLevel="BODY"
             readWriteTimeout="5s"
             timeout="10s">
        <tags>
            <tag name="id" value="132-987-655"/>
            <tag name="customer" value="California Miner"/>
            <tag name="hostname" value="${env.Hostname}"/>
            <tag name="version" value="${SensorVersion}"/>
        </tags>
    </influx2>
</configuration>

=>

mine-sensor,id=132-987-655,customer="California Miner",hostname=example.com,sensor-version=v1.00 altitude=10

Adding support to run in blazor wasm ?

I am trying to get the client db api to run in a blazor app and am getting a platform not supported error, are you planning to allow the library to work with blazor web assembly ?

` var influxDBClient = InfluxDBClientFactory.Create("https://us-west-2-1.aws.cloud2.influxdata.com/", Token);

    int i = 0;

    var fluxTables = await influxDBClient.GetQueryApi().QueryAsync(flux, orgid);
    fluxTables.ForEach(fluxTable =>
    {
        var fluxRecords = fluxTable.Records;
        fluxRecords.ForEach(fluxRecord =>
        {
            Console.WriteLine(i + "\t" + $"{fluxRecord.GetTime()}\t{fluxRecord.GetValue()}\t{fluxRecord.GetValueByIndex(8)}");
            i++;
        });
    });

`

Conflicting libraries - csvhelper?

I just cleaned/upgraded all the include libraries in my test app and when doing a flux query am getting the error:

System.MissingMethodException: Method not found: 'Void CsvHelper.CsvReader..ctor(System.IO.TextReader)'.
at InfluxDB.Client.Core.Internal.AbstractQueryClient.<>c__DisplayClass8_0.b__1()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at InfluxDB.Client.Core.Internal.AbstractQueryClient.Query(RestRequest query, Action2 consumer, Action1 onError, Action onComplete)

The code statement that is failing is:
influxDBClient.GetQueryApi().QueryAsync(flux, InfluxDB_OrgID, (cancellable, record) =>{(),
(error) =>
{
// error handling while processing result
Console.WriteLine(error.ToString());

                        }, () =>
                        {
                          // on complete
                          Console.WriteLine("Query completed");
                        }).GetAwaiter().GetResult();}

The flux query I am running works ok in the InfluxDB web query as well as with a command line curl statement, so I am sure that is not the issue.

I have get/create bucket statements operating ok, so sure its not something simple!

I do have the NuGet library CsvHelper (by Josh close) v15.0.4 installed .. could this be causing a library name clash?

The library hangs on the Flush method

I tried to upload many records via WriteAPI. When I try to send to many records (10000-100000) they are doesn't be received all. When I try to split to 1000 records per request the library hangs on Flush method. If I doesn't use Flush before disposing WriteAPI some data is lost.

The sample code:

            var start = 0;
            for (;;)
            {
                var historyBars = bars.Skip(start).Take(MaxBarsPerRequest).ToArray();
                if (historyBars.Length == 0)
                {
                    return;
                }
                if (start != 0)
                    await Task.Delay(100, token);
                start += MaxBarsPerRequest;
                m_logger.LogDebug(
                    $"Write bars for {historyBars.First().Security}. From: {historyBars.First().Date}, To: {historyBars.Last().Date}");
                using var api = m_client.GetWriteApi();
                api.WriteMeasurements(HistoryBarConstant.Bucket, HistoryBarConstant.OrgId, WritePrecision.S,
                    historyBars);
                //api.Flush();
            }

InfluxDB 2.0 Query limits

Hi all,
I have a query that basically aggregates data using aggregateWindow, then join the aggregate data together to get OHLCV bar. Below, you can see a sample of my query

The issue

The query works perfectly well from the Data Explorer. In returns me a data set for whatever time period I specify (start, end)
However, when I execute exactly the same query using C# client, I have the following behaviour:

Time Period - Result
5 days - no results
1 day - no results
300 minutes - no results
150 minutes - RETURNS DATA

Example of C#

var fluxQuery = await File.ReadAllTextAsync("./query.flux")
using var influxDBClient = InfluxDBClientFactory.Create(_configurationBuilder["apiUrl"], _configurationBuilder["apiToken"].ToCharArray());
            var api = influxDBClient.GetQueryApi();
            await api.QueryAsync(fluxQuery, _configurationBuilder["org"],
                (cancellable, record) => { callback(record); },
                delegate(Exception exception) { Console.WriteLine(exception.ToString()); },
                () => { Console.WriteLine("Completed"); });

Note:
I do not receive any exceptions, it's completes with no result

One more observation:
A simple "from() ... bla bla bla " query works from C# and Data Explorer very well for any time period

Is there any limitations of such type of query?

The Query Sample

Getting data

parameters = {
	symbol: "@ESM20",
	start: 2020-01-01T00:00:00Z,
	end: 2020-05-01T00:00:00Z,
	every: 1m,
}
dataset = from(bucket: "test")
	|> range(start: parameters.start, stop: parameters.end)
	|> filter(fn: (r) =>
		(r["_measurement"] == "tickdata"))
	|> filter(fn: (r) =>
		(r["_field"] == "Last" or r["_field"] == "Size"))
	|> filter(fn: (r) =>
		(r["symbol"] == parameters.symbol))

pre-aggregate individual values

high = dataset
	|> filter(fn: (r) =>
		(r["_field"] == "Last"))
	|> aggregateWindow(every: 1m, fn: max)
low = dataset
	|> filter(fn: (r) =>
		(r["_field"] == "Last"))
	|> aggregateWindow(every: 1m, fn: min)
....

join tables together

hl = join(tables: {d_low: low, d_high: high}, on: ["_time", "_field"])
	|> map(fn: (r) =>
		({_time: r._time, high: r._value_d_high, low: r._value_d_low}))

.....

Final result

join(tables: {d_volume: volume, d_ohlc: ohlc}, on: ["_time"])
	|> map(fn: (r) =>
		({
			_time: r._time,
			high: r.high,
			low: r.low,
			close: r.close,
			open: r.open,
			volume: r._value,
		}))
	|> yield(name: "1")

Make API method async when implementation inside call other async methods

Calling a sync method, which implementaton calls async method and waits for its result, on the UI thread freezes UI thread.

Such an example is Health() method:

public Check Health()
{
      return GetHealth(_healthService.HealthGetAsync());
}

It should be:

public async Task<Check> HealthAsync()
{
      await _healthService.HealthGetAsync();
}

to allow CLI async state machine do its work.

The TravisCI build fails to TimeoutException

Error Message:
 System.TimeoutException : Service start timed out after 00:00:10
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
   at WireMock.Server.FluentMockServer..ctor(IFluentMockServerSettings settings)
   at WireMock.Server.FluentMockServer.Start(IFluentMockServerSettings settings)
   at InfluxDB.Client.Core.Test.AbstractMockServerTest.SetUp() in /home/travis/build/bonitoo-io/influxdb-client-csharp/Client.Core.Test/AbstractMockServerTest.cs:line 16
--TearDown
   at InfluxDB.Client.Core.Test.AbstractMockServerTest.ShutdownServer() in /home/travis/build/bonitoo-io/influxdb-client-csharp/Client.Core.Test/AbstractMockServerTest.cs:line 27

Travis Build

unauthorized access with beta 1

I can't get this to work with the new InfluxDB v2.0 beta 1. i always get a "unauthorized access" exception. i tried to run influx db beta 1 on multiple systems and with different access tokens. the error was always the same.

The batch item wasn't processed successfully because: InfluxDB.Client.Core.Exceptions.HttpException: unauthorized access
   at InfluxDB.Client.WriteApi.<>c__DisplayClass5_2.<.ctor>b__18(Exception e) in C:\source\influxdb-client-csharp\Client\WriteApi.cs:line 124
   at System.Reactive.Linq.ObservableImpl.SelectMany`2.ObservableSelector._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\SelectMany.cs:line 865

are there some compatibility changes in beta 1? with the alphas it worked fine.

Parsing from string to double should use CultureInfo

We are using the client to fetch double values from Influx and our application uses German CultureInfo (CultureInfo("de-DE")) and had following issue:

Expected value: 6.1949943235120708
Received value: 6194994323512071.0

We think it could be a problem in FluxCsvParser where you convert string values to double without using CulturInfo (for example CultureInfo.InvariantCulture would fix the issue for us).
Currently we can work around the issue by manually setting the CultureInfo of the current thread (CultureInfo.CurrentCulture = CultureInfo.InvariantCulture) before each API call but that's no solution at all 😊

Lack of handling HTTP Forbidden (403)

When using authenticated connection and 403 is returned, there is exception InfluxDB.Client.Core.Exceptions.HTTPException without any notice about the reason. There is no Internal exception object nor the Error Code is customized.
I has shown the situation in the screenshot.

Screenshot

Add DeleteService

Add a DeleteApi that will be able to delete data by /delete endpoint.

  • check documentation
  • create DeleteApi - follow BucketsApi structure
  • add Docs (copy from swagger)
  • add Tests

Finding buckets without an organisation

Hi,
I've been playing with the latest C# client and I'm trying to list buckets using the BucketsAPI.FindBucketsAsync() method but it always returns empty.

If I specify the Organisation name using the BucketsAPI.FindBucketsByOrgNameAsync(string orgName) method, it works but this then requires me to pass an organisation name around which I would prefer not to need to do.

I've tried using the InfluxDBOptionsBuilder to create the InfluxDBClient with the InfluxDBOptionsBuilder.Org(string orgName) method but it still doesn't make the FindBucketsAsync() method work.

Am I missing something? I'm currently using a token with all permissions.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.