Skip to content

Commit

Permalink
get with deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
prkhrkat committed Sep 27, 2024
1 parent 0ef2a59 commit 6ebba83
Showing 1 changed file with 39 additions and 1 deletion.
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
}

0 comments on commit 6ebba83

Please sign in to comment.