Skip to content

Commit

Permalink
chore: just update the is_pushed column
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 2, 2024
1 parent 4e448d8 commit 99ac328
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions upstream/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}

0 comments on commit 99ac328

Please sign in to comment.