-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.go
158 lines (130 loc) · 3.91 KB
/
datasource.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
package util
import (
"fmt"
time2 "github.com/peter-mount/piweather.center/util/time"
"github.com/peter-mount/piweather.center/weather/value"
"time"
)
// DataSource represents a collection of values to be plotted
type DataSource interface {
// Size of the DataSource
Size() int
// Get a specific entry in the DataSource
Get(int) (time.Time, value.Value)
// Period returns the Period of the entries within the DataSource
Period() time2.Period
// GetYRange returns the Range of values in the DataSource
GetYRange() *value.Range
// GetUnit returns the Unit of the values in the DataSource
GetUnit() *value.Unit
// ForEach calls a function for each entry in the DataSource
ForEach(func(int, time.Time, value.Value))
}
// SliceDataSource returns a DataSource over a slice of Entry's
func SliceDataSource(s []Entry, period time2.Period, unit *value.Unit) (DataSource, error) {
yRange := value.NewRange(unit)
for _, e := range s {
if err := yRange.Add(e.Value); err != nil {
return nil, err
}
}
return &sliceDataSource{
s: s,
period: period,
yRange: yRange,
unit: unit,
}, nil
}
type sliceDataSource struct {
s []Entry
period time2.Period
yRange *value.Range
unit *value.Unit
}
type Entry struct {
Time time.Time
Value value.Value
}
func (s *sliceDataSource) Size() int { return len(s.s) }
func (s *sliceDataSource) Get(i int) (time.Time, value.Value) {
e := s.s[i]
return e.Time, e.Value
}
func (s *sliceDataSource) Period() time2.Period { return s.period }
func (s *sliceDataSource) GetYRange() *value.Range { return s.yRange }
func (s *sliceDataSource) GetUnit() *value.Unit { return s.unit }
func (s *sliceDataSource) ForEach(f func(int, time.Time, value.Value)) {
for i, e := range s.s {
f(i, e.Time, e.Value)
}
}
// PseudoDataSource returns a DataSource based on the output of a calculator
func PseudoDataSource(c value.Calculator, period time2.Period, unit *value.Unit, stepSize time.Duration, t value.Time) (DataSource, error) {
if stepSize < time.Minute {
return nil, fmt.Errorf("invalid stepSize %v", stepSize)
}
var s []Entry
tm := period.Start()
for tm.Before(period.End()) {
t.SetTime(tm)
v, err := c(t)
if err != nil {
return nil, err
}
s = append(s, Entry{
Time: tm,
Value: v,
})
tm = tm.Add(stepSize)
}
return SliceDataSource(s, period, unit)
}
// LimitedPseudoDataSource is more optimal than PseudoDataSource as it doesn't have to precalculate the results as the min/max Value's
// are provided, so calculations are made on the fly.
func LimitedPseudoDataSource(c value.Calculator, period time2.Period, unit *value.Unit, stepSize time.Duration, min, max value.Value, t value.Time) (DataSource, error) {
if stepSize < time.Minute {
return nil, fmt.Errorf("invalid stepSize %v", stepSize)
}
yRange := value.NewRange(unit)
_ = yRange.Add(min)
_ = yRange.Add(max)
return &limitedDataSource{
c: c,
t: t,
size: int(period.DurationMinutes() / stepSize.Minutes()),
stepSize: stepSize,
period: period,
unit: unit,
yRange: yRange,
}, nil
}
type limitedDataSource struct {
c value.Calculator
t value.Time
size int
stepSize time.Duration
period time2.Period
unit *value.Unit
yRange *value.Range
}
func (s *limitedDataSource) Size() int { return s.size }
func (s *limitedDataSource) Get(i int) (time.Time, value.Value) {
t := s.t.Clone()
t.SetTime(s.period.Start().Add(s.stepSize * time.Duration(i)))
v, _ := s.c(t)
return t.Time(), v
}
func (s *limitedDataSource) Period() time2.Period { return s.period }
func (s *limitedDataSource) GetYRange() *value.Range { return s.yRange }
func (s *limitedDataSource) GetUnit() *value.Unit { return s.unit }
func (s *limitedDataSource) ForEach(f func(int, time.Time, value.Value)) {
t := s.t.Clone()
t.SetTime(s.period.Start())
i := 0
for s.period.Contains(t.Time()) {
v, _ := s.c(t)
f(i, t.Time(), v)
i++
t.SetTime(t.Time().Add(s.stepSize))
}
}