Skip to content

Commit

Permalink
Split filtering function
Browse files Browse the repository at this point in the history
  • Loading branch information
ikerreyes committed Nov 13, 2024
1 parent a9f9669 commit 53b6c8a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 12 deletions.
29 changes: 17 additions & 12 deletions storage/dvo_recommendations_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,30 +661,35 @@ func (storage DVORecommendationsDBStorage) filterReportWithHeartbeats(dvoReport

seenObjects := map[string]bool{}

for _, rec := range reportData.WorkloadRecommendations {
workloads := rec.Workloads
i := 0 // output index
for _, x := range workloads {
if _, ok := aliveInstances[x.UID]; ok {
// copy and increment index
workloads[i] = x
i++
seenObjects[x.UID] = true
}
}
rec.Workloads = workloads[:i]
for i, rec := range reportData.WorkloadRecommendations {
reportData.WorkloadRecommendations[i].Workloads = filterWorkloads(rec.Workloads, aliveInstances, seenObjects)
}

bReport, err := json.Marshal(reportData)
if err != nil {
return dvoReport, err
}
fmt.Print(string(bReport))
dvoReport.Report = string(bReport)
dvoReport.Objects = uint(len(seenObjects)) // #nosec G115

return dvoReport, nil
}

// filterWorkloads: use aliveInstances to filter the workloads and add seen objects to a map
func filterWorkloads(workloads []types.DVOWorkload, aliveInstances map[string]bool, seenObjects map[string]bool) []types.DVOWorkload {
i := 0 // output index
for _, x := range workloads {
if _, ok := aliveInstances[x.UID]; ok {
// copy and increment index
workloads[i] = x
i++
seenObjects[x.UID] = true
}
}
return workloads[:i]
}

// DeleteReportsForOrg deletes all reports related to the specified organization from the storage.
func (storage DVORecommendationsDBStorage) DeleteReportsForOrg(orgID types.OrgID) error {
_, err := storage.connection.Exec("DELETE FROM dvo.dvo_report WHERE org_id = $1;", orgID)
Expand Down
60 changes: 60 additions & 0 deletions storage/dvo_recommendations_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "embed"
"encoding/json"
"fmt"
"reflect"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -838,4 +839,63 @@ func TestReadWorkloadsForClusterAndNamespace_HearbeatsFiltering(t *testing.T) {
helpers.FailOnError(t, err)

assert.Equal(t, uint(1), report.Objects)
assert.Contains(t, report.Report, "UID-0099")
assert.NotContains(t, report.Report, "UID-0100")
}


func NewDVOWorkload(uid string) types.DVOWorkload {
return types.DVOWorkload{
UID: uid,
}
}


func TestFilterWorkloads(t *testing.T) {

aliveInstances := map[string]bool{"x": true, "y": true, "z": true}

testCases := []struct{
workloads []types.DVOWorkload
seen []string
}{
{
workloads: []types.DVOWorkload{NewDVOWorkload("x")},
seen: []string{"x"},
},
{
workloads: []types.DVOWorkload{NewDVOWorkload("a")},
seen: []string{},
},
{
workloads: []types.DVOWorkload{NewDVOWorkload("a"), NewDVOWorkload("x")},
seen: []string{"x"},
},
{
workloads: []types.DVOWorkload{NewDVOWorkload("a"), NewDVOWorkload("x"), NewDVOWorkload("b"), NewDVOWorkload("y")},
seen: []string{"x", "y"},
},
}

for i, tt := range testCases {
t.Run("case-" + fmt.Sprint(i), func(t *testing.T) {
gotSeen := map[string]bool{}
expectedSeen := map[string]bool{}
for _, v := range tt.seen {
expectedSeen[v] = true
}
got := filterWorkloads(tt.workloads, aliveInstances, gotSeen)

Check failure on line 887 in storage/dvo_recommendations_storage_test.go

View workflow job for this annotation

GitHub Actions / Tests for Go 1.20

undefined: filterWorkloads

Check failure on line 887 in storage/dvo_recommendations_storage_test.go

View workflow job for this annotation

GitHub Actions / Tests for Go 1.21

undefined: filterWorkloads

if reflect.DeepEqual(expectedSeen, gotSeen) {
t.Errorf("Seen objects error got = %v, want %v", gotSeen, expectedSeen)
}
assert.Len(t, got, len(tt.seen))
gotUIDs := []string{}
for _, workload := range got{
append(gotUIDs, workload.UID)

Check failure on line 895 in storage/dvo_recommendations_storage_test.go

View workflow job for this annotation

GitHub Actions / Tests for Go 1.20

append(gotUIDs, workload.UID) (value of type []string) is not used
}
assert.Equal(t, tt.seen, gotUIDs)
})
}

}

0 comments on commit 53b6c8a

Please sign in to comment.