Skip to content

Commit

Permalink
Merge pull request #32 from pilksoc/logging
Browse files Browse the repository at this point in the history
feat: added a wicked new method to regen kubes
  • Loading branch information
djpiper28 authored Mar 3, 2024
2 parents d53ac2c + a72438d commit 045f0a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions kube_cache/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func New(metrics *metrics.Metrics, ai *aiStuff.KubeAi, url string) *Database {
return database
}

func (db *Database) SetKubeImage(kube Kube, image []byte) error {
result := db.Db.Model(&kube).Update("image", image)
return result.Error
}

func (db *Database) GetKubeImage(id string) ([]byte, error) {
var kube Kube
result := db.Db.First(&kube, "id = ?", id)
Expand Down
25 changes: 25 additions & 0 deletions kube_cache/server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,38 @@ func (s *Server) Use(engine *gin.Engine) {
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("/kubeRecipeByIds/:id1/:id2", s.GetKubeRecipe)
}

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"})
}

func (s *Server) GetAllKubeRecipes(c *gin.Context) {
recipes, err := s.Database.GetAllKubeRecipes()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions kube_cache/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (s *Server) Index(c *gin.Context) {
for _, kube := range kubes {
kubesHtml += fmt.Sprintf(`<div style="max-width: 100px; max-height: 100px;">
<h3>%s</h3>
<img src="./kubeImageById/%s" style="max-width: 100px; max-height: 100px;"/>
</div>`, kube.Name, kube.Id)
<img src="./kubeImageById/%s" href="./kubeImageByIdNew/%s" style="max-width: 100px; max-height: 100px;"/>
</div>`, kube.Name, kube.Id, kube.Id)
}

body := fmt.Sprintf(`<!DOCTYPE html>
Expand Down

0 comments on commit 045f0a2

Please sign in to comment.