-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
394 lines (346 loc) · 8.7 KB
/
worker.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/icrowley/fake"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"strings"
"sync"
"time"
)
const (
moscowMinLon = 37.3468
moscowMinLat = 55.5593
moscowMaxLon = 37.8961
moscowMaxLat = 55.9146
)
var ConstantAddresses = []Location{
{
Point: &GeoPoint{
Lat: 55.797116,
Lon: 37.537862,
},
},
{
Point: &GeoPoint{
55.744584, 37.565937,
},
},
{
Point: &GeoPoint{
55.765906, 37.683876,
},
},
{
Point: &GeoPoint{
55.757219, 37.600293,
},
},
{
Point: &GeoPoint{
55.707944, 37.683233,
},
},
}
var ConstantCouriers = []Courier{
{Name: "Иван Васильев", Phone: "79039992231"},
{Name: "Герман Стерлигов", Phone: "88005553535"},
{Name: "Мстистлав Зеркальный", Phone: "78961235566"},
{Name: "Константин Константинопольский", Phone: "79000011010"},
{Name: "Борис Седых", Phone: "71127764388"},
{Name: "Гавриил Степанов", Phone: "79110234578"},
{Name: "Марат Ежов", Phone: "79167562345"},
{Name: "Самуил Остапенко", Phone: "79991112222"},
{Name: "Фарид Рахманов", Phone: "79458765134"},
{Name: "Аскольд Щичко", Phone: "79901123476"},
}
var (
AddrI = 0
CourI = 0
)
type Geometry struct {
Coordinates [][2]float64 `json:"coordinates"`
}
type Route struct {
Geometry *Geometry `json:"geometry"`
Legs []*Leg `json:"legs"`
}
type Annotation struct {
Duration []float32 `json:"duration"`
}
type Leg struct {
Annotation *Annotation `json:"annotation"`
}
type RouteResponse struct {
Routes []*Route `json:"routes"`
}
type Worker struct {
URL string
courier *Courier
orders []*Order
locations []*GeoPoint
durations []float32
iloc, idur int
Interval time.Duration
client *http.Client
addrI int
}
func NewWorker(url string) *Worker {
client := &http.Client{}
return &Worker{
courier: &Courier{},
client: client,
URL: url,
}
}
func (w *Worker) CreateCourier() error {
err := fake.SetLang("ru")
if err != nil {
log.Printf("%s", err)
return err
}
name := fake.FullName()
phone := fake.Phone()
var courier Courier
if CourI < len(ConstantCouriers) {
courier = ConstantCouriers[CourI]
CourI++
} else {
courier = Courier{
Name: name,
Phone: phone,
}
}
res, _ := json.Marshal(courier)
response, err := http.Post(w.buildURLCreate(w.URL), "application/json", bytes.NewReader(res))
if err != nil {
log.Printf("%s", err)
return err
}
body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
log.Printf("%s", err)
return err
}
err = json.Unmarshal(body, w.courier)
if err != nil {
log.Printf("%s", body)
return err
}
return nil
}
func (w *Worker) CreateOrder(routesURL string) error {
locationSrc := w.getRandomLocation(moscowMinLat, moscowMaxLat, moscowMinLon, moscowMaxLon)
locationDest := w.getRandomLocation(moscowMinLat, moscowMaxLat, moscowMinLon, moscowMaxLon)
order := &Order{}
order.OrderNumber = rand.Int() % 10000
if AddrI < len(ConstantAddresses) {
order.Destination = Location{
Point: &GeoPoint{
Lat: ConstantAddresses[AddrI].Point.Lat,
Lon: ConstantAddresses[AddrI].Point.Lon,
},
}
AddrI++
} else {
order.Destination = Location{
Point: &GeoPoint{
Lat: locationDest.Point.Lat,
Lon: locationDest.Point.Lon,
},
}
}
if w.orders == nil || len(w.orders) == 0 {
order.Source = Location{
Point: &GeoPoint{
Lat: locationSrc.Point.Lat,
Lon: locationSrc.Point.Lon,
},
}
} else {
order.Source = w.orders[0].Source
}
res, _ := json.Marshal(order)
response, err := http.Post(w.buildURLCreateOrder(w.URL), "application/json", bytes.NewReader(res))
if err != nil {
log.Printf("%s", err)
return err
}
body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
log.Printf("%s", err)
return err
}
err = json.Unmarshal(body, order)
if err != nil {
log.Printf("%s", body)
return err
}
w.orders = append(w.orders, order)
return nil
}
func (w *Worker) DeleteCourier() error {
req, err := http.NewRequest(http.MethodDelete, w.buildURLDelete(w.URL), nil)
if err != nil {
return err
}
resp, err := w.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.New(fmt.Sprintf("Not valid status code: expected 204, but %d", resp.StatusCode))
}
return nil
}
func (w *Worker) buildURLCreate(base string) string {
return fmt.Sprintf("%s%s", base, "/couriers")
}
func (w *Worker) buildURLCreateOrder(base string) string {
return fmt.Sprintf("%s/couriers/%s/orders", base, w.courier.ID)
}
func (w *Worker) buildURLUpdateOrder(base string, orderID string) string {
return fmt.Sprintf("%s/couriers/%s/orders/%s", base, w.courier.ID, orderID)
}
func (w *Worker) buildURLUpdate(base string) string {
return fmt.Sprintf("%s%s%s", base, "/couriers/", w.courier.ID)
}
func (w *Worker) buildURLGetRoute(routesURL string, locations []*Location) string {
buf := strings.Builder{}
buf.WriteString(fmt.Sprintf("%s/route/v1/driving/", routesURL))
for i, l := range locations {
buf.WriteString(fmt.Sprintf("%f,%f", l.Point.Lon, l.Point.Lat))
if i != len(locations)-1 {
buf.WriteByte(';')
}
}
buf.WriteString("?geometries=geojson&annotations=duration&overview=full")
return buf.String()
}
func (w *Worker) buildURLDelete(base string) string {
return fmt.Sprintf("%s%s%s", base, "/couriers/", w.courier.ID)
}
func (w *Worker) getRoute(routesURL string) error {
locations := make([]*Location, 0)
if w.orders == nil {
locations = append(locations, w.getRandomLocation(moscowMinLat, moscowMaxLat, moscowMinLon, moscowMaxLon))
locations = append(locations, w.getRandomLocation(moscowMinLat, moscowMaxLat, moscowMinLon, moscowMaxLon))
} else {
locations = append(locations, &w.orders[0].Source)
for _, o := range w.orders {
locations = append(locations, &o.Destination)
}
}
buildStr := w.buildURLGetRoute(routesURL, locations)
log.Println(buildStr)
response, err := http.Get(buildStr)
if err != nil {
log.Printf("%s", err)
return err
}
b, _ := ioutil.ReadAll(response.Body)
resp := RouteResponse{}
err = json.Unmarshal(b, &resp)
if err != nil {
log.Printf("%s", b)
panic(err)
}
log.Printf("%s\n", b)
for _, r := range resp.Routes[0].Geometry.Coordinates {
point := GeoPoint{Lon: r[0], Lat: r[1]}
w.locations = append(w.locations, &point)
}
for _, l := range resp.Routes[0].Legs {
for _, r := range l.Annotation.Duration {
w.durations = append(w.durations, r)
}
}
return nil
}
func (w *Worker) UpdateLocation(wg *sync.WaitGroup, speed int, routesURL string, interval time.Duration, ctx context.Context) {
w.getRoute(routesURL)
defer wg.Done()
timer := time.NewTimer(time.Millisecond)
for {
if w.idur == len(w.durations) {
log.Println("sending cancel orders...")
w.SendCancelOrder()
return
}
select {
case <-timer.C:
if err := w.update(); err != nil {
return
}
timer.Reset(time.Duration((w.durations[w.idur]*1000)/float32(speed)) * time.Millisecond)
w.idur++
case <-ctx.Done():
return
}
}
}
func (w *Worker) update() error {
w.courier.Location = w.getNextLocationFromRoute()
body, err := json.Marshal(w.courier)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPut, w.buildURLUpdate(w.URL), bytes.NewReader(body))
resp, err := w.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (w *Worker) getNextLocationFromRoute() *Location {
dlat, dlon := w.locations[w.iloc].Lat, w.locations[w.iloc].Lon
if w.iloc < len(w.locations)-1 {
w.iloc += 1
}
return &Location{&GeoPoint{dlat, dlon}}
}
func (w *Worker) getRandomLocation(minLat, maxLat, minLon, maxLon float64) *Location {
return &Location{&GeoPoint{
Lat: trim(minLat+rand.Float64()*(maxLat-minLat), 4),
Lon: trim(minLon+rand.Float64()*(maxLon-minLon), 4),
}}
}
func (w *Worker) SendCancelOrder() {
for _, o := range w.orders {
url := w.buildURLUpdateOrder(w.URL, o.ID)
bodyStruct := struct {
DeliveredAt int64 `json:"delivered_at"`
}{
time.Now().Unix(),
}
body, _ := json.Marshal(bodyStruct)
if req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(body)); err != nil {
log.Println(err)
} else {
if resp, err := w.client.Do(req); err != nil {
log.Println(err)
} else {
if resp.StatusCode != http.StatusOK {
log.Printf("error: expected %d, got %d", http.StatusOK, resp.StatusCode)
}
}
}
}
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func trim(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}