Coverage for src/signalk_cli/history/output.py: 98%

37 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-22 09:25 +0000

1"""CSV, JSON and Feather writers for the history CLI.""" 

2 

3from __future__ import annotations 

4 

5import csv 

6import json 

7from typing import IO, Any 

8 

9from .._arrow import as_text 

10from ._results import CARDINALITY_COLUMNS, long_rows, wide_rows 

11 

12 

13def _text(v: Any) -> str: 

14 """CLI text for a value: JSON for objects/arrays, empty for null.""" 

15 return as_text(v) or "" 

16 

17 

18# --------------------------------------------------------------------------- 

19# Long (timestamp, path, value) 

20# --------------------------------------------------------------------------- 

21 

22 

23def write_csv( 

24 result: dict[str, Any], sink: IO[str], no_header: bool 

25) -> tuple[int, set[str]]: 

26 """Write result as CSV rows (timestamp, path, value). Returns (row_count, unique_paths).""" 

27 timestamps, paths, values = long_rows(result) 

28 writer = csv.writer(sink) 

29 if not no_header: 

30 writer.writerow(["timestamp", "path", "value"]) 

31 for ts, path, val in zip(timestamps, paths, values): 

32 writer.writerow([ts, path, _text(val)]) 

33 return len(timestamps), set(paths) 

34 

35 

36def write_json( 

37 result: dict[str, Any], sink: IO[str], indent: int | None = None 

38) -> tuple[int, set[str]]: 

39 """Write result as a JSON array of {timestamp, path, value} objects.""" 

40 timestamps, paths, values = long_rows(result) 

41 rows = [ 

42 {"timestamp": ts, "path": p, "value": _text(v)} 

43 for ts, p, v in zip(timestamps, paths, values) 

44 ] 

45 sink.write(json.dumps(rows, indent=indent)) 

46 return len(rows), set(paths) 

47 

48 

49# --------------------------------------------------------------------------- 

50# Wide (timestamp, path, min_value/avg_value/max_value or array element columns) 

51# --------------------------------------------------------------------------- 

52 

53 

54def write_csv_wide( 

55 result: dict[str, Any], sink: IO[str], no_header: bool 

56) -> tuple[int, set[str]]: 

57 """Write result as CSV with wide value columns.""" 

58 timestamps, paths, value_cols = wide_rows(result) 

59 col_names = list(value_cols) 

60 writer = csv.writer(sink) 

61 if not no_header: 61 ↛ 63line 61 didn't jump to line 63 because the condition on line 61 was always true

62 writer.writerow(["timestamp", "path", *col_names]) 

63 for i, (ts, path) in enumerate(zip(timestamps, paths)): 

64 writer.writerow([ts, path, *(_text(value_cols[c][i]) for c in col_names)]) 

65 return len(timestamps), set(paths) 

66 

67 

68def write_json_wide( 

69 result: dict[str, Any], sink: IO[str], indent: int | None = None 

70) -> tuple[int, set[str]]: 

71 """Write result as a JSON array of wide row objects.""" 

72 timestamps, paths, value_cols = wide_rows(result) 

73 rows = [ 

74 {"timestamp": ts, "path": p, **{c: _text(v[i]) for c, v in value_cols.items()}} 

75 for i, (ts, p) in enumerate(zip(timestamps, paths)) 

76 ] 

77 sink.write(json.dumps(rows, indent=indent)) 

78 return len(rows), set(paths) 

79 

80 

81# --------------------------------------------------------------------------- 

82# Cardinality 

83# --------------------------------------------------------------------------- 

84 

85 

86def cardinality_text(row: dict[str, Any]) -> dict[str, str]: 

87 """Cardinality row as CLI text, with empty strings for stats that don't apply.""" 

88 return {col: _text(row[col]) for col in CARDINALITY_COLUMNS}