-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwgps.go
238 lines (222 loc) · 5.92 KB
/
rwgps.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
package rwgps
import (
"context"
"fmt"
"strconv"
"sync"
"time"
api "github.com/bzimmer/activity"
"github.com/bzimmer/activity/rwgps"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"golang.org/x/time/rate"
"github.com/bzimmer/gravl"
"github.com/bzimmer/gravl/activity"
)
const Provider = "rwgps"
var before sync.Once //nolint:gochecknoglobals // once
func athlete(c *cli.Context) error {
client := gravl.Runtime(c).RideWithGPS
ctx, cancel := context.WithTimeout(c.Context, c.Duration("timeout"))
defer cancel()
user, err := client.Users.AuthenticatedUser(ctx)
if err != nil {
return err
}
log.Info().Int64("id", int64(user.ID)).Str("username", user.Name).Msg(c.Command.Name)
gravl.Runtime(c).Metrics.IncrCounter([]string{Provider, c.Command.Name}, 1)
err = gravl.Runtime(c).Encoder.Encode(user)
if err != nil {
return err
}
return nil
}
func athleteCommand() *cli.Command {
return &cli.Command{
Name: "athlete",
Aliases: []string{"t"},
Usage: "Query for the authenticated athlete",
Action: athlete,
}
}
func trips(c *cli.Context, kind string) error {
client := gravl.Runtime(c).RideWithGPS
ctx, cancel := context.WithTimeout(c.Context, c.Duration("timeout"))
defer cancel()
user, err := client.Users.AuthenticatedUser(ctx)
if err != nil {
return err
}
var metric string
var trips []*rwgps.Trip
switch kind {
case "trips":
metric = "activity"
trips, err = client.Trips.Trips(ctx, user.ID, api.Pagination{Total: c.Int("count")})
case "routes":
metric = "route"
trips, err = client.Trips.Routes(ctx, user.ID, api.Pagination{Total: c.Int("count")})
default:
trips, err = nil, fmt.Errorf("unknown type '%s'", kind)
}
if err != nil {
return err
}
enc := gravl.Runtime(c).Encoder
gravl.Runtime(c).Metrics.IncrCounter([]string{Provider, c.Command.Name}, 1)
for i, trip := range trips {
gravl.Runtime(c).Metrics.IncrCounter([]string{Provider, metric}, 1)
log.Info().
Time("date", trip.DepartedAt).
Int64("id", trip.ID).
Str("name", trip.Name).
Msg(c.Command.Name)
err = enc.Encode([]any{i, trip})
if err != nil {
return err
}
}
return nil
}
func activitiesCommand() *cli.Command {
return &cli.Command{
Name: "activities",
Aliases: []string{"A"},
Usage: "Query activities for the authenticated athlete",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "count",
Aliases: []string{"N"},
Value: 0,
Usage: "The number of activities to query from RideWithGPS",
},
},
Action: func(c *cli.Context) error { return trips(c, "trips") },
}
}
func routesCommand() *cli.Command {
return &cli.Command{
Name: "routes",
Usage: "Query routes for an athlete from RideWithGPS",
Aliases: []string{"R"},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "count",
Aliases: []string{"N"},
Value: 0,
Usage: "The number of routes to query from RideWithGPS",
},
},
Action: func(c *cli.Context) error { return trips(c, "routes") },
}
}
func entity(c *cli.Context, f func(context.Context, int64) (any, error)) error {
args := c.Args()
enc := gravl.Runtime(c).Encoder
for i := 0; i < args.Len(); i++ {
err := func() error {
ctx, cancel := context.WithTimeout(c.Context, c.Duration("timeout"))
defer cancel()
x, err := strconv.ParseInt(args.Get(i), 0, 0)
if err != nil {
return err
}
v, err := f(ctx, x)
if err != nil {
return err
}
gravl.Runtime(c).Metrics.IncrCounter([]string{Provider, c.Command.Name}, 1)
return enc.Encode(v)
}()
if err != nil {
return err
}
}
return nil
}
func activityCommand() *cli.Command {
return &cli.Command{
Name: "activity",
Aliases: []string{"a"},
Usage: "Query an activity from RideWithGPS",
Action: func(c *cli.Context) error {
client := gravl.Runtime(c).RideWithGPS
return entity(c, func(ctx context.Context, id int64) (any, error) {
trip, err := client.Trips.Trip(ctx, id)
if err != nil {
return nil, err
}
log.Info().Int64("id", trip.ID).Str("name", trip.Name).Msg(c.Command.Name)
return trip, nil
})
},
}
}
func routeCommand() *cli.Command {
return &cli.Command{
Name: "route",
Aliases: []string{"r"},
Usage: "Query a route from RideWithGPS",
Action: func(c *cli.Context) error {
client := gravl.Runtime(c).RideWithGPS
return entity(c, func(ctx context.Context, id int64) (any, error) {
route, err := client.Trips.Route(ctx, id)
if err != nil {
return nil, err
}
log.Info().Int64("id", route.ID).Str("name", route.Name).Msg(c.Command.Name)
return route, nil
})
},
}
}
func Before(c *cli.Context) error {
var err error
before.Do(func() {
var client *rwgps.Client
client, err = rwgps.NewClient(
rwgps.WithClientCredentials(c.String("rwgps-client-id"), ""),
rwgps.WithTokenCredentials(c.String("rwgps-access-token"), "", time.Time{}),
rwgps.WithHTTPTracing(c.Bool("http-tracing")),
rwgps.WithRateLimiter(rate.NewLimiter(
rate.Every(c.Duration("rate-limit")), c.Int("rate-burst"))))
if err != nil {
return
}
gravl.Runtime(c).RideWithGPS = client
gravl.Runtime(c).Metrics.IncrCounter([]string{Provider, "client", "created"}, 1)
log.Info().Msg("created rwgps client")
})
return err
}
func Command() *cli.Command {
return &cli.Command{
Name: "rwgps",
Category: "activity",
Usage: "Query RideWithGPS for rides and routes",
Description: "Operations supported by the RideWithGPS API",
Flags: append(AuthFlags(), activity.RateLimitFlags()...),
Before: Before,
Subcommands: []*cli.Command{
activitiesCommand(),
activityCommand(),
athleteCommand(),
routeCommand(),
routesCommand(),
},
}
}
func AuthFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "rwgps-client-id",
Usage: "RideWithGPS client id",
EnvVars: []string{"RWGPS_CLIENT_ID"},
},
&cli.StringFlag{
Name: "rwgps-access-token",
Usage: "RideWithGPS access token",
EnvVars: []string{"RWGPS_ACCESS_TOKEN"},
},
}
}