From 2ebb0c9272e65308bd88821d78adbe317f0917e9 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Tue, 2 Jan 2024 13:15:43 +0545 Subject: [PATCH] chore: just update the is_pushed column --- upstream/jobs.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/upstream/jobs.go b/upstream/jobs.go index 1a4e89cf..c1625add 100644 --- a/upstream/jobs.go +++ b/upstream/jobs.go @@ -3,7 +3,6 @@ package upstream import ( "fmt" - "github.com/flanksource/commons/logger" "github.com/flanksource/duty/context" "github.com/flanksource/duty/models" "github.com/google/uuid" @@ -28,7 +27,7 @@ func SyncCheckStatuses(ctx context.Context, config UpstreamConfig, batchSize int return count, nil } - logger.Tracef("pushing %d check_statuses to upstream", len(checkStatuses)) + ctx.Tracef("pushing %d check_statuses to upstream", len(checkStatuses)) if err := client.Push(ctx, &PushData{AgentName: config.AgentName, CheckStatuses: checkStatuses}); err != nil { return 0, fmt.Errorf("failed to push check_statuses to upstream: %w", err) } @@ -63,18 +62,19 @@ func SyncConfigChanges(ctx context.Context, config UpstreamConfig, batchSize int return count, nil } - logger.Tracef("pushing %d config_changes to upstream", len(configChanges)) + ctx.Tracef("pushing %d config_changes to upstream", len(configChanges)) if err := client.Push(ctx, &PushData{AgentName: config.AgentName, ConfigChanges: configChanges}); err != nil { return 0, fmt.Errorf("failed to push config_changes to upstream: %w", err) } + ids := make([]string, len(configChanges)) for i := range configChanges { - configChanges[i].IsPushed = true + ids[i] = configChanges[i].ID } - - if err := ctx.DB().Save(&configChanges).Error; err != nil { - return 0, fmt.Errorf("failed to save config_changes: %w", err) + if err := ctx.DB().Model(&models.ConfigChange{}).Where("id IN ?", ids).Update("is_pushed", true).Error; err != nil { + return 0, fmt.Errorf("failed to update is_pushed on config_changes: %w", err) } + count += len(configChanges) } } @@ -84,32 +84,33 @@ func SyncConfigAnalyses(ctx context.Context, config UpstreamConfig, batchSize in client := NewUpstreamClient(config) count := 0 for { - var configChanges []models.ConfigAnalysis + var analyses []models.ConfigAnalysis if err := ctx.DB().Select("config_analysis.*"). Joins("LEFT JOIN config_items ON config_items.id = config_analysis.config_id"). Where("config_items.agent_id = ?", uuid.Nil). Where("config_analysis.is_pushed IS FALSE"). Limit(batchSize). - Find(&configChanges).Error; err != nil { + Find(&analyses).Error; err != nil { return 0, fmt.Errorf("failed to fetch config_analysis: %w", err) } - if len(configChanges) == 0 { + if len(analyses) == 0 { return count, nil } - logger.Tracef("pushing %d config_analysis to upstream", len(configChanges)) - if err := client.Push(ctx, &PushData{AgentName: config.AgentName, ConfigAnalysis: configChanges}); err != nil { + ctx.Tracef("pushing %d config_analyses to upstream", len(analyses)) + if err := client.Push(ctx, &PushData{AgentName: config.AgentName, ConfigAnalysis: analyses}); err != nil { return 0, fmt.Errorf("failed to push config_analysis to upstream: %w", err) } - for i := range configChanges { - configChanges[i].IsPushed = true + ids := make([]uuid.UUID, len(analyses)) + for i := range analyses { + ids[i] = analyses[i].ID } - - if err := ctx.DB().Save(&configChanges).Error; err != nil { - return 0, fmt.Errorf("failed to save config_analysis: %w", err) + if err := ctx.DB().Model(&models.ConfigAnalysis{}).Where("id IN ?", ids).Update("is_pushed", true).Error; err != nil { + return 0, fmt.Errorf("failed to update is_pushed on config_analysis: %w", err) } - count += len(configChanges) + + count += len(analyses) } }