Skip to content

Commit 882d87f

Browse files
committed
feat(dashboards): add ai dashboard generate and run user listing
1 parent 9c42451 commit 882d87f

6 files changed

Lines changed: 381 additions & 4 deletions

File tree

‎docs/source/Resources/Dashboards/Dashboard_Type.rst‎

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ DashboardCreateInfo
5151
| **visible**: bool
5252
| Dashboard visibility status
5353
54+
| *Optional* **type**: "dashboard" or "blueprint" or "ai" or "custom"
55+
| Dashboard type (default: "dashboard")
56+
57+
| *Optional* **per_user**: bool
58+
| Gives each run user an individual dashboard. Only valid on "ai" dashboards
59+
60+
| *Optional* **ai_instructions**: Optional[str]
61+
| Rules for the AI generator, up to 10,000 characters
62+
5463

5564
.. _icon:
5665

@@ -198,9 +207,15 @@ DashboardInfo
198207
| **background**: any
199208
| Background configuration
200209
201-
| **type**: str
210+
| **type**: "dashboard" or "blueprint" or "ai" or "custom"
202211
| Dashboard type
203212
213+
| **per_user**: bool
214+
| Whether each run user gets an individual dashboard. Only valid on "ai" dashboards
215+
216+
| **ai_instructions**: Optional[str]
217+
| Rules for the AI generator, up to 10,000 characters
218+
204219
| **blueprint_device_behavior**: "more_than_one" or "always"
205220
| Blueprint device behavior mode
206221
@@ -526,4 +541,86 @@ widgetOverwriteOptions
526541

527542
**Type:**
528543

529-
| Literal["start_date", "end_date", "timezone"]
544+
| Literal["start_date", "end_date", "timezone"]
545+
546+
.. _GenerateHistoryEntry:
547+
548+
GenerateHistoryEntry
549+
--------------------
550+
551+
One earlier turn of the generation conversation.
552+
553+
**Attributes:**
554+
555+
| **role**: "user" or "assistant"
556+
| Who sent the message
557+
558+
| **content**: str
559+
| Message text
560+
561+
562+
.. _GenerateParams:
563+
564+
GenerateParams
565+
--------------
566+
567+
Body for an AI dashboard generation.
568+
569+
**Attributes:**
570+
571+
| **prompt**: str
572+
| What to build or change
573+
574+
| *Optional* **history**: list[:ref:`GenerateHistoryEntry`]
575+
| Earlier turns of the conversation
576+
577+
| *Optional* **run_user**: :ref:`GenericID`
578+
| Run user whose individual dashboard to build or edit. Requires ``per_user``
579+
580+
581+
.. _GenerateResponse:
582+
583+
GenerateResponse
584+
----------------
585+
586+
**Attributes:**
587+
588+
| **id**: :ref:`GenericID`
589+
| Dashboard identifier
590+
591+
592+
.. _RunUsersQuery:
593+
594+
RunUsersQuery
595+
-------------
596+
597+
**Attributes:**
598+
599+
| *Optional* **search**: str
600+
| Matches name or email
601+
602+
| *Optional* **page**: int
603+
| Page number, starting at 1
604+
605+
| *Optional* **amount**: int
606+
| Rows per page, default and maximum 1000
607+
608+
609+
.. _DashboardRunUser:
610+
611+
DashboardRunUser
612+
----------------
613+
614+
**Attributes:**
615+
616+
| **id**: :ref:`GenericID`
617+
| Run user identifier
618+
619+
| **name**: Optional[str]
620+
| Run user name
621+
622+
| **email**: Optional[str]
623+
| Run user email
624+
625+
| **has_dashboard**: bool
626+
| Whether the run user already has an individual dashboard

‎docs/source/Resources/Dashboards/index.rst‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,74 @@ See: `Dashboard Overview <https://docs.tago.io/docs/tagoio/dashboards/>`_
296296
print(analyses) # [{'id': 'analysis-id-123', 'name': 'Analysis #1'}, ...]
297297
298298
299+
========
300+
generate
301+
========
302+
303+
Builds or edits an AI dashboard from a prompt. Without ``run_user``, it changes the dashboard's own setup. With ``run_user``, it changes that run user's individual dashboard, which requires ``per_user`` on the AI dashboard. A generation takes 15 to 60 seconds, so this call waits up to 180 seconds. The server answers 429 when the profile already runs 3 generations or the dashboard is already generating.
304+
305+
See: `Dashboard Overview <https://docs.tago.io/docs/tagoio/dashboards/>`_
306+
307+
**Parameters:**
308+
309+
| **dashboardID**: :ref:`GenericID`
310+
| AI dashboard identifier
311+
312+
| **params**: :ref:`GenerateParams`
313+
| Prompt, optional conversation history, and optional run user ID
314+
315+
**Returns:**
316+
317+
| :ref:`GenerateResponse`
318+
| The dashboard ID
319+
320+
.. code-block:: python
321+
322+
# If receive an error "Authorization Denied", check policy "Dashboard" / "Edit" in Access Management.
323+
from tagoio_sdk import Resources
324+
325+
resources = Resources()
326+
result = resources.dashboards.generate("dashboard-id-123", {
327+
"prompt": "Add a chart with the temperature of the last 24 hours",
328+
"history": [
329+
{"role": "user", "content": "Show the current humidity"},
330+
{"role": "assistant", "content": "Added a humidity display."},
331+
],
332+
})
333+
print(result) # {'id': 'dashboard-id-123'}
334+
335+
336+
============
337+
listRunUsers
338+
============
339+
340+
Lists the run users of an AI dashboard with ``per_user`` on, and whether each one already has an individual dashboard. Returns 404 when the dashboard is not an AI dashboard with ``per_user`` on.
341+
342+
See: `Dashboard Overview <https://docs.tago.io/docs/tagoio/dashboards/>`_
343+
344+
**Parameters:**
345+
346+
| **dashboardID**: :ref:`GenericID`
347+
| AI dashboard identifier
348+
349+
| *Optional* **queryObj**: :ref:`RunUsersQuery`
350+
| Search and pagination. ``search`` matches name or email; ``page`` starts at 1; ``amount`` defaults to 1000, the maximum.
351+
352+
**Returns:**
353+
354+
| list[:ref:`DashboardRunUser`]
355+
| Run users with their individual dashboard status
356+
357+
.. code-block:: python
358+
359+
# If receive an error "Authorization Denied", check policy "Dashboard" / "Access" in Access Management.
360+
from tagoio_sdk import Resources
361+
362+
resources = Resources()
363+
users = resources.dashboards.listRunUsers("dashboard-id-123", {"search": "john"})
364+
print(users) # [{'id': 'user-id-123', 'name': 'John', 'email': 'john@example.com', 'has_dashboard': True}]
365+
366+
299367
=============================
300368
runWidgetHeaderButtonAnalysis
301369
=============================

