private async Task <Organization> EnsureOrganizationCreated(InfluxDBClient influxDBClient) { var orgs = await influxDBClient.GetOrganizationsApi().FindOrganizationsAsync(); var organization = orgs.FirstOrDefault(r => r.Name == _options.Organization); if (organization == null) { organization = await influxDBClient.GetOrganizationsApi().CreateOrganizationAsync(_options.Organization); } return(organization); }
protected async Task <Organization> FindMyOrg() { var org = (await Client.GetOrganizationsApi().FindOrganizationsAsync(100)) .FirstOrDefault(organization => organization.Name.Equals("my-org")); Assert.IsNotNull(org, "my-org is required for integration tests"); return(org); }
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); }
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(); }
protected async Task <Organization> FindMyOrg() { return((await Client.GetOrganizationsApi().FindOrganizationsAsync()) .First(organization => organization.Name.Equals("my-org"))); }
public async Task CreateMonitoringAndAlerting() { MockServer .Given(Request.Create().UsingGet()) .RespondWith(CreateResponse("{}", "application/json")); MockServer .Given(Request.Create().UsingPost()) .RespondWith(CreateResponse("{}", "application/json")); var org = (await _client.GetOrganizationsApi().FindOrganizationsAsync()) .First(organization => organization.Name.Equals("my-org")); var checksApi = _client.GetChecksApi(); var notificationEndpointsApi = _client.GetNotificationEndpointsApi(); var notificationRulesApi = _client.GetNotificationRulesApi(); var checks = await checksApi.FindChecksAsync(org.Id, new FindOptions()); foreach (var delete in checks._Checks.Where(ne => ne.Name.EndsWith("-IT"))) { await checksApi.DeleteCheckAsync(delete); } var rules = await notificationRulesApi.FindNotificationRulesAsync(org.Id, new FindOptions()); foreach (var delete in rules._NotificationRules.Where(ne => ne.Name.EndsWith("-IT"))) { await notificationRulesApi.DeleteNotificationRuleAsync(delete); } var endpoints = await notificationEndpointsApi.FindNotificationEndpointsAsync(org.Id, new FindOptions()); foreach (var delete in endpoints._NotificationEndpoints.Where(ne => ne.Name.EndsWith("-IT"))) { await notificationEndpointsApi.DeleteNotificationEndpointAsync(delete); } // // Create Threshold Check // // Set status to 'Critical' if the 'current' value for 'stock' measurement is lesser than '35' // var query = "from(bucket: \"my-bucket\") " + "|> range(start: v.timeRangeStart, stop: v.timeRangeStop) " + "|> filter(fn: (r) => r._measurement == \"stock\") " + "|> filter(fn: (r) => r.company == \"zyz\") " + "|> aggregateWindow(every: 5s, fn: mean) " + "|> filter(fn: (r) => r._field == \"current\") " + "|> yield(name: \"mean\")"; var threshold = new LesserThreshold(value: 35F, level: CheckStatusLevel.CRIT, type: LesserThreshold.TypeEnum.Lesser); var message = "The Stock price for XYZ is on: ${ r._level } level!"; await checksApi.CreateThresholdCheckAsync(AbstractItClientTest.GenerateName("XYZ Stock value"), query, "5s", message, threshold, org.Id); // // Create Slack Notification endpoint // var url = MockServerUrl; var endpoint = await notificationEndpointsApi.CreateSlackEndpointAsync( AbstractItClientTest.GenerateName("Slack Endpoint"), url, org.Id); // // Create Notification Rule // // Send message if the status is 'Critical' // await notificationRulesApi.CreateSlackRuleAsync( AbstractItClientTest.GenerateName("Critical status to Slack"), "10s", "${ r._message }", RuleStatusLevel.CRIT, endpoint, org.Id); // // Write Data // var now = DateTime.UtcNow; var point1 = PointData .Measurement("stock") .Tag("company", "zyz") .Field("current", 33.65) .Timestamp(now, WritePrecision.Ns); var writeApi = _client.GetWriteApi(); var listener = new WriteApiTest.EventListener(writeApi); writeApi.WritePoint("my-bucket", "my-org", point1); writeApi.Flush(); listener.WaitToSuccess(); var start = DateTime.UtcNow; while (!MockServer.LogEntries.Any() && (DateTime.UtcNow - start).TotalSeconds < 30) { Thread.Sleep(100); } var requestEntry = MockServer.LogEntries.Last(); Assert.AreEqual($"{MockServerUrl}/", requestEntry.RequestMessage.Url); var json = (JObject)requestEntry.RequestMessage.BodyAsJson; Assert.IsNotNull(json.GetValue("attachments")); var attachments = (JArray)json.GetValue("attachments"); Assert.AreEqual(1, attachments.Count); Assert.AreEqual("The Stock price for XYZ is on: crit level!", attachments[0]["text"].ToString()); }