|
| 1 | +"""This module contains the application class.""" |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from datetime import datetime |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from nextmv.base_model import BaseModel |
| 8 | +from nextmv.cloud.client import Client |
| 9 | + |
| 10 | + |
| 11 | +class Metadata(BaseModel): |
| 12 | + """Metadata of a run, whether it was successful or not.""" |
| 13 | + |
| 14 | + status: str |
| 15 | + """Status of the run.""" |
| 16 | + created_at: datetime |
| 17 | + """Date and time when the run was created.""" |
| 18 | + duration: float |
| 19 | + """Duration of the run in milliseconds.""" |
| 20 | + input_size: float |
| 21 | + """Size of the input in bytes.""" |
| 22 | + output_size: float |
| 23 | + """Size of the output in bytes.""" |
| 24 | + error: str |
| 25 | + """Error message if the run failed.""" |
| 26 | + application_id: str |
| 27 | + """ID of the application where the run was submitted to.""" |
| 28 | + application_instance_id: str |
| 29 | + """ID of the instance where the run was submitted to.""" |
| 30 | + application_version_id: str |
| 31 | + """ID of the version of the application where the run was submitted to.""" |
| 32 | + |
| 33 | + |
| 34 | +class RunResult(BaseModel): |
| 35 | + """Result of a run, wheter it was successful or not.""" |
| 36 | + |
| 37 | + id: str |
| 38 | + """ID of the run.""" |
| 39 | + user_email: str |
| 40 | + """Email of the user who submitted the run.""" |
| 41 | + name: str |
| 42 | + """Name of the run.""" |
| 43 | + description: str |
| 44 | + """Description of the run.""" |
| 45 | + metadata: Metadata |
| 46 | + """Metadata of the run.""" |
| 47 | + output: dict[str, Any] |
| 48 | + """Output of the run.""" |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class Application: |
| 53 | + """An application is a published decision model that can be executed.""" |
| 54 | + |
| 55 | + client: Client |
| 56 | + """Client to use for interacting with the Nextmv Cloud API.""" |
| 57 | + id: str |
| 58 | + """ID of the application.""" |
| 59 | + endpoint: str = "v1/applications/{id}" |
| 60 | + """Base endpoint for the application.""" |
| 61 | + default_instance_id: str = "devint" |
| 62 | + """Default instance ID to use for submitting runs.""" |
| 63 | + |
| 64 | + def __post_init__(self): |
| 65 | + """Logic to run after the class is initialized.""" |
| 66 | + |
| 67 | + self.endpoint = self.endpoint.format(id=self.id) |
| 68 | + |
| 69 | + def new_run( |
| 70 | + self, |
| 71 | + input: dict[str, Any] = None, |
| 72 | + instance_id: str | None = None, |
| 73 | + name: str | None = None, |
| 74 | + description: str | None = None, |
| 75 | + upload_id: str | None = None, |
| 76 | + options: dict[str, Any] | None = None, |
| 77 | + ) -> str: |
| 78 | + """ |
| 79 | + Submit an input to start a new run of the application. Returns the |
| 80 | + run_id of the submitted run. |
| 81 | +
|
| 82 | + Args: |
| 83 | + input: Input to use for the run. |
| 84 | + instance_id: ID of the instance to use for the run. If not |
| 85 | + provided, the default_instance_id will be used. |
| 86 | + name: Name of the run. |
| 87 | + description: Description of the run. |
| 88 | + upload_id: ID to use when running a large input. |
| 89 | + options: Options to use for the run. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + ID of the submitted run. |
| 93 | + """ |
| 94 | + |
| 95 | + payload = {} |
| 96 | + if input is not None: |
| 97 | + payload["input"] = input |
| 98 | + if name is not None: |
| 99 | + payload["name"] = name |
| 100 | + if description is not None: |
| 101 | + payload["description"] = description |
| 102 | + if upload_id is not None: |
| 103 | + payload["upload_id"] = upload_id |
| 104 | + if options is not None: |
| 105 | + payload["options"] = options |
| 106 | + |
| 107 | + query_params = { |
| 108 | + "instance_id": instance_id if instance_id is not None else self.default_instance_id, |
| 109 | + } |
| 110 | + response = self.client.post( |
| 111 | + endpoint=f"{self.endpoint}/runs", |
| 112 | + payload=payload, |
| 113 | + query_params=query_params, |
| 114 | + ) |
| 115 | + |
| 116 | + return response.json()["run_id"] |
| 117 | + |
| 118 | + def run_result( |
| 119 | + self, |
| 120 | + run_id: str, |
| 121 | + ) -> RunResult: |
| 122 | + """ |
| 123 | + Get the result of a run. |
| 124 | +
|
| 125 | + Args: |
| 126 | + run_id: ID of the run. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + Result of the run. |
| 130 | + """ |
| 131 | + |
| 132 | + response = self.client.get( |
| 133 | + endpoint=f"{self.endpoint}/runs/{run_id}", |
| 134 | + ) |
| 135 | + |
| 136 | + return RunResult.from_dict(response.json()) |
0 commit comments