Skip to content

Commit 723629f

Browse files
authored
feat: Add delete metrics support influxdata#31 (influxdata#32)
1 parent 2a23bde commit 723629f

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44
1. [#24](https://github.com/influxdata/influxdb-client-python/issues/24): Added possibility to write dictionary-style object
55
1. [#27](https://github.com/influxdata/influxdb-client-python/issues/27): Added possibility to write bytes type of data
6+
1. [#31](https://github.com/influxdata/influxdb-client-python/issues/31): Added support for delete metrics
67

78
### API
89
1. [#28](https://github.com/bonitoo-io/influxdb-client-python/pull/28): Updated swagger to latest version

influxdb_client/client/delete_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import datetime
2+
3+
from influxdb_client import DefaultService, DeletePredicateRequest
4+
5+
6+
class DeleteApi(object):
7+
8+
def __init__(self, influxdb_client):
9+
self._influxdb_client = influxdb_client
10+
self._service = DefaultService(influxdb_client.api_client)
11+
12+
def delete(self, start: datetime, stop: object, predicate: object, bucket_id: str, org_id: str) -> None:
13+
"""
14+
Delete Time series data from InfluxDB.
15+
:param start: start time
16+
:param stop: stop time
17+
:param predicate: predicate
18+
:param bucket_id: bucket id from which data will be deleted
19+
:param org_id: organization id
20+
:return:
21+
"""
22+
predicate_request = DeletePredicateRequest(start=start, stop=stop, predicate=predicate)
23+
return self._service.delete_post(delete_predicate_request=predicate_request, bucket_id=bucket_id, org_id=org_id)

influxdb_client/client/influxdb_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
44
from influxdb_client.client.authorizations_api import AuthorizationsApi
55
from influxdb_client.client.bucket_api import BucketsApi
6+
from influxdb_client.client.delete_api import DeleteApi
67
from influxdb_client.client.labels_api import LabelsApi
78
from influxdb_client.client.organizations_api import OrganizationsApi
89
from influxdb_client.client.query_api import QueryApi
@@ -144,6 +145,13 @@ def ready(self) -> Ready:
144145
ready_service = ReadyService(self.api_client)
145146
return ready_service.get_ready()
146147

148+
def delete_api(self) -> DeleteApi:
149+
"""
150+
Gets the delete metrics API instance
151+
:return: delete api
152+
"""
153+
return DeleteApi(self)
154+
147155

148156
class _Configuration(Configuration):
149157
def __init__(self):

tests/test_DeleteApi.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from influxdb_client.client.write_api import SYNCHRONOUS
2+
3+
from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point, WriteOptions
4+
from tests.base_test import BaseTest
5+
6+
7+
class DeleteApiTest(BaseTest):
8+
9+
def setUp(self) -> None:
10+
super(DeleteApiTest, self).setUp()
11+
response = self.buckets_api.find_buckets()
12+
13+
for bucket in response.buckets:
14+
if bucket.name.endswith("_IT"):
15+
print("Delete bucket: ", bucket.name)
16+
self.buckets_api.delete_bucket(bucket)
17+
18+
self.bucket = self.create_test_bucket()
19+
self.organization = self.find_my_org()
20+
21+
resource = PermissionResource(type="buckets", org_id=self.organization.id, id=self.bucket.id)
22+
read_bucket = Permission(resource=resource, action="read")
23+
write_bucket = Permission(resource=resource, action="write")
24+
25+
authorization = self.client.authorizations_api().create_authorization(org_id=self.organization.id,
26+
permissions=[read_bucket, write_bucket])
27+
self.auth_token = authorization.token
28+
self.client.close()
29+
self.client = InfluxDBClient(url=self.host, token=self.auth_token, debug=True, org=self.org)
30+
self.delete_api = self.client.delete_api()
31+
32+
def test_delete_buckets(self):
33+
34+
write_api = self.client.write_api(write_options=SYNCHRONOUS)
35+
p1 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 7.0).time(1)
36+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p1)
37+
38+
p2 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(2)
39+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p2)
40+
41+
p3 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(3)
42+
p4 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 10.0).time(4)
43+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p3, p4])
44+
45+
p5 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(5)
46+
p6 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 12.0).time(6)
47+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p5, p6])
48+
49+
p7 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(7)
50+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p7)
51+
p8 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(8)
52+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p8)
53+
54+
p9 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(9)
55+
p10 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(10)
56+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p9, p10])
57+
58+
p11 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(11)
59+
p12 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 13.0).time(12)
60+
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p11, p12])
61+
62+
q = f'from(bucket:\"{self.bucket.name}\") |> range(start: 1970-01-01T00:00:00.000000001Z)'
63+
print(q)
64+
flux_tables = self.client.query_api().query(query=q, org=self.organization.id)
65+
self.assertEqual(len(flux_tables), 1)
66+
self.assertEqual(len(flux_tables[0].records), 12)
67+
68+
start = "1970-01-01T00:00:00.000000001Z"
69+
stop = "1970-01-01T00:00:00.000000012Z"
70+
self.delete_api.delete(start, stop, "", bucket_id=self.bucket.id, org_id=self.organization.id)
71+
72+
flux_tables2 = self.client.query_api().query(
73+
f'from(bucket:"{self.bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)',
74+
org=self.organization.id)
75+
self.assertEqual(len(flux_tables2), 0)

tests/test_LabelsApi.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ def test_clone_label(self):
7979

8080
label = self.labels_api.create_label(name, self.organization.id, properties)
8181

82-
cloned = self.labels_api.clone_label(cloned_name=name, label=label)
83-
84-
self.assertEqual(cloned.name, name)
82+
cloned = self.labels_api.clone_label(cloned_name=name+"_clone", label=label)
83+
self.assertEqual(cloned.name, name+"_clone")
8584
self.assertEqual(cloned.properties, properties)
8685

8786
def test_find_label_by_id(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy