Skip to content

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

Closed
wants to merge 5 commits into from
Closed
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 landingai/data_management/__init__.py
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
6 changes: 6 additions & 0 deletions landingai/data_management/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
GET_DEFECTS = "get_defects"
GET_PROJECT_MODEL_INFO = "get_project_model_info"
GET_FAST_TRAINING_EXPORT = "get_fast_training_export"
EVENT_LOGS = "event_logs"


ROUTES = {
Expand Down Expand Up @@ -94,6 +95,11 @@
"endpoint": "api/{version}/project/with_users",
"method": requests.get,
},
EVENT_LOGS: {
"root_url": "LANDING_API",
"endpoint": "api/event/export",
"method": requests.post,
},
}

_URL_ROOTS = {
Expand Down
70 changes: 70 additions & 0 deletions landingai/data_management/export.py
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'
Copy link
Member

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.


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just webbrowser.open(signed_url) to directly download the resource through the browser.

43 changes: 30 additions & 13 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tqdm = ">=4.64.0,<5.0.0"
tenacity = "^8.2.3"
pandas = "2.0"
ruff = "^0.1.8"
freezegun = "^1.4.0"

[tool.poetry.group.dev.dependencies]
autoflake = "1.*"
Expand Down
8 changes: 8 additions & 0 deletions tests/data/responses/test_export_event_logs.yaml
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
19 changes: 19 additions & 0 deletions tests/unit/landingai/data_management/test_export.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return the signed_url and check the return value