private async Task <Bucket> EnsureBucketCreated(InfluxDBClient influxDBClient, Organization organization)
        {
            var bucket = await influxDBClient.GetBucketsApi().FindBucketByNameAsync(_options.Bucket);

            if (bucket == null)
            {
                var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, (int)TimeSpan.Parse(_options.RetentionPolicy).TotalSeconds);
                bucket = await influxDBClient.GetBucketsApi().CreateBucketAsync(_options.Bucket, retention, organization.Id);
            }

            return(bucket);
        }
        public async Task ParseDate()
        {
            const string data = "{\"links\":{\"self\":\"/api/v2/buckets/0376298868765000/log\"},\"logs\":[" +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Created\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T07:33:44.390263749Z\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.252492+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.334601+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.437055+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.568936+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.64818+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.749176+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.82996+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.908611+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:41.9828+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.090233+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.193205+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.271324+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.338836+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.446591+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.549676+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.631707+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.714726+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.806946+01:00\"}," +
                                "{\"links\":{\"user\":\"/api/v2/users/037624e8d440e000\"},\"description\":\"Bucket Updated\",\"userID\":\"037624e8d440e000\",\"time\":\"2019-02-26T08:15:42.889206+01:00\"}]}";

            MockServer
            .Given(Request.Create().UsingGet())
            .RespondWith(CreateResponse(data, "application/json"));

            var logs = await _client.GetBucketsApi().FindBucketLogsAsync("id");

            Assert.AreEqual(20, logs.Count);
        }
Example #3
0
        private async Task RecreateBucket()
        {
            var bucketApi    = InfluxClient.GetBucketsApi();
            var orgApi       = InfluxClient.GetOrganizationsApi();
            var organization = (await orgApi.FindOrganizationsAsync()).First();
            var bucket       = await bucketApi.FindBucketByNameAsync(Configuration.InfluxBucket);

            if (bucket?.Name == Configuration.InfluxBucket)
            {
                await bucketApi.DeleteBucketAsync(bucket);

                //var deleteApi = client.GetDeleteApi();
                //await deleteApi.Delete(DateTime.MinValue, DateTime.MaxValue, "true",
                //    bucket, organization);
            }
            await bucketApi.CreateBucketAsync(Configuration.InfluxBucket, organization);
        }
        private Bucket GetBucketOrDefault(InfluxDBClient createBucketClient, string bucketName)
        {
            //TODO use Maybe monad ?
            Bucket bucket;

            try
            {
                bucket = createBucketClient.GetBucketsApi()
                         .FindBucketByNameAsync(bucketName)
                         .GetAwaiter().GetResult();
            }
            catch (HttpException ex)
            {
                SelfLog.WriteLine($"Error while trying to get {_connectionInfo.BucketName} (Org: {_connectionInfo.OrganizationId}), Message : {ex.Message}. Check if Token has enough access to read (if only write to bucket then set to False parameter {_connectionInfo.CreateBucketIfNotExists}) or set AllAccessToken or is active token");
                throw new InfluxDbClientCreateBucketException($"Cannot create bucket {_connectionInfo.BucketName}. Check if Token has enough access or set AllAccessToken or is active", ex);
            }

            return(bucket);
        }
        private Bucket CreateBucket(InfluxDBClient createBucketClient)
        {
            var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, (int)_connectionInfo.BucketRetentionPeriod.TotalSeconds);

            Bucket bucket;

            try
            {
                bucket = createBucketClient
                         .GetBucketsApi()
                         .CreateBucketAsync(_connectionInfo.BucketName, retention, _connectionInfo.OrganizationId)
                         .GetAwaiter().GetResult();
            }
            catch (HttpException ex)
            {
                SelfLog.WriteLine($"Error while creating {_connectionInfo.BucketName} (Org: {_connectionInfo.OrganizationId}), Message : {ex.Message}. Check if Token has enough access or set AllAccessToken or is active");
                throw new InfluxDbClientCreateBucketException($"Cannot create bucket {_connectionInfo.BucketName}. Check if Token has enough access or set AllAccessToken or is active", ex);
            }

            SelfLog.WriteLine($"Bucket {bucket.Name} ({bucket.Id} / Org: {bucket.OrgID}) created at {bucket.CreatedAt}");

            return(bucket);
        }
Example #6
0
        public static async Task Example(InfluxDBClient influxDB)
        {
            var organizationClient = influxDB.GetOrganizationsApi();

            var medicalGMBH = await organizationClient
                              .CreateOrganization("Medical Corp " +
                                                  DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff",
                                                                           CultureInfo.InvariantCulture));


            //
            // Create New Bucket with retention 1h
            //
            var temperatureBucket = await influxDB.GetBucketsApi().CreateBucket("temperature-sensors", medicalGMBH.Id);

            //
            // Add Permissions to read and write to the Bucket
            //
            var resource = new PermissionResource
            {
                Type = PermissionResource.TypeEnum.Buckets, OrgID = medicalGMBH.Id, Id = temperatureBucket.Id
            };
            var readBucket = new Permission
            {
                Resource = resource,
                Action   = Permission.ActionEnum.Read
            };

            var writeBucket = new Permission
            {
                Resource = resource,
                Action   = Permission.ActionEnum.Write
            };

            var authorization = await influxDB.GetAuthorizationsApi()
                                .CreateAuthorization(medicalGMBH, new List <Permission> {
                readBucket, writeBucket
            });

            Console.WriteLine($"The token to write to temperature-sensors bucket is: {authorization.Token}");

            influxDB.Dispose();

            //
            // Create new client with specified authorization token
            //

            influxDB = InfluxDBClientFactory.Create("http://localhost:9999", authorization.Token.ToCharArray());

            var writeOptions = WriteOptions
                               .CreateNew()
                               .BatchSize(5000)
                               .FlushInterval(1000)
                               .JitterInterval(1000)
                               .RetryInterval(5000)
                               .Build();

            //
            // Write data
            //
            using (var writeClient = influxDB.GetWriteApi(writeOptions))
            {
                //
                // Write by POCO
                //
                var temperature = new Temperature {
                    Location = "south", Value = 62D, Time = DateTime.UtcNow
                };
                writeClient.WriteMeasurement("temperature-sensors", medicalGMBH.Id, WritePrecision.Ns, temperature);

                //
                // Write by Point
                //
                var point = Point.Measurement("temperature")
                            .Tag("location", "west")
                            .Field("value", 55D)
                            .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);
                writeClient.WritePoint("temperature-sensors", medicalGMBH.Id, point);

                //
                // Write by LineProtocol
                //
                var record = "temperature,location=north value=60.0";
                writeClient.WriteRecord("temperature-sensors", medicalGMBH.Id, WritePrecision.Ns, record);

                writeClient.Flush();
                Thread.Sleep(2000);
            }

            //
            // Read data
            //
            var fluxTables = await influxDB.GetQueryApi().Query("from(bucket:\"temperature-sensors\") |> range(start: 0)", medicalGMBH.Id);

            fluxTables.ForEach(fluxTable =>
            {
                var fluxRecords = fluxTable.Records;
                fluxRecords.ForEach(fluxRecord =>
                {
                    Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
                });
            });


            influxDB.Dispose();
        }