-
Notifications
You must be signed in to change notification settings - Fork 16
/
counter.go
50 lines (42 loc) · 1.28 KB
/
counter.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
package types
import (
"time"
sq "github.com/Masterminds/squirrel"
"github.com/gbl08ma/sqalx"
"github.com/palantir/stacktrace"
)
// Counter is a multipurpose integer counter
type Counter struct {
CounterName string `dbKey:"true"`
CounterValue int
UpdatedAt time.Time
}
// GetCounters returns all registered counters
func GetCounters(node sqalx.Node, pagParams *PaginationParams) ([]*Counter, uint64, error) {
s := sdb.Select().
OrderBy("counter.counter_name ASC")
s = applyPaginationParameters(s, pagParams)
return GetWithSelectAndCount[*Counter](node, s)
}
// GetCountersWithNames returns the counters with the specified names
func GetCountersWithNames(node sqalx.Node, names []string) (map[string]*Counter, error) {
s := sdb.Select().
Where(sq.Eq{"counter.counter_name": names})
items, err := GetWithSelect[*Counter](node, s)
if err != nil {
return map[string]*Counter{}, stacktrace.Propagate(err, "")
}
result := make(map[string]*Counter, len(items))
for i := range items {
result[items[i].CounterName] = items[i]
}
return result, nil
}
// Update updates or inserts the Counter
func (obj *Counter) Update(node sqalx.Node) error {
return Update(node, obj)
}
// Delete deletes the Counter
func (obj *Counter) Delete(node sqalx.Node) error {
return Delete(node, obj)
}