|
12 | 12 | from tagoio_sdk.modules.Resources.Dashboards_Type import AnalysisRelated |
13 | 13 | from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardCreateInfo |
14 | 14 | from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardInfo |
| 15 | +from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardRunUser |
15 | 16 | from tagoio_sdk.modules.Resources.Dashboards_Type import DevicesRelated |
| 17 | +from tagoio_sdk.modules.Resources.Dashboards_Type import GenerateParams |
| 18 | +from tagoio_sdk.modules.Resources.Dashboards_Type import GenerateResponse |
16 | 19 | from tagoio_sdk.modules.Resources.Dashboards_Type import PublicKeyResponse |
| 20 | +from tagoio_sdk.modules.Resources.Dashboards_Type import RunUsersQuery |
17 | 21 | from tagoio_sdk.modules.Utils.dateParser import dateParser |
18 | 22 | from tagoio_sdk.modules.Utils.dateParser import dateParserList |
19 | 23 |
|
20 | 24 |
|
| 25 | +# ? AI generation takes 15 to 60 seconds, longer than the default request timeout. |
| 26 | +GENERATE_REQUEST_TIMEOUT = 180 # seconds |
| 27 | + |
| 28 | + |
21 | 29 | class Dashboards(TagoIOModule): |
22 | 30 | def listDashboard(self, queryObj: Optional[Query] = None) -> List[DashboardInfo]: |
23 | 31 | """ |
@@ -335,6 +343,78 @@ def runWidgetHeaderButtonAnalysis( |
335 | 343 |
|
336 | 344 | return result |
337 | 345 |
|
| 346 | + def generate(self, dashboardID: GenericID, params: GenerateParams) -> GenerateResponse: |
| 347 | + """ |
| 348 | + @description: |
| 349 | + Builds or edits an AI dashboard from a prompt. Without `run_user`, it changes the |
| 350 | + dashboard's own setup. With `run_user`, it changes that run user's individual dashboard, |
| 351 | + which requires `per_user` on the AI dashboard. A generation takes 15 to 60 seconds. |
| 352 | + The server answers 429 when the profile already runs 3 generations or the dashboard |
| 353 | + is already generating. |
| 354 | +
|
| 355 | + @see: |
| 356 | + https://docs.tago.io/docs/tagoio/dashboards/ Dashboard Overview |
| 357 | +
|
| 358 | + @example: |
| 359 | + If receive an error "Authorization Denied", check policy **Dashboard** / **Edit** in Access Management. |
| 360 | + ```python |
| 361 | + resources = Resources() |
| 362 | + result = resources.dashboards.generate("dashboard-id-123", { |
| 363 | + "prompt": "Add a chart with the temperature of the last 24 hours", |
| 364 | + "history": [ |
| 365 | + {"role": "user", "content": "Show the current humidity"}, |
| 366 | + {"role": "assistant", "content": "Added a humidity display."}, |
| 367 | + ], |
| 368 | + }) |
| 369 | + print(result) # {'id': 'dashboard-id-123'} |
| 370 | + ``` |
| 371 | + """ |
| 372 | + result = self.doRequest( |
| 373 | + { |
| 374 | + "path": f"/dashboard/{dashboardID}/generate", |
| 375 | + "method": "POST", |
| 376 | + "body": params, |
| 377 | + "timeout": GENERATE_REQUEST_TIMEOUT, |
| 378 | + } |
| 379 | + ) |
| 380 | + |
| 381 | + return result |
| 382 | + |
| 383 | + def listRunUsers(self, dashboardID: GenericID, queryObj: Optional[RunUsersQuery] = None) -> List[DashboardRunUser]: |
| 384 | + """ |
| 385 | + @description: |
| 386 | + Lists the run users of an AI dashboard with `per_user` on, and whether each one |
| 387 | + already has an individual dashboard. `search` matches name or email. Returns 404 |
| 388 | + when the dashboard is not an AI dashboard with `per_user` on. |
| 389 | +
|
| 390 | + @see: |
| 391 | + https://docs.tago.io/docs/tagoio/dashboards/ Dashboard Overview |
| 392 | +
|
| 393 | + @example: |
| 394 | + If receive an error "Authorization Denied", check policy **Dashboard** / **Access** in Access Management. |
| 395 | + ```python |
| 396 | + resources = Resources() |
| 397 | + users = resources.dashboards.listRunUsers("dashboard-id-123", {"search": "john", "page": 1, "amount": 100}) |
| 398 | + print(users) # [{'id': 'user-id-123', 'name': 'John', 'email': 'john@example.com', 'has_dashboard': True}] |
| 399 | + ``` |
| 400 | + """ |
| 401 | + if queryObj is None: |
| 402 | + queryObj = {} |
| 403 | + |
| 404 | + result = self.doRequest( |
| 405 | + { |
| 406 | + "path": f"/dashboard/{dashboardID}/run-users", |
| 407 | + "method": "GET", |
| 408 | + "params": { |
| 409 | + "search": queryObj.get("search"), |
| 410 | + "page": queryObj.get("page") or 1, |
| 411 | + "amount": queryObj.get("amount") or 1000, |
| 412 | + }, |
| 413 | + } |
| 414 | + ) |
| 415 | + |
| 416 | + return result |
| 417 | + |
338 | 418 | def __init__(self, params): |
339 | 419 | super().__init__(params) |
340 | 420 | self.widgets = Widgets(params) |
0 commit comments