Skip to content

feat: delete_api also accept datetime as a value for start and stop #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. [#319](https://github.com/influxdata/influxdb-client-python/pull/319): Add supports for array expressions in query parameters
2. [#320](https://github.com/influxdata/influxdb-client-python/pull/320): Add JSONEncoder to encode query results to JSON
1. [#317](https://github.com/influxdata/influxdb-client-python/pull/317): `delete_api` also accept `datetime` as a value for `start` and `stop`

### Bug Fixes
1. [#321](https://github.com/influxdata/influxdb-client-python/pull/321): Fixes return type for dashboard when `include=properties` is used
Expand Down
9 changes: 8 additions & 1 deletion influxdb_client/client/delete_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Delete time series data from InfluxDB."""

import datetime
from datetime import datetime

from influxdb_client import DeleteService, DeletePredicateRequest
from influxdb_client.client.util.date_utils import get_date_helper


class DeleteApi(object):
Expand All @@ -24,5 +25,11 @@ def delete(self, start: datetime, stop: object, predicate: object, bucket: str,
:param org: organization id or name
:return:
"""
date_helper = get_date_helper()
if isinstance(start, datetime):
start = date_helper.to_utc(start)
if isinstance(stop, datetime):
stop = date_helper.to_utc(stop)

predicate_request = DeletePredicateRequest(start=start, stop=stop, predicate=predicate)
return self._service.post_delete(delete_predicate_request=predicate_request, bucket=bucket, org=org)
27 changes: 20 additions & 7 deletions tests/test_DeleteApi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from datetime import datetime

from pytz import UTC

from influxdb_client import PermissionResource, Permission, InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
from tests.base_test import BaseTest
Expand All @@ -11,7 +15,6 @@ def setUp(self) -> None:

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()
Expand All @@ -25,15 +28,14 @@ def setUp(self) -> None:
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.client = InfluxDBClient(url=self.host, token=self.auth_token, org=self.org)
self.delete_api = self.client.delete_api()

def test_delete_buckets(self):

self._write_data()

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)
Expand All @@ -52,19 +54,30 @@ def test_delete_buckets_by_name(self):
self._write_data()

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_and_verify(start, stop)

def test_start_stop_types(self):
starts_stops = [
("1970-01-01T00:00:00.000000001Z", "1970-01-01T00:00:00.000000012Z"),
(datetime(1970, 1, 1, 0, 0, 0, 0, UTC), datetime(1970, 1, 1, 0, 0, 0, 1, UTC)),
(datetime(1970, 1, 1, 0, 0, 0, 0), datetime(1970, 1, 1, 0, 0, 0, 1))
]
for start_stop in starts_stops:
self._write_data()
self._delete_and_verify(start_stop[0], start_stop[1])

def _delete_and_verify(self, start, stop):
self.delete_api.delete(start, stop, "", bucket=self.bucket.name, org=self.organization.name)

flux_tables2 = self.client.query_api().query(
flux_tables = 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)
self.assertEqual(len(flux_tables), 0)

def _write_data(self):

Expand Down