# Studio Plan D: Split TiDB and TiKV CPU Metrics

## Summary

Fix the metrics evidence view where TiDB CPU and TiKV CPU can be merged into the
same chart group. The issue is visible in workspace
`ws-1776310780344-7012f3255`, diagnosis run
`clinic-downloads/2026-04-05/diagnosis-001`.

The root cause is that `collect_evidence.py` currently uses a single CPU PromQL
for both components:

```promql
rate(process_cpu_seconds_total{component=~"tidb|tikv"}[1m])
```

The frontend then groups metric charts by PromQL, so TiDB and TiKV CPU share the
same group. This plan fixes both the collection-side identity and the UI
fallback grouping.

This plan only changes TiDB/TiKV CPU metric grouping. It does not change Key
Evidence, Quick Links, Studio width, or per-entry Grafana link behavior beyond
preserving existing metric links.

## Key Changes

- In `collect_evidence.py`, split CPU collection and metric generation into
  component-specific PromQL:

```promql
rate(process_cpu_seconds_total{component="tidb"}[1m])
rate(process_cpu_seconds_total{component="tikv"}[1m])
```

- Generate CPU metric records with explicit identity fields:
  - `component`: `tidb` or `tikv`
  - `metric_group_id`: `tidb_cpu:<nodegroup>` or `tikv_cpu:<nodegroup>`
- Ensure each CPU group uses its own:
  - `promql`
  - `chart_data`
  - `link`
- Update compact metric output to preserve the new identity fields when writing
  `evidence_summary_for_agent.json` and hydrated reports.
- Update `DiagnosisEvidencePanel` grouping logic:
  - Prefer `metric_group_id` when present.
  - Fallback to `component + name + promql`.
  - Preserve current behavior for old files without new fields.
- Ensure TiKV Thread CPU remains separate from TiKV process CPU.

## Test Plan

- Add or update unit coverage for `collect_evidence.py` to verify TiDB CPU and
  TiKV CPU records have different PromQL strings.
- Verify generated CPU records include `component` and `metric_group_id`.
- Verify compact metric output preserves `component` and `metric_group_id`.
- Add or update frontend grouping coverage if available; otherwise use a fixture
  to manually verify TiDB CPU and TiKV CPU render as separate cards/charts.
- Confirm old `metrics.json` files without `metric_group_id` still render.
- Manually inspect the target workspace run and confirm:
  - TiDB CPU is one chart group.
  - TiKV CPU is another chart group.
  - TiKV Thread CPU is not merged into either process CPU chart.

## Assumptions

- The mixed chart is caused by shared PromQL-based grouping, not by incorrect
  raw Prometheus labels.
- Splitting TiDB and TiKV CPU at collection time is preferable because it makes
  links, chart data, and report summaries match the visible UI grouping.
