From fe20fa6f68faff012f75e610eacaacd447b4e192 Mon Sep 17 00:00:00 2001 From: Danny Piper Date: Mon, 4 Mar 2024 19:43:53 +0000 Subject: [PATCH] feat: you can now do manual crafting --- kube_cache/model/model.go | 4 +- kube_cache/server/cache.go | 91 ++++++++++++++++++++------------------ kube_cache/server/index.go | 75 ++++++++++++++++++++++++++++++- 3 files changed, 124 insertions(+), 46 deletions(-) diff --git a/kube_cache/model/model.go b/kube_cache/model/model.go index f53da92..ab72c00 100644 --- a/kube_cache/model/model.go +++ b/kube_cache/model/model.go @@ -70,8 +70,8 @@ func New(metrics *metrics.Metrics, ai *aiStuff.KubeAi, url string) *Database { } func (db *Database) SetKubeImage(kube Kube, image []byte) error { - result := db.Db.Model(&kube).Update("image", image) - return result.Error + result := db.Db.Model(&kube).Update("image", image) + return result.Error } func (db *Database) GetKubeImage(id string) ([]byte, error) { diff --git a/kube_cache/server/cache.go b/kube_cache/server/cache.go index 0f764aa..075ee1d 100644 --- a/kube_cache/server/cache.go +++ b/kube_cache/server/cache.go @@ -1,6 +1,7 @@ package server import ( + "errors" "log" "github.com/CosmicKube/kube_cache/aiStuff" @@ -23,14 +24,17 @@ func New(metrics *metrics.Metrics, database *model.Database, ai *aiStuff.KubeAi) } func (s *Server) Use(engine *gin.Engine) { - engine.GET("/", s.Index) engine.GET("/cache_metrics", s.CacheMetrics) engine.GET("/kubes", s.GetAllKubes) engine.GET("/kubeRecipes", s.GetAllKubeRecipes) engine.GET("/kubeById/:id", s.GetKube) engine.GET("/kubeImageById/:id", s.GetKubeImage) - engine.GET("/kubeImageByIdNew/:id", s.RegenerateImage) + engine.GET("/kubeImageByIdNew/:id", s.RegenerateImage) engine.GET("/kubeRecipeByIds/:id1/:id2", s.GetKubeRecipe) + + // index + engine.GET("/", s.Index) + engine.POST("/", s.IndexPost) } func (s *Server) CacheMetrics(c *gin.Context) { @@ -38,27 +42,27 @@ func (s *Server) CacheMetrics(c *gin.Context) { } func (s *Server) RegenerateImage(c *gin.Context) { - id := c.Param("id") - kube, err := s.Database.GetKube(id) - if err != nil { - c.JSON(500, gin.H{"error": err.Error()}) - return - } - - image, err := s.Ai.GenerateDalleForKube(kube.Name) - if err != nil { - log.Printf("Error generating Dalle for kube: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return - } - - err = s.Database.SetKubeImage(kube, image) - if err != nil { - log.Printf("Cannot save kube image: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return - } - c.JSON(200, gin.H{"message": "Image regenerated"}) + id := c.Param("id") + kube, err := s.Database.GetKube(id) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + image, err := s.Ai.GenerateDalleForKube(kube.Name) + if err != nil { + log.Printf("Error generating Dalle for kube: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + err = s.Database.SetKubeImage(kube, image) + if err != nil { + log.Printf("Cannot save kube image: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + c.JSON(200, gin.H{"message": "Image regenerated"}) } func (s *Server) GetAllKubeRecipes(c *gin.Context) { @@ -82,7 +86,7 @@ func (s *Server) GetKubeImage(c *gin.Context) { c.Data(200, "image/png", image) c.Header("Cache-Control", cacheControlHeader) s.Metrics.IncrementCacheHits() - s.Metrics.IncrementImagesRetrieved() + s.Metrics.IncrementImagesRetrieved() } func (s *Server) GetAllKubes(c *gin.Context) { @@ -110,53 +114,56 @@ func (s *Server) GetKube(c *gin.Context) { func (s *Server) GetKubeRecipe(c *gin.Context) { id1 := c.Param("id1") id2 := c.Param("id2") + outputId, err := s.craft(c, id1, id2) + if err != nil { + log.Printf("Error crafting kube: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + c.JSON(200, outputId) +} + +func (s *Server) craft(c *gin.Context, id1, id2 string) (model.KubeRecipe, error) { recipe, err := s.Database.GetKubeRecipe(id1, id2) if err != nil { s.Metrics.IncrementCacheMisses() kube1, err := s.Database.GetKube(id1) if err != nil { - log.Printf("Cannot get kube: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Cannot get kube 1") } kube2, err := s.Database.GetKube(id2) if err != nil { - log.Printf("Cannot get kube: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Cannot get kube 2") } - newKube, err := s.Ai.GenerateKubeRecipe(kube1.Name, kube2.Name) + newKubeId, err := s.Ai.GenerateKubeRecipe(kube1.Name, kube2.Name) if err != nil { log.Printf("Cannot generate kube recipe: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Cannot generate kube recipe") } - image, err := s.Ai.GenerateDalleForKube(newKube) + image, err := s.Ai.GenerateDalleForKube(newKubeId) if err != nil { log.Printf("Error generating Dalle for kube: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Error generating Dalle for kube") } - err = s.Database.SetKubeRecipe(kube1, kube2, newKube, image) + err = s.Database.SetKubeRecipe(kube1, kube2, newKubeId, image) if err != nil { log.Printf("Cannot save kube recipe: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Cannot save kube recipe") } recipe, err = s.Database.GetKubeRecipe(id1, id2) if err != nil { log.Printf("Cannot get kube recipe: %s", err) - c.JSON(500, gin.H{"error": err.Error()}) - return + return model.KubeRecipe{}, errors.New("Cannot get kube recipe") } + } else { s.Metrics.IncrementCacheHits() } c.Header("Cache-Control", cacheControlHeader) - c.JSON(200, recipe) + return recipe, nil } diff --git a/kube_cache/server/index.go b/kube_cache/server/index.go index d85a1f8..7a26a46 100644 --- a/kube_cache/server/index.go +++ b/kube_cache/server/index.go @@ -2,10 +2,59 @@ package server import ( "fmt" + "log" + "github.com/CosmicKube/kube_cache/model" "github.com/gin-gonic/gin" ) +func (s *Server) allKubesSelect(kubes []model.Kube) string { + options := "" + for _, kube := range kubes { + options += fmt.Sprintf(``, kube.Id, kube.Name) + } + return options +} + +const ( + op1 = "op1" + op2 = "op2" +) + +func (s *Server) IndexPost(c *gin.Context) { + id1 := c.PostForm(op1) + id2 := c.PostForm(op2) + recipe, err := s.craft(c, id1, id2) + if err != nil { + log.Printf("Cannot craft kube: %s", err) + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + kube, err := s.Database.GetKube(recipe.Output.String()) + if err != nil { + c.JSON(500, gin.H{"error": err.Error()}) + return + } + + html := fmt.Sprintf(` + + New Kube | Kube Cache + + +

New Kube: %s

+

Back

+ %s +

Regenerate Image

+`, + kube.Name, + kube.Id, + kube.Id, + kube.Name) + + c.Data(200, "text/html", []byte(html)) +} + func (s *Server) Index(c *gin.Context) { kubes, err := s.Database.GetAllKubes() if err != nil { @@ -22,11 +71,31 @@ func (s *Server) Index(c *gin.Context) { `, kube.Name, kube.Id, kube.Id, kube.Name) } + selects := s.allKubesSelect(kubes) + crafterHtml := fmt.Sprintf(`
+ + + + + + + +
+ `, + op1, op1, + selects, + op2, op2, + selects) + body := fmt.Sprintf(` Kube Cache - +

Kube Cache

Endpoints

+

Create New Kube

+ %s

Kubes

%s

End of cache

-`, kubesHtml) +`, crafterHtml, kubesHtml) c.Data(200, "text/html", []byte(body)) }