Skip to content

cli

cli ¤

Command-line interface for PANTHER metrics.

Functions:

Name Description
cmd_export

Export metrics records to JSON.

cmd_list

List recent metrics records.

cmd_show

Show detailed information about a specific metrics record.

format_duration

Format duration for display.

format_size

Format size in MB for display.

format_timestamp

Format ISO timestamp for display.

get_storage

Get the metrics storage instance.

main

Main CLI entry point.

cmd_export ¤

cmd_export(args: Namespace) -> int

Export metrics records to JSON.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def cmd_export(args: argparse.Namespace) -> int:
    """Export metrics records to JSON."""
    storage = get_storage()

    try:
        records = storage.read_records(date=args.date, limit=args.limit)
    except Exception as e:
        print(f"Error reading metrics: {e}", file=sys.stderr)
        return 1

    if args.output:
        try:
            with open(args.output, "w") as f:
                json.dump(records, f, indent=2)
            print(f"Exported {len(records)} records to {args.output}")
        except Exception as e:
            print(f"Error writing to {args.output}: {e}", file=sys.stderr)
            return 1
    else:
        json.dump(records, sys.stdout, indent=2)

    return 0

cmd_list ¤

cmd_list(args: Namespace) -> int

List recent metrics records.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
 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
def cmd_list(args: argparse.Namespace) -> int:
    """List recent metrics records."""
    storage = get_storage()

    try:
        records = storage.read_records(limit=args.limit)
    except Exception as e:
        print(f"Error reading metrics: {e}", file=sys.stderr)
        return 1

    if not records:
        print("No metrics records found.")
        return 0

    # Print header
    print(f"{'Run ID':<36} {'Type':<8} {'Timestamp':<19} {'Key Metrics'}")
    print("-" * 80)

    # Print records
    for record in records:
        run_id = record.get("run_id", "unknown")[:35]
        record_type = record.get("type", "unknown")
        timestamp = format_timestamp(record.get("timestamp", ""))

        # Extract key metrics based on type
        metrics = record.get("metrics", {})
        key_metrics = []

        if record_type == "builder":
            if "container.build_seconds" in metrics:
                duration = format_duration(metrics["container.build_seconds"])
                key_metrics.append(f"build: {duration}")
            if "container.image_mb" in metrics:
                size = format_size(metrics["container.image_mb"])
                key_metrics.append(f"image: {size}")
            if "artifact.dist_mb" in metrics:
                size = format_size(metrics["artifact.dist_mb"])
                key_metrics.append(f"dist: {size}")

        elif record_type == "tests":
            if "pytest.total_seconds" in metrics:
                duration = format_duration(metrics["pytest.total_seconds"])
                key_metrics.append(f"duration: {duration}")
            if "pytest.item_count" in metrics:
                count = int(metrics["pytest.item_count"])
                key_metrics.append(f"tests: {count}")
            if "pytest.failed" in metrics and "pytest.passed" in metrics:
                failed = int(metrics["pytest.failed"])
                passed = int(metrics["pytest.passed"])
                key_metrics.append(f"passed: {passed}, failed: {failed}")
            if "pytest.coverage_pct" in metrics:
                coverage = metrics["pytest.coverage_pct"]
                key_metrics.append(f"coverage: {coverage:.1f}%")

        key_metrics_str = ", ".join(key_metrics) if key_metrics else "no metrics"
        print(f"{run_id:<36} {record_type:<8} {timestamp:<19} {key_metrics_str}")

    return 0

cmd_show ¤

cmd_show(args: Namespace) -> int

