nufi-python 1.0.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.
Files changed (52) hide show
  1. nufi/__init__.py +52 -0
  2. nufi/auth.py +364 -0
  3. nufi/cli/__init__.py +0 -0
  4. nufi/cli/commands/__init__.py +1 -0
  5. nufi/cli/commands/auth.py +113 -0
  6. nufi/cli/commands/common.py +198 -0
  7. nufi/cli/commands/compiles.py +507 -0
  8. nufi/cli/commands/custom_evaluation_templates.py +206 -0
  9. nufi/cli/commands/datasets.py +697 -0
  10. nufi/cli/commands/evaluations.py +854 -0
  11. nufi/cli/commands/leaderboards.py +763 -0
  12. nufi/cli/commands/models.py +1347 -0
  13. nufi/cli/commands/monitoring.py +350 -0
  14. nufi/cli/commands/profile.py +123 -0
  15. nufi/cli/commands/projects.py +308 -0
  16. nufi/cli/commands/servings.py +306 -0
  17. nufi/cli/main.py +66 -0
  18. nufi/client.py +103 -0
  19. nufi/config.py +281 -0
  20. nufi/evaluator.py +559 -0
  21. nufi/exceptions.py +58 -0
  22. nufi/http_utils.py +51 -0
  23. nufi/models/__init__.py +185 -0
  24. nufi/models/compiles.py +79 -0
  25. nufi/models/custom_evaluation_templates.py +111 -0
  26. nufi/models/datasets.py +94 -0
  27. nufi/models/evaluations.py +253 -0
  28. nufi/models/leaderboards.py +178 -0
  29. nufi/models/monitoring.py +179 -0
  30. nufi/models/pagination.py +33 -0
  31. nufi/models/projects.py +32 -0
  32. nufi/models/registry.py +138 -0
  33. nufi/models/servings.py +27 -0
  34. nufi/py.typed +1 -0
  35. nufi/resources/__init__.py +34 -0
  36. nufi/resources/base.py +190 -0
  37. nufi/resources/compiles.py +193 -0
  38. nufi/resources/custom_evaluation_templates.py +126 -0
  39. nufi/resources/datasets.py +786 -0
  40. nufi/resources/evaluations.py +428 -0
  41. nufi/resources/leaderboards.py +413 -0
  42. nufi/resources/models.py +726 -0
  43. nufi/resources/monitoring.py +82 -0
  44. nufi/resources/projects.py +114 -0
  45. nufi/resources/servings.py +80 -0
  46. nufi/transport.py +248 -0
  47. nufi_python-1.0.0.dist-info/METADATA +228 -0
  48. nufi_python-1.0.0.dist-info/RECORD +52 -0
  49. nufi_python-1.0.0.dist-info/WHEEL +5 -0
  50. nufi_python-1.0.0.dist-info/entry_points.txt +2 -0
  51. nufi_python-1.0.0.dist-info/licenses/LICENSE +21 -0
  52. nufi_python-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,350 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+
5
+ import typer
6
+
7
+ from nufi.cli.commands import common
8
+
9
+ app = typer.Typer(
10
+ help="Read NuFi monitoring data.",
11
+ invoke_without_command=True,
12
+ context_settings=common.CONTEXT_SETTINGS,
13
+ )
14
+
15
+
16
+ @app.callback()
17
+ def monitoring(ctx: typer.Context):
18
+ common.show_help_if_no_subcommand(ctx)
19
+
20
+
21
+ @app.command("cluster")
22
+ def cluster(
23
+ duration: str = typer.Option("1h", "--duration", help="Time range."),
24
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
25
+ output: common.OutputChoice = typer.Option(
26
+ common.OutputChoice.table,
27
+ "--output",
28
+ help="Output format.",
29
+ ),
30
+ verbose: bool = typer.Option(False, "--verbose", help="Show detailed errors."),
31
+ ):
32
+ client = None
33
+ try:
34
+ client = common.make_client(profile_name)
35
+ cluster_result = client.monitoring.cluster(duration=duration)
36
+ device_infos = client.monitoring.devices()
37
+ device_nodes_by_type = _fetch_device_nodes(client, device_infos)
38
+ nodes_result = client.monitoring.nodes()
39
+ except Exception as exc:
40
+ common.handle_cli_error(exc, verbose=verbose)
41
+ finally:
42
+ if client is not None:
43
+ client.close()
44
+
45
+ if output == common.OutputChoice.json:
46
+ common.print_json(
47
+ {
48
+ "cluster": cluster_result,
49
+ "devices": device_infos,
50
+ "deviceNodes": device_nodes_by_type,
51
+ "nodes": nodes_result,
52
+ }
53
+ )
54
+ return
55
+
56
+ common.console.print("Overview")
57
+ _print_cluster_overview(cluster_result)
58
+ common.console.print("Devices")
59
+ _print_device_rows(_device_rows(device_infos, device_nodes_by_type))
60
+ common.console.print("Nodes")
61
+ _print_nodes(
62
+ nodes_result.nodes,
63
+ _device_type_counts(device_nodes_by_type),
64
+ )
65
+
66
+
67
+ @app.command("devices")
68
+ def devices(
69
+ node_name: str | None = typer.Option(None, "--node", help="Filter by node name."),
70
+ device_type: str | None = typer.Option(
71
+ None, "--type", help="Filter by device type."
72
+ ),
73
+ status: str | None = typer.Option(
74
+ None, "--status", help="Filter by device status."
75
+ ),
76
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
77
+ output: common.OutputChoice = typer.Option(
78
+ common.OutputChoice.table,
79
+ "--output",
80
+ help="Output format.",
81
+ ),
82
+ verbose: bool = typer.Option(False, "--verbose", help="Show detailed errors."),
83
+ ):
84
+ client = None
85
+ try:
86
+ client = common.make_client(profile_name)
87
+ device_infos = _filter_device_infos(client.monitoring.devices(), device_type)
88
+ device_nodes_by_type = _fetch_device_nodes(client, device_infos)
89
+ except Exception as exc:
90
+ common.handle_cli_error(exc, verbose=verbose)
91
+ finally:
92
+ if client is not None:
93
+ client.close()
94
+
95
+ device_nodes_by_type = _filter_device_nodes_by_type(
96
+ device_nodes_by_type,
97
+ node_name=node_name,
98
+ status=status,
99
+ )
100
+ rows = _device_rows(device_infos, device_nodes_by_type)
101
+ if output == common.OutputChoice.json:
102
+ common.print_json(
103
+ {
104
+ "devices": device_infos,
105
+ "deviceNodes": device_nodes_by_type,
106
+ }
107
+ )
108
+ return
109
+
110
+ _print_device_rows(rows)
111
+
112
+
113
+ @app.command("nodes")
114
+ def nodes(
115
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
116
+ output: common.OutputChoice = typer.Option(
117
+ common.OutputChoice.table,
118
+ "--output",
119
+ help="Output format.",
120
+ ),
121
+ verbose: bool = typer.Option(False, "--verbose", help="Show detailed errors."),
122
+ ):
123
+ client = None
124
+ try:
125
+ client = common.make_client(profile_name)
126
+ device_infos = client.monitoring.devices()
127
+ nodes_result = client.monitoring.nodes()
128
+ if device_infos:
129
+ device_nodes_by_type = _fetch_device_nodes(client, device_infos)
130
+ device_type_counts = _device_type_counts(device_nodes_by_type)
131
+ else:
132
+ device_type_counts = {}
133
+ except Exception as exc:
134
+ common.handle_cli_error(exc, verbose=verbose)
135
+ finally:
136
+ if client is not None:
137
+ client.close()
138
+
139
+ if output == common.OutputChoice.json:
140
+ common.print_json({"nodes": nodes_result, "deviceTypes": device_type_counts})
141
+ return
142
+
143
+ _print_nodes(nodes_result.nodes, device_type_counts)
144
+
145
+
146
+ def _print_nodes(
147
+ nodes: list[Any], device_type_counts: dict[str, dict[str, str]] | None = None
148
+ ) -> None:
149
+ device_type_counts = device_type_counts or {}
150
+ common.print_table(
151
+ [
152
+ "Node",
153
+ "CPU %",
154
+ "Memory %",
155
+ "Disk %",
156
+ "Net Rx",
157
+ "Net Tx",
158
+ "Devices",
159
+ "Device Types",
160
+ ],
161
+ [
162
+ [
163
+ node.name,
164
+ _fmt(node.cpu),
165
+ _fmt(node.memory),
166
+ _fmt(node.disk),
167
+ _metric_with_unit(node.network_rx, node.network_unit),
168
+ _metric_with_unit(node.network_tx, node.network_unit),
169
+ f"{node.devices_in_use}/{node.devices_total}",
170
+ _format_device_type_counts(device_type_counts.get(node.name, {})),
171
+ ]
172
+ for node in nodes
173
+ ],
174
+ width=140,
175
+ )
176
+
177
+
178
+ def _print_cluster_overview(result: Any) -> None:
179
+ overview = result.cluster_overview
180
+ common.print_table(
181
+ ["CPU %", "Memory %", "Disk %"],
182
+ [
183
+ [
184
+ _fmt(overview.cpu.usage),
185
+ _fmt(overview.memory.usage),
186
+ _fmt(overview.disk.usage),
187
+ ]
188
+ ],
189
+ )
190
+
191
+
192
+ def _print_device_rows(rows: list[dict[str, str]]) -> None:
193
+ common.print_table(
194
+ [
195
+ "Node",
196
+ "Type",
197
+ "Vendor",
198
+ "Resource",
199
+ "Status",
200
+ "Usage %",
201
+ "Memory",
202
+ "Temp",
203
+ "Power",
204
+ "Allocated To",
205
+ ],
206
+ [
207
+ [
208
+ row["node"],
209
+ row["type"],
210
+ row["vendor"],
211
+ row["resourceKey"],
212
+ row["status"],
213
+ row["usage"],
214
+ row["memory"],
215
+ row["temp"],
216
+ row["power"],
217
+ row["allocatedTo"],
218
+ ]
219
+ for row in rows
220
+ ],
221
+ width=180,
222
+ )
223
+
224
+
225
+ def _filter_device_infos(device_infos: list[Any], device_type: str | None) -> list[Any]:
226
+ if device_type is None:
227
+ return device_infos
228
+ return [item for item in device_infos if item.key == device_type]
229
+
230
+
231
+ def _fetch_device_nodes(client: Any, device_infos: list[Any]) -> dict[str, Any]:
232
+ return {item.key: client.monitoring.device_nodes(item.key) for item in device_infos}
233
+
234
+
235
+ def _filter_device_nodes_by_type(
236
+ device_nodes_by_type: dict[str, Any],
237
+ *,
238
+ node_name: str | None = None,
239
+ status: str | None = None,
240
+ ) -> dict[str, Any]:
241
+ if node_name is None and status is None:
242
+ return device_nodes_by_type
243
+
244
+ filtered = {}
245
+ for device_type, result in device_nodes_by_type.items():
246
+ result_copy = result.model_copy(deep=True)
247
+ nodes = []
248
+ total_usage = 0.0
249
+ total = 0
250
+ in_use = 0
251
+
252
+ for node in result_copy.nodes:
253
+ if node_name is not None and node.name != node_name:
254
+ continue
255
+ if status is not None:
256
+ node.devices = [
257
+ device for device in node.devices if device.status == status
258
+ ]
259
+ if not node.devices:
260
+ continue
261
+ node.devices_total = len(node.devices)
262
+ node.devices_in_use = sum(
263
+ 1 for device in node.devices if device.status == "in_use"
264
+ )
265
+ total += node.devices_total
266
+ in_use += node.devices_in_use
267
+ total_usage += sum(device.usage for device in node.devices)
268
+ nodes.append(node)
269
+
270
+ result_copy.nodes = nodes
271
+ result_copy.device_summary.total = total
272
+ result_copy.device_summary.in_use = in_use
273
+ result_copy.device_summary.free = total - in_use
274
+ result_copy.device_summary.avg_utilization = (
275
+ round(total_usage / total, 1) if total else 0
276
+ )
277
+ filtered[device_type] = result_copy
278
+ return filtered
279
+
280
+
281
+ def _device_type_counts(
282
+ device_nodes_by_type: dict[str, Any],
283
+ ) -> dict[str, dict[str, str]]:
284
+ counts: dict[str, dict[str, str]] = {}
285
+ for device_type, result in device_nodes_by_type.items():
286
+ for node in result.nodes:
287
+ counts.setdefault(node.name, {})[device_type] = (
288
+ f"{node.devices_in_use}/{node.devices_total}"
289
+ )
290
+ return counts
291
+
292
+
293
+ def _format_device_type_counts(counts: dict[str, str]) -> str:
294
+ return ", ".join(
295
+ f"{device_type} {count}" for device_type, count in sorted(counts.items())
296
+ )
297
+
298
+
299
+ def _device_rows(
300
+ device_infos: list[Any],
301
+ device_nodes_by_type: dict[str, Any],
302
+ *,
303
+ node_name: str | None = None,
304
+ status: str | None = None,
305
+ ) -> list[dict[str, str]]:
306
+ rows = []
307
+ for info in device_infos:
308
+ for node in device_nodes_by_type.get(info.key).nodes:
309
+ if node_name is not None and node.name != node_name:
310
+ continue
311
+ for device in node.devices:
312
+ if status is not None and device.status != status:
313
+ continue
314
+ rows.append(
315
+ {
316
+ "node": node.name,
317
+ "type": info.key,
318
+ "vendor": info.vendor,
319
+ "resourceKey": info.resource_key,
320
+ "status": device.status,
321
+ "usage": _fmt(device.usage),
322
+ "memory": f"{_fmt(device.memory_used)}/{_fmt(device.memory_total)}",
323
+ "temp": _fmt(device.temp),
324
+ "power": _fmt(device.power),
325
+ "allocatedTo": _allocated_to(device),
326
+ }
327
+ )
328
+ return rows
329
+
330
+
331
+ def _allocated_to(device: Any) -> str:
332
+ if device.allocation is not None:
333
+ return f"{device.allocation.resource_type}/{device.allocation.resource_name}"
334
+ if device.pod_name:
335
+ return device.pod_name
336
+ return ""
337
+
338
+
339
+ def _metric_with_unit(value: float | None, unit: str | None) -> str:
340
+ if value is None:
341
+ return ""
342
+ return f"{_fmt(value)} {unit or ''}".strip()
343
+
344
+
345
+ def _fmt(value: float | int | None) -> str:
346
+ if value is None:
347
+ return ""
348
+ if float(value).is_integer():
349
+ return str(int(value))
350
+ return str(value)
@@ -0,0 +1,123 @@
1
+ import typer
2
+ from rich.console import Console
3
+ from rich.table import Table
4
+
5
+ from nufi.cli.commands import common
6
+ from nufi import config
7
+ from nufi.exceptions import ConfigurationError
8
+
9
+ app = typer.Typer(
10
+ help="Manage NuFi CLI profiles.",
11
+ invoke_without_command=True,
12
+ context_settings=common.CONTEXT_SETTINGS,
13
+ )
14
+ console = Console()
15
+
16
+
17
+ @app.callback()
18
+ def profile(ctx: typer.Context):
19
+ common.show_help_if_no_subcommand(ctx)
20
+
21
+
22
+ @app.command("create")
23
+ def create_profile(
24
+ name: str = typer.Argument(..., help="Profile name."),
25
+ base_domain: str | None = typer.Option(
26
+ None,
27
+ "--base-domain",
28
+ help="Base domain used to derive api.<domain> and keycloak.<domain>.",
29
+ ),
30
+ tls_enabled: bool | None = typer.Option(
31
+ None,
32
+ "--tls-enabled/--no-tls-enabled",
33
+ help="Use HTTPS for derived URLs (default: HTTP for nufi.local, HTTPS otherwise).",
34
+ ),
35
+ api_url: str | None = typer.Option(None, "--api-url", help="API URL override."),
36
+ keycloak_issuer: str | None = typer.Option(
37
+ None,
38
+ "--keycloak-issuer",
39
+ help="Keycloak issuer URL override.",
40
+ ),
41
+ client_id: str = typer.Option(
42
+ config.DEFAULT_CLIENT_ID,
43
+ "--client-id",
44
+ help="Keycloak client ID.",
45
+ ),
46
+ ):
47
+ try:
48
+ base_domain, protocol_value = _resolve_profile_create_defaults(
49
+ base_domain=base_domain,
50
+ tls_enabled=tls_enabled,
51
+ api_url=api_url,
52
+ keycloak_issuer=keycloak_issuer,
53
+ )
54
+ profile = config.create_profile(
55
+ name,
56
+ base_domain=base_domain,
57
+ protocol=protocol_value,
58
+ api_url=api_url,
59
+ keycloak_issuer=keycloak_issuer,
60
+ client_id=client_id,
61
+ )
62
+ except ConfigurationError as exc:
63
+ common.handle_cli_error(exc, verbose=False)
64
+ console.print(f"[bold green]Profile saved:[/] {name}")
65
+ console.print(f"API: {profile.resolved_api_url}")
66
+ console.print(f"Keycloak: {profile.resolved_keycloak_issuer}")
67
+ console.print(f"Project: {profile.active_project}")
68
+
69
+
70
+ def _resolve_profile_create_defaults(
71
+ *,
72
+ base_domain: str | None,
73
+ tls_enabled: bool | None,
74
+ api_url: str | None,
75
+ keycloak_issuer: str | None,
76
+ ) -> tuple[str | None, str]:
77
+ has_api_url = api_url is not None
78
+ has_keycloak_issuer = keycloak_issuer is not None
79
+ has_partial_override = has_api_url != has_keycloak_issuer
80
+ if has_partial_override and base_domain is None:
81
+ missing = "--keycloak-issuer" if has_api_url else "--api-url"
82
+ raise ConfigurationError(
83
+ f"Partial URL override requires {missing} or --base-domain."
84
+ )
85
+
86
+ if base_domain is None and not has_api_url and not has_keycloak_issuer:
87
+ base_domain = config.DEFAULT_BASE_DOMAIN
88
+
89
+ if tls_enabled is not None:
90
+ return base_domain, "https" if tls_enabled else "http"
91
+ if base_domain == config.DEFAULT_BASE_DOMAIN:
92
+ return base_domain, "http"
93
+ return base_domain, "https"
94
+
95
+
96
+ @app.command("use")
97
+ def use_profile(name: str = typer.Argument(..., help="Profile name.")):
98
+ try:
99
+ config.use_profile(name)
100
+ except ConfigurationError as exc:
101
+ console.print(f"[bold red]Error:[/] {exc}")
102
+ raise typer.Exit(code=1) from exc
103
+ console.print(f"[bold green]Active profile:[/] {name}")
104
+
105
+
106
+ @app.command("list")
107
+ def list_profiles():
108
+ active = None
109
+ try:
110
+ active = config.get_active_profile_name()
111
+ except ConfigurationError:
112
+ pass
113
+
114
+ table = Table("Active", "Name", "Project", "API URL", "Keycloak Issuer")
115
+ for item in config.list_profiles():
116
+ table.add_row(
117
+ "*" if item.name == active else "",
118
+ item.name,
119
+ item.active_project,
120
+ item.resolved_api_url,
121
+ item.resolved_keycloak_issuer,
122
+ )
123
+ console.print(table)