Skip to content

Commit

Permalink
fix: fix preferences update
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Jun 30, 2024
1 parent 188d0dc commit 458e876
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
22 changes: 13 additions & 9 deletions plugin/processor/compute_instance/compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package compute_instance
import (
"fmt"
"github.com/kaytu-io/kaytu/pkg/utils"
"log"
"strconv"
"strings"
"sync/atomic"

"github.com/kaytu-io/kaytu/pkg/plugin/proto/src/golang"
"github.com/kaytu-io/kaytu/pkg/plugin/sdk"
Expand All @@ -21,6 +21,7 @@ type ComputeInstanceProcessor struct {
publishResultSummary func(summary *golang.ResultSummary)
kaytuAcccessToken string
jobQueue *sdk.JobQueue
lazyloadCounter atomic.Uint32
}

func NewComputeInstanceProcessor(
Expand All @@ -31,7 +32,6 @@ func NewComputeInstanceProcessor(
kaytuAcccessToken string,
jobQueue *sdk.JobQueue,
) *ComputeInstanceProcessor {
log.Println("creating processor")
r := &ComputeInstanceProcessor{
provider: prv,
metricProvider: metricPrv,
Expand All @@ -40,20 +40,24 @@ func NewComputeInstanceProcessor(
publishResultSummary: publishResultSummary,
kaytuAcccessToken: kaytuAcccessToken,
jobQueue: jobQueue,
// configuration: configurations,
// lazyloadCounter: lazyloadCounter,
lazyloadCounter: atomic.Uint32{},
}

jobQueue.Push(NewListComputeInstancesJob(r))
return r
}

func (m *ComputeInstanceProcessor) ReEvaluate(id string, items []*golang.PreferenceItem) {
log.Println("Reevaluate unimplemented")
// v, _ := m.items.Get(id)
// v.Preferences = items
// m.items.Set(id, v)
// m.jobQueue.Push(NewOptimizeEC2InstanceJob(m, v))
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))
}

func (m *ComputeInstanceProcessor) ExportNonInteractive() *golang.NonInteractiveExport {
Expand Down
16 changes: 14 additions & 2 deletions plugin/processor/compute_instance/compute_instance_item.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compute_instance

import (
"cloud.google.com/go/compute/apiv1/computepb"
"fmt"
"github.com/kaytu-io/kaytu/pkg/plugin/proto/src/golang"
"github.com/kaytu-io/kaytu/pkg/utils"
Expand All @@ -23,6 +24,7 @@ type ComputeInstanceItem struct {
Skipped bool
LazyLoadingEnabled bool
SkipReason string
Instance *computepb.Instance
Disks []compute.Disk
Metrics map[string][]kaytu.Datapoint
DisksMetrics map[string]map[string][]kaytu.Datapoint
Expand Down Expand Up @@ -244,11 +246,21 @@ func (i ComputeInstanceItem) ToOptimizationItem() *golang.ChartOptimizationItem
deviceRows, deviceProps := i.Devices()

status := ""
if i.Wastage.RightSizing.Recommended != nil {
if i.Skipped {
status = fmt.Sprintf("skipped - %s", i.SkipReason)
} else if i.LazyLoadingEnabled && !i.OptimizationLoading {
status = "press enter to load"
} else if i.OptimizationLoading {
status = "loading"
} else if i.Wastage.RightSizing.Recommended != nil {
totalSaving := 0.0
totalCurrentCost := 0.0
totalSaving += i.Wastage.RightSizing.Current.Cost - i.Wastage.RightSizing.Recommended.Cost
totalCurrentCost += i.Wastage.RightSizing.Current.Cost
for _, d := range i.Wastage.VolumeRightSizing {
totalSaving += d.Current.Cost - d.Recommended.Cost
totalCurrentCost += d.Current.Cost
}
status = fmt.Sprintf("%s (%.2f%%)", utils.FormatPriceFloat(totalSaving), (totalSaving/totalCurrentCost)*100)
}

Expand Down Expand Up @@ -284,7 +296,7 @@ func (i ComputeInstanceItem) ToOptimizationItem() *golang.ChartOptimizationItem
DevicesChartRows: deviceRows,
DevicesProperties: deviceProps,
Preferences: i.Preferences,
Description: "description placeholder",
Description: i.Wastage.RightSizing.Description,
Loading: i.OptimizationLoading,
Skipped: i.Skipped,
SkipReason: wrapperspb.String(i.SkipReason),
Expand Down
11 changes: 9 additions & 2 deletions plugin/processor/compute_instance/job_compute_instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@ func (job *ListComputeInstancesJob) Run(ctx context.Context) error {
MachineType: util.TrimmedString(*instance.MachineType, "/"),
Region: util.TrimmedString(*instance.Zone, "/"),
Platform: instance.GetCpuPlatform(),
OptimizationLoading: false,
OptimizationLoading: true,
Preferences: preferences.DefaultComputeEnginePreferences,
Skipped: false,
LazyLoadingEnabled: false,
SkipReason: "NA",
Instance: instance,
Disks: disks,
Metrics: nil,
DisksMetrics: nil,
}

log.Printf("OI instance: %s", oi.Name)
if !oi.Skipped {
job.processor.lazyloadCounter.Add(1)
if job.processor.lazyloadCounter.Load() > uint32(1) {
oi.LazyLoadingEnabled = true
}
}

job.processor.items.Set(oi.Id, oi)
job.processor.publishOptimizationItem(oi.ToOptimizationItem())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ func (job *GetComputeInstanceMetricsJob) Run(ctx context.Context) error {
endTime := time.Now() // end time of requested time series
startTime := endTime.Add(-24 * 1 * time.Hour) // start time of requested time series

// metricName string,
// instanceID string,
// startTime time.Time,
// endTime time.Time,
// periodInSeconds int64,
cpuRequest := job.processor.metricProvider.NewTimeSeriesRequest(
fmt.Sprintf(
`metric.type="%s" AND resource.labels.instance_id="%s"`,
Expand Down Expand Up @@ -211,11 +206,12 @@ func (job *GetComputeInstanceMetricsJob) Run(ctx context.Context) error {
MachineType: util.TrimmedString(*job.instance.MachineType, "/"),
Region: util.TrimmedString(*job.instance.Zone, "/"),
Platform: job.instance.GetCpuPlatform(),
OptimizationLoading: false,
OptimizationLoading: true,
Preferences: preferences.DefaultComputeEnginePreferences,
Skipped: false,
LazyLoadingEnabled: false,
SkipReason: "NA",
Instance: job.instance,
Disks: job.disks,
Metrics: instanceMetrics,
DisksMetrics: disksMetrics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (job *OptimizeComputeInstancesJob) Description() string {
}

func (job *OptimizeComputeInstancesJob) Run(ctx context.Context) error {
if job.item.LazyLoadingEnabled {
job.processor.jobQueue.Push(NewGetComputeInstanceMetricsJob(job.processor, job.item.Instance, job.item.Disks))
return nil
}

requestId := uuid.NewString()

Expand Down Expand Up @@ -95,6 +99,8 @@ func (job *OptimizeComputeInstancesJob) Run(ctx context.Context) error {
LazyLoadingEnabled: false,
SkipReason: "NA",
Metrics: job.item.Metrics,
DisksMetrics: job.item.DisksMetrics,
Instance: job.item.Instance,
Disks: job.item.Disks,
Wastage: *response,
}
Expand Down

0 comments on commit 458e876

Please sign in to comment.