updates on the sweeper and eligibility things in the order

This commit is contained in:
2026-10-06 15:41:16 +05:30
parent 9b94be1f07
commit efad4d3d12
10 changed files with 803 additions and 35 deletions

View File

@@ -149,17 +149,16 @@ func collectEligibleCandidates(nearby []redis.GeoLocation) ([]*milerCandidate, [
continue
}
// Only today's open stops count towards the cap. An order assigned
// yesterday or last month and never closed (rider app not updated,
// order abandoned) used to hold the rider at the cap forever, so the
// one rider actually working was skipped for every new order.
var activeCount int64
db.DB.Model(&models.BookingAssignment{}).
Where("mileruserid = ? AND assignmentstatus IN ? AND assignedat >= ?", milerUserID, []string{
constants.AssignmentAssigned,
constants.AssignmentAccepted,
}, startOfISTDay(time.Now())).
Count(&activeCount)
// Only a rider whose app is actually reporting can take an order. A
// status left at "Assigned" by an app that stopped weeks ago used to
// make that rider look like the least busy one, so orders went to
// nobody. Compared in SQL against the database clock, which is right
// whatever type the column has.
if !milerHasFreshGPS(milerUserID) {
continue
}
activeCount := openStopsToday(milerUserID)
if activeCount >= maxActiveBookings() {
continue
@@ -189,6 +188,39 @@ func collectEligibleCandidates(nearby []redis.GeoLocation) ([]*milerCandidate, [
return candidates, aiCandidates
}
// openStopsToday is what counts towards the per-rider cap: the rider's open
// assignments (Assigned / Accepted) made since midnight India time, on orders
// that are not cancelled. Before, every open record of any age counted, and
// nothing closed a record when ops cancelled its order — so a rider with a
// pile of old or cancelled orders sat at the cap for good and every new order
// stayed Pending.
func openStopsToday(milerUserID int) int64 {
var n int64
db.DB.Table("bookingassignments AS ba").
Joins("JOIN pickupbookings pb ON pb.bookingid = ba.bookingid").
Where("ba.mileruserid = ? AND ba.assignmentstatus IN ? AND ba.assignedat >= ? AND pb.status <> ?",
milerUserID,
[]string{constants.AssignmentAssigned, constants.AssignmentAccepted},
startOfISTDay(time.Now()),
constants.BookingCancelled).
Count(&n)
return n
}
// milerHasFreshGPS reports whether the rider's last position is recent enough
// (ASSIGNMENT_MAX_GPS_AGE_MINUTES, default 15; 0 turns the check off).
func milerHasFreshGPS(milerUserID int) bool {
maxAge := maxGPSAgeMinutes()
if maxAge == 0 {
return true
}
var n int64
db.DB.Model(&models.MilerProfile{}).
Where("userid = ? AND lastlocationupdatedat >= NOW() - make_interval(mins => ?)", milerUserID, maxAge).
Count(&n)
return n > 0
}
// startOfISTDay is midnight today in India, the boundary for "today's"
// open stops. An absolute instant, so it compares correctly with assignedat
// whether the column stores IST wall-clock or a real timestamp.