kalbio 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kalbio/dashboards.py ADDED
@@ -0,0 +1,276 @@
1
+ """
2
+ Dashboards module for the Kaleidoscope system.
3
+
4
+ This module provides classes and services for working with dashboards in Kaleidoscope.
5
+ Dashboards summarize data across a workspace in some way, allowing for data comparison,
6
+ status review, and more.
7
+
8
+ Classes:
9
+ Dashboard: Represents a single dashboard with its categories and configurations.
10
+ DashboardsService: Service class for managing and querying dashboards.
11
+
12
+ Example:
13
+ ```python
14
+ # get all dashboards
15
+ dashboards = client.dashboards.get_dashboards()
16
+ ```
17
+ """
18
+
19
+ import logging
20
+ from functools import lru_cache
21
+ from kalbio._kaleidoscope_model import _KaleidoscopeBaseModel
22
+ from kalbio.client import KaleidoscopeClient
23
+ from pydantic import TypeAdapter
24
+ from typing import List, Literal, Union
25
+
26
+ _logger = logging.getLogger(__name__)
27
+
28
+ type DashboardType = Union[
29
+ Literal["decision"],
30
+ Literal["data"],
31
+ Literal["chart"],
32
+ Literal["field"],
33
+ Literal["summary"],
34
+ ]
35
+ """Type alias representing the valid types of dashboards in the system.
36
+
37
+ This type defines the allowed string values for the `dashboard_type` field
38
+ in Dashboard models.
39
+ """
40
+
41
+
42
+ class DashboardCategory(_KaleidoscopeBaseModel):
43
+ """Represents the definition of a DashboardCategory in the Kaleidoscope system.
44
+
45
+ A DashboardCategory defines a summary and aggregation for entities and sets in that
46
+ dashboard.
47
+
48
+ Attributes:
49
+ id (str): UUID of the Dashboard Category.
50
+ dashboard_id (str): The dashboard this category is a part of.
51
+ category_name (str): The name of the category.
52
+ operation_definition_ids (List[str]): The operation activity definitions reflected in this category.
53
+ label_ids (List[List[str]]): The labels reflected in this category.
54
+ field_ids (List[str]): The fields reflected in this category.
55
+ """
56
+
57
+ dashboard_id: str
58
+ category_name: str
59
+ operation_definition_ids: List[str]
60
+ label_ids: List[List[str]]
61
+ field_ids: List[str]
62
+
63
+ def __str__(self):
64
+ return f"{self.id}:{self.category_name}"
65
+
66
+
67
+ class Dashboard(_KaleidoscopeBaseModel):
68
+ """Represents a dashboard in the Kaleidoscope system.
69
+
70
+ A Dashboard represents an aggregation and summarization of the state of a workspace with respect
71
+ to both entity data and activity.
72
+
73
+ Attributes:
74
+ id (str): UUID of the dashboard.
75
+ dashboard_name (str): The name of the dashboard.
76
+ dashboard_description (str): The description of the dashboard.
77
+ dashboard_type (DashboardType): The type of the dashboard, representing how it aggregates data.
78
+ record_ids (List[str]): List of record IDs associated with the dashboard.
79
+ record_set_ids (List[str]): List of record set IDs associated with the dashboard.
80
+ """
81
+
82
+ dashboard_name: str
83
+ dashboard_description: str
84
+ dashboard_type: DashboardType
85
+ record_ids: List[str]
86
+ record_set_ids: List[str]
87
+
88
+ def __str__(self):
89
+ return f"{self.dashboard_name}"
90
+
91
+ def add_category(
92
+ self,
93
+ category_name: str,
94
+ operation_definition_ids: List[str],
95
+ label_ids: List[List[str]],
96
+ field_ids: List[str],
97
+ ) -> DashboardCategory | None:
98
+ """Create a new dashboard category on this dashboard.
99
+
100
+ Args:
101
+ category_name (str): The name of the new category.
102
+ operation_definition_ids (List[str]): A list of operation definition IDs to include in the category.
103
+ label_ids (List[List[str]]): A list of label IDs to include in the category.
104
+ field_ids (List[str]): A list of field IDs to include in the category.
105
+
106
+ Returns:
107
+ (DashboardCategory | None): The newly created category object, or None if creation failed.
108
+ """
109
+ try:
110
+ data = data = {
111
+ "category_name": category_name,
112
+ "operation_definition_ids": operation_definition_ids,
113
+ "label_ids": label_ids,
114
+ "field_ids": field_ids,
115
+ }
116
+ resp = self._client._post(
117
+ f"/dashboards/{self.id}/categories",
118
+ data,
119
+ )
120
+ return resp
121
+ except Exception as e:
122
+ _logger.error(f"Error creating a category for this dashboard: {e}")
123
+ return None
124
+
125
+ def remove_category(self, category_id: str):
126
+ """Remove a category from the dashboard.
127
+
128
+ Args:
129
+ category_id (str): The unique identifier of the category to be removed.
130
+ """
131
+ try:
132
+ self._client._delete(
133
+ f"/dashboards/{self.id}/categories/{category_id}",
134
+ )
135
+ except Exception as e:
136
+ _logger.error(f"Error removing this category: {e}")
137
+ return
138
+
139
+ def get_categories(self) -> List[DashboardCategory]:
140
+ """Retrieve all categories associated with this dashboard.
141
+
142
+ Returns:
143
+ List[DashboardCategory]: A list of DashboardCategory objects associated with this dashboard.
144
+ """
145
+ try:
146
+ resp = self._client._get(
147
+ f"/dashboards/{self.id}/categories",
148
+ )
149
+ return resp
150
+ except Exception as e:
151
+ _logger.error(f"Error fetching categories of this dashboard: {e}")
152
+ return []
153
+
154
+ def add_record(self, record_id: str):
155
+ """Add a record to the dashboard.
156
+
157
+ Args:
158
+ record_id (str): The unique identifier of the record to be added.
159
+ """
160
+ try:
161
+ data = {"record_id": record_id}
162
+ resp = self._client._post(f"/dashboards/{self.id}/records", data)
163
+ if resp:
164
+ self.record_ids = resp.record_ids
165
+ except Exception as e:
166
+ _logger.error(f"Error adding record to this dashboard: {e}")
167
+ return
168
+
169
+ def remove_record(self, record_id: str):
170
+ """Remove a record from the dashboard.
171
+
172
+ Args:
173
+ record_id (str): The unique identifier of the record to be removed.
174
+ """
175
+ try:
176
+ self._client._delete(
177
+ f"/dashboards/{self.id}/records/{record_id}",
178
+ )
179
+ except Exception as e:
180
+ _logger.error(f"Error removing this record: {e}")
181
+ return
182
+
183
+ def add_set(self, set_id):
184
+ """Add a set to the dashboard.
185
+
186
+ Args:
187
+ set_id (str): The unique identifier of the set to be added.
188
+ """
189
+ try:
190
+ data = {"set_id": set_id}
191
+ resp = self._client._post(f"/dashboards/{self.id}/sets", data)
192
+ if resp:
193
+ self.record_set_ids = resp.record_set_id
194
+ except Exception as e:
195
+ _logger.error(f"Error adding set to this dashboard: {e}")
196
+ return
197
+
198
+ def remove_set(self, set_id: str):
199
+ """Remove a set from the dashboard.
200
+
201
+ Args:
202
+ set_id (str): The unique identifier of the set to be removed.
203
+ """
204
+ try:
205
+ resp = self._client._delete(
206
+ f"/dashboards/{self.id}/sets/{set_id}",
207
+ )
208
+ if resp:
209
+ self.record_set_ids = resp.record_set_id
210
+ except Exception as e:
211
+ _logger.error(f"Error removing this set: {e}")
212
+ return
213
+
214
+
215
+ class DashboardsService:
216
+ """Service class for managing and retrieving dashboards from the Kaleidoscope API.
217
+
218
+ This service provides methods to fetch dashboards. It handles the conversion of raw API responses
219
+ into validated Dashboard objects.
220
+
221
+ Attributes:
222
+ client (KaleidoscopeClient): The Kaleidoscope client instance used for API communication.
223
+
224
+ Example:
225
+ ```python
226
+ client = KaleidoscopeClient(...)
227
+ dashboards = client.dashboards.get_dashboards()
228
+ ```
229
+ """
230
+
231
+ def __init__(self, client: KaleidoscopeClient):
232
+ self._client = client
233
+
234
+ def _create_dashboard(self, data: dict) -> Dashboard:
235
+ """Create a Dashboard instance from the provided data dictionary.
236
+
237
+ Args:
238
+ data (dict): A dictionary containing the data required to instantiate a Dashboard.
239
+
240
+ Returns:
241
+ Dashboard: The validated and initialized Dashboard instance.
242
+ """
243
+ dashboard = TypeAdapter(Dashboard).validate_python(data)
244
+ dashboard._set_client(self._client)
245
+ return dashboard
246
+
247
+ def _create_dashboard_list(self, data: list[dict]) -> List[Dashboard]:
248
+ """Convert a list of dashboard data dictionaries into a list of Dashboard objects.
249
+
250
+ Args:
251
+ data (list[dict]): The input data representing dashboards.
252
+
253
+ Returns:
254
+ List[Dashboard]: A list of Dashboard instances with the client set.
255
+ """
256
+ dashboards = TypeAdapter(List[Dashboard]).validate_python(data)
257
+
258
+ for dashboard in dashboards:
259
+ dashboard._set_client(self._client)
260
+
261
+ return dashboards
262
+
263
+ @lru_cache
264
+ def get_dashboards(self) -> List[Dashboard]:
265
+ """Retrieve a list of dashboards from the client.
266
+
267
+ Returns:
268
+ List[Dashboard]: A list of Dashboard objects created from the response.
269
+ """
270
+ try:
271
+ resp = self._client._get("/dashboards")
272
+ return self._create_dashboard_list(resp)
273
+ except Exception as e:
274
+ _logger.error(f"Error fetching dashboards: {e}")
275
+ self.get_dashboards.cache_clear()
276
+ return []