forked from strava/go.strava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.go
356 lines (284 loc) · 9.06 KB
/
streams.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package strava
import (
"encoding/json"
"errors"
"fmt"
)
// A StreamSet is a collection of possible streams for an Activity, Segment or SegmentEffort.
// Some types may be nil if they weren't requested or do not exist.
// Time is the only required stream for Uploaded activities.
// Manually created activties have no streams.
type StreamSet struct {
Time *IntegerStream // Seconds from start
Location *LocationStream // [lat, lng] tuples
Distance *DecimalStream // Distance in meters from start
Elevation *DecimalStream // Elevation in meters
Speed *DecimalStream // Speed in meters per second
HeartRate *IntegerStream // Heart Rate in beats per minute if available
Cadence *IntegerStream // Running or Cycling cadence in revolutions per minute
Power *IntegerStream // Cycling power output in watts
Temperature *IntegerStream // Temperature in Celsius
Moving *BooleanStream // Derived from speed and time to give some idea of when moving
Grade *DecimalStream // Grade or pitch in the road in percent
}
// A LocationStream represents [lat, lng] data.
// Values of [0, 0], aka. Null Island, should be considered as nil or unavailable
type LocationStream struct {
Stream
Data [][2]float64
}
// An IntegerStream represents time series data that is integer based
// such as Time, HeartRate, Cadence, Power or Temperature.
// Any nil/unavailable values in the original data will be 0 in the Data slice.
// To determine if a value is nil, check for it in RawData slice
type IntegerStream struct {
Stream
Data []int
RawData []*int
}
// A DecimalStream represents time series data that is decimal based
// such as Distance, Elevation, Speed or Grade.
// Any nil/unavailable values in the original data will be 0 in the Data slice.
// To determine if a value is nil, check for it in RawData slice
type DecimalStream struct {
Stream
Data []float64
RawData []*float64
}
// A BooleanStream represents time series data that is binary, or yes/no in nature.
// An example is the Moving stream
type BooleanStream struct {
Stream
Data []bool
}
// A Stream represents time series data of a given type.
// A streams for a given object are the same length. For every time in the Time stream
// there will be corresponding information in all the other available streams.
type Stream struct {
Type StreamType `json:"type"`
SeriesType string `json:"series_type"`
OriginalSize int `json:"original_size"`
Resolution string `json:"resolution"`
}
type StreamType string
var StreamTypes = struct {
Time StreamType
Location StreamType
Distance StreamType
Elevation StreamType
Speed StreamType
HeartRate StreamType
Cadence StreamType
Power StreamType
Temperature StreamType
Moving StreamType
Grade StreamType
}{"time", "latlng", "distance", "altitude", "velocity_smooth", "heartrate",
"cadence", "watts", "temp", "moving", "grade_smooth"}
type filler interface {
fill([]interface{})
}
type ActivityStreamsService streamsService
type SegmentStreamsService streamsService
type SegmentEffortStreamsService streamsService
type streamsService struct {
client *Client
parentType int
}
var types = struct {
Activity int
Segment int
SegmentEffort int
}{1, 2, 3}
func NewActivityStreamsService(client *Client) *ActivityStreamsService {
return &ActivityStreamsService{client, types.Activity}
}
func NewSegmentStreamsService(client *Client) *SegmentStreamsService {
return &SegmentStreamsService{client, types.Segment}
}
func NewSegmentEffortStreamsService(client *Client) *SegmentEffortStreamsService {
return &SegmentEffortStreamsService{client, types.SegmentEffort}
}
/*********************************************************/
type ActivityStreamsGetCall struct {
streamsGetCall
}
type SegmentStreamsGetCall struct {
streamsGetCall
}
type SegmentEffortStreamsGetCall struct {
streamsGetCall
}
type streamsGetCall struct {
service streamsService
id int64
types []StreamType
ops map[string]interface{}
}
/*********************************************************/
func (s *ActivityStreamsService) Get(activityId int64, types []StreamType) *ActivityStreamsGetCall {
call := &ActivityStreamsGetCall{}
call.service = streamsService(*s)
call.id = activityId
call.ops = make(map[string]interface{})
call.types = make([]StreamType, len(types))
copy(call.types, types)
return call
}
func (c *ActivityStreamsGetCall) Resolution(resolution string) *ActivityStreamsGetCall {
c.ops["resolution"] = resolution
return c
}
func (c *ActivityStreamsGetCall) SeriesType(seriesType string) *ActivityStreamsGetCall {
c.ops["series_type"] = seriesType
return c
}
/*********************************************************/
func (s *SegmentStreamsService) Get(segmentId int64, types []StreamType) *SegmentStreamsGetCall {
call := &SegmentStreamsGetCall{}
call.service = streamsService(*s)
call.id = segmentId
call.ops = make(map[string]interface{})
call.types = make([]StreamType, len(types))
copy(call.types, types)
return call
}
func (c *SegmentStreamsGetCall) Resolution(resolution string) *SegmentStreamsGetCall {
c.ops["resolution"] = resolution
return c
}
func (c *SegmentStreamsGetCall) SeriesType(seriesType string) *SegmentStreamsGetCall {
c.ops["series_type"] = seriesType
return c
}
/*********************************************************/
func (s *SegmentEffortStreamsService) Get(segmentEffortId int64, types []StreamType) *SegmentEffortStreamsGetCall {
call := &SegmentEffortStreamsGetCall{}
call.service = streamsService(*s)
call.id = segmentEffortId
call.ops = make(map[string]interface{})
call.types = make([]StreamType, len(types))
copy(call.types, types)
return call
}
func (c *SegmentEffortStreamsGetCall) Resolution(resolution string) *SegmentEffortStreamsGetCall {
c.ops["resolution"] = resolution
return c
}
func (c *SegmentEffortStreamsGetCall) SeriesType(seriesType string) *SegmentEffortStreamsGetCall {
c.ops["series_type"] = seriesType
return c
}
/*********************************************************/
func (c *streamsGetCall) Do() (*StreamSet, error) {
var source string
switch c.service.parentType {
case types.Activity:
source = "activities"
case types.Segment:
source = "segments"
case types.SegmentEffort:
source = "segment_efforts"
}
if source == "" {
return nil, errors.New("invalid stream parent type")
}
if len(c.types) == 0 {
return nil, errors.New("no streamtypes requested")
}
types := string(c.types[0])
for i := 1; i < len(c.types); i++ {
types += "," + string(c.types[i])
}
path := fmt.Sprintf("/%s/%d/streams/%s", source, c.id, types)
data, err := c.service.client.run("GET", path, c.ops)
if err != nil {
return nil, err
}
var set StreamSet
streams := make([]interface{}, 0, 10)
json.Unmarshal(data, &streams)
for _, stream := range streams {
m := stream.(map[string]interface{})
var base interface{}
var s Stream
s.Type = StreamType(m["type"].(string))
s.SeriesType = m["series_type"].(string)
s.OriginalSize = int(m["original_size"].(float64))
s.Resolution = m["resolution"].(string)
switch StreamType(m["type"].(string)) {
case StreamTypes.Time:
set.Time = &IntegerStream{s, nil, nil}
base = set.Time
case StreamTypes.Location:
set.Location = &LocationStream{s, nil}
base = set.Location
case StreamTypes.Distance:
set.Distance = &DecimalStream{s, nil, nil}
base = set.Distance
case StreamTypes.Elevation:
set.Elevation = &DecimalStream{s, nil, nil}
base = set.Elevation
case StreamTypes.Speed:
set.Speed = &DecimalStream{s, nil, nil}
base = set.Speed
case StreamTypes.HeartRate:
set.HeartRate = &IntegerStream{s, nil, nil}
base = set.HeartRate
case StreamTypes.Cadence:
set.Cadence = &IntegerStream{s, nil, nil}
base = set.Cadence
case StreamTypes.Power:
set.Power = &IntegerStream{s, nil, nil}
base = set.Power
case StreamTypes.Temperature:
set.Temperature = &IntegerStream{s, nil, nil}
base = set.Temperature
case StreamTypes.Moving:
set.Moving = &BooleanStream{s, nil}
base = set.Moving
case StreamTypes.Grade:
set.Grade = &DecimalStream{s, nil, nil}
base = set.Grade
}
f := base.(filler)
f.fill(m["data"].([]interface{}))
}
return &set, nil
}
func (s *LocationStream) fill(data []interface{}) {
s.Data = make([][2]float64, len(data))
for i, v := range data {
if l, ok := v.([]interface{}); ok {
s.Data[i] = [2]float64{l[0].(float64), l[1].(float64)}
}
}
}
func (s *IntegerStream) fill(data []interface{}) {
s.Data = make([]int, len(data))
s.RawData = make([]*int, len(data))
for i, v := range data {
if f, ok := v.(float64); ok {
s.Data[i] = int(f)
s.RawData[i] = &s.Data[i]
}
}
}
func (s *DecimalStream) fill(data []interface{}) {
var ok bool
s.Data = make([]float64, len(data))
s.RawData = make([]*float64, len(data))
for i, v := range data {
if s.Data[i], ok = v.(float64); ok {
s.RawData[i] = &s.Data[i]
}
}
}
func (s *BooleanStream) fill(data []interface{}) {
s.Data = make([]bool, len(data))
for i, v := range data {
if f, ok := v.(bool); ok {
s.Data[i] = f
}
}
}