in action/prometheus/metrics.go [123:153]
func generateTerminalLineChart(response *PromResponse, metricName string) error {
var yValues []float64
// Iterate over the results and extract the values for the specified metric
for _, result := range response.Data.Result {
if name, ok := result.Metric["__name__"]; ok && name == metricName {
// Parse the metric value
if valueStr, ok := result.Value[1].(string); ok {
value, err := strconv.ParseFloat(valueStr, 64)
if err != nil {
return fmt.Errorf("error converting value to float: %v", err)
} else {
yValues = append(yValues, value)
}
} else {
return fmt.Errorf("error converting value to float: %v", result.Value[1])
}
}
}
// Check if any values were found
if len(yValues) == 0 {
return fmt.Errorf("no data found for metric: %s", metricName)
}
// Generate and display the ASCII line chart
graph := asciigraph.Plot(yValues, asciigraph.Width(50), asciigraph.Height(10), asciigraph.Caption(metricName))
tool.Logger.Infof(graph)
return nil
}