Skip to content
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

feat: get users tasks & submitted transactions #289

Merged
merged 4 commits into from
Jul 9, 2024
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
56 changes: 55 additions & 1 deletion autotx/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,35 @@ def get_all(self) -> list[models.Task]:

return tasks

def get_from_user(self, app_user_id: str) -> list[models.Task]:
client = get_db_client("public")

result = client.table("tasks").select("*").eq("app_id", self.app_id).eq("app_user_id", app_user_id).execute()

tasks = []

for task_data in result.data:
tasks.append(
models.Task(
id=task_data["id"],
app_user_id=task_data["app_user_id"],
prompt=task_data["prompt"],
address=task_data["address"],
chain_id=task_data["chain_id"],
created_at=task_data["created_at"],
updated_at=task_data["updated_at"],
running=task_data["running"],
error=task_data["error"],
messages=json.loads(task_data["messages"]),
logs=[models.TaskLog(**log) for log in json.loads(task_data["logs"])] if task_data["logs"] else None,
intents=[load_intent(intent) for intent in json.loads(task_data["intents"])],
previous_task_id=task_data["previous_task_id"],
feedback=task_data["feedback"]
)
)

return tasks

def get_app_by_api_key(api_key: str) -> models.App | None:
client = get_db_client("public")

Expand Down Expand Up @@ -288,7 +317,32 @@ def get_transactions(app_id: str, app_user_id: str, task_id: str, address: str,
[TransactionBase(**tx) for tx in json.loads(result.data[0]["transactions"])],
result.data[0]["task_id"]
)


def get_submitted_transactions_from_user(
app_id: str,
app_user_id: str,
) -> list[list[TransactionBase]]:
client = get_db_client("public")
result = (
client.table("submitted_batches")
.select("transactions")
.eq("app_id", app_id)
.eq("app_user_id", app_user_id)
.not_.is_("submitted_on", "null")
.execute()
)

if len(result.data) == 0:
return []

submitted_batches = []
for batch in result.data:
submitted_batches.append(
[TransactionBase(**tx) for tx in json.loads(batch["transactions"])]
)

return submitted_batches

def submit_transactions(app_id: str, app_user_id: str, submitted_batch_id: str) -> None:
client = get_db_client("public")

Expand Down
23 changes: 23 additions & 0 deletions autotx/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ def get_task(task_id: str, authorization: Annotated[str | None, Header()] = None
task = get_task_or_404(task_id, tasks)
return task

@app_router.get("/api/v1/tasks/user/{user_id}")
def get_user_tasks(
user_id: str, authorization: Annotated[str | None, Header()] = None
) -> dict[str, list[Any]]:
(app, app_user) = authorize_app_and_user(authorization, user_id)
tasks = db.TasksRepository(app_id=app.id).get_from_user(app_user.id)
user_tasks = [
{
"id": task.id,
"prompt": task.prompt,
"address": task.address,
"chain_id": task.chain_id,
"intents": task.intents,
}
for task in tasks
]
user_submitted_transactions = [
batch for batch in db.get_submitted_transactions_from_user(app.id, app_user.id)
]

return { "tasks": user_tasks, "submitted_transactions": user_submitted_transactions }


@app_router.get("/api/v1/tasks/{task_id}/intents", response_model=List[Intent])
def get_intents(task_id: str, authorization: Annotated[str | None, Header()] = None) -> Any:
app = authorize(authorization)
Expand Down
4 changes: 2 additions & 2 deletions autotx/tests/api/test_send_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ def test_send_transactions():
assert batches[1].created_at is not None
assert batches[1].submitted_on is None

assert batch1["batch_id"] == batches[0].id
assert batch1["batch_id"] in [batch.id for batch in batches]
assert len(batch1["transactions"]) == 1

assert batch2["batch_id"] == batches[1].id
assert batch2["batch_id"] in [batch.id for batch in batches]
assert len(batch2["transactions"]) == 1

response = client.post(f"/api/v1/tasks/{task_id}/transactions", params={
Expand Down
Loading