Skip to content

Commit

Permalink
chore: soft deleted non-required push topology components
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Nov 8, 2024
1 parent 7caa486 commit cbddf1e
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions push/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"io"

"github.com/flanksource/commons/properties"
"github.com/flanksource/duty"
dutyAPI "github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
dutydb "github.com/flanksource/duty/db"
Expand All @@ -13,7 +13,6 @@ import (
"github.com/flanksource/incident-commander/db"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/samber/lo"
)

func PushTopology(c echo.Context) error {
Expand Down Expand Up @@ -69,20 +68,14 @@ func PushTopology(c echo.Context) error {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "error saving components: %v", dutydb.ErrorDetails(err)))
}

var idsToDelete []string
if err := ctx.DB().Model(&models.Component{}).Select("id").Where("topology_id = ?", data.TopologyID).Where("deleted_at IS NULL").Where("id NOT IN ?", returnedIDs).Find(&idsToDelete).Error; err != nil {
// Soft delete component ids which were not created
if err := ctx.DB().Model(&models.Component{}).
Where("topology_id = ?", data.TopologyID).
Where("deleted_at IS NULL").
Where("id NOT IN ?", returnedIDs).
UpdateColumn("deleted_at", duty.Now()).Error; err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "error querying old components: %v", dutydb.ErrorDetails(err)))
}

if len(idsToDelete) > 0 {
chunkSize := properties.Int(5000, "push.topology.delete_chunk_size")
chunks := lo.Chunk(idsToDelete, chunkSize)
for _, chunk := range chunks {
if err := models.DeleteComponentsWithIDs(ctx.DB(), chunk); err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "error deleting old components: %v", dutydb.ErrorDetails(err)))
}
}
}

return dutyAPI.WriteSuccess(c, data)
}

0 comments on commit cbddf1e

Please sign in to comment.