package main

import "encoding/json"

// BuildDashboardPayloadJSON builds the same JSON shape the browser expects (including latency).
// collectorToCentralMs is 采集器到监控中心 TCP 建连毫秒数，失败传 -1。
func BuildDashboardPayloadJSON(collectorToCentralMs float64) ([]byte, error) {
	m, err := CollectMetrics()
	if err != nil {
		return nil, err
	}
	b, err := json.Marshal(m)
	if err != nil {
		return nil, err
	}
	var o map[string]any
	if err := json.Unmarshal(b, &o); err != nil {
		return nil, err
	}
	o["latency"] = map[string]any{
		"clientToServer": collectorToCentralMs,
		"external":       readExternalLatencies(),
	}
	return json.Marshal(o)
}
