-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0acc9a
commit e8a83d0
Showing
14 changed files
with
235 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
import time | ||
|
||
from tulip_api.asyncio import TulipAPI, TulipTable, TulipTableCSVUploader | ||
|
||
concurrency = 40 | ||
filename = "example.csv" | ||
table_id = "PT929bpqB3s84bbTf" | ||
|
||
|
||
async def main(): | ||
start_time = time.time() | ||
with TulipAPI( | ||
"abc.tulip.co", | ||
concurrency=concurrency, | ||
) as api: | ||
uploaded_records = await TulipTableCSVUploader( | ||
TulipTable(api, table_id), filename | ||
).execute(create_random_id=True, warn_on_failure=True) | ||
print( | ||
f"Uploaded {uploaded_records} to table {table_id} in {round(time.time() - start_time, 2)} seconds." | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
set -e | ||
cd "$(dirname "${BASH_SOURCE[0]}")" | ||
|
||
cd .. | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
|
||
pip3 install -r dev-requirements.txt >/dev/null | ||
echo "Installed DEV-REQUIREMENTS..." | ||
|
||
rm -rf -f dist | ||
|
||
python3 -m build >/dev/null | ||
echo "Built community-tulip-api package." | ||
|
||
cd dist | ||
|
||
pip3 uninstall community-tulip-api -y >/dev/null | ||
echo "Uninstalled existing community-tulip-api installation." | ||
|
||
pip3 install $(ls -AU | head -1) >/dev/null | ||
echo "Installed new community-tulip-api build." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .cached_tulip_table import CachedTulipTable | ||
from .tulip_api import TulipAPI | ||
from .tulip_machine import TulipMachine | ||
from .tulip_table import TulipTable | ||
from .tulip_table_csv_upload import TulipTableCSVUploader | ||
from .tulip_table_link import TulipTableLink | ||
from tulip_api.cached_tulip_table import CachedTulipTable | ||
from tulip_api.tulip_api import TulipAPI | ||
from tulip_api.tulip_machine import TulipMachine | ||
from tulip_api.tulip_table import TulipTable | ||
from tulip_api.tulip_table_csv_upload import TulipTableCSVUploader | ||
from tulip_api.tulip_table_link import TulipTableLink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
from .tulip_api import TulipAPI | ||
|
||
# from .tulip_machine import TulipMachine | ||
from .tulip_table import TulipTable | ||
|
||
# from .tulip_table_link import TulipTableLink | ||
from tulip_api.asyncio.tulip_api import TulipAPI | ||
from tulip_api.asyncio.tulip_table import TulipTable | ||
from tulip_api.asyncio.tulip_table_csv_upload import TulipTableCSVUploader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Any, Dict | ||
|
||
from tulip_api.asyncio.tulip_api import TulipAPI | ||
|
||
|
||
class TulipMachine: | ||
""" | ||
An interface with the machine event reporting api. | ||
""" | ||
|
||
attributes_report_path: str = "attributes/report" | ||
|
||
def __init__(self, tulip_api: TulipAPI, machine_id: str): | ||
self.tulip_api = tulip_api | ||
self.machine_id = machine_id | ||
|
||
async def send_event(self, attributes: Dict[str, Any]): | ||
""" | ||
POST `/attributes/report` | ||
`attributes`: A dict with attributeId: value pairs. | ||
``` | ||
{ | ||
"attributeIdA": "valueA", | ||
"attributeIdB": "valueB", | ||
} | ||
``` | ||
""" | ||
await self.tulip_api.make_request_expect_nothing( | ||
TulipMachine.attributes_report_path, | ||
"POST", | ||
json=self._construct_attributes(attributes), | ||
) | ||
|
||
def _construct_attributes(self, attributes: Dict[str, Any]): | ||
return { | ||
"attributes": [ | ||
{"machineId": self.machine_id, "attributeId": key, "value": value} | ||
for key, value in attributes.items() | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from csv import DictReader | ||
from typing import Any, Dict, Iterable, TextIO, Union | ||
|
||
from dateutil import parser | ||
|
||
from tulip_api.asyncio.tulip_table import TulipTable | ||
|
||
|
||
class TulipTableCSVUploader: | ||
tulip_table: TulipTable | ||
|
||
def __init__(self, tulip_table: TulipTable, csv_file: Union[str, TextIO]): | ||
self.tulip_table = tulip_table | ||
self.csv_file = csv_file | ||
|
||
async def execute(self, create_random_id=False, warn_on_failure=False) -> int: | ||
if isinstance(self.csv_file, str): | ||
with open(self.csv_file, "r") as csv_file: | ||
return await self._upload_records( | ||
csv_file, | ||
create_random_id=create_random_id, | ||
warn_on_failure=warn_on_failure, | ||
) | ||
|
||
return await self._upload_records( | ||
self.csv_file, | ||
create_random_id=create_random_id, | ||
warn_on_failure=warn_on_failure, | ||
) | ||
|
||
async def _upload_records( | ||
self, file: TextIO, create_random_id=False, warn_on_failure=False | ||
): | ||
table_columns = (await self.tulip_table.get_details())["columns"] | ||
column_types = { | ||
column["name"]: column["dataType"]["type"] for column in table_columns | ||
} | ||
reader = DictReader(file) | ||
TulipTableCSVUploader._validate_csv_columns(reader.fieldnames, column_types) | ||
|
||
return await self.tulip_table.create_records( | ||
TulipTableCSVUploader._yield_coerced_records(reader, column_types), | ||
warn_on_failure=warn_on_failure, | ||
create_random_id=create_random_id, | ||
) | ||
|
||
@staticmethod | ||
def _coerce_type(value: Any, type: str): | ||
if type == "string": | ||
return str(value) | ||
if type == "integer": | ||
if isinstance(value, str): | ||
return int(float(value)) | ||
return int(value) | ||
if type == "float": | ||
return float(value) | ||
if type == "boolean": | ||
return bool(value) | ||
if type == "timestamp": | ||
return parser.parse(value, ignoretz=True).strftime("%Y-%m-%dT%H:%M:%SZ") | ||
|
||
raise Exception("Unsupported datatype: {type}. Value: {value}") | ||
|
||
@staticmethod | ||
def _coerce_record_types(record: Dict[str, Any], column_types: Dict[str, str]): | ||
new_record = {} | ||
for column_id, value in record.items(): | ||
if column_id not in column_types: | ||
raise Exception( | ||
f"Column {column_id} found in record, but not in table." | ||
) | ||
new_record[column_id] = TulipTableCSVUploader._coerce_type( | ||
value, column_types[column_id] | ||
) | ||
return new_record | ||
|
||
@staticmethod | ||
def _yield_coerced_records(records: Iterable, column_types: Dict[str, str]): | ||
for record in records: | ||
yield TulipTableCSVUploader._coerce_record_types(record, column_types) | ||
|
||
@staticmethod | ||
def _validate_csv_columns(csv_fieldnames, table_columns): | ||
for csv_fieldname in csv_fieldnames: | ||
if not csv_fieldname in table_columns: | ||
raise Exception( | ||
f"Column {csv_fieldname} is not found in the Tulip Table columns." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.