Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Martin <chris@cmartinit.co.uk>
  • Loading branch information
d80tb7 committed Jul 4, 2024
2 parents 167825b + 91a346f commit 26b9ca6
Show file tree
Hide file tree
Showing 36 changed files with 740 additions and 137 deletions.
28 changes: 28 additions & 0 deletions internal/common/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package maps

import "github.com/armadaproject/armada/internal/common/interfaces"

// FromSlice maps element e of slice into map entry keyFunc(e): valueFunc(e)
func FromSlice[S []E, E any, K comparable, V any](slice S, keyFunc func(E) K, valueFunc func(E) V) map[K]V {
rv := make(map[K]V, len(slice))
for _, elem := range slice {
rv[keyFunc(elem)] = valueFunc(elem)
}
return rv
}

// MapValues maps the values of m into valueFunc(v).
func MapValues[M ~map[K]VA, K comparable, VA any, VB any](m M, valueFunc func(VA) VB) map[K]VB {
rv := make(map[K]VB, len(m))
Expand Down Expand Up @@ -75,3 +84,22 @@ func Filter[M ~map[K]V, K comparable, V any](m M, predicate func(K, V) bool) M {
}
return rv
}

// RemoveInPlace removes elements that match keySelector
func RemoveInPlace[K comparable, V any](m map[K]V, keySelector func(K) bool) {
for k := range m {
if keySelector(k) {
delete(m, k)
}
}
}

func Keys[K comparable, V any](m map[K]V) []K {
i := 0
result := make([]K, len(m))
for k := range m {
result[i] = k
i++
}
return result
}
42 changes: 42 additions & 0 deletions internal/common/maps/maps_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package maps

import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)

func TestFromSlice(t *testing.T) {
actual := FromSlice(
[]int{1, 2},
func(elem int) string { return strconv.Itoa(elem) },
func(elem int) float64 { return float64(elem) * 0.1 },
)
expected := map[string]float64{
"1": 0.1,
"2": 0.2,
}
assert.Equal(t, expected, actual)
}

func TestMapKeys(t *testing.T) {
m := map[string][]int{
"foo": {1, 2, 3},
Expand Down Expand Up @@ -176,3 +190,31 @@ func TestFilterKeys(t *testing.T) {
}
assert.Equal(t, expected, actual)
}

func TestRemoveInPlace(t *testing.T) {
m := map[int]string{
1: "one",
2: "two",
3: "three",
4: "four",
}
RemoveInPlace(m, func(i int) bool {
return i > 2
})
expected := map[int]string{
1: "one",
2: "two",
}
assert.Equal(t, expected, m)
}

func TestKeys(t *testing.T) {
m := map[int]string{
1: "one",
2: "two",
}
result := Keys(m)
slices.Sort(result)
expected := []int{1, 2}
assert.Equal(t, expected, result)
}
1 change: 1 addition & 0 deletions internal/common/metrics/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type QueueMetricProvider interface {
GetQueuedJobMetrics(queueName string) []*QueueMetrics
GetRunningJobMetrics(queueName string) []*QueueMetrics
GetQueuePriorites() map[string]float64
}

type QueueMetrics struct {
Expand Down
Loading

0 comments on commit 26b9ca6

Please sign in to comment.