Skip to content

Commit

Permalink
chore: moved code
Browse files Browse the repository at this point in the history
Signed-off-by: AlexsJones <alexsimonjones@gmail.com>
  • Loading branch information
AlexsJones committed Mar 27, 2023
1 parent 0852c65 commit a194d4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
41 changes: 1 addition & 40 deletions cmd/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package analyze

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -97,7 +96,7 @@ var AnalyzeCmd = &cobra.Command{
for _, analysis := range *analysisResults {

if explain {
parsedText, err := parseViaAI(ctx, aiClient, analysis.Error)
parsedText, err := analyzer.ParseViaAI(ctx, aiClient, analysis.Error)
if err != nil {
color.Red("Error: %v", err)
continue
Expand Down Expand Up @@ -129,44 +128,6 @@ var AnalyzeCmd = &cobra.Command{
},
}

func parseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) {
// parse the text with the AI backend

inputKey := strings.Join(prompt, " ")
// Check for cached data
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
// find in viper cache
if viper.IsSet(sEnc) {
// retrieve data from cache
response := viper.GetString(sEnc)
if response == "" {
color.Red("error retrieving cached data")
return "", nil
}
output, err := base64.StdEncoding.DecodeString(response)
if err != nil {
color.Red("error decoding cached data: %v", err)
return "", nil
}
return string(output), nil
}

response, err := aiClient.GetCompletion(ctx, inputKey)
if err != nil {
color.Red("error getting completion: %v", err)
return "", nil
}

if !viper.IsSet(sEnc) {
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
if err := viper.WriteConfig(); err != nil {
color.Red("error writing config: %v", err)
return "", nil
}
}
return response, nil
}

func init() {

// array of strings flag
Expand Down
45 changes: 43 additions & 2 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package analyzer

import (
"context"
"encoding/base64"
"strings"

"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/viper"
)

func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool, analysisResults *[]Analysis) error {
Expand All @@ -20,13 +24,50 @@ func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI
}

err = AnalyzePersistentVolumeClaim(ctx, client, aiClient, explain, analysisResults)
if err != nil {
if err != nil {
return err
}

err = AnalyzeEndpoints(ctx, client, aiClient, explain, analysisResults)
if err != nil {
return err
}
return nil
}

func ParseViaAI(ctx context.Context, aiClient ai.IAI, prompt []string) (string, error) {
// parse the text with the AI backend
inputKey := strings.Join(prompt, " ")
// Check for cached data
sEnc := base64.StdEncoding.EncodeToString([]byte(inputKey))
// find in viper cache
if viper.IsSet(sEnc) {
// retrieve data from cache
response := viper.GetString(sEnc)
if response == "" {
color.Red("error retrieving cached data")
return "", nil
}
output, err := base64.StdEncoding.DecodeString(response)
if err != nil {
color.Red("error decoding cached data: %v", err)
return "", nil
}
return string(output), nil
}

response, err := aiClient.GetCompletion(ctx, inputKey)
if err != nil {
color.Red("error getting completion: %v", err)
return "", nil
}

if !viper.IsSet(sEnc) {
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
if err := viper.WriteConfig(); err != nil {
color.Red("error writing config: %v", err)
return "", nil
}
}
return response, nil
}

0 comments on commit a194d4a

Please sign in to comment.