Merge branch 'main' into feature/configuration-new-format

Signed-off-by: HOLLEVILLE Matthis <99146727+matthisholleville@users.noreply.github.com>
This commit is contained in:
HOLLEVILLE Matthis
2023-04-05 16:10:34 +02:00
committed by GitHub
4 changed files with 229 additions and 51 deletions

View File

@@ -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,72 @@ 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: problems,
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(output string) error {
if len(a.Results) == 0 {
return nil
}
var bar *progressbar.ProgressBar
if output != "json" {
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
if output != "json" {
bar.Add(1)
}
a.Results[index] = analysis
}
return nil
}

View File

@@ -0,0 +1,123 @@
package analysis
import (
"encoding/json"
"fmt"
"github.com/k8sgpt-ai/k8sgpt/pkg/analyzer"
"github.com/stretchr/testify/require"
"testing"
)
func TestAnalysis_NoProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{},
Namespace: "default",
}
expected := JsonOutput{
Status: StateOK,
Problems: 0,
Results: []analyzer.Result{},
}
gotJson, err := analysis.JsonOutput()
if err != nil {
t.Error(err)
}
got := JsonOutput{}
err = json.Unmarshal(gotJson, &got)
if err != nil {
t.Error(err)
}
fmt.Println(got)
fmt.Println(expected)
require.Equal(t, got, expected)
}
func TestAnalysis_ProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
{
"Deployment",
"test-deployment",
[]string{"test-problem"},
"test-solution",
"parent-resource"},
},
Namespace: "default",
}
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 1,
Results: []analyzer.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem"},
"test-solution",
"parent-resource"},
},
}
gotJson, err := analysis.JsonOutput()
if err != nil {
t.Error(err)
}
got := JsonOutput{}
err = json.Unmarshal(gotJson, &got)
if err != nil {
t.Error(err)
}
fmt.Println(got)
fmt.Println(expected)
require.Equal(t, got, expected)
}
func TestAnalysis_MultipleProblemJsonOutput(t *testing.T) {
analysis := Analysis{
Results: []analyzer.Result{
{
"Deployment",
"test-deployment",
[]string{"test-problem", "another-test-problem"},
"test-solution",
"parent-resource"},
},
Namespace: "default",
}
expected := JsonOutput{
Status: StateProblemDetected,
Problems: 2,
Results: []analyzer.Result{
{"Deployment",
"test-deployment",
[]string{"test-problem", "another-test-problem"},
"test-solution",
"parent-resource"},
},
}
gotJson, err := analysis.JsonOutput()
if err != nil {
t.Error(err)
}
got := JsonOutput{}
err = json.Unmarshal(gotJson, &got)
if err != nil {
t.Error(err)
}
fmt.Println(got)
fmt.Println(expected)
require.Equal(t, got, expected)
}