-
Notifications
You must be signed in to change notification settings - Fork 29
feat: Create a Exporter class to allow export data from LandingLens #174
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .media import Media # noqa | ||
from .metadata import Metadata # noqa | ||
from .export import Exporter # noqa |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from typing import Optional | ||
|
||
from landingai.data_management.client import EVENT_LOGS, LandingLens | ||
from datetime import datetime, timezone | ||
import requests | ||
import logging | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Exporter: | ||
"""Export management API client. | ||
This class provides a set of APIs to export data from LandingLens. | ||
For example, you can use this class to export all the available event logs of your organization. | ||
|
||
Example | ||
------- | ||
>>> client = Exporter(project_id, api_key) | ||
>>> client.export_event_logs("2023-06-01", '/path/to/save/file.csv') | ||
>>> # The csv file will be saved in the desired path. | ||
|
||
Parameters | ||
---------- | ||
project_id: int | ||
LandingLens project id. Can override this default in individual commands. | ||
api_key: Optional[str] | ||
LandingLens API Key. If it's not provided, it will be read from the environment variable LANDINGAI_API_KEY, or from .env file on your project root directory. | ||
""" | ||
|
||
def __init__(self, project_id: int, api_key: Optional[str] = None): | ||
self._client = LandingLens(project_id=project_id, api_key=api_key) | ||
|
||
def export_event_logs(self, from_date: str, save_path: str) -> None: | ||
"""Exports the event logs of the organization from the given time range. | ||
|
||
Parameters | ||
---------- | ||
from_date: str | ||
In date following the format "YYYY-MM-DD" | ||
save_path: | ||
Desired path to save the csv file to: '/path/to/save/file.csv' | ||
|
||
Returns | ||
---------- | ||
None | ||
The csv file will be saved in the desired path. | ||
""" | ||
try: | ||
datetime.strptime(from_date, "%Y-%m-%d") | ||
except ValueError: | ||
raise ValueError("from_date must be in YYYY-MM-DD format") | ||
from_timestamp = datetime.strptime( | ||
f"{from_date} 00:00:00.00000", "%Y-%m-%d %H:%M:%S.%f" | ||
) | ||
to_timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f") | ||
|
||
_LOGGER.info("Exporting event logs...") | ||
resp = self._client._api( | ||
EVENT_LOGS, | ||
params={"fromTimestamp": from_timestamp, "toTimestamp": to_timestamp}, | ||
) | ||
signed_url = resp["data"].get("signedUrl") | ||
_LOGGER.debug("Signed URL: ", signed_url) | ||
self._download_file_from_signed_url(signed_url, save_path) | ||
print(f"Event logs exported successfully to path: {save_path}") | ||
|
||
def _download_file_from_signed_url(self, signed_url: str, save_path: str) -> None: | ||
response = requests.get(signed_url) | ||
with open(save_path, "wb") as file: | ||
file.write(response.content) | ||
Comment on lines
+67
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
responses: | ||
- response: | ||
auto_calculate_content_length: false | ||
body: '{"code":0,"message":"","data":{"s3Path":"s3://.../1622505600-1706572800.csv","signedUrl":"https://landinglens-bucket.s3.somepath/1622505600-1706572800.csv?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential...-SignedHeaders=host"}}' | ||
content_type: text/plain | ||
method: POST | ||
status: 200 | ||
url: https://app.landing.ai/api/event/export?fromTimestamp=2021-06-01 00:00:00&toTimestamp=2024-01-30 00:00:00.000000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import responses | ||
|
||
from landingai.data_management.export import Exporter | ||
from freezegun import freeze_time | ||
from unittest import mock | ||
|
||
|
||
@responses.activate | ||
@mock.patch("landingai.data_management.export.Exporter._download_file_from_signed_url") | ||
@freeze_time("2024-01-30 00:00:00.000000") # Mock the current date and time | ||
def test_export_event_logs(mocked_method): | ||
responses._add_from_file( | ||
file_path="tests/data/responses/test_export_event_logs.yaml" | ||
) | ||
project_id = 12345 | ||
api_key = "land_sk_12345" | ||
client = Exporter(project_id, api_key) | ||
res = client.export_event_logs("2021-06-01", "./local/path/to/save/file.csv") | ||
assert res is None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can return the signed_url and check the return value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optionally have to_date, if not provided, default to current time.