forked from cncf/devstatscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_points.go
90 lines (83 loc) · 1.68 KB
/
ts_points.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
80
81
82
83
84
85
86
87
88
89
90
package devstatscode
import (
"fmt"
"time"
)
// TSPoint keeps single time series point
type TSPoint struct {
t time.Time
added time.Time
period string
name string
tags map[string]string
fields map[string]interface{}
}
// TSPoints keeps batch of TSPoint values to write
type TSPoints []TSPoint
// Str - string pretty print
func (p *TSPoint) Str() string {
return fmt.Sprintf(
"%s %s %s period: %s tags: %+v fields: %+v",
ToYMDHDate(p.t),
ToYMDHDate(p.added),
p.name,
p.period,
p.tags,
p.fields,
)
}
// Str - string pretty print
func (ps *TSPoints) Str() string {
s := ""
for i, p := range *ps {
s += fmt.Sprintf("#%d %s\n", i+1, p.Str())
}
return s
}
// NewTSPoint returns new point as specified by args
func NewTSPoint(ctx *Ctx, name, period string, tags map[string]string, fields map[string]interface{}, t time.Time, exact bool) TSPoint {
var (
otags map[string]string
ofields map[string]interface{}
)
if tags != nil {
otags = make(map[string]string)
for k, v := range tags {
otags[k] = v
}
}
if fields != nil {
ofields = make(map[string]interface{})
for k, v := range fields {
ofields[k] = v
}
}
var pt time.Time
if exact {
pt = t
} else {
pt = HourStart(t)
}
p := TSPoint{
t: pt,
added: time.Now(),
name: name,
period: period,
tags: otags,
fields: ofields,
}
if ctx.Debug > 0 {
Printf("NewTSPoint: %s\n", p.Str())
}
return p
}
// AddTSPoint add single point to the batch
func AddTSPoint(ctx *Ctx, pts *TSPoints, pt TSPoint) {
if ctx.Debug > 0 {
Printf("AddTSPoint: %s\n", pt.Str())
}
*pts = append(*pts, pt)
if ctx.Debug > 0 {
Printf("AddTSPoint: point added, now %d points\n", len(*pts))
}
}