-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart_petitions.go
79 lines (62 loc) · 1.81 KB
/
chart_petitions.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
"strings"
"time"
promapi "github.com/prometheus/client_golang/api/prometheus/v1"
prommodel "github.com/prometheus/common/model"
)
func chartPetitions(api promapi.API) ([]Stat, error) {
promctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
r := promapi.Range{
Start: time.Now().Add(-1 * 7 * 24 * time.Hour),
End: time.Now(),
Step: 1 * time.Hour,
}
top15PetitionsQuery := `topk(15, delta(petitions_signatures[24h]))`
result, warnings, err := api.Query(promctx, top15PetitionsQuery, time.Now())
if err != nil {
return nil, err
}
if len(warnings) > 0 {
return nil, fmt.Errorf("Received some warnings: %s", warnings)
}
if result.Type() != prommodel.ValVector {
t := result.Type().String()
return nil, fmt.Errorf("Got a %s from Prometheus, not matrix", t)
}
resultVector := result.(prommodel.Vector)
petitionIDs := []string{}
for _, stream := range resultVector {
petitionIDs = append(petitionIDs, string(stream.Metric["id"]))
}
petitionIDQueryFrag := strings.Join(petitionIDs, "|")
petitionsQuery := fmt.Sprintf(`sum by (action, id) (
delta(
petitions_signatures{id=~"%s"}[1h]
)
)`, petitionIDQueryFrag)
result, warnings, err = api.QueryRange(promctx, petitionsQuery, r)
if err != nil {
return nil, err
}
if len(warnings) > 0 {
return nil, fmt.Errorf("Received some warnings: %s", warnings)
}
if result.Type() != prommodel.ValMatrix {
t := result.Type().String()
return nil, fmt.Errorf("Got a %s from Prometheus, not matrix", t)
}
resultMatrix := result.(prommodel.Matrix)
stats := make([]Stat, 0)
for _, stream := range resultMatrix {
stat, err := makeChart("signatures", "petitions exporter", stream)
if err != nil {
return nil, err
}
stats = append(stats, stat)
}
return stats, nil
}