‎src/tagoio_sdk/infrastructure/api_request.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class RequestParams(TypedDict):
2121
body: Optional[any]
2222
params: Optional[any]
2323
headers: Optional[any]
24+
timeout: Optional[float]
2425

2526

2627
class TagoIORequestError(Exception):
@@ -87,7 +88,8 @@ def request() -> ResultHandlerResponse:
8788
url=url,
8889
data=dataBody,
8990
params=requestParams.get("params"),
90-
timeout=config.tagoSDKconfig["requestTimeout"],
91+
timeout=requestParams.get("timeout")
92+
or config.tagoSDKconfig["requestTimeout"],
9193
)
9294
)
9395

‎src/tagoio_sdk/modules/Resources/Dashboards.py‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@
1212
from tagoio_sdk.modules.Resources.Dashboards_Type import AnalysisRelated
1313
from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardCreateInfo
1414
from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardInfo
15+
from tagoio_sdk.modules.Resources.Dashboards_Type import DashboardRunUser
1516
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
1619
from tagoio_sdk.modules.Resources.Dashboards_Type import PublicKeyResponse
20+
from tagoio_sdk.modules.Resources.Dashboards_Type import RunUsersQuery
1721
from tagoio_sdk.modules.Utils.dateParser import dateParser
1822
from tagoio_sdk.modules.Utils.dateParser import dateParserList
1923

2024

25+
# ? AI generation takes 15 to 60 seconds, longer than the default request timeout.
26+
GENERATE_REQUEST_TIMEOUT = 180 # seconds
27+
28+
2129
class Dashboards(TagoIOModule):
2230
def listDashboard(self, queryObj: Optional[Query] = None) -> List[DashboardInfo]:
2331
"""
@@ -335,6 +343,78 @@ def runWidgetHeaderButtonAnalysis(
335343

336344
return result
337345

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+
338418
def __init__(self, params):
339419
super().__init__(params)
340420
self.widgets = Widgets(params)

‎src/tagoio_sdk/modules/Resources/Dashboards_Type.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ class Arrangement(TypedDict):
2020
tab: Optional[str]
2121

2222

23+
DashboardType = Literal["dashboard", "blueprint", "ai", "custom"]
24+
25+
2326
class DashboardCreateInfo(TypedDict):
2427
label: str
2528
arrangement: list[Arrangement]
2629
tags: list[TagsObj]
2730
visible: bool
31+
type: Optional[DashboardType]
32+
per_user: Optional[bool]
33+
ai_instructions: Optional[str]
2834

2935

3036
class icon(TypedDict):
@@ -72,7 +78,9 @@ class DashboardInfo(TypedDict):
7278
tabs: list
7379
icon: icon
7480
background: any
75-
type: str
81+
type: DashboardType
82+
per_user: bool
83+
ai_instructions: Optional[str]
7684
blueprint_device_behavior: Literal["more_than_one", "always"]
7785
blueprint_selector_behavior: Literal["open", "closed", "always_open", "always_closed"]
7886
blueprint_devices: BlueprintDeviceConfig
@@ -176,6 +184,34 @@ class GetDataModel(TypedDict):
176184
amount: Optional[Union[int, float]]
177185

178186

187+
class GenerateHistoryEntry(TypedDict):
188+
role: Literal["user", "assistant"]
189+
content: str
190+
191+
192+
class GenerateParams(TypedDict, total=False):
193+
prompt: str
194+
history: list[GenerateHistoryEntry]
195+
run_user: GenericID
196+
197+
198+
class GenerateResponse(TypedDict):
199+
id: GenericID
200+
201+
202+
class RunUsersQuery(TypedDict, total=False):
203+
search: str
204+
page: int
205+
amount: int
206+
207+
208+
class DashboardRunUser(TypedDict):
209+
id: GenericID
210+
name: Optional[str]
211+
email: Optional[str]
212+
has_dashboard: bool
213+
214+
179215
class PublicKeyResponse(TypedDict):
180216
token: GenericToken
181217
expire_time: ExpireTimeOption

0 commit comments

Comments
 (0)