forked from subscan-explorer/prometheus-statuspage-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
51 lines (42 loc) · 1.16 KB
/
metrics.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
package main
import (
"encoding/json"
)
type statuspageMetricPoint struct {
Timestamp int64 `json:"timestamp"`
Value json.Number `json:"value"`
}
type statuspageMetrics map[string][]statuspageMetricPoint
type statuspageMetricsPayload struct {
Data statuspageMetrics `json:"data"`
}
const MAX_NUMBER_OF_METRIC_POINTS_PER_REQUEST = 3000
func chunkMetrics(metrics statuspageMetrics) []statuspageMetrics {
chunkedMetrics := []statuspageMetrics{}
chunk := statuspageMetrics{}
capacity := MAX_NUMBER_OF_METRIC_POINTS_PER_REQUEST
for metricID, points := range metrics {
var start, end int
length := len(points)
for start < length {
end = start + capacity
if end > length {
end = length
}
if _, ok := chunk[metricID]; ok {
chunk[metricID] = append(chunk[metricID], points[start:end]...)
} else {
chunk[metricID] = points[start:end]
}
capacity = capacity - (end - start)
if capacity == 0 {
chunkedMetrics = append(chunkedMetrics, chunk)
chunk = statuspageMetrics{}
capacity = MAX_NUMBER_OF_METRIC_POINTS_PER_REQUEST
}
start = end
}
}
chunkedMetrics = append(chunkedMetrics, chunk)
return chunkedMetrics
}