MCPcopy Create free account
hub / github.com/deepflowio/deepflow / statsQuery

Function statsQuery

cli/ctl/promql.go:169–212  ·  view source on GitHub ↗
(cmd *cobra.Command, from, to int64, metric string, targetLabels, appLabels []string)

Source from the content-addressed store, hash-verified

167}
168
169func statsQuery(cmd *cobra.Command, from, to int64, metric string, targetLabels, appLabels []string) error {
170 server := common.GetServerInfo(cmd)
171 url := fmt.Sprintf("http://%s:%d/prom/api/v1/analysis?from=%d&to=%d", server.IP, server.Port, from, to)
172 if metric != "" {
173 url += fmt.Sprintf("&metric=%s", metric)
174 }
175 if len(targetLabels) > 0 {
176 url += fmt.Sprintf("&target=%s", strings.Join(targetLabels, ","))
177 }
178 if len(appLabels) > 0 {
179 url += fmt.Sprintf("&app=%s", strings.Join(appLabels, ","))
180 }
181 response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
182 if err != nil {
183 fmt.Println(err)
184 return err
185 }
186
187 t := table.New()
188 t.SetHeader(response.Get("Columns").MustStringArray())
189 tableItems := make([][]string, 0, len(response.Get("Values").MustArray()))
190 var rowItems []string
191 for i := range response.Get("Values").MustArray() {
192 row := response.Get("Values").GetIndex(i)
193 rowItems = make([]string, 0, len(response.Get("Columns").MustStringArray()))
194 for k, v := range response.Get("Columns").MustStringArray() {
195 col := row.GetIndex(k)
196 var val string
197 switch strings.ToUpper(v) {
198 case "QUERY_COUNT":
199 val = strconv.Itoa(col.MustInt())
200 case "AVG_DURATION(MS)", "MAX_DURATION(MS)":
201 val = fmt.Sprintf("%f", col.MustFloat64())
202 default:
203 val = col.MustString()
204 }
205 rowItems = append(rowItems, val)
206 }
207 tableItems = append(tableItems, rowItems)
208 }
209 t.AppendBulk(tableItems)
210 t.Render()
211 return nil
212}

Callers 1

statsSubCommandFunction · 0.85

Calls 6

NewMethod · 0.80
SetHeaderMethod · 0.80
AppendBulkMethod · 0.80
RenderMethod · 0.80
GetMethod · 0.65
GetORGIDMethod · 0.45

Tested by

no test coverage detected