Skip to content

Commit

Permalink
Merge pull request #29 from kaytu-io/compute-instance-recommendations
Browse files Browse the repository at this point in the history
fix: add summary
  • Loading branch information
artaasadi committed Jun 30, 2024
2 parents 2c6b366 + 5e5fc7d commit f9515be
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (

require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.10.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -23,6 +25,12 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.24.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1h
cloud.google.com/go/monitoring v1.18.0 h1:NfkDLQDG2UR3WYZVQE8kwSbUIEyIqJUPl+aOQdFH1T4=
cloud.google.com/go/monitoring v1.18.0/go.mod h1:c92vVBCeq/OB4Ioyo+NbN2U7tlg5ZH41PZcdvfc+Lcg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down Expand Up @@ -63,9 +67,24 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kaytu-io/kaytu v0.14.0-rc.0.0.20240619114201-51f5733d10b6 h1:OyrvbIttya/35K+6N1tyI953BlKclIkHGWIZ/IPYheg=
github.com/kaytu-io/kaytu v0.14.0-rc.0.0.20240619114201-51f5733d10b6/go.mod h1:csDLaCgTnp/D9SZuo55KjOzp/BYfkhkeSkGuEiCljtc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
Expand Down Expand Up @@ -121,6 +140,7 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
41 changes: 37 additions & 4 deletions plugin/processor/compute_instance/compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute_instance

import (
"fmt"
"github.com/kaytu-io/kaytu/pkg/style"
"github.com/kaytu-io/kaytu/pkg/utils"
"strconv"
"strings"
Expand All @@ -22,6 +23,8 @@ type ComputeInstanceProcessor struct {
kaytuAcccessToken string
jobQueue *sdk.JobQueue
lazyloadCounter atomic.Uint32

summary util.ConcurrentMap[string, ComputeInstanceSummary]
}

func NewComputeInstanceProcessor(
Expand Down Expand Up @@ -51,10 +54,6 @@ func (m *ComputeInstanceProcessor) ReEvaluate(id string, items []*golang.Prefere
v, _ := m.items.Get(id)
v.Preferences = items
m.items.Set(id, v)
fmt.Println("HERE===================")
fmt.Println("Instance Metrics", len(v.Metrics))
fmt.Println("Disk Metrics", len(v.DisksMetrics))
fmt.Println("Disks", len(v.Disks))
v.OptimizationLoading = true
m.publishOptimizationItem(v.ToOptimizationItem())
m.jobQueue.Push(NewOptimizeComputeInstancesJob(m, v))
Expand Down Expand Up @@ -142,3 +141,37 @@ func (m *ComputeInstanceProcessor) exportCsv() []*golang.CSVRow {
})
return rows
}

func (m *ComputeInstanceProcessor) ResultsSummary() *golang.ResultSummary {
summary := &golang.ResultSummary{}
var totalCost, savings float64
m.summary.Range(func(_ string, item ComputeInstanceSummary) bool {
totalCost += item.CurrentRuntimeCost
savings += item.Savings
return true
})

summary.Message = fmt.Sprintf("Current runtime cost: %s, Savings: %s",
style.CostStyle.Render(fmt.Sprintf("%s", utils.FormatPriceFloat(totalCost))), style.SavingStyle.Render(fmt.Sprintf("%s", utils.FormatPriceFloat(savings))))
return summary
}

func (m *ComputeInstanceProcessor) UpdateSummary(itemId string) {
i, ok := m.items.Get(itemId)
if ok && i.Wastage.RightSizing.Recommended != nil {
totalSaving := 0.0
totalCurrentCost := 0.0
for _, v := range i.Wastage.VolumeRightSizing {
totalSaving += v.Current.Cost - v.Recommended.Cost
totalCurrentCost += v.Current.Cost
}
totalSaving += i.Wastage.RightSizing.Current.Cost - i.Wastage.RightSizing.Recommended.Cost
totalCurrentCost += i.Wastage.RightSizing.Current.Cost

m.summary.Set(itemId, ComputeInstanceSummary{
CurrentRuntimeCost: totalCurrentCost,
Savings: totalSaving,
})
}
m.publishResultSummary(m.ResultsSummary())
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (job *ListComputeInstancesJob) Run(ctx context.Context) error {

job.processor.items.Set(oi.Id, oi)
job.processor.publishOptimizationItem(oi.ToOptimizationItem())
job.processor.UpdateSummary(oi.Id)

job.processor.jobQueue.Push(NewGetComputeInstanceMetricsJob(job.processor, instance, disks))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (job *GetComputeInstanceMetricsJob) Run(ctx context.Context) error {
job.processor.publishOptimizationItem(oi.ToOptimizationItem())

job.processor.jobQueue.Push(NewOptimizeComputeInstancesJob(job.processor, oi))
job.processor.UpdateSummary(oi.Id)

return nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (job *OptimizeComputeInstancesJob) Run(ctx context.Context) error {

job.processor.items.Set(job.item.Id, job.item)
job.processor.publishOptimizationItem(job.item.ToOptimizationItem())
job.processor.UpdateSummary(job.item.Id)

return nil
}
6 changes: 6 additions & 0 deletions plugin/processor/compute_instance/summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package compute_instance

type ComputeInstanceSummary struct {
CurrentRuntimeCost float64
Savings float64
}

0 comments on commit f9515be

Please sign in to comment.