|
1 | 1 | package helpers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "sync" |
| 5 | + |
4 | 6 | "github.com/kubescape/opa-utils/reporthandling/apis" |
5 | | - "github.com/kubescape/opa-utils/reporthandling/internal/slices" |
| 7 | + "golang.org/x/exp/maps" |
6 | 8 | ) |
7 | 9 |
|
| 10 | +var allListsPool = &sync.Pool{ |
| 11 | + New: func() interface{} { |
| 12 | + return &AllLists{} |
| 13 | + }, |
| 14 | +} |
| 15 | + |
| 16 | +// GetAllListsFromPool get the AllLists object from the pool |
| 17 | +func GetAllListsFromPool() *AllLists { |
| 18 | + l := allListsPool.Get().(*AllLists) |
| 19 | + // reset the object before returning it as it might be dirty |
| 20 | + l.Clear() |
| 21 | + return l |
| 22 | +} |
| 23 | + |
| 24 | +// PutAllListsToPool put the AllLists object back to the pool |
| 25 | +func PutAllListsToPool(l *AllLists) { |
| 26 | + allListsPool.Put(l) |
| 27 | +} |
| 28 | + |
8 | 29 | // ReportObject any report object must be compliment with a map[string]interface{} structures |
9 | 30 | type ReportObject map[string]interface{} |
10 | 31 |
|
11 | 32 | // AllLists lists of resources/policies grouped by the status, this structure is meant for internal use of report handling and not an API |
12 | 33 | type AllLists struct { |
13 | | - passed []string |
14 | | - failed []string |
15 | | - skipped []string |
16 | | - excluded []string |
17 | | - other []string |
| 34 | + itemToStatus map[string]apis.ScanningStatus |
| 35 | + passed int |
| 36 | + failed int |
| 37 | + skipped int |
| 38 | + other int |
18 | 39 | } |
19 | 40 |
|
20 | | -type Iterator interface { |
21 | | - HasNext() bool |
22 | | - Next() string |
23 | | - Len() int |
| 41 | +func (all *AllLists) Failed() int { return all.failed } |
| 42 | +func (all *AllLists) Passed() int { return all.passed } |
| 43 | +func (all *AllLists) Skipped() int { return all.skipped } |
| 44 | +func (all *AllLists) Other() int { return all.other } |
| 45 | +func (all *AllLists) Len() int { |
| 46 | + return all.failed + all.passed + all.skipped + all.other |
24 | 47 | } |
25 | | - |
26 | | -type AllListsIterator struct { |
27 | | - allLists *AllLists |
28 | | - size int |
29 | | - index int |
30 | | - failedIndex int |
31 | | - passIndex int |
32 | | - skippedIndex int |
33 | | - otherIndex int |
| 48 | +func (all *AllLists) All() map[string]apis.ScanningStatus { |
| 49 | + return all.itemToStatus |
34 | 50 | } |
35 | 51 |
|
36 | | -func (all *AllLists) createIterator() Iterator { |
37 | | - return &AllListsIterator{ |
38 | | - size: len(all.failed) + len(all.passed) + len(all.skipped) + len(all.other), |
39 | | - allLists: all, |
| 52 | +// Initialize initialize the AllLists object map with the given size - this is an optimization for the map |
| 53 | +func (all *AllLists) Initialize(size int) { |
| 54 | + if all.itemToStatus == nil { |
| 55 | + all.itemToStatus = make(map[string]apis.ScanningStatus, size) |
40 | 56 | } |
41 | 57 | } |
42 | 58 |
|
43 | | -func (iter *AllListsIterator) Len() int { |
44 | | - return iter.size |
| 59 | +// Clear remove all items and reset the counters |
| 60 | +func (all *AllLists) Clear() { |
| 61 | + if all.itemToStatus != nil { |
| 62 | + maps.Clear(all.itemToStatus) |
| 63 | + all.passed = 0 |
| 64 | + all.failed = 0 |
| 65 | + all.skipped = 0 |
| 66 | + all.other = 0 |
| 67 | + } |
45 | 68 | } |
46 | 69 |
|
47 | | -func (iter *AllListsIterator) HasNext() bool { |
48 | | - return iter.index < iter.size |
49 | | -} |
| 70 | +// Append append single string to matching status list |
| 71 | +func (all *AllLists) Append(status apis.ScanningStatus, str ...string) { |
| 72 | + if all.itemToStatus == nil { |
| 73 | + all.itemToStatus = make(map[string]apis.ScanningStatus, len(str)) |
| 74 | + } |
50 | 75 |
|
51 | | -func (iter *AllListsIterator) Next() string { |
52 | | - var item string |
53 | | - if iter.HasNext() { |
54 | | - if iter.failedIndex < len(iter.allLists.failed) { |
55 | | - item = iter.allLists.failed[iter.failedIndex] |
56 | | - iter.failedIndex++ |
57 | | - } else if iter.passIndex < len(iter.allLists.passed) { |
58 | | - item = iter.allLists.passed[iter.passIndex] |
59 | | - iter.passIndex++ |
60 | | - } else if iter.skippedIndex < len(iter.allLists.skipped) { |
61 | | - item = iter.allLists.skipped[iter.skippedIndex] |
62 | | - iter.skippedIndex++ |
63 | | - } else if iter.otherIndex < len(iter.allLists.other) { |
64 | | - item = iter.allLists.other[iter.otherIndex] |
65 | | - iter.otherIndex++ |
| 76 | + for _, s := range str { |
| 77 | + oldStatus, exist := all.itemToStatus[s] |
| 78 | + if !exist { |
| 79 | + all.itemToStatus[s] = status |
| 80 | + switch status { |
| 81 | + case apis.StatusPassed: |
| 82 | + all.passed++ |
| 83 | + case apis.StatusFailed: |
| 84 | + all.failed++ |
| 85 | + case apis.StatusSkipped: |
| 86 | + all.skipped++ |
| 87 | + default: |
| 88 | + all.other++ |
| 89 | + } |
| 90 | + // element exist with different status |
| 91 | + } else if oldStatus != status { |
| 92 | + // check if the new status is more significant |
| 93 | + if result := apis.Compare(oldStatus, status); result == status { |
| 94 | + all.itemToStatus[s] = status |
| 95 | + switch status { |
| 96 | + case apis.StatusPassed: |
| 97 | + all.passed++ |
| 98 | + case apis.StatusFailed: |
| 99 | + all.failed++ |
| 100 | + case apis.StatusSkipped: |
| 101 | + all.skipped++ |
| 102 | + default: |
| 103 | + all.other++ |
| 104 | + } |
| 105 | + |
| 106 | + // update the old status |
| 107 | + switch oldStatus { |
| 108 | + case apis.StatusPassed: |
| 109 | + all.passed-- |
| 110 | + case apis.StatusFailed: |
| 111 | + all.failed-- |
| 112 | + case apis.StatusSkipped: |
| 113 | + all.skipped-- |
| 114 | + default: |
| 115 | + all.other-- |
| 116 | + } |
| 117 | + } |
66 | 118 | } |
67 | | - iter.index++ |
68 | 119 | } |
69 | | - return item |
70 | 120 | } |
71 | 121 |
|
72 | | -// GetAllResources |
73 | | - |
74 | | -func (all *AllLists) Failed() []string { return all.failed } |
75 | | -func (all *AllLists) Passed() []string { return append(all.passed, all.excluded...) } |
76 | | -func (all *AllLists) Skipped() []string { return all.skipped } |
77 | | -func (all *AllLists) Other() []string { return all.other } |
78 | | -func (all *AllLists) All() Iterator { |
79 | | - return all.createIterator() |
| 122 | +// Update AllLists objects with |
| 123 | +func (all *AllLists) Update(all2 *AllLists) { |
| 124 | + for item, status := range all2.itemToStatus { |
| 125 | + all.Append(apis.ScanningStatus(status), item) |
| 126 | + } |
80 | 127 | } |
81 | 128 |
|
82 | | -// Append append single string to matching status list |
83 | | -func (all *AllLists) Append(status apis.ScanningStatus, str ...string) { |
| 129 | +func (all *AllLists) GetItems(status apis.ScanningStatus) []string { |
| 130 | + var amount int |
84 | 131 | switch status { |
85 | 132 | case apis.StatusPassed: |
86 | | - all.passed = append(all.passed, str...) |
87 | | - case apis.StatusSkipped: |
88 | | - all.skipped = append(all.skipped, str...) |
| 133 | + amount = all.passed |
89 | 134 | case apis.StatusFailed: |
90 | | - all.failed = append(all.failed, str...) |
| 135 | + amount = all.failed |
| 136 | + case apis.StatusSkipped: |
| 137 | + amount = all.skipped |
91 | 138 | default: |
92 | | - all.other = append(all.other, str...) |
| 139 | + amount = all.other |
93 | 140 | } |
94 | | -} |
95 | | - |
96 | | -// Update AllLists objects with |
97 | | -func (all *AllLists) Update(all2 *AllLists) { |
98 | | - all.passed = append(all.passed, all2.passed...) |
99 | | - all.skipped = append(all.skipped, all2.skipped...) |
100 | | - all.failed = append(all.failed, all2.failed...) |
101 | | - all.other = append(all.other, all2.other...) |
102 | | -} |
103 | 141 |
|
104 | | -// ToUnique - Call this function only when setting the List |
105 | | -func (all *AllLists) toUniqueBase() { |
106 | | - // remove duplications from each resource list |
107 | | - all.failed = slices.UniqueStrings(all.failed) |
108 | | - all.passed = slices.UniqueStrings(all.passed) |
109 | | - all.skipped = slices.UniqueStrings(all.skipped) |
110 | | - all.other = slices.UniqueStrings(all.other) |
111 | | -} |
112 | | - |
113 | | -// ToUnique - Call this function only when setting the List |
114 | | -func (all *AllLists) ToUniqueControls() { |
115 | | - all.toUniqueBase() |
116 | | -} |
117 | | - |
118 | | -// ToUnique - Call this function only when setting the List |
119 | | -func (all *AllLists) ToUniqueResources() { |
120 | | - all.failed = slices.UniqueStrings(all.failed) |
121 | | - |
122 | | - const heuristicCapacity = 100 // alloc 100 slots to the stack. The rest would go to the heap - see https://github.com/golang/go/issues/58215 |
123 | | - |
124 | | - trimmed := append(make([]string, 0, heuristicCapacity), make([]string, 0, max(len(all.failed)+len(all.excluded)+len(all.passed)+len(all.skipped), heuristicCapacity)-heuristicCapacity)...) |
125 | | - |
126 | | - // remove failed from excluded list |
127 | | - trimmed = append(trimmed, all.failed...) |
128 | | - all.skipped = slices.TrimStableUnique(all.skipped, trimmed) |
129 | | - |
130 | | - // remove failed and skipped from passed list |
131 | | - trimmed = append(trimmed, all.skipped...) |
132 | | - all.passed = slices.TrimStableUnique(all.passed, trimmed) |
133 | | - |
134 | | - // remove failed, skipped and passed from "other" list |
135 | | - trimmed = append(trimmed, all.passed...) |
136 | | - all.other = slices.TrimStableUnique(all.other, trimmed) |
137 | | -} |
| 142 | + if amount == 0 { |
| 143 | + return []string{} |
| 144 | + } |
138 | 145 |
|
139 | | -func max(a, b int) int { |
140 | | - if a > b { |
141 | | - return a |
| 146 | + items := make([]string, 0, amount) |
| 147 | + for item, itemStatus := range all.itemToStatus { |
| 148 | + if itemStatus == status { |
| 149 | + items = append(items, item) |
| 150 | + } |
142 | 151 | } |
143 | 152 |
|
144 | | - return b |
| 153 | + return items |
145 | 154 | } |
0 commit comments