Skip to content

Commit

Permalink
feat: you can now do manual crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Mar 4, 2024
1 parent 5efcd77 commit fe20fa6
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 46 deletions.
4 changes: 2 additions & 2 deletions kube_cache/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
91 changes: 49 additions & 42 deletions kube_cache/server/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"log"

"github.com/CosmicKube/kube_cache/aiStuff"
Expand All @@ -23,42 +24,45 @@ 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) {
c.Data(200, "text/plain", []byte(s.Metrics.String()))
}

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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
75 changes: 73 additions & 2 deletions kube_cache/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<option value="%s">%s</option>`, 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(`<!DOCTYPE HTML>
<head>
<Title>New Kube | Kube Cache</Title>
</head>
<body>
<h1>New Kube: %s</h1>
<p><a href='./'>Back</a></p>
<img src="./kubeImageById/%s" style="max-width: 100px; max-height: 100px;" loading="lazy" alt="%s"/>
<p><a href="./kubeImageByIdNew/%s">Regenerate Image</a></p>
</body>`,
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 {
Expand All @@ -22,11 +71,31 @@ func (s *Server) Index(c *gin.Context) {
</div>`, kube.Name, kube.Id, kube.Id, kube.Name)
}

selects := s.allKubesSelect(kubes)
crafterHtml := fmt.Sprintf(`<form action="/" method="post">
<label for="%s">Name</label>
<select name="%s">
%s
</select>
<label for="%s">Name</label>
<select name="%s">
%s
</select>
<button type="submit">Craft</button>
</form>
`,
op1, op1,
selects,
op2, op2,
selects)

body := fmt.Sprintf(`<!DOCTYPE html>
<head>
<title>Kube Cache</title>
</head>
<body>
<body class="display: flex; margin: 0px; flex-direction: column; max-width: 100vw;">
<h1>Kube Cache</h1>
<h2>Endpoints</h2>
<ul>
Expand All @@ -39,12 +108,14 @@ func (s *Server) Index(c *gin.Context) {
<li><a href="./cache_metrics">/cache_metrics</a></li>
<li><a href="./metrics">/metrics</a></li>
</ul>
<h2>Create New Kube</h2>
%s
<h2>Kubes</h2>
<div style="display: flex; flex-direction: row; flex-wrap: wrap; gap: 10px; align-items: center; align-content: center;">
%s
</div>
<p>End of cache</p>
</body>`, kubesHtml)
</body>`, crafterHtml, kubesHtml)

c.Data(200, "text/html", []byte(body))
}

0 comments on commit fe20fa6

Please sign in to comment.