-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
57 lines (50 loc) · 1.22 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package cloudcostexplorer
import (
"strings"
)
// Item contains the keys and values of a single cost explorer item, based on the query groups.
type Item struct {
Keys []ItemKey
Values []float64
}
func NewItem(keys []ItemKey, periods int) *Item {
ret := &Item{
Keys: keys,
}
for range periods {
ret.Values = append(ret.Values, 0.0)
}
return ret
}
// Search returns whether the search string is contained on any item key value.
func (i *Item) Search(search string) bool {
sv := strings.ToLower(search)
for _, key := range i.Keys {
if kv, ok := key.Value.(string); ok {
if strings.Contains(
strings.ToLower(kv),
strings.ToLower(sv),
) {
return true
}
}
}
return false
}
// ItemKey is the id and value of one item dimension, like "ID=service, Value=EC2".
type ItemKey struct {
ID string
Value any // can be ItemValue or ValueOutput.
}
// ItemValue is the default item value, a monetary cost.
type ItemValue struct {
Value float64
}
// DefaultItemKeysHash is the default function for hashing the list of item keys.
func DefaultItemKeysHash(keys []ItemKey) string {
var values []string
for _, key := range keys {
values = append(values, key.ID)
}
return DefaultHash(strings.Join(values, "-"))
}