@@ -2,9 +2,11 @@ package api
2
2
3
3
import (
4
4
"context"
5
+ "strings"
5
6
"time"
6
7
7
8
"github.com/gin-gonic/gin"
9
+ "github.com/juju/errors"
8
10
"github.com/loopfz/gadgeto/zesty"
9
11
"github.com/prometheus/client_golang/prometheus"
10
12
"github.com/prometheus/client_golang/prometheus/promauto"
@@ -30,7 +32,7 @@ func collectMetrics(ctx context.Context) {
30
32
for {
31
33
select {
32
34
case <- tick .C :
33
- stats , err := task .LoadStateCount (dbp )
35
+ stats , err := task .LoadStateCount (dbp , nil )
34
36
if err != nil {
35
37
logrus .Warn (err )
36
38
}
@@ -45,6 +47,10 @@ func collectMetrics(ctx context.Context) {
45
47
}()
46
48
}
47
49
50
+ type StatsIn struct {
51
+ Tags []string `query:"tag" explode:"true"`
52
+ }
53
+
48
54
// StatsOut aggregates different business stats:
49
55
// - a map of task states and their count
50
56
type StatsOut struct {
@@ -53,14 +59,26 @@ type StatsOut struct {
53
59
54
60
// Stats handles the http request to fetch µtask statistics
55
61
// common to all instances
56
- func Stats (c * gin.Context ) (* StatsOut , error ) {
62
+ func Stats (c * gin.Context , in * StatsIn ) (* StatsOut , error ) {
57
63
dbp , err := zesty .NewDBProvider (utask .DBName )
58
64
if err != nil {
59
65
return nil , err
60
66
}
61
67
68
+ tags := make (map [string ]string , len (in .Tags ))
69
+ for _ , t := range in .Tags {
70
+ parts := strings .Split (t , "=" )
71
+ if len (parts ) != 2 {
72
+ return nil , errors .BadRequestf ("invalid tag %s" , t )
73
+ }
74
+ if parts [0 ] == "" || parts [1 ] == "" {
75
+ return nil , errors .BadRequestf ("invalid tag %s" , t )
76
+ }
77
+ tags [parts [0 ]] = parts [1 ]
78
+ }
79
+
62
80
out := StatsOut {}
63
- out .TaskStates , err = task .LoadStateCount (dbp )
81
+ out .TaskStates , err = task .LoadStateCount (dbp , tags )
64
82
if err != nil {
65
83
return nil , err
66
84
}
0 commit comments