Adds `langgraph deploy metrics` with compact, agent-friendly JSON grouped by section, chart, and series. Existing time-range and metric filters are preserved; host-backend and its UI response schema are unchanged. - Default: real `project_metric`, inferred units, three-decimal values, latest/min/max/avg, and `[ISO timestamp, value]` points; empty sections are explicit. - `--summary`: same shape without points. `--raw`: original host-backend schema without reshaping or rounding. Mutually exclusive. - Bounds always report the requested window: end defaults to now (UTC), and start defaults to 15 minutes before the resolved end. With only `--end-time`, start is 15 minutes before that value; with only `--start-time`, end is now. The CLI sends both bounds explicitly and reports identical values in the output. `step_seconds` is the most common within-series timestamp gap (smallest on ties), uses an integer for whole seconds, and is null only when no series has two timestamps. Statistics ignore missing/non-finite values; latest means latest non-missing sample. Size comparison (realistic synthetic unit-test fixture, identical compact JSON serialization; not a live deployment benchmark): | Before: raw UI schema | After: default | After: summary | | --- | --- | --- | | 4,302 bytes | 1,226 bytes (72% smaller) | 806 bytes (81% smaller) | Known gotcha (pre-existing, unchanged here): pass `--host-url` after the subcommand. A group-level `langgraph deploy --host-url ... metrics <id>` silently ignores that option and can hit prod instead, producing a confusing 403. ```bash langgraph deploy metrics --host-url https://dev.api.host.langchain.com <id> ``` Follow-up: https://github.com/langchain-ai/langgraph/pull/9562 adds compute and Postgres tier scaling, stacked on this branch. Retarget it to `main` after this PR merges. ## References - [Slack thread](https://langchain.slack.com/archives/C0BHM1C5R5L/p1793216413707489) Made by [Open SWE](https://github.com/langchain-ai/open-swe) · [view thread](https://openswe.langchain.dev/agents/5c8cafeb-c089-587f-aa85-af220f2f13f6) · openai:gpt-6.1-sol (medium) --------- Co-authored-by: Hari Dhanushkodi <hari-dhanushkodi@users.noreply.github.com> Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.5 KiB
Python
95 lines
3.5 KiB
Python
"""Compact deployment chart responses for command-line consumers."""
|
|
|
|
import math
|
|
from collections import Counter, defaultdict
|
|
from datetime import datetime, timezone
|
|
from statistics import fmean
|
|
from typing import Any
|
|
|
|
|
|
def _timestamp(value: str) -> datetime:
|
|
timestamp = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
return timestamp.replace(tzinfo=timestamp.tzinfo or timezone.utc).astimezone(
|
|
timezone.utc
|
|
)
|
|
|
|
|
|
def _iso(value: datetime) -> str:
|
|
return value.isoformat().replace("+00:00", "Z")
|
|
|
|
|
|
def _unit(title: str, metric: str | None) -> str | None:
|
|
if "%" in title:
|
|
return "percent"
|
|
if metric in {
|
|
"restart_count",
|
|
"replica_count",
|
|
"worker_count",
|
|
"lg_run_count",
|
|
"run_count",
|
|
}:
|
|
return "count"
|
|
return None
|
|
|
|
|
|
def compact_deployment_metrics(
|
|
response: dict[str, Any],
|
|
deployment_id: str,
|
|
*,
|
|
start_time: str,
|
|
end_time: str,
|
|
summary: bool = False,
|
|
) -> dict[str, Any]:
|
|
"""Reshape UI charts without mutating the response; step is in seconds."""
|
|
sections = {}
|
|
spacings: Counter[float] = Counter()
|
|
source_sections = list(response.get("sub_sections") or [])
|
|
if response.get("charts"):
|
|
source_sections.insert(0, response)
|
|
for section in source_sections:
|
|
charts = {}
|
|
for chart in section.get("charts") or []:
|
|
by_series = defaultdict(list)
|
|
for point in chart.get("data") or []:
|
|
timestamp = _timestamp(point["timestamp"])
|
|
value = point.get("value")
|
|
if value is not None and not math.isfinite(value):
|
|
value = None
|
|
by_series[point["series_id"]].append((timestamp, value))
|
|
series_output = {}
|
|
for series in chart.get("series") or []:
|
|
points = sorted(by_series[series["id"]], key=lambda point: point[0])
|
|
times = sorted({timestamp for timestamp, _ in points})
|
|
spacings.update(
|
|
(b - a).total_seconds()
|
|
for a, b in zip(times, times[1:], strict=False)
|
|
)
|
|
values = [value for _, value in points if value is not None]
|
|
metric = series.get("project_metric")
|
|
stats = {
|
|
"metric": metric,
|
|
"unit": _unit(chart["title"], metric),
|
|
"latest": round(values[-1], 3) if values else None,
|
|
"min": round(min(values), 3) if values else None,
|
|
"max": round(max(values), 3) if values else None,
|
|
"avg": round(fmean(values), 3) if values else None,
|
|
}
|
|
if not summary:
|
|
stats["points"] = [
|
|
[
|
|
_iso(timestamp),
|
|
round(value, 3) if value is not None else None,
|
|
]
|
|
for timestamp, value in points
|
|
]
|
|
series_output[series["name"]] = stats
|
|
charts[chart["title"]] = series_output or {"_empty": True}
|
|
sections[section["title"]] = charts or {"_empty": True}
|
|
step = min(spacings, key=lambda gap: (-spacings[gap], gap)) if spacings else None
|
|
return {
|
|
"deployment_id": deployment_id,
|
|
"start_time": start_time,
|
|
"end_time": end_time,
|
|
"step_seconds": int(step) if step is not None and step.is_integer() else step,
|
|
"sections": sections or {"_empty": True},
|
|
}
|