Skip to content

Commit

Permalink
Merge pull request #23 from k8sgpt-ai/feature/optimisation-caching
Browse files Browse the repository at this point in the history
feat: adding performance optimisation through local caching
  • Loading branch information
AlexsJones committed Mar 22, 2023
2 parents 05d2aef + e4f138d commit 35c8dee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
AI Powered Kubernetes debugging for SRE, Platform and DevOps teams.
<br />

<img src="images/demo.gif" width=650px; />
<img src="images/demo2.gif" width=650px; />

## What is k8sgpt?

Expand Down
Binary file added images/demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 38 additions & 5 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package analyzer

import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/fatih/color"
"github.com/k8sgpt-ai/k8sgpt/pkg/ai"
"github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -50,20 +52,51 @@ func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient *ai.Cl
}

}
for key, value := range brokenPods {
fmt.Printf("%s: %s\n", color.YellowString(key), color.RedString(value[0]))

count := 0
for key, value := range brokenPods {
fmt.Printf("%s: %s: %s\n", color.CyanString("%d", count), color.YellowString(key), color.RedString(value[0]))
count++
if explain {
s := spinner.New(spinner.CharSets[35], 100*time.Millisecond) // Build our new spinner
s.Start()

response, err := aiClient.GetCompletion(ctx, strings.Join(value, " "))
inputValue := strings.Join(value, " ")

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

color.Green(string(output))
continue
}

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

fmt.Printf("%s\n", color.GreenString(response))
if !viper.IsSet(sEnc) {
viper.Set(sEnc, base64.StdEncoding.EncodeToString([]byte(response)))
if err := viper.WriteConfig(); err != nil {
return err
}
}
}
}

Expand Down

0 comments on commit 35c8dee

Please sign in to comment.