Skip to content

Commit

Permalink
feat: Add delete metrics support #31 (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek authored Nov 1, 2019
1 parent 2a23bde commit 723629f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features
1. [#24](https://github.com/influxdata/influxdb-client-python/issues/24): Added possibility to write dictionary-style object
1. [#27](https://github.com/influxdata/influxdb-client-python/issues/27): Added possibility to write bytes type of data
1. [#31](https://github.com/influxdata/influxdb-client-python/issues/31): Added support for delete metrics

### API
1. [#28](https://github.com/bonitoo-io/influxdb-client-python/pull/28): Updated swagger to latest version
Expand Down
23 changes: 23 additions & 0 deletions influxdb_client/client/delete_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import datetime

from influxdb_client import DefaultService, DeletePredicateRequest


class DeleteApi(object):

def __init__(self, influxdb_client):
self._influxdb_client = influxdb_client
self._service = DefaultService(influxdb_client.api_client)

def delete(self, start: datetime, stop: object, predicate: object, bucket_id: str, org_id: str) -> None:
"""
Delete Time series data from InfluxDB.
:param start: start time
:param stop: stop time
:param predicate: predicate
:param bucket_id: bucket id from which data will be deleted
:param org_id: organization id
:return:
"""
predicate_request = DeletePredicateRequest(start=start, stop=stop, predicate=predicate)
return self._service.delete_post(delete_predicate_request=predicate_request, bucket_id=bucket_id, org_id=org_id)
8 changes: 8 additions & 0 deletions influxdb_client/client/influxdb_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService
from influxdb_client.client.authorizations_api import AuthorizationsApi
from influxdb_client.client.bucket_api import BucketsApi
from influxdb_client.client.delete_api import DeleteApi
from influxdb_client.client.labels_api import LabelsApi
from influxdb_client.client.organizations_api import OrganizationsApi
from influxdb_client.client.query_api import QueryApi
Expand Down Expand Up @@ -144,6 +145,13 @@ def ready(self) -> Ready:
ready_service = ReadyService(self.api_client)
return ready_service.get_ready()

def delete_api(self) -> DeleteApi:
"""
Gets the delete metrics API instance
:return: delete api
"""
return DeleteApi(self)


class _Configuration(Configuration):
def __init__(self):
Expand Down
75 changes: 75 additions & 0 deletions tests/test_DeleteApi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from influxdb_client.client.write_api import SYNCHRONOUS

from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point, WriteOptions
from tests.base_test import BaseTest


class DeleteApiTest(BaseTest):

def setUp(self) -> None:
super(DeleteApiTest, self).setUp()
response = self.buckets_api.find_buckets()

for bucket in response.buckets:
if bucket.name.endswith("_IT"):
print("Delete bucket: ", bucket.name)
self.buckets_api.delete_bucket(bucket)

self.bucket = self.create_test_bucket()
self.organization = self.find_my_org()

resource = PermissionResource(type="buckets", org_id=self.organization.id, id=self.bucket.id)
read_bucket = Permission(resource=resource, action="read")
write_bucket = Permission(resource=resource, action="write")

authorization = self.client.authorizations_api().create_authorization(org_id=self.organization.id,
permissions=[read_bucket, write_bucket])
self.auth_token = authorization.token
self.client.close()
self.client = InfluxDBClient(url=self.host, token=self.auth_token, debug=True, org=self.org)
self.delete_api = self.client.delete_api()

def test_delete_buckets(self):

write_api = self.client.write_api(write_options=SYNCHRONOUS)
p1 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 7.0).time(1)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p1)

p2 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(2)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p2)

p3 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(3)
p4 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 10.0).time(4)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p3, p4])

p5 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(5)
p6 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 12.0).time(6)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p5, p6])

p7 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 8.0).time(7)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p7)
p8 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(8)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=p8)

p9 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 9.0).time(9)
p10 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(10)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p9, p10])

p11 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 11.0).time(11)
p12 = Point(measurement_name="h2o").tag("location", "coyote_creek").field("watter_level", 13.0).time(12)
write_api.write(bucket=self.bucket.name, org=self.organization.name, record=[p11, p12])

q = f'from(bucket:\"{self.bucket.name}\") |> range(start: 1970-01-01T00:00:00.000000001Z)'
print(q)
flux_tables = self.client.query_api().query(query=q, org=self.organization.id)
self.assertEqual(len(flux_tables), 1)
self.assertEqual(len(flux_tables[0].records), 12)

start = "1970-01-01T00:00:00.000000001Z"
stop = "1970-01-01T00:00:00.000000012Z"
self.delete_api.delete(start, stop, "", bucket_id=self.bucket.id, org_id=self.organization.id)

flux_tables2 = self.client.query_api().query(
f'from(bucket:"{self.bucket.name}") |> range(start: 1970-01-01T00:00:00.000000001Z)',
org=self.organization.id)
self.assertEqual(len(flux_tables2), 0)
5 changes: 2 additions & 3 deletions tests/test_LabelsApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def test_clone_label(self):

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

cloned = self.labels_api.clone_label(cloned_name=name, label=label)

self.assertEqual(cloned.name, name)
cloned = self.labels_api.clone_label(cloned_name=name+"_clone", label=label)
self.assertEqual(cloned.name, name+"_clone")
self.assertEqual(cloned.properties, properties)

def test_find_label_by_id(self):
Expand Down

0 comments on commit 723629f

Please sign in to comment.