-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeseries.go
229 lines (198 loc) · 4.63 KB
/
timeseries.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"os"
"sort"
"strconv"
"time"
)
const dateformat = "2006-01-02"
// Record is a timestamped float64
type Record struct {
Datum float64
timestamp time.Time
}
// TimeSeries is a series of Records
type TimeSeries struct {
granularity time.Duration
// records must remain sorted by timestamp
records []*Record
}
func (t *TimeSeries) sort() {
sort.Slice(t.records, func(i, j int) bool {
return t.records[i].timestamp.Before(t.records[j].timestamp)
})
}
// NewTimeSeries creates a new TimeSeries
func NewTimeSeries() *TimeSeries {
return &TimeSeries{
granularity: 24 * time.Hour,
records: make([]*Record, 0),
}
}
// Read parses a CSV file
func (t *TimeSeries) Read(db string) (err error) {
csvf, err := os.Open(db)
if err != nil {
return err
}
defer func() {
closeErr := csvf.Close()
if err == nil {
err = closeErr
}
}()
reader := csv.NewReader(bufio.NewReader(csvf))
reader.FieldsPerRecord = 2
for {
values, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
return err
}
timestamp, err := time.Parse(time.RFC3339, values[0])
if err != nil {
timestamp, err = time.Parse(dateformat, values[0])
if err != nil {
return err
}
}
datum, err := strconv.ParseFloat(values[1], 64)
if err != nil {
return err
}
t.records = append(t.records, &Record{
timestamp: timestamp,
Datum: datum,
})
}
t.sort()
return nil
}
// Write creates a CSV file
func (t *TimeSeries) Write(db string) error {
csvf, err := os.Create(db)
if err != nil {
return err
}
writer := csv.NewWriter(bufio.NewWriter(csvf))
for _, record := range t.records {
values := []string{
record.timestamp.Format(time.RFC3339),
fmt.Sprintf("%v", record.Datum),
}
err = writer.Write(values)
if err != nil {
return err
}
}
writer.Flush()
if err := writer.Error(); err != nil {
return err
}
return csvf.Close()
}
// InvalidTimestamp describes an error with the provided timestamp
type InvalidTimestamp time.Time
func (t InvalidTimestamp) Error() string {
return fmt.Sprintf("invalid timestamp %v", time.Time(t))
}
// Lookup finds the datum for a given timestamp
func (t *TimeSeries) Lookup(timestamp time.Time) (float64, error) {
n := len(t.records)
i := sort.Search(n, func(i int) bool {
return !t.records[i].timestamp.Before(timestamp)
})
if i == n || t.records[i].timestamp != timestamp {
return 0, InvalidTimestamp(timestamp)
}
return t.records[i].Datum, nil
}
// Add creates a new record
func (t *TimeSeries) Add(timestamp time.Time, datum float64) {
t.records = append(t.records, &Record{
timestamp: timestamp,
Datum: datum,
})
}
// Since returns a new TimeSeries with data since timestamp
func (t *TimeSeries) Since(timestamp time.Time) *TimeSeries {
i := sort.Search(len(t.records), func(i int) bool {
return !t.records[i].timestamp.Before(timestamp)
})
return &TimeSeries{
granularity: t.granularity,
records: t.records[i:],
}
}
// Resample updates the series by averaging close records
func (t *TimeSeries) Resample(d time.Duration) {
// Make a list of values for each duration
data := make(map[time.Time][]float64)
for _, record := range t.records {
timestamp := record.timestamp
timestamp = timestamp.Truncate(d)
values, ok := data[timestamp]
if !ok {
data[timestamp] = make([]float64, 0)
}
data[timestamp] = append(values, record.Datum)
}
// Calculate mean for each duration
records := make([]*Record, len(data))
i := 0
for timestamp, values := range data {
var total float64
for _, value := range values {
total += value
}
records[i] = &Record{
timestamp: timestamp,
Datum: total / float64(len(values)),
}
i++
}
// Use resampled values
t.granularity = d
t.records = records
t.sort()
}
// Interpolate fills in missing records using linear interpolation
func (t *TimeSeries) Interpolate() {
// Look for gaps (no records for a duration)
duration := t.granularity
var last *Record
var missing []*Record
for index, record := range t.records {
at := record.timestamp.Round(duration)
if index == 0 {
last = record
continue
}
interval := at.Sub(last.timestamp)
periods := float64(interval / duration)
if periods == 0 {
last = record
continue
}
startValue := last.Datum
endValue := record.Datum
step := (endValue - startValue) / periods
for period := periods - 1; period > 0; period-- {
at = at.Add(-duration)
missing = append(missing, &Record{
timestamp: at,
Datum: startValue + step*period,
})
}
last = record
}
// Sort in the interpolated records
t.records = append(t.records, missing...)
t.sort()
}