@@ -244,18 +244,22 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse, flagVer
244
244
Rows : [][]interface {}{row },
245
245
}
246
246
247
- var predictionMetrics string
247
+ var out string
248
248
apiMetrics , err := getAPIMetrics (ctx .App .Name , api .Name )
249
- if err != nil || apiMetrics == nil {
250
- predictionMetrics = "\n metrics are not available yet\n "
251
- } else {
252
- statusTable = appendNetworkMetrics (statusTable , apiMetrics )
253
- if api .Tracker != nil {
254
- predictionMetrics = "\n " + predictionMetricsTable (apiMetrics , api ) + "\n "
249
+ statusTable = appendNetworkMetrics (statusTable , apiMetrics ) // adds blank stats when there is an error
250
+ out = "\n " + table .MustFormat (statusTable ) + "\n "
251
+
252
+ var predictionMetrics string
253
+ if err != nil {
254
+ if ! strings .Contains (err .Error (), "api is still initializing" ) {
255
+ predictionMetrics = fmt .Sprintf ("\n error fetching metrics: %s\n " , err .Error ())
255
256
}
256
257
}
257
258
258
- out := "\n " + table .MustFormat (statusTable ) + "\n "
259
+ if api .Tracker != nil && len (predictionMetrics ) == 0 {
260
+ predictionMetrics = "\n " + predictionMetricsTable (apiMetrics , api ) + "\n "
261
+ }
262
+
259
263
out += predictionMetrics
260
264
261
265
out += "\n " + console .Bold ("url: " ) + apiEndpoint
@@ -275,44 +279,55 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse, flagVer
275
279
return out , nil
276
280
}
277
281
278
- func getAPIMetrics (appName , apiName string ) (* schema.APIMetrics , error ) {
282
+ func getAPIMetrics (appName , apiName string ) (schema.APIMetrics , error ) {
279
283
params := map [string ]string {"appName" : appName , "apiName" : apiName }
280
284
httpResponse , err := HTTPGet ("/metrics" , params )
281
285
if err != nil {
282
- return nil , err
286
+ return schema. APIMetrics {} , err
283
287
}
284
288
285
289
var apiMetrics schema.APIMetrics
286
290
err = json .Unmarshal (httpResponse , & apiMetrics )
287
291
if err != nil {
288
- return nil , err
292
+ return schema. APIMetrics {} , err
289
293
}
290
294
291
- return & apiMetrics , nil
295
+ return apiMetrics , nil
292
296
}
293
297
294
- func appendNetworkMetrics (apiTable table.Table , apiMetrics * schema.APIMetrics ) table.Table {
295
- headers := []table.Header {
296
- {Title : "avg latency" , Hidden : apiMetrics .NetworkStats .Latency == nil },
297
- {Title : "2XX" , Hidden : apiMetrics .NetworkStats .Code2XX == 0 },
298
- {Title : "4XX" , Hidden : apiMetrics .NetworkStats .Code4XX == 0 },
299
- {Title : "5XX" , Hidden : apiMetrics .NetworkStats .Code5XX == 0 },
300
- }
301
-
302
- latency := ""
303
- if apiMetrics .NetworkStats .Latency != nil {
304
- if * apiMetrics .NetworkStats .Latency < 1000 {
305
- latency = fmt .Sprintf ("%.6g ms" , * apiMetrics .NetworkStats .Latency )
306
- } else {
307
- latency = fmt .Sprintf ("%.6g s" , (* apiMetrics .NetworkStats .Latency )/ 1000 )
298
+ func appendNetworkMetrics (apiTable table.Table , apiMetrics schema.APIMetrics ) table.Table {
299
+ latency := "-"
300
+ code2XX := "-"
301
+ code4XX := 0
302
+ code5XX := 0
303
+
304
+ if apiMetrics .NetworkStats != nil {
305
+ code4XX = apiMetrics .NetworkStats .Code4XX
306
+ code5XX = apiMetrics .NetworkStats .Code5XX
307
+ if apiMetrics .NetworkStats .Latency != nil {
308
+ if * apiMetrics .NetworkStats .Latency < 1000 {
309
+ latency = fmt .Sprintf ("%.6g ms" , * apiMetrics .NetworkStats .Latency )
310
+ } else {
311
+ latency = fmt .Sprintf ("%.6g s" , (* apiMetrics .NetworkStats .Latency )/ 1000 )
312
+ }
313
+ }
314
+ if apiMetrics .NetworkStats .Code2XX != 0 {
315
+ code2XX = s .Int (apiMetrics .NetworkStats .Code2XX )
308
316
}
309
317
}
310
318
319
+ headers := []table.Header {
320
+ {Title : "avg latency" },
321
+ {Title : "2XX" },
322
+ {Title : "4XX" , Hidden : code4XX == 0 },
323
+ {Title : "5XX" , Hidden : code5XX == 0 },
324
+ }
325
+
311
326
row := []interface {}{
312
327
latency ,
313
- apiMetrics . NetworkStats . Code2XX ,
314
- apiMetrics . NetworkStats . Code4XX ,
315
- apiMetrics . NetworkStats . Code5XX ,
328
+ code2XX ,
329
+ code4XX ,
330
+ code5XX ,
316
331
}
317
332
318
333
apiTable .Headers = append (apiTable .Headers , headers ... )
@@ -321,7 +336,7 @@ func appendNetworkMetrics(apiTable table.Table, apiMetrics *schema.APIMetrics) t
321
336
return apiTable
322
337
}
323
338
324
- func predictionMetricsTable (apiMetrics * schema.APIMetrics , api * context.API ) string {
339
+ func predictionMetricsTable (apiMetrics schema.APIMetrics , api * context.API ) string {
325
340
if api .Tracker == nil {
326
341
return ""
327
342
}
@@ -332,20 +347,23 @@ func predictionMetricsTable(apiMetrics *schema.APIMetrics, api *context.API) str
332
347
return regressionMetricsTable (apiMetrics )
333
348
}
334
349
335
- func regressionMetricsTable (apiMetrics * schema.APIMetrics ) string {
350
+ func regressionMetricsTable (apiMetrics schema.APIMetrics ) string {
336
351
minStr := "-"
337
- if apiMetrics .RegressionStats .Min != nil {
338
- minStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Min )
339
- }
340
-
341
352
maxStr := "-"
342
- if apiMetrics .RegressionStats .Max != nil {
343
- maxStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Max )
344
- }
345
-
346
353
avgStr := "-"
347
- if apiMetrics .RegressionStats .Avg != nil {
348
- avgStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Avg )
354
+
355
+ if apiMetrics .RegressionStats != nil {
356
+ if apiMetrics .RegressionStats .Min != nil {
357
+ minStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Min )
358
+ }
359
+
360
+ if apiMetrics .RegressionStats .Max != nil {
361
+ maxStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Max )
362
+ }
363
+
364
+ if apiMetrics .RegressionStats .Avg != nil {
365
+ avgStr = fmt .Sprintf ("%.9g" , * apiMetrics .RegressionStats .Avg )
366
+ }
349
367
}
350
368
351
369
t := table.Table {
@@ -360,7 +378,7 @@ func regressionMetricsTable(apiMetrics *schema.APIMetrics) string {
360
378
return table .MustFormat (t )
361
379
}
362
380
363
- func classificationMetricsTable (apiMetrics * schema.APIMetrics ) string {
381
+ func classificationMetricsTable (apiMetrics schema.APIMetrics ) string {
364
382
classList := make ([]string , len (apiMetrics .ClassDistribution ))
365
383
366
384
i := 0
0 commit comments