MCPcopy
hub / github.com/elastic/go-elasticsearch / main

Function main

_examples/encoding/gjson.go:43–112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41}
42
43func main() {
44 es, err := elasticsearch.New()
45 if err != nil {
46 log.Fatalf("Error creating client: %s", err)
47 }
48 defer func() {
49 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
50 defer cancel()
51 if err := es.Close(ctx); err != nil {
52 fmt.Printf("Error closing the client: %s\n", err)
53 }
54 }()
55
56 res, err := es.Cluster.Stats(es.Cluster.Stats.WithHuman())
57 if err != nil {
58 log.Fatalf("Error getting response: %s", err)
59 }
60 defer res.Body.Close()
61
62 json := read(res.Body)
63
64 fmt.Println(strings.Repeat("─", 50))
65 faint.Print("cluster ")
66 // Get cluster name
67 bold.Print(gjson.Get(json, "cluster_name"))
68
69 faint.Print(" status=")
70 // Get cluster health status
71 status := gjson.Get(json, "status")
72 switch status.Str {
73 case "green":
74 bold.Add(color.FgHiGreen).Print(status)
75 case "yellow":
76 bold.Add(color.FgHiYellow).Print(status)
77 case "red":
78 bold.Add(color.FgHiRed).Print(status)
79 default:
80 bold.Add(color.FgHiRed, color.Underline).Print(status)
81 }
82 fmt.Println("\n" + strings.Repeat("─", 50))
83
84 stats := []string{
85 "indices.count",
86 "indices.docs.count",
87 "indices.store.size",
88 "nodes.count.total",
89 "nodes.os.mem.used_percent",
90 "nodes.process.cpu.percent",
91 "nodes.jvm.versions.#.version",
92 "nodes.jvm.mem.heap_used",
93 "nodes.jvm.mem.heap_max",
94 "nodes.fs.free",
95 }
96
97 var maxwidth int
98 for _, item := range stats {
99 if len(item) > maxwidth {
100 maxwidth = len(item)

Callers

nothing calls this directly

Calls 8

readFunction · 0.85
PrintfMethod · 0.80
CloseMethod · 0.65
StatsMethod · 0.65
AddMethod · 0.65
WithTimeoutMethod · 0.45
WithHumanMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected