Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get with deduplicate in resource qualifier mapping #5921

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion pkg/resourceQualifiers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,22 @@ func getIdentifierNamesForAppEnv(envName string, appName string) *SelectionIdent

func DeduplicateQualifierMappings(mappings []*QualifierMapping) []*QualifierMapping {
uniqueMappings := make(map[string]*QualifierMapping)
filteredMappings := filterOutParentChildMappings(mappings)

// removing parentChildMappings from mappings
filteredMappingsMap := make(map[int]struct{})
for _, mapping := range filteredMappings {
filteredMappingsMap[mapping.Id] = struct{}{}
}

remainingMappings := make([]*QualifierMapping, 0)
for _, mapping := range mappings {
if _, found := filteredMappingsMap[mapping.Id]; !found {
remainingMappings = append(remainingMappings, mapping)
}
}

for _, mapping := range filteredMappings {
key := generateKey(mapping)
if existing, found := uniqueMappings[key]; found {
if mapping.UpdatedOn.After(existing.UpdatedOn) {
Expand All @@ -86,10 +100,34 @@ func DeduplicateQualifierMappings(mappings []*QualifierMapping) []*QualifierMapp
for _, mapping := range uniqueMappings {
result = append(result, mapping)
}

result = append(result, remainingMappings...)
return result
}

func generateKey(mapping *QualifierMapping) string {
return fmt.Sprintf("%d-%d-%d-%d-%d-%s", mapping.ResourceId, mapping.ResourceType, mapping.QualifierId, mapping.IdentifierKey, mapping.IdentifierValueInt, mapping.IdentifierValueString)
}

// filterOutParentChildMappings removes parent-child related mappings from the list
func filterOutParentChildMappings(mappings []*QualifierMapping) []*QualifierMapping {
parentChildRelatedMappings := make(map[int]bool)

for _, mapping := range mappings {
// Add parent and child mappings to the map as these are involved in parent-child relationship
if mapping.ParentIdentifier != 0 {
parentChildRelatedMappings[mapping.ParentIdentifier] = true
parentChildRelatedMappings[mapping.Id] = true
}
}

filteredMappings := make([]*QualifierMapping, 0)
for _, mapping := range mappings {
// skip mappings which are involved in parent-child relationship
if parentChildRelatedMappings[mapping.Id] {
continue
}
filteredMappings = append(filteredMappings, mapping)
}

return filteredMappings
}
Loading