Skip to content

Commit

Permalink
fix: fix purge sample data
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Dec 20, 2024
1 parent 673723c commit d960e2c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 60 deletions.
59 changes: 0 additions & 59 deletions services/describe/scheduler_describe_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,65 +337,6 @@ func (s *Scheduler) cleanupDescribeResourcesNotInIntegrations(ctx context.Contex
return
}

func (s *Scheduler) cleanupDescribeResourcesForIntegrations(ctx context.Context, connectionIds []string) {
for _, connectionId := range connectionIds {
var searchAfter []any
for {
esResp, err := es.GetResourceIDsForIntegrationFromES(ctx, s.es, connectionId, searchAfter, 1000)
if err != nil {
s.logger.Error("failed to get resource ids from es", zap.Error(err))
break
}

if len(esResp.Hits.Hits) == 0 {
break
}
deletedCount := 0
for _, hit := range esResp.Hits.Hits {
searchAfter = hit.Sort

resource := es2.Resource{
ResourceID: hit.Source.ResourceID,
IntegrationID: hit.Source.IntegrationID,
ResourceType: strings.ToLower(hit.Source.ResourceType),
IntegrationType: hit.Source.IntegrationType,
}
keys, idx := resource.KeysAndIndex()
deletedCount += 1
key := es2.HashOf(keys...)
resource.EsID = key
resource.EsIndex = idx
err = s.es.Delete(key, idx)
if err != nil {
s.logger.Error("failed to delete resource from open-search", zap.Error(err))
return
}

lookupResource := es2.LookupResource{
ResourceID: hit.Source.ResourceID,
IntegrationID: hit.Source.IntegrationID,
ResourceType: strings.ToLower(hit.Source.ResourceType),
IntegrationType: hit.Source.IntegrationType,
}
deletedCount += 1
keys, idx = lookupResource.KeysAndIndex()
key = es2.HashOf(keys...)
lookupResource.EsID = key
lookupResource.EsIndex = idx
err = s.es.Delete(key, idx)
if err != nil {
s.logger.Error("failed to delete lookup from open-search", zap.Error(err))
return
}
}

s.logger.Info("deleted old resources", zap.Int("deleted_count", deletedCount), zap.String("connection_id", connectionId))
}
}

return
}

func (s *Scheduler) cleanupDescribeResourcesForConnectionAndResourceType(IntegrationID, resourceType string) error {
root := make(map[string]any)
root["query"] = map[string]any{
Expand Down
8 changes: 7 additions & 1 deletion services/describe/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/opengovern/opencomply/pkg/types"
"golang.org/x/net/context"
"net/http"
"regexp"
"sort"
Expand Down Expand Up @@ -3843,7 +3845,11 @@ func (h HttpServer) PurgeSampleData(c echo.Context) error {
ids = append(ids, i)
}

go es.CleanupSummariesForJobs(h.Scheduler.logger, h.Scheduler.es, ids)
_, err = h.Scheduler.es.ES().Indices.Delete([]string{types.BenchmarkSummaryIndex}, h.Scheduler.es.ES().Indices.Delete.WithContext(context.Background()))
if err != nil {
h.Scheduler.logger.Error("failed to delete summary job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete summary jobs")
}

return c.NoContent(http.StatusOK)
}

0 comments on commit d960e2c

Please sign in to comment.