Skip to content

Commit

Permalink
Monitor venue (#11)
Browse files Browse the repository at this point in the history
Add monitor venue
  • Loading branch information
oriser authored Dec 11, 2023
1 parent 63e36fd commit 18a4291
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
7 changes: 6 additions & 1 deletion service/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ func (g *groupOrder) fetchDetails() (*wolt.OrderDetails, error) {
}

func (g *groupOrder) fetchVenue() (*wolt.Venue, error) {
venue, err := g.woltGroup.VenueDetails()
details, err := g.fetchDetails()
if err != nil {
return nil, fmt.Errorf("get group details: %w", err)
}

venue, err := g.woltGroup.VenueDetails(details)
if err != nil {
return nil, fmt.Errorf("get venue details: %w", err)
}
Expand Down
41 changes: 41 additions & 0 deletions service/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,14 @@ func (h *Service) getRateForGroup(receiver, groupID, messageID string) (groupRat

ctx, cancel := context.WithTimeout(context.Background(), h.cfg.TimeoutForReady)
defer cancel()

monitorCtx, monitorCancel := context.WithCancel(ctx)
go h.monitorVenue(monitorCtx, order, receiver, messageID)
if err = order.WaitUntilFinished(ctx, h.cfg.WaitBetweenStatusCheck); err != nil {
monitorCancel()
return GroupRate{}, fmt.Errorf("wait for group to finish: %w", err)
}
monitorCancel()

details, err := order.Details()
if err != nil {
Expand All @@ -259,3 +264,39 @@ func (h *Service) getRateForGroup(receiver, groupID, messageID string) (groupRat
}
return h.buildGroupRates(rates, details.Host, deliveryRate), nil
}

func (h *Service) monitorVenue(ctx context.Context, order *groupOrder, receiver, initialMessageID string) {
details, err := order.Details()
if err != nil {
log.Printf("Error getting details for order %q: %v\n", order.id, err)
return
}

// TODO: Configure that
ticker := time.NewTicker(30 * time.Second)

online := true
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
venue, err := order.woltGroup.VenueDetails(details)
if err != nil {
log.Printf("Error getting venue for order %q: %v\n", order.id, err)
continue
}

if !venue.Online && online {
h.informEvent(receiver, ":red_circle: Pay attention. The venue went offline :(", "", initialMessageID)
online = false
}

if venue.Online && !online {
h.informEvent(receiver, ":large_green_circle: The venue is back online :)", "", initialMessageID)
online = true
}

}
}
}
7 changes: 1 addition & 6 deletions wolt/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,7 @@ func (g *Group) Details() (*OrderDetails, error) {
return ParseOrderDetails(output)
}

func (g *Group) VenueDetails() (*Venue, error) {
details, err := g.Details()
if err != nil {
return nil, fmt.Errorf("get group details: %w", err)
}

func (g *Group) VenueDetails(details *OrderDetails) (*Venue, error) {
req, err := g.prepareReq("GET", g.joinApiAddr(fmt.Sprintf("/v3/venues/%s", details.Details.VenueID)), nil, nil)
if err != nil {
return nil, fmt.Errorf("prepare venue request: %w", err)
Expand Down
7 changes: 4 additions & 3 deletions wolt/venue.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type Venue struct {
DeliverySpecs struct {
DeliveryPricing PriceRanges `json:"delivery_pricing"`
} `json:"delivery_specs"`
Names []VenueName `json:"name"`
Link string `json:"public_url"`
City string `json:"city"`
Names []VenueName `json:"name"`
Link string `json:"public_url"`
City string `json:"city"`
Online bool `json:"online"`

Name string
ParsedCoordinate Coordinate `json:"-"`
Expand Down

0 comments on commit 18a4291

Please sign in to comment.