forked from nullitics/nullitics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
196 lines (182 loc) · 4.54 KB
/
stats.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package nullitics
import (
"errors"
"strconv"
"strings"
"time"
)
// Stats is an aggregated data from the various site-related statistics over a
// given time period.
type Stats struct {
Start time.Time
Interval time.Duration
URIs Frame
Sessions Frame
Refs Frame
Countries Frame
Devices Frame
}
func (stats *Stats) frames() []*Frame {
return []*Frame{&stats.URIs, &stats.Sessions, &stats.Refs, &stats.Countries, &stats.Devices}
}
// CSV returns a CSV-formatted text stats representation.
func (stats *Stats) CSV() string {
b := &strings.Builder{}
// Header: timestamp and interval as a comment
b.WriteByte('#')
b.WriteString(stats.Start.Format(time.RFC3339))
b.WriteByte(',')
b.WriteString(stats.Interval.String())
b.WriteByte('\n')
// Frames
for _, frame := range stats.frames() {
for _, row := range frame.Rows {
b.WriteString(row.Name)
for _, v := range row.Values {
b.WriteByte(',')
b.WriteString(strconv.Itoa(v))
}
b.WriteByte('\n')
}
b.WriteByte('\n')
}
return b.String()
}
// ParseStatsCSV returns a Stats instance from the given CSV text string.
func ParseStatsCSV(s string) (*Stats, error) {
lines := strings.Split(s, "\n")
if lines[0] == "" {
return &Stats{Interval: time.Hour * 24}, nil
}
if lines[0][0] != '#' {
return nil, errors.New("first line must be a comment")
}
parts := strings.Split(lines[0][1:], ",")
if len(parts) != 2 {
return nil, errors.New("first line must contain a timestamp and interval")
}
t, err := time.Parse(time.RFC3339, parts[0])
if err != nil {
return nil, err
}
d, err := time.ParseDuration(parts[1])
if err != nil {
return nil, err
}
stats := &Stats{Start: t, Interval: d}
lines = lines[1:]
n := 0
for _, frame := range stats.frames() {
frame.Grow(n)
for len(lines) > 0 {
line := lines[0]
lines = lines[1:]
if line == "" {
break
}
parts := strings.Split(line, ",")
if len(parts) < 2 {
return nil, errors.New("expected at least two fields per row")
}
n = len(parts) - 1
frame.Grow(n)
row := frame.Row(parts[0])
for i, v := range parts[1:] {
n, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return nil, err
}
row.Values[i] = int(n)
}
}
}
return stats, nil
}
// Frame is a matrix-shaped data frame, used to store time series of named
// integer values.
type Frame struct {
len int
Rows []Row
}
// Len returns the current length of the row time series (i.e. row width)
// within the frame.
func (f *Frame) Len() int {
if f.len == 0 && len(f.Rows) > 0 {
f.len = len(f.Rows[0].Values)
}
return f.len
}
// Grow resizes all the rows of the frame to the given width. Same width will
// be also applied to the new rows that will be created in this frame.
func (f *Frame) Grow(size int) {
if size == f.Len() {
return
}
f.len = size
for i := range f.Rows {
row := &f.Rows[i]
if m := size - len(row.Values); m > 0 {
row.Values = append(row.Values, make([]int, m)...)
} else {
row.Values = row.Values[:size]
}
}
}
// Row returns an existing row by its name or inserts one. It ensures that rows
// are sorted alphabetically within the data frame.
func (f *Frame) Row(name string) *Row {
i, found := f.find(name)
if !found {
f.Rows = append(f.Rows, Row{})
copy(f.Rows[i+1:], f.Rows[i:])
f.Rows[i].Name = name
f.Rows[i].Values = make([]int, f.Len())
}
return &f.Rows[i]
}
// Delete removes a row from the frame
func (f *Frame) Delete(name string) bool {
i, found := f.find(name)
if found {
copy(f.Rows[i:], f.Rows[i+1:])
f.Rows = f.Rows[:len(f.Rows)-1]
}
return found
}
// Find looks up for a row in the frame. It returns an index of the row and a
// flag if such row exists. If the flag is false - the row should be inserted
// at the provided index.
func (f *Frame) find(name string) (int, bool) {
i, j := 0, len(f.Rows)
for i < j {
h := int(uint(i+j) >> 1)
if f.Rows[h].Name < name {
i = h + 1
} else {
j = h
}
}
found := i < len(f.Rows) && f.Rows[i].Name == name
return i, found
}
// Row is a single record for names time series in the stats data frame. Row
// has a unique name and a series of integer values, one for each time slot of
// the data frame.
type Row struct {
Name string
Values []int
}
// Get is a helper method to safely read Row values without taking care about row bounds.
func (r *Row) Get(i int) int {
if i < 0 || i >= len(r.Values) {
return 0
}
return r.Values[i]
}
// Last returns the sum of the last n elements in the Row
func (r *Row) Last(n int) (sum int) {
for i := 0; i < n; i++ {
sum = sum + r.Get(len(r.Values)-1-i)
}
return sum
}