From 5e5fc7def4c9b2f5f89976a3658fe8dc1174172e Mon Sep 17 00:00:00 2001 From: artaasadi Date: Sun, 30 Jun 2024 13:52:11 +0200 Subject: [PATCH] fix: add summary --- go.mod | 8 ++++ go.sum | 20 +++++++++ .../compute_instance/compute_instance.go | 41 +++++++++++++++++-- .../job_compute_instance_list.go | 1 + .../job_get_compute_instance_metrics.go | 1 + .../job_optimize_compute_instance.go | 1 + plugin/processor/compute_instance/summary.go | 6 +++ 7 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 plugin/processor/compute_instance/summary.go diff --git a/go.mod b/go.mod index 1f419ec..732967d 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index ea7dd46..d44d9ac 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/plugin/processor/compute_instance/compute_instance.go b/plugin/processor/compute_instance/compute_instance.go index dc7f196..1b3740f 100644 --- a/plugin/processor/compute_instance/compute_instance.go +++ b/plugin/processor/compute_instance/compute_instance.go @@ -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" @@ -22,6 +23,8 @@ type ComputeInstanceProcessor struct { kaytuAcccessToken string jobQueue *sdk.JobQueue lazyloadCounter atomic.Uint32 + + summary util.ConcurrentMap[string, ComputeInstanceSummary] } func NewComputeInstanceProcessor( @@ -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)) @@ -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()) +} diff --git a/plugin/processor/compute_instance/job_compute_instance_list.go b/plugin/processor/compute_instance/job_compute_instance_list.go index f2c41d5..2b57b73 100644 --- a/plugin/processor/compute_instance/job_compute_instance_list.go +++ b/plugin/processor/compute_instance/job_compute_instance_list.go @@ -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)) } diff --git a/plugin/processor/compute_instance/job_get_compute_instance_metrics.go b/plugin/processor/compute_instance/job_get_compute_instance_metrics.go index 534a591..89aed6a 100644 --- a/plugin/processor/compute_instance/job_get_compute_instance_metrics.go +++ b/plugin/processor/compute_instance/job_get_compute_instance_metrics.go @@ -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 diff --git a/plugin/processor/compute_instance/job_optimize_compute_instance.go b/plugin/processor/compute_instance/job_optimize_compute_instance.go index 55e67e4..8784747 100644 --- a/plugin/processor/compute_instance/job_optimize_compute_instance.go +++ b/plugin/processor/compute_instance/job_optimize_compute_instance.go @@ -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 } diff --git a/plugin/processor/compute_instance/summary.go b/plugin/processor/compute_instance/summary.go new file mode 100644 index 0000000..12b44de --- /dev/null +++ b/plugin/processor/compute_instance/summary.go @@ -0,0 +1,6 @@ +package compute_instance + +type ComputeInstanceSummary struct { + CurrentRuntimeCost float64 + Savings float64 +}