-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
1423 lines (1242 loc) · 47.2 KB
/
Copy pathcli.py
File metadata and controls
1423 lines (1242 loc) · 47.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Simple cmd2-based CLI wrapper for the Beacon Python SDK."""
from __future__ import annotations
import argparse
import ast
import shutil
import time
from io import BytesIO
from pathlib import Path
from typing import Any, Optional, cast
import cmd2
import pandas as pd
from cmd2 import Cmd2ArgumentParser
from rich import box
from rich.console import Console
from rich.live import Live
from rich.panel import Panel
from rich.table import Table
from beacon_api import Client, Dataset
from beacon_api.table import DataTable
from beacon_api.query import (
Arrow,
CSV as QueryCSV,
GeoParquet,
JSONQuery,
NetCDF,
Parquet,
SQLQuery,
BaseQuery,
)
def _build_startup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Beacon CLI")
parser.add_argument(
"--url",
dest="url",
help="Automatically connect to the given Beacon base URL on startup.",
)
parser.add_argument(
"--jwt-token",
dest="jwt_token",
help="Optional bearer token to use for the startup connection.",
)
return parser
def _connect_arg_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Connect to a Beacon node")
parser.add_argument("url", help="Beacon base URL, e.g. https://beacon.example.com")
parser.add_argument(
"--jwt-token",
dest="jwt_token",
help="Optional bearer token for Authorization header",
)
parser.add_argument(
"--basic-auth",
nargs=2,
metavar=("USER", "PASSWORD"),
help="Optional username/password for basic auth",
)
return parser
def _sql_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Execute a SQL query against the connected Beacon node"
)
parser.add_argument(
"sql",
nargs=argparse.REMAINDER,
help="SQL statement to run (wrap in quotes for complex expressions)",
)
parser.add_argument(
"--rows",
type=int,
default=5,
help="Number of rows to display in the preview table (default: 5)",
)
parser.add_argument(
"--page",
action="store_true",
help="Open a pager with the full result table (preview format only)",
)
parser.add_argument(
"--format",
choices=["preview", "parquet", "csv", "arrow", "netcdf", "geoparquet"],
default="preview",
help="preview renders a table; other options stream raw bytes for piping or saving",
)
parser.add_argument(
"--output-path",
help="Optional destination file when exporting (defaults to stdout for piping)",
)
parser.add_argument(
"--stream",
action="store_true",
help="Use streaming execution and report time-to-first-row (preview format only)",
)
parser.add_argument(
"--geo-columns",
nargs=2,
metavar=("LON_COLUMN", "LAT_COLUMN"),
help="Required when --format geoparquet to map longitude/latitude columns",
)
return parser
def _dataset_list_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="List datasets registered with the Beacon node"
)
parser.add_argument(
"--pattern", help="Optional glob/regex pattern resolved by the server"
)
parser.add_argument(
"--limit", type=int, help="Maximum number of datasets to return"
)
parser.add_argument("--offset", type=int, help="Offset for pagination")
return parser
def _dataset_schema_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(description="Inspect a dataset schema")
parser.add_argument(
"file_path", help="Dataset path exactly as registered on the Beacon node"
)
return parser
def _table_schema_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(description="Inspect a logical table schema")
parser.add_argument("table_name", help="Name of the logical table to inspect")
return parser
def _dataset_upload_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Upload a dataset file to the Beacon node (admin only)"
)
parser.add_argument("local_path", help="Local file path to upload")
parser.add_argument(
"destination_path",
help="Destination path on the Beacon node, e.g. /data/datasets/file.parquet",
)
parser.add_argument("--force", help="Force the action even when beacon version mismatches.", default=False)
return parser
def _dataset_download_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Download a dataset file from the Beacon node (admin only)"
)
parser.add_argument("dataset_path", help="Path to the dataset on the Beacon node")
parser.add_argument(
"local_path", help="Local path where the downloaded file should be saved"
)
parser.add_argument("--force", help="Force the action even when beacon version mismatches.", default=False)
return parser
def _dataset_delete_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Delete a dataset file from the Beacon node (admin only)"
)
parser.add_argument(
"dataset_path", help="Path to the dataset on the Beacon node to delete"
)
parser.add_argument("--force", help="Force the action even when beacon version mismatches.", default=False)
return parser
def _create_table_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Create a logical table on the Beacon node (admin only)"
)
parser.add_argument("table_name", help="Name of the logical table to create")
parser.add_argument(
"dataset_glob_paths",
nargs="+",
help="One or more dataset glob paths that back the logical table",
)
parser.add_argument(
"file_format", help="Dataset file format (parquet, csv, zarr, ...)"
)
parser.add_argument("--description", help="Optional table description")
parser.add_argument("--delimiter", help="CSV delimiter (only for csv format)")
parser.add_argument(
"--statistics-columns",
nargs="+",
help="Statistics columns for Zarr datasets",
)
parser.add_argument(
"--option",
dest="options",
action="append",
metavar="KEY=VALUE",
help="Additional format-specific kwargs (may be repeated)",
)
return parser
def _delete_table_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Delete a logical table from the Beacon node (admin only)"
)
parser.add_argument("table_name", help="Name of the logical table to delete")
return parser
def _parse_option_pairs(pairs: list[str] | None) -> dict[str, Any]:
if not pairs:
return {}
parsed: dict[str, Any] = {}
for pair in pairs:
if "=" not in pair:
raise ValueError(f"Expected KEY=VALUE format, got '{pair}'")
key, raw_value = pair.split("=", 1)
key = key.strip()
if not key:
raise ValueError("Option keys cannot be empty")
value: Any = raw_value
try:
value = ast.literal_eval(raw_value)
except (ValueError, SyntaxError):
value = raw_value
parsed[key] = value
return parsed
def _coerce_value(raw: str) -> Any:
lowered = raw.strip().lower()
if lowered in {"none", "null"}:
return None
try:
return ast.literal_eval(raw)
except (ValueError, SyntaxError):
return raw
def _parse_key_value(spec: str) -> tuple[str, str]:
if "=" not in spec:
raise ValueError(f"Expected COLUMN=VALUE format, got '{spec}'")
column, value = spec.split("=", 1)
column = column.strip()
if not column:
raise ValueError("Column name cannot be empty")
return column, value.strip()
def _parse_select_spec(spec: str) -> tuple[str, Optional[str]]:
column, _, alias = spec.strip().partition(":")
column = column.strip()
alias = alias.strip() or None
if not column:
raise ValueError("Select columns cannot be empty")
return column, alias
def _parse_optional_value(raw: str) -> Any:
return None if raw.strip().lower() in {"none", "null"} else _coerce_value(raw)
def _query_builder_arg_parser() -> Cmd2ArgumentParser:
parser = Cmd2ArgumentParser(
description="Build and run a JSON query using the fluent Beacon builder"
)
target = parser.add_mutually_exclusive_group(required=True)
target.add_argument("--table", help="Logical table name to query")
target.add_argument(
"--dataset",
help="Dataset path registered on the Beacon node (supports suffix matching)",
)
parser.add_argument(
"--select",
action="append",
metavar="COLUMN[:ALIAS]",
help="Select column definition; repeat to add multiple columns",
)
parser.add_argument(
"--equals",
action="append",
metavar="COLUMN=VALUE",
help="Equality filter; repeat for multiple filters",
)
parser.add_argument(
"--not-equals",
dest="not_equals",
action="append",
metavar="COLUMN=VALUE",
help="Not-equals filter; repeat for multiple filters",
)
parser.add_argument(
"--range",
dest="ranges",
action="append",
nargs=3,
metavar=("COLUMN", "MIN", "MAX"),
help="Range filter; use 'none' for open bounds",
)
parser.add_argument(
"--bbox",
dest="bboxes",
action="append",
nargs=6,
metavar=("LON_COL", "LAT_COL", "MIN_LON", "MAX_LON", "MIN_LAT", "MAX_LAT"),
help="Bounding box filter (repeatable)",
)
parser.add_argument(
"--is-null",
dest="is_null",
action="append",
metavar="COLUMN",
help="IS NULL filter",
)
parser.add_argument(
"--is-not-null",
dest="is_not_null",
action="append",
metavar="COLUMN",
help="IS NOT NULL filter",
)
parser.add_argument(
"--distinct",
action="append",
metavar="COLUMN",
help="Add DISTINCT on the provided columns",
)
parser.add_argument(
"--sort",
action="append",
metavar="COLUMN[:asc|desc]",
help="Add ORDER BY clauses (default ascending)",
)
parser.add_argument(
"--rows",
type=int,
default=5,
help="Number of rows to show in preview output",
)
parser.add_argument(
"--page",
action="store_true",
help="Open a pager with the full preview table",
)
parser.add_argument(
"--format",
choices=["preview", "parquet", "csv", "arrow", "netcdf", "geoparquet"],
default="preview",
help="preview renders a table; other options stream raw bytes for piping or saving",
)
parser.add_argument(
"--output-path",
help="Destination file path when exporting parquet/csv/arrow/netcdf/geoparquet",
)
parser.add_argument(
"--stream",
action="store_true",
help="Use streaming execution for preview results",
)
parser.add_argument(
"--geo-columns",
nargs=2,
metavar=("LON_COLUMN", "LAT_COLUMN"),
help="Required when --format geoparquet to map longitude/latitude columns",
)
return parser
CONNECT_PARSER = _connect_arg_parser()
SQL_PARSER = _sql_arg_parser()
DATASET_LIST_PARSER = _dataset_list_arg_parser()
DATASET_SCHEMA_PARSER = _dataset_schema_arg_parser()
TABLE_SCHEMA_PARSER = _table_schema_arg_parser()
QUERY_BUILDER_PARSER = _query_builder_arg_parser()
DATASET_UPLOAD_PARSER = _dataset_upload_arg_parser()
DATASET_DOWNLOAD_PARSER = _dataset_download_arg_parser()
DATASET_DELETE_PARSER = _dataset_delete_arg_parser()
CREATE_TABLE_PARSER = _create_table_arg_parser()
DELETE_TABLE_PARSER = _delete_table_arg_parser()
class BeaconCli(cmd2.Cmd):
"""Interactive shell for the Beacon Python SDK."""
prompt = "beacon_cli> "
intro = (
"beacon_cli shell. Type 'help'/'?' for commands. Start with 'connect <url>'."
)
def __init__(self) -> None:
super().__init__(allow_cli_args=False)
self.client: Optional[Client] = None
self.console = Console()
def _resolve_dataset(self, file_path: str) -> Dataset | None:
client = cast(Client, self.client)
try:
datasets = client.list_datasets(pattern=file_path)
except Exception as exc: # pragma: no cover - CLI helper
self.perror(f"Failed to resolve dataset '{file_path}': {exc}")
return None
if file_path in datasets:
return datasets[file_path]
for path, dataset in datasets.items():
if path.endswith(file_path):
return dataset
self.perror(f"Dataset '{file_path}' was not found on the server.")
return None
def _resolve_table(self, table_name: str) -> DataTable | None:
client = cast(Client, self.client)
try:
tables = client.list_tables()
except Exception as exc: # pragma: no cover - CLI helper
self.perror(f"Failed to resolve table '{table_name}': {exc}")
return None
if table_name in tables:
return tables[table_name]
matches = [table for name, table in tables.items() if name.lower() == table_name.lower()]
if matches:
return matches[0]
available = ", ".join(sorted(tables)) if tables else "none"
self.perror(
f"Table '{table_name}' was not found on the server. Available tables: {available}"
)
return None
def _render_status(self) -> None:
client = cast(Client, self.client)
try:
info = client.get_server_info()
except Exception as exc: # pragma: no cover - thin wrapper
self.perror(f"Unable to fetch server info: {exc}")
return
admin_state = "unknown"
try:
admin_state = "yes" if client.session.is_admin() else "no"
except Exception as exc: # pragma: no cover - best-effort
admin_state = f"error: {exc}"
table = Table(box=box.SIMPLE_HEAVY)
table.add_column("Property", style="bold cyan")
table.add_column("Value", overflow="fold")
table.add_row("Base URL", client.session.base_url)
table.add_row("Beacon version", str(info.get("beacon_version", "unknown")))
table.add_row("Datasets enabled", str(info.get("datasets", "?")))
table.add_row("Admin access", admin_state)
panel = Panel(table, title="Connection status", border_style="green")
self.console.print(panel)
def _format_bytes(self, num_bytes: int | None) -> str:
if num_bytes is None:
return "unknown"
units = ["B", "KB", "MB", "GB", "TB"]
value = float(num_bytes)
for unit in units:
if value < 1024 or unit == units[-1]:
return f"{value:.2f} {unit}"
value /= 1024
return f"{value:.2f} TB"
def _build_query_metrics(
self,
total_seconds: float,
first_row_seconds: float,
*,
streamed: bool,
response_bytes: int | None = None,
) -> list[tuple[str, str]]:
label = "First rows" if streamed else "Data ready"
return [
("Total duration", f"{total_seconds:.2f}s"),
(label, f"{first_row_seconds:.2f}s"),
("Response size", self._format_bytes(response_bytes)),
]
def _print_query_metrics(
self,
total_seconds: float,
first_row_seconds: float,
*,
streamed: bool,
use_stderr: bool = False,
response_bytes: int | None = None,
) -> None:
console = self.console if not use_stderr else Console(stderr=True)
metrics_table = Table(show_header=False, box=box.SIMPLE)
for name, value in self._build_query_metrics(
total_seconds,
first_row_seconds,
streamed=streamed,
response_bytes=response_bytes,
):
metrics_table.add_row(name, value)
console.print(Panel(metrics_table, title="Query metrics", border_style="cyan"))
def _build_dataframe_panel(
self,
df: pd.DataFrame,
title: str,
*,
max_rows: int | None,
metrics: list[tuple[str, str]] | None = None,
source_df: pd.DataFrame | None = None,
subtitle: str | None = None,
) -> Panel:
source = source_df if source_df is not None else df
if df.empty:
message = "Query returned no rows"
if metrics:
metric_lines = "\n".join(f"{name}: {value}" for name, value in metrics)
message = f"{message}\n{metric_lines}"
return Panel(message, title=title, border_style="red")
display_df = df if max_rows is None else df.head(max_rows)
try:
column_mem = source.memory_usage(index=False, deep=True)
except Exception:
column_mem = None
label_map: dict[str, str] = {}
for column in display_df.columns:
dtype = (
source[column].dtype
if column in source.columns
else display_df[column].dtype
)
size_bytes: int | None = None
if column_mem is not None and column in column_mem:
size_value = column_mem[column]
if isinstance(size_value, (int, float)):
size_bytes = int(size_value)
size_str = self._format_bytes(size_bytes)
label_map[column] = f"{column}({dtype}, {size_str})"
display_df = display_df.rename(columns=label_map)
table = Table(
title=title,
show_header=True,
header_style="bold green",
box=box.MINIMAL_DOUBLE_HEAD,
expand=True,
)
column_names = list(display_df.columns)
if not column_names:
table.add_column("Value")
column_names = ["Value"]
else:
for column in column_names:
table.add_column(str(column))
for _, row in display_df.iterrows():
table.add_row(*(str(row[col]) for col in column_names))
if metrics:
table.add_section()
column_count = len(column_names)
for name, value in metrics:
cells = [f"{name}: {value}"] + [""] * (column_count - 1)
table.add_row(*cells)
if subtitle is not None:
subtitle_text = subtitle
elif max_rows is None or len(display_df) == len(source):
subtitle_text = f"{len(source)} rows"
else:
subtitle_text = f"showing {len(display_df)} of {len(source)} rows"
return Panel(table, border_style="green", subtitle=subtitle_text)
def _run_query_to_dataframe(
self, query: BaseQuery
) -> tuple[pd.DataFrame, float, int]:
query.set_output(Parquet())
start = time.perf_counter()
response = query.execute()
payload = response.content
total_elapsed = time.perf_counter() - start
df = pd.read_parquet(BytesIO(payload))
return df, total_elapsed, len(payload)
def _export_query_results(
self,
query: BaseQuery,
fmt: str,
output_path: str | None,
*,
geo_columns: tuple[str, str] | None = None,
) -> None:
output_map: dict[str, Any] = {
"parquet": Parquet,
"csv": QueryCSV,
"arrow": Arrow,
"netcdf": NetCDF,
}
if fmt == "geoparquet":
lon_col, lat_col = cast(tuple[str, str], geo_columns)
query.set_output(
GeoParquet(longitude_column=lon_col, latitude_column=lat_col)
)
else:
output_cls = output_map[fmt]
query.set_output(output_cls())
start = time.perf_counter()
bytes_written = 0
try:
response = query.execute()
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Query execution failed: {exc}")
return
total_elapsed = time.perf_counter() - start
if output_path:
destination = Path(output_path).expanduser()
destination.parent.mkdir(parents=True, exist_ok=True)
with open(destination, "wb") as handle:
for chunk in response.iter_content(chunk_size=1024 * 1024):
if chunk:
handle.write(chunk)
bytes_written += len(chunk)
self.console.print(
Panel(
f"Saved {fmt.upper()} results to [green]{destination}[/green]",
border_style="green",
)
)
self._print_query_metrics(
total_elapsed,
total_elapsed,
streamed=False,
response_bytes=bytes_written,
)
return
output_stream = getattr(self.stdout, "buffer", self.stdout)
for chunk in response.iter_content(chunk_size=1024 * 1024):
if chunk:
output_stream.write(chunk)
bytes_written += len(chunk)
if hasattr(output_stream, "flush"):
output_stream.flush()
self._print_query_metrics(
total_elapsed,
total_elapsed,
streamed=False,
use_stderr=True,
response_bytes=bytes_written,
)
def _collect_streaming_preview(
self,
query: BaseQuery,
max_rows: int,
) -> tuple[pd.DataFrame, float, float, int]:
start = time.perf_counter()
preview_frames: list[pd.DataFrame] = []
rows_collected = 0
first_row_elapsed: float | None = None
total_bytes = 0
stream = query.execute_streaming()
for batch in stream:
if batch.num_rows == 0:
continue
total_bytes += batch.nbytes
if first_row_elapsed is None:
first_row_elapsed = time.perf_counter() - start
if rows_collected < max_rows:
preview_frames.append(batch.to_pandas())
rows_collected += batch.num_rows
total_elapsed = time.perf_counter() - start
preview_df = (
pd.concat(preview_frames, ignore_index=True)
if preview_frames
else pd.DataFrame()
)
return (
preview_df.head(max_rows),
total_elapsed,
first_row_elapsed or total_elapsed,
total_bytes,
)
def _validate_output_flags(self, fmt: str, page: bool, stream: bool) -> bool:
if page and fmt != "preview":
self.perror("--page is only supported when using --format preview")
return False
if stream and fmt != "preview":
self.perror("--stream is only supported when --format preview is selected")
return False
if page and stream:
self.perror("--page cannot be combined with --stream")
return False
return True
def _render_query_results(
self,
query: BaseQuery,
*,
fmt: str,
rows: int,
page: bool,
stream: bool,
output_path: str | None,
geo_columns: tuple[str, str] | None = None,
) -> None:
if fmt == "geoparquet" and geo_columns is None:
self.perror("GeoParquet output requires --geo-columns LON_COLUMN LAT_COLUMN")
return
if fmt != "preview":
self._export_query_results(query, fmt, output_path, geo_columns=geo_columns)
return
if stream:
try:
df, total_elapsed, first_row, total_bytes = self._collect_streaming_preview(
query, max_rows=rows
)
except Exception as exc: # pragma: no cover - streaming helper
self.perror(f"Streaming query execution failed: {exc}")
return
metrics_rows = self._build_query_metrics(
total_elapsed,
first_row,
streamed=True,
response_bytes=total_bytes,
)
self._render_dataframe_preview(
df,
title="JSON query results" if isinstance(query, JSONQuery) else "Query results",
max_rows=rows,
metrics=metrics_rows,
)
return
try:
df, total_elapsed, response_bytes = self._run_query_to_dataframe(query)
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Query execution failed: {exc}")
return
metrics_rows = self._build_query_metrics(
total_elapsed,
total_elapsed,
streamed=False,
response_bytes=response_bytes,
)
title = "JSON query results" if isinstance(query, JSONQuery) else "SQL results"
if page:
self._render_dataframe_pager(
df,
title=title,
metrics=metrics_rows,
)
else:
self._render_dataframe_preview(
df,
title=title,
max_rows=rows,
metrics=metrics_rows,
)
def _connect_to_beacon(
self,
url: str,
jwt_token: str | None = None,
basic_auth: list[str] | None = None,
) -> None:
auth_tuple = tuple(basic_auth) if basic_auth else None
try:
client = Client(
url=url,
jwt_token=jwt_token,
basic_auth=auth_tuple, # type: ignore[arg-type]
)
except Exception as exc: # pragma: no cover - thin CLI wrapper
self.perror(f"Failed to connect: {exc}")
return
self.client = client
self.poutput(f"Connected to {url}")
def show_dashboard(self) -> None:
"""Render a full-screen command overview using Rich."""
terminal_width = shutil.get_terminal_size((100, 40)).columns
self.console.clear()
header = Panel(
"[bold cyan]beacon_cli[/bold cyan]\nConnect to Beacon nodes and explore/manage the content.",
border_style="cyan",
width=terminal_width,
)
self.console.print(header)
table = Table(
expand=True, box=None, show_header=True, header_style="bold magenta"
)
table.add_column("Command")
table.add_column("Description")
table.add_row(
"connect <url> [--jwt-token TOKEN] [--basic-auth USERNAME PASSWORD]", "Connect to a Beacon node via flags"
)
table.add_row("status", "Show connection health, version, and admin state")
table.add_row(
"list_datasets [--pattern --limit --offset]",
"List datasets registered with the server",
)
table.add_row(
"dataset_schema <path>", "Inspect a dataset's schema in a table view"
)
table.add_row("list_tables", "List logical tables, types, and descriptions")
table.add_row(
"list_table_schema <name>", "Inspect a logical table's schema"
)
table.add_row(
"create_table <name> <globs...> <format>", "Create a logical table (admin)"
)
table.add_row("delete_table <name>", "Delete a logical table (admin)")
table.add_row("upload_dataset <local> <dest>", "Upload a dataset file (admin)")
table.add_row(
"download_dataset <remote> <local>", "Download a dataset file (admin)"
)
table.add_row("delete_dataset <remote>", "Delete a dataset file (admin)")
table.add_row(
"query_builder [--table/--dataset]",
"Build a fluent JSON query with select/filter flags",
)
table.add_row(
"sql <statement> [--rows]", "Execute a SQL query and preview the result"
)
table.add_row("dashboard", "Reopen this overview screen")
table.add_row("help", "List available commands and their usage. To get help on a specific command, run 'help <command>'.")
table.add_row("quit", "Exit beacon_cli")
panel = Panel(
table, title="Command palette", border_style="magenta", width=terminal_width
)
self.console.print(panel)
self.console.print(
"[dim]Tip: run 'connect https://host' to start working with Beacon data.[/dim]",
justify="center",
)
def _require_connection(self) -> bool:
if self.client is None:
self.perror("Not connected. Run 'connect <url>' first.")
return False
return True
@cmd2.with_category("Session")
@cmd2.with_argparser(CONNECT_PARSER)
def do_connect(self, args: argparse.Namespace) -> None:
"""Connect to a Beacon node."""
self._connect_to_beacon(
url=args.url,
jwt_token=args.jwt_token,
basic_auth=list(args.basic_auth) if args.basic_auth else None,
)
@cmd2.with_category("Session")
def do_status(self, _: cmd2.Statement) -> None:
"""Show connection status including Beacon version and admin state."""
if not self._require_connection():
return
self._render_status()
@cmd2.with_category("Tables")
def do_list_tables(self, _: cmd2.Statement) -> None:
"""List available tables and their metadata."""
if not self._require_connection():
return
client = cast(Client, self.client)
try:
tables = client.list_tables()
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Failed to list tables: {exc}")
return
if not tables:
self.console.print(
Panel("No tables were returned by the server.", border_style="yellow")
)
return
rich_table = Table(title="Tables", show_lines=True)
rich_table.add_column("Table name", style="bold")
rich_table.add_column("Type", overflow="fold")
rich_table.add_column("Description", overflow="fold")
for name, table_obj in sorted(tables.items()):
table_type = table_obj.get_table_type()
if isinstance(table_type, dict):
table_type_str = ", ".join(table_type.keys()) or "dict"
else:
table_type_str = str(table_type)
rich_table.add_row(name, table_type_str, table_obj.get_table_description())
self.console.print(rich_table)
@cmd2.with_category("Tables")
@cmd2.with_argparser(CREATE_TABLE_PARSER)
def do_create_table(self, args: argparse.Namespace) -> None:
"""Create a logical table on the Beacon node (admin only)."""
if not self._require_connection():
return
client = cast(Client, self.client)
try:
extra_kwargs = _parse_option_pairs(args.options)
except ValueError as exc:
self.perror(str(exc))
return
if args.delimiter:
extra_kwargs["delimiter"] = args.delimiter
if args.statistics_columns:
extra_kwargs["statistics"] = {
"columns": args.statistics_columns
}
try:
client.create_logical_table(
table_name=args.table_name,
dataset_glob_paths=args.dataset_glob_paths,
file_format=args.file_format,
description=args.description,
**extra_kwargs,
)
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Failed to create table: {exc}")
return
self.console.print(
Panel(
f"Created logical table [bold]{args.table_name}[/bold]",
border_style="green",
)
)
@cmd2.with_category("Tables")
@cmd2.with_argparser(DELETE_TABLE_PARSER)
def do_delete_table(self, args: argparse.Namespace) -> None:
"""Delete a logical table from the Beacon node (admin only)."""
if not self._require_connection():
return
client = cast(Client, self.client)
try:
client.delete_table(args.table_name)
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Failed to delete table: {exc}")
return
self.console.print(
Panel(
f"Deleted logical table [bold]{args.table_name}[/bold]",
border_style="red",
)
)
@cmd2.with_category("Datasets")
@cmd2.with_argparser(DATASET_LIST_PARSER)
def do_list_datasets(self, args: argparse.Namespace) -> None:
"""List datasets registered on the Beacon node."""
if not self._require_connection():
return
client = cast(Client, self.client)
try:
datasets = client.list_datasets(
pattern=args.pattern, limit=args.limit, offset=args.offset
)
except Exception as exc: # pragma: no cover - CLI wrapper
self.perror(f"Failed to list datasets: {exc}")
return