Output single list of json objects

This commit is contained in:
2026-03-16 02:42:04 +01:00
parent 7f93466645
commit 1c7637fbc3
4 changed files with 49 additions and 33 deletions

View File

@@ -30,6 +30,10 @@ func runTrend(args []string) {
results := fanOutTrend(sf.targets, filter, win)
if sf.jsonOut {
printTrendJSONArray(results)
return
}
for _, r := range results {
if hdr := targetHeader(r.target, r.resp.GetSource(), len(sf.targets)); hdr != "" {
fmt.Println(hdr)
@@ -38,11 +42,7 @@ func runTrend(args []string) {
fmt.Fprintf(os.Stderr, "error from %s: %v\n", r.target, r.err)
continue
}
if sf.jsonOut {
printTrendJSON(r)
} else {
printTrendTable(r)
}
printTrendTable(r)
if len(sf.targets) > 1 {
fmt.Println()
}
@@ -90,7 +90,7 @@ func printTrendTable(r trendResult) {
printTable(os.Stdout, rows)
}
func printTrendJSON(r trendResult) {
func printTrendJSONArray(results []trendResult) {
type point struct {
Ts int64 `json:"ts"`
Count int64 `json:"count"`
@@ -100,14 +100,22 @@ func printTrendJSON(r trendResult) {
Target string `json:"target"`
Points []point `json:"points"`
}
o := out{
Source: r.resp.Source,
Target: r.target,
Points: make([]point, len(r.resp.Points)),
rows := make([]out, 0, len(results))
for _, r := range results {
if r.err != nil {
fmt.Fprintf(os.Stderr, "error from %s: %v\n", r.target, r.err)
continue
}
o := out{
Source: r.resp.Source,
Target: r.target,
Points: make([]point, len(r.resp.Points)),
}
for i, p := range r.resp.Points {
o.Points[i] = point{Ts: p.TimestampUnix, Count: p.Count}
}
rows = append(rows, o)
}
for i, p := range r.resp.Points {
o.Points[i] = point{Ts: p.TimestampUnix, Count: p.Count}
}
b, _ := json.Marshal(o)
b, _ := json.Marshal(rows)
fmt.Println(string(b))
}