Skip to content

Commit

Permalink
feat(api): allow to filter tasks with tags (#256)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bétrancourt <[email protected]>
  • Loading branch information
rclsilver authored Apr 9, 2021
1 parent c8a8325 commit 62e0a7d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
24 changes: 21 additions & 3 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package api

import (
"context"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/juju/errors"
"github.com/loopfz/gadgeto/zesty"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand All @@ -30,7 +32,7 @@ func collectMetrics(ctx context.Context) {
for {
select {
case <-tick.C:
stats, err := task.LoadStateCount(dbp)
stats, err := task.LoadStateCount(dbp, nil)
if err != nil {
logrus.Warn(err)
}
Expand All @@ -45,6 +47,10 @@ func collectMetrics(ctx context.Context) {
}()
}

type StatsIn struct {
Tags []string `query:"tag" explode:"true"`
}

// StatsOut aggregates different business stats:
// - a map of task states and their count
type StatsOut struct {
Expand All @@ -53,14 +59,26 @@ type StatsOut struct {

// Stats handles the http request to fetch µtask statistics
// common to all instances
func Stats(c *gin.Context) (*StatsOut, error) {
func Stats(c *gin.Context, in *StatsIn) (*StatsOut, error) {
dbp, err := zesty.NewDBProvider(utask.DBName)
if err != nil {
return nil, err
}

tags := make(map[string]string, len(in.Tags))
for _, t := range in.Tags {
parts := strings.Split(t, "=")
if len(parts) != 2 {
return nil, errors.BadRequestf("invalid tag %s", t)
}
if parts[0] == "" || parts[1] == "" {
return nil, errors.BadRequestf("invalid tag %s", t)
}
tags[parts[0]] = parts[1]
}

out := StatsOut{}
out.TaskStates, err = task.LoadStateCount(dbp)
out.TaskStates, err = task.LoadStateCount(dbp, tags)
if err != nil {
return nil, err
}
Expand Down
18 changes: 14 additions & 4 deletions models/task/stats.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package task

import (
"encoding/json"
"time"

"github.com/juju/errors"
Expand Down Expand Up @@ -46,13 +47,22 @@ func RegisterTaskTime(templateName string, taskCreation, resCreation time.Time)
}

// LoadStateCount returns a map containing the count of tasks grouped by state
func LoadStateCount(dbp zesty.DBProvider) (sc map[string]float64, err error) {
func LoadStateCount(dbp zesty.DBProvider, tags map[string]string) (sc map[string]float64, err error) {
defer errors.DeferredAnnotatef(&err, "Failed to load task stats")

query, params, err := sqlgenerator.PGsql.Select(`state, count(state) as state_count`).
sel := sqlgenerator.PGsql.Select(`state, count(state) as state_count`).
From(`"task"`).
GroupBy(`state`).
ToSql()
GroupBy(`state`)

if len(tags) > 0 {
b, err := json.Marshal(tags)
if err != nil {
return nil, err
}
sel = sel.Where(`"task".tags @> ?::jsonb`, string(b))
}

query, params, err := sel.ToSql()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 62e0a7d

Please sign in to comment.