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 e121544
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions upstream/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ func SyncConfigChanges(ctx context.Context, config UpstreamConfig, batchSize int
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 +85,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 {
logger.Tracef("pushing %d config_analysis 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 e121544

Please sign in to comment.