Skip to content

Commit 1b8be4c

Browse files
rolincovabednar
authored andcommitted
Add Delete service (influxdata#44)
1 parent d851d7c commit 1b8be4c

File tree

6 files changed

+269
-2
lines changed

6 files changed

+269
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### API
44
1. [#42](https://github.com/influxdata/influxdb-client-csharp/pull/42): Updated swagger to latest version
5-
5+
2. [#43](https://github.com/influxdata/influxdb-client-csharp/issues/43) Added DeleteApi
66

77
## 1.1.0 [2019-10-11]
88

Client.Test/ItDeleteApiTest.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Threading.Tasks;
5+
using InfluxDB.Client.Api.Domain;
6+
using InfluxDB.Client.Core;
7+
using InfluxDB.Client.Writes;
8+
using NUnit.Framework;
9+
10+
namespace InfluxDB.Client.Test
11+
{
12+
[TestFixture]
13+
public class ItDeleteApiTest : AbstractItClientTest
14+
{
15+
[SetUp]
16+
public new async Task SetUp()
17+
{
18+
_organization = await FindMyOrg();
19+
20+
var retention = new BucketRetentionRules(BucketRetentionRules.TypeEnum.Expire, 3600);
21+
22+
_bucket = await Client.GetBucketsApi()
23+
.CreateBucketAsync(GenerateName("h2o"), retention, _organization);
24+
25+
//
26+
// Add Permissions to read and write to the Bucket
27+
//
28+
var resource =
29+
new PermissionResource(PermissionResource.TypeEnum.Buckets, _bucket.Id, null, _organization.Id);
30+
31+
var readBucket = new Permission(Permission.ActionEnum.Read, resource);
32+
var writeBucket = new Permission(Permission.ActionEnum.Write, resource);
33+
34+
var loggedUser = await Client.GetUsersApi().MeAsync();
35+
Assert.IsNotNull(loggedUser);
36+
37+
var authorization = await Client.GetAuthorizationsApi()
38+
.CreateAuthorizationAsync(await FindMyOrg(), new List<Permission> {readBucket, writeBucket});
39+
40+
_token = authorization.Token;
41+
42+
Client.Dispose();
43+
var options = new InfluxDBClientOptions.Builder().Url(InfluxDbUrl).AuthenticateToken(_token.ToCharArray())
44+
.Org(_organization.Id).Bucket(_bucket.Id).Build();
45+
Client = InfluxDBClientFactory.Create(options);
46+
_queryApi = Client.GetQueryApi();
47+
}
48+
49+
[TearDown]
50+
protected new void After()
51+
{
52+
_writeApi.Dispose();
53+
}
54+
55+
private Bucket _bucket;
56+
private QueryApi _queryApi;
57+
private WriteApi _writeApi;
58+
private DeleteApi _deleteApi;
59+
private Organization _organization;
60+
private string _token;
61+
62+
[Measurement("h2o")]
63+
private class H20Measurement
64+
{
65+
[Column("location", IsTag = true)] public string Location { get; set; }
66+
67+
[Column("level")] public double? Level { get; set; }
68+
69+
[Column(IsTimestamp = true)] public DateTime Time { get; set; }
70+
}
71+
72+
[Test]
73+
public async Task Delete()
74+
{
75+
Client.Dispose();
76+
77+
Environment.SetEnvironmentVariable("point-datacenter", "LA");
78+
ConfigurationManager.AppSettings["point-sensor.version"] = "1.23a";
79+
80+
var options = new InfluxDBClientOptions.Builder().Url(InfluxDbUrl)
81+
.AuthenticateToken(_token.ToCharArray())
82+
.AddDefaultTag("id", "132-987-655")
83+
.AddDefaultTag("customer", "California Miner")
84+
.AddDefaultTag("env-var", "${env.point-datacenter}")
85+
.AddDefaultTag("sensor-version", "${point-sensor.version}")
86+
.Build();
87+
88+
Client = InfluxDBClientFactory.Create(options);
89+
90+
var point = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 1);
91+
var point2 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 2);
92+
var point3 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 3);
93+
var point4 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 4);
94+
var point5 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 5);
95+
var point6 = PointData.Measurement("h2o_feet").Tag("location", "west").Field("water_level", 6);
96+
97+
_writeApi = Client.GetWriteApi();
98+
var listener = new WriteApiTest.EventListener(_writeApi);
99+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point);
100+
_writeApi.Flush();
101+
102+
listener.WaitToSuccess();
103+
104+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point2);
105+
_writeApi.Flush();
106+
107+
listener.WaitToSuccess();
108+
109+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point3);
110+
_writeApi.Flush();
111+
112+
listener.WaitToSuccess();
113+
114+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point4);
115+
_writeApi.Flush();
116+
117+
listener.WaitToSuccess();
118+
119+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point5);
120+
_writeApi.Flush();
121+
122+
listener.WaitToSuccess();
123+
124+
_writeApi.WritePoint(_bucket.Name, _organization.Id, point6);
125+
_writeApi.Flush();
126+
127+
listener.WaitToSuccess();
128+
129+
string query = "from(bucket:\"" + _bucket.Name +
130+
"\") |> range(start: 1970-01-01T00:00:00.000000001Z) |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")";
131+
132+
_queryApi = Client.GetQueryApi();
133+
var tables = await _queryApi.QueryAsync(query, _organization.Id);
134+
135+
Assert.AreEqual(1, tables.Count);
136+
Assert.AreEqual(6, tables[0].Records.Count);
137+
138+
_deleteApi = Client.GetDeleteApi();
139+
await _deleteApi.Delete(DateTime.Now.AddHours(-1), DateTime.Now, "", _bucket, _organization);
140+
141+
var tablesAfterDelete = await _queryApi.QueryAsync(query, _organization.Id);
142+
143+
Assert.AreEqual(0, tablesAfterDelete.Count);
144+
}
145+
}
146+
}

Client/DeleteApi.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Threading.Tasks;
4+
using InfluxDB.Client.Api.Domain;
5+
using InfluxDB.Client.Api.Service;
6+
using InfluxDB.Client.Core;
7+
using NodaTime;
8+
9+
namespace InfluxDB.Client
10+
{
11+
public class DeleteApi
12+
{
13+
private readonly DefaultService _service;
14+
15+
protected internal DeleteApi(DefaultService service)
16+
{
17+
Arguments.CheckNotNull(service, nameof(service));
18+
19+
_service = service;
20+
}
21+
22+
/// <summary>
23+
/// Delete Time series data from InfluxDB.
24+
/// </summary>
25+
/// <param name="start">Start time</param>
26+
/// <param name="stop">Stop time</param>
27+
/// <param name="predicate">Sql where like delete statement</param>
28+
/// <param name="bucket">The bucket from which data will be deleted</param>
29+
/// <param name="org">The organization of the above bucket</param>
30+
public async Task Delete(DateTime start, DateTime stop, string predicate, Bucket bucket, Organization org)
31+
{
32+
Arguments.CheckNotNull(start, "Start is required");
33+
Arguments.CheckNotNull(stop, "Stop is required");
34+
Arguments.CheckNotNull(predicate, "Predicate is required");
35+
Arguments.CheckNotNull(bucket, "Bucket is required");
36+
Arguments.CheckNotNull(org, "Organization is required");
37+
38+
await Delete(start, stop, predicate, bucket.Id, org.Id);
39+
}
40+
41+
/// <summary>
42+
/// Delete Time series data from InfluxDB.
43+
/// </summary>
44+
/// <param name="start">Start time</param>
45+
/// <param name="stop">Stop time</param>
46+
/// <param name="predicate">Sql where like delete statement</param>
47+
/// <param name="bucket">The bucket from which data will be deleted</param>
48+
/// <param name="org">The organization of the above bucket</param>
49+
public async Task Delete(DateTime start, DateTime stop, string predicate, string bucket, string org)
50+
{
51+
Arguments.CheckNotNull(start, "Start is required");
52+
Arguments.CheckNotNull(stop, "Stop is required");
53+
Arguments.CheckNotNull(predicate, "Predicate is required");
54+
Arguments.CheckNonEmptyString(bucket, "Bucket is required");
55+
Arguments.CheckNonEmptyString(org, "Organization is required");
56+
57+
var predicateRequest = new DeletePredicateRequest(start, stop, predicate);
58+
59+
await Delete(predicateRequest, bucket, org);
60+
}
61+
62+
/// <summary>
63+
/// Delete Time series data from InfluxDB.
64+
/// </summary>
65+
/// <param name="predicate">Predicate delete request</param>
66+
/// <param name="bucket">The bucket from which data will be deleted</param>
67+
/// <param name="org">The organization of the above bucket</param>
68+
public async Task Delete(DeletePredicateRequest predicate, string bucket, string org)
69+
{
70+
await _service.DeletePostAsync(predicate, null, org, bucket, null, null);
71+
}
72+
}
73+
}

Client/InfluxDBClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ public ChecksApi GetChecksApi()
284284

285285
return new ChecksApi(service);
286286
}
287+
288+
/// <summary>
289+
/// Get the Delete client.
290+
/// </summary>
291+
/// <returns>the new client instance for Delete API</returns>
292+
public DeleteApi GetDeleteApi()
293+
{
294+
var service = new DefaultService((Configuration) _apiClient.Configuration)
295+
{
296+
ExceptionFactory = _exceptionFactory
297+
};
298+
299+
return new DeleteApi(service);
300+
}
287301

288302
/// <summary>
289303
/// Set the log level for the request and response information.

Client/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The reference client that allows query, write and management (bucket, organizati
1212
- [Data Point](#by-data-point)
1313
- [POCO](#by-poco)
1414
- [Default Tags](#default-tags)
15+
- [Delete data](#delete-data)
1516
- [InfluxDB 2.0 Management API](#management-api)
1617
- sources, buckets
1718
- tasks
@@ -546,6 +547,35 @@ writeApi.EventHandler += (sender, eventArgs) =>
546547
};
547548
```
548549

550+
## Delete Data
551+
552+
Delete data from specified bucket:
553+
554+
```c#
555+
using InfluxDB.Client;
556+
using InfluxDB.Client.Api.Domain;
557+
558+
namespace Examples
559+
{
560+
public static class WriteLineProtocol
561+
{
562+
private static readonly char[] Token = "".ToCharArray();
563+
564+
public static void Main(string[] args)
565+
{
566+
var influxDBClient = InfluxDBClientFactory.Create("http://localhost:9999", Token);
567+
568+
//
569+
// Delete data
570+
//
571+
await influxDB.GetDeleteApi().Delete(DateTime.UtcNow.AddMinutes(-1), DateTime.Now, "", "bucket", "org");
572+
573+
influxDBClient.Dispose();
574+
}
575+
}
576+
}
577+
```
578+
549579
## Management API
550580

551581
The client has following management API:

Examples/PlatformExample.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ public static async Task Example(InfluxDBClient influxDB)
117117
Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
118118
});
119119
});
120-
120+
121+
//
122+
// Delete data
123+
//
124+
await influxDB.GetDeleteApi().Delete(DateTime.UtcNow.AddMinutes(-1), DateTime.Now, "", temperatureBucket, medicalGMBH);
121125

122126
influxDB.Dispose();
123127
}

0 commit comments

Comments
 (0)