Skip to content

Commit

Permalink
Merge pull request #22 from pilksoc/metrics
Browse files Browse the repository at this point in the history
feat: added more cache and metrics and shit
  • Loading branch information
djpiper28 authored Mar 3, 2024
2 parents edfa0ab + f58d300 commit 99d576b
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 18 deletions.
17 changes: 15 additions & 2 deletions kube_cache/aiStuff/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/CosmicKube/kube_cache/metrics"
"io"
"log"
"net/http"
)

type KubeAi struct {
Endpoint, Apikey, ModelId string
Metrics *metrics.Metrics
}

func New(endpoint, apiKey, modelId string) *KubeAi {
func New(metrics *metrics.Metrics, endpoint, apiKey, modelId string) *KubeAi {
return &KubeAi{
Metrics: metrics,
Endpoint: endpoint,
Apikey: apiKey,
ModelId: modelId,
Expand Down Expand Up @@ -78,7 +81,7 @@ type aiReq struct {
TopP float32 `json:"top_p"`
}

func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error) {
func (ai *KubeAi) generateKubeRecipe(kubeName1, kubeName2 string) (string, error) {
url := fmt.Sprintf("%s/openai/deployments/Dalle3/images/generations?api-version=2024-02-15-preview", ai.Endpoint)

postReq := aiReq{
Expand Down Expand Up @@ -146,5 +149,15 @@ func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error
return actualLegitMessage, nil
}

ai.Metrics.IncrementGptRequests()
return aiResp2.Name, nil
}

func (ai *KubeAi) GenerateKubeRecipe(kubeName1, kubeName2 string) (string, error) {
res, err := ai.generateKubeRecipe(kubeName1, kubeName2)
if err != nil {
ai.Metrics.IncrementDalleErrors()
return "", err
}
return res, nil
}
4 changes: 3 additions & 1 deletion kube_cache/aiStuff/dalle.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (ai *KubeAi) generateDalleForKube(kubeName string) ([]byte, error) {
func (ai *KubeAi) GenerateDalleForKube(kubeName string) ([]byte, error) {
img, err := ai.generateDalleForKube(kubeName)
if err != nil {
ai.Metrics.IncrementDalleErrors()
log.Println("Cannot generate image falling back to default image")
defaultFile, err := os.Create("default.png")
defaultFile, err := os.Open("default.png")
if err != nil {
log.Printf("Error creating default file: %s", err)
return nil, err
Expand All @@ -118,5 +119,6 @@ func (ai *KubeAi) GenerateDalleForKube(kubeName string) ([]byte, error) {

return img, nil
}
ai.Metrics.IncrementDalleRequests()
return img, nil
}
Binary file modified kube_cache/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions kube_cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/CosmicKube/kube_cache/aiStuff"
"github.com/CosmicKube/kube_cache/metrics"
"github.com/CosmicKube/kube_cache/model"
"github.com/CosmicKube/kube_cache/server"
"github.com/gin-contrib/cors"
Expand All @@ -23,13 +24,15 @@ func main() {
log.Println(err)
}

metrics := metrics.New()

log.Println("Creating AI client...")
ai := aiStuff.New(os.Getenv("OPENAI_ENDPOINT"),
ai := aiStuff.New(metrics, os.Getenv("OPENAI_ENDPOINT"),
os.Getenv("OPENAI_API_KEY"),
os.Getenv("OPENAI_MODEL_ID"))

log.Println("Using configuration for database...")
database := model.New(ai, os.Getenv("DATABASE_URL"))
database := model.New(metrics, ai, os.Getenv("DATABASE_URL"))

log.Println("Starting server...")
router := gin.Default()
Expand All @@ -51,6 +54,7 @@ func main() {
}
return url
}

p.Use(router)

router.Use(cors.New(cors.Config{
Expand All @@ -69,7 +73,7 @@ func main() {
log.Println(len(body))

log.Println("Start up the API")
server := server.New(database, ai)
server := server.New(metrics, database, ai)
server.Use(router)
log.Fatal(router.Run("0.0.0.0:8080"))
}
72 changes: 72 additions & 0 deletions kube_cache/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package metrics

import (
"fmt"
"sync"
)

type Metrics struct {
Lock sync.Mutex
CacheHits, CacheMisses int
ImagesRetrieved int
DalleRequests int
GptRequests int
DalleErrors int
GptErrors int
}

func New() *Metrics {
return &Metrics{}
}

func (m *Metrics) IncrementCacheHits() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.CacheHits++
}

func (m *Metrics) IncrementCacheMisses() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.CacheMisses++
}

func (m *Metrics) IncrementImagesRetrieved() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.ImagesRetrieved++
}

func (m *Metrics) IncrementDalleRequests() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.DalleRequests++
}

func (m *Metrics) IncrementGptRequests() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.GptRequests++
}

func (m *Metrics) IncrementDalleErrors() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.DalleErrors++
}

func (m *Metrics) IncrementGptErrors() {
m.Lock.Lock()
defer m.Lock.Unlock()
m.GptErrors++
}

func (m *Metrics) String() string {
return fmt.Sprintf(`kube_cache_hits=%d
kube_cache_misses=%d
kube_images_retrieved=%d
kube_dalle_requests=%d
kube_gpt_requests=%d
kube_dalle_errors=%d
kube_gpt_errors=%d`, m.CacheHits, m.CacheMisses, m.ImagesRetrieved, m.DalleRequests, m.GptRequests, m.DalleErrors, m.GptErrors)
}
8 changes: 5 additions & 3 deletions kube_cache/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log"

"github.com/CosmicKube/kube_cache/aiStuff"
"github.com/CosmicKube/kube_cache/metrics"
"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down Expand Up @@ -40,10 +41,11 @@ type KubeRecipe struct {
}

type Database struct {
Db *gorm.DB
Db *gorm.DB
Metrics *metrics.Metrics
}

func New(ai *aiStuff.KubeAi, url string) *Database {
func New(metrics *metrics.Metrics, ai *aiStuff.KubeAi, url string) *Database {
log.Println("Connecting to database...")
db, err := gorm.Open(postgres.Open(url), &gorm.Config{
PrepareStmt: true,
Expand All @@ -59,7 +61,7 @@ func New(ai *aiStuff.KubeAi, url string) *Database {
log.Fatal(err)
}

database := &Database{Db: db}
database := &Database{Db: db, Metrics: metrics}
log.Println("Seeding database...")
database.seed(ai)

Expand Down
20 changes: 18 additions & 2 deletions kube_cache/server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ import (
"log"

"github.com/CosmicKube/kube_cache/aiStuff"
"github.com/CosmicKube/kube_cache/metrics"
"github.com/CosmicKube/kube_cache/model"
"github.com/gin-gonic/gin"
)

type Server struct {
Database *model.Database
Ai *aiStuff.KubeAi
Metrics *metrics.Metrics
}

func New(database *model.Database, ai *aiStuff.KubeAi) *Server {
return &Server{Database: database, Ai: ai}
func New(metrics *metrics.Metrics, database *model.Database, ai *aiStuff.KubeAi) *Server {
return &Server{Database: database, Ai: ai, Metrics: metrics}
}

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("/kubeRecipeByIds/:id1/:id2", s.GetKubeRecipe)
}

func (s *Server) CacheMetrics(c *gin.Context) {
c.Data(200, "text/plain", []byte(s.Metrics.String()))
}

func (s *Server) GetAllKubeRecipes(c *gin.Context) {
recipes, err := s.Database.GetAllKubeRecipes()
if err != nil {
Expand All @@ -41,9 +48,12 @@ func (s *Server) GetKubeImage(c *gin.Context) {
if err != nil {
log.Printf("Cannot get kube image: %s", err)
c.JSON(500, gin.H{"error": err.Error()})
s.Metrics.IncrementCacheMisses()
return
}
c.Data(200, "image/png", image)
c.Header("Cache-Control", cacheControlHeader)
s.Metrics.IncrementCacheHits()
}

func (s *Server) GetAllKubes(c *gin.Context) {
Expand All @@ -60,9 +70,12 @@ func (s *Server) GetKube(c *gin.Context) {
kube, err := s.Database.GetKube(id)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
s.Metrics.IncrementCacheMisses()
return
}
c.JSON(200, kube)
c.Header("Cache-Control", cacheControlHeader)
s.Metrics.IncrementCacheHits()
}

// 24 hours
Expand All @@ -73,6 +86,7 @@ func (s *Server) GetKubeRecipe(c *gin.Context) {
id2 := c.Param("id2")
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)
Expand Down Expand Up @@ -114,6 +128,8 @@ func (s *Server) GetKubeRecipe(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()})
return
}
} else {
s.Metrics.IncrementCacheHits()
}
c.Header("Cache-Control", cacheControlHeader)
c.JSON(200, recipe)
Expand Down
14 changes: 7 additions & 7 deletions kube_cache/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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;"/>
<img src="./kubeImageById/%s" style="max-width: 100px; max-height: 100px;"/>
</div>`, kube.Name, kube.Id)
}

Expand All @@ -29,14 +29,14 @@ func (s *Server) Index(c *gin.Context) {
<h1>Kube Cache</h1>
<h2>Endpoints</h2>
<ul>
<li><a href="/kubes">/kubes</a></li>
<li><a href="/kubeById/:id">/kubeById/:id</a></li>
<li><a href="/kubeImageById/:id">/kubeImageById/:id</a></li>
<li><a href="/kubeRecipes">/kubeRecipes</a></li>
<li><a href="/kubeRecipeByIds/:id1/:id2">/kubeRecipeByIds/:id1/:id2</a></li>
<li><a href="./kubes">/kubes</a></li>
<li><a href="./kubeById/:id">/kubeById/:id</a></li>
<li><a href="./kubeImageById/:id">/kubeImageById/:id</a></li>
<li><a href="./kubeRecipes">/kubeRecipes</a></li>
<li><a href="./kubeRecipeByIds/:id1/:id2">/kubeRecipeByIds/:id1/:id2</a></li>
</ul>
<h2>Kubes</h2>
<div style="display: flex; flex-direction: row; flex-wrap: wrap; gap: 10px; width: 100%">
<div style="display: flex; flex-direction: row; flex-wrap: wrap; gap: 10px;">
%s
</div>
</body>`, kubesHtml)
Expand Down

0 comments on commit 99d576b

Please sign in to comment.