Show detailed information about a specific metrics record.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
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
def cmd_show(args: argparse.Namespace) -> int:
    """Show detailed information about a specific metrics record."""
    storage = get_storage()

    try:
        record = storage.get_record_by_id(args.run_id)
    except Exception as e:
        print(f"Error reading metrics: {e}", file=sys.stderr)
        return 1

    if not record:
        print(f"No record found with run ID: {args.run_id}")
        return 1

    # Print basic information
    print(f"Run ID: {record.get('run_id', 'unknown')}")
    print(f"Type: {record.get('type', 'unknown')}")
    print(f"Timestamp: {format_timestamp(record.get('timestamp', ''))}")
    print(f"Git Commit: {record.get('git_commit', 'unknown')}")

    # Print configuration hash if available
    if "config_hash" in record:
        print(f"Config Hash: {record['config_hash']}")

    print()

    # Print metrics
    metrics = record.get("metrics", {})
    if metrics:
        print("Metrics:")
        for name, value in sorted(metrics.items()):
            if name.endswith("_seconds"):
                formatted_value = format_duration(value)
            elif name.endswith("_mb"):
                formatted_value = format_size(value)
            elif name.endswith("_pct"):
                formatted_value = f"{value:.1f}%"
            elif isinstance(value, float):
                formatted_value = f"{value:.2f}"
            else:
                formatted_value = str(value)

            print(f"  {name}: {formatted_value}")
        print()

    # Print tags if available
    tags = record.get("tags", {})
    if tags:
        print("Tags:")
        for metric_name, metric_tags in sorted(tags.items()):
            print(f"  {metric_name}: {metric_tags}")
        print()

    # Print additional data
    extra_keys = set(record.keys()) - {
        "run_id",
        "type",
        "timestamp",
        "git_commit",
        "metrics",
        "tags",
    }
    if extra_keys:
        print("Additional Data:")
        for key in sorted(extra_keys):
            value = record[key]
            if isinstance(value, (dict, list)):
                print(f"  {key}:")
                print("   ", json.dumps(value, indent=2).replace("\n", "\n    "))
            else:
                print(f"  {key}: {value}")

    return 0

format_duration ¤

format_duration(seconds: float) -> str

Format duration for display.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
21
22
23
24
25
26
27
28
29
30
def format_duration(seconds: float) -> str:
    """Format duration for display."""
    if seconds < 60:
        return f"{seconds:.1f}s"
    elif seconds < 3600:
        minutes = seconds / 60
        return f"{minutes:.1f}m"
    else:
        hours = seconds / 3600
        return f"{hours:.1f}h"

format_size ¤

format_size(mb: float) -> str

Format size in MB for display.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
33
34
35
36
37
38
39
def format_size(mb: float) -> str:
    """Format size in MB for display."""
    if mb < 1024:
        return f"{mb:.1f} MB"
    else:
        gb = mb / 1024
        return f"{gb:.1f} GB"

format_timestamp ¤

format_timestamp(timestamp: str) -> str

Format ISO timestamp for display.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
12
13
14
15
16
17
18
def format_timestamp(timestamp: str) -> str:
    """Format ISO timestamp for display."""
    try:
        dt = datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
        return dt.strftime("%Y-%m-%d %H:%M:%S")
    except ValueError:
        return timestamp

get_storage ¤

get_storage() -> JSONLinesStorage

Get the metrics storage instance.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
42
43
44
45
46
def get_storage() -> JSONLinesStorage:
    """Get the metrics storage instance."""
    project_root = Path.cwd()
    storage_path = project_root / ".panther-metrics"
    return JSONLinesStorage(storage_path)

main ¤

main() -> int

Main CLI entry point.

Source code in .venv/lib/python3.10/site-packages/panther/metrics/cli.py
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
def main() -> int:
    """Main CLI entry point."""
    parser = argparse.ArgumentParser(
        prog="panther-metrics", description="PANTHER metrics management CLI"
    )

    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    # List command
    list_parser = subparsers.add_parser("ls", aliases=["list"], help="List recent metrics records")
    list_parser.add_argument(
        "--limit",
        "-n",
        type=int,
        default=20,
        help="Maximum number of records to show (default: 20)",
    )

    # Show command
    show_parser = subparsers.add_parser(
        "show", help="Show detailed information about a specific record"
    )
    show_parser.add_argument("run_id", help="Run ID to show details for")

    # Export command
    export_parser = subparsers.add_parser("export", help="Export metrics records to JSON")
    export_parser.add_argument("--output", "-o", help="Output file (default: stdout)")
    export_parser.add_argument("--date", "-d", help="Filter by date (YYYY-MM-DD format)")
    export_parser.add_argument(
        "--limit", "-n", type=int, help="Maximum number of records to export"
    )

    args = parser.parse_args()

    if not args.command:
        parser.print_help()
        return 1

    # Route to appropriate command handler
    if args.command in ("ls", "list"):
        return cmd_list(args)
    elif args.command == "show":
        return cmd_show(args)
    elif args.command == "export":
        return cmd_export(args)
    else:
        print(f"Unknown command: {args.command}", file=sys.stderr)
        return 1