mirror of
https://github.com/k8sgpt-ai/k8sgpt.git
synced 2025-09-16 15:20:38 +00:00
chore: made json output prettier and improved output
Signed-off-by: Thomas Schuetz <thomas.schuetz@t-sc.eu>
This commit is contained in:
@@ -2,10 +2,16 @@ package analysis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
|
||||
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
|
||||
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Analysis struct {
|
||||
@@ -19,6 +25,19 @@ type Analysis struct {
|
||||
Explain bool
|
||||
}
|
||||
|
||||
type AnalysisStatus string
|
||||
|
||||
const (
|
||||
StateOK AnalysisStatus = "OK"
|
||||
StateProblemDetected AnalysisStatus = "ProblemDetected"
|
||||
)
|
||||
|
||||
type JsonOutput struct {
|
||||
Status AnalysisStatus `json:"status"`
|
||||
Problems int `json:"problems"`
|
||||
Results []analyzer.Result `json:"results"`
|
||||
}
|
||||
|
||||
func (a *Analysis) RunAnalysis() error {
|
||||
|
||||
activeFilters := viper.GetStringSlice("active_filters")
|
||||
@@ -70,3 +89,70 @@ func (a *Analysis) RunAnalysis() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Analysis) JsonOutput() ([]byte, error) {
|
||||
var problems int
|
||||
var status AnalysisStatus
|
||||
for _, result := range a.Results {
|
||||
problems += len(result.Error)
|
||||
}
|
||||
if problems > 0 {
|
||||
status = StateProblemDetected
|
||||
} else {
|
||||
status = StateOK
|
||||
}
|
||||
|
||||
result := JsonOutput{
|
||||
Problems: len(a.Results),
|
||||
Results: a.Results,
|
||||
Status: status,
|
||||
}
|
||||
output, err := json.MarshalIndent(result, "", " ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error marshalling json: %v", err)
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (a *Analysis) PrintOutput() {
|
||||
fmt.Println("")
|
||||
if len(a.Results) == 0 {
|
||||
fmt.Println(color.GreenString("No problems detected"))
|
||||
}
|
||||
for n, result := range a.Results {
|
||||
fmt.Printf("%s %s(%s)\n", color.CyanString("%d", n),
|
||||
color.YellowString(result.Name), color.CyanString(result.ParentObject))
|
||||
for _, err := range result.Error {
|
||||
fmt.Printf("- %s %s\n", color.RedString("Error:"), color.RedString(err))
|
||||
}
|
||||
fmt.Println(color.GreenString(result.Details + "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Analysis) GetAIResults(progressBar bool) error {
|
||||
if len(a.Results) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bar *progressbar.ProgressBar
|
||||
if progressBar {
|
||||
bar = progressbar.Default(int64(len(a.Results)))
|
||||
}
|
||||
|
||||
for index, analysis := range a.Results {
|
||||
parsedText, err := a.AIClient.Parse(a.Context, analysis.Error, a.NoCache)
|
||||
if err != nil {
|
||||
// Check for exhaustion
|
||||
if strings.Contains(err.Error(), "status code: 429") {
|
||||
color.Red("Exhausted API quota. Please try again later")
|
||||
os.Exit(1)
|
||||
}
|
||||
color.Red("Error: %v", err)
|
||||
continue
|
||||
}
|
||||
analysis.Details = parsedText
|
||||
bar.Add(1)
|
||||
a.Results[index] = analysis
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user