Skip to content

Commit

Permalink
Merge pull request #539 from isucon/fix-get-rides
Browse files Browse the repository at this point in the history
appGetRidesのFareの計算を修正
  • Loading branch information
wtks authored Nov 30, 2024
2 parents bc470db + 05d8703 commit 4ba5075
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"name": "SG-S120-47",
"model": "ストリームギア S1"
},
"fare": 4000,
"fare": 2500,
"evaluation": 4,
"requested_at": 1732568969000,
"completed_at": 1732571012000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"name": "IC-LX38-6303",
"model": "インペリアルクラフト LUXE"
},
"fare": 1800,
"fare": 500,
"evaluation": 5,
"requested_at": 1732617529000,
"completed_at": 1732618912000
Expand Down
34 changes: 30 additions & 4 deletions bench/benchmarker/world/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/isucon/isucon14/bench/internal/concurrent"
"github.com/samber/lo"
)

type UserState int
Expand Down Expand Up @@ -205,7 +206,7 @@ func (u *User) Tick(ctx *Context) error {
// 過去のリクエストを確認する
err := u.CheckRequestHistory(ctx)
if err != nil {
return err
return WrapCodeError(ErrorCodeFailedToCheckRequestHistory, err)
}

// リクエストを作成する
Expand All @@ -232,11 +233,36 @@ func (u *User) Deactivate() {
}

func (u *User) CheckRequestHistory(ctx *Context) error {
_, err := u.Client.GetRequests(ctx)
res, err := u.Client.GetRequests(ctx)
if err != nil {
return WrapCodeError(ErrorCodeFailedToCheckRequestHistory, err)
return err
}
if len(res.Requests) != len(u.RequestHistory) {
return fmt.Errorf("ライドの数が想定数と一致していません: expected=%d, got=%d", len(u.RequestHistory), len(res.Requests))
}

historyMap := lo.KeyBy(u.RequestHistory, func(r *Request) string { return r.ServerID })
for _, req := range res.Requests {
expected, ok := historyMap[req.ID]
if !ok {
return fmt.Errorf("想定されないライドが含まれています: id=%s", req.ID)
}
if !req.DestinationCoordinate.Equals(expected.DestinationPoint) || !req.PickupCoordinate.Equals(expected.PickupPoint) {
return fmt.Errorf("ライドの座標情報が正しくありません: id=%s", req.ID)
}
if req.Fare != expected.Fare() {
return fmt.Errorf("ライドの運賃が正しくありません: id=%s", req.ID)
}
if req.Evaluation != expected.CalculateEvaluation().Score() {
return fmt.Errorf("ライドの評価が正しくありません: id=%s", req.ID)
}
if req.Chair.ID != expected.Chair.ServerID || req.Chair.Name != expected.Chair.RegisteredData.Name || req.Chair.Model != expected.Chair.Model.Name || req.Chair.Owner != expected.Chair.Owner.RegisteredData.Name {
return fmt.Errorf("ライドの椅子の情報が正しくありません: id=%s", req.ID)
}
if !req.CompletedAt.Equal(expected.ServerCompletedAt) {
return fmt.Errorf("ライドの完了日時が正しくありません: id=%s", req.ID)
}
}
// TODO: ここでvalidationも行う?

return nil
}
Expand Down
28 changes: 23 additions & 5 deletions webapp/go/app_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ type getAppRidesResponseItemChair struct {
func appGetRides(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(*User)

tx, err := db.Beginx()
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
defer tx.Rollback()

rides := []Ride{}
if err := db.Select(
if err := tx.Select(
&rides,
`SELECT * FROM rides WHERE user_id = ? ORDER BY created_at DESC`,
user.ID,
Expand All @@ -195,7 +202,7 @@ func appGetRides(w http.ResponseWriter, r *http.Request) {

items := []getAppRidesResponseItem{}
for _, ride := range rides {
status, err := getLatestRideStatus(db, ride.ID)
status, err := getLatestRideStatus(tx, ride.ID)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
Expand All @@ -204,11 +211,17 @@ func appGetRides(w http.ResponseWriter, r *http.Request) {
continue
}

fare, err := calculateDiscountedFare(tx, user.ID, &ride, ride.PickupLatitude, ride.PickupLongitude, ride.DestinationLatitude, ride.DestinationLongitude)
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}

item := getAppRidesResponseItem{
ID: ride.ID,
PickupCoordinate: Coordinate{Latitude: ride.PickupLatitude, Longitude: ride.PickupLongitude},
DestinationCoordinate: Coordinate{Latitude: ride.DestinationLatitude, Longitude: ride.DestinationLongitude},
Fare: calculateSale(ride),
Fare: fare,
Evaluation: *ride.Evaluation,
RequestedAt: ride.CreatedAt.UnixMilli(),
CompletedAt: ride.UpdatedAt.UnixMilli(),
Expand All @@ -217,7 +230,7 @@ func appGetRides(w http.ResponseWriter, r *http.Request) {
item.Chair = getAppRidesResponseItemChair{}

chair := &Chair{}
if err := db.Get(chair, `SELECT * FROM chairs WHERE id = ?`, ride.ChairID); err != nil {
if err := tx.Get(chair, `SELECT * FROM chairs WHERE id = ?`, ride.ChairID); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
Expand All @@ -226,7 +239,7 @@ func appGetRides(w http.ResponseWriter, r *http.Request) {
item.Chair.Model = chair.Model

owner := &Owner{}
if err := db.Get(owner, `SELECT * FROM owners WHERE id = ?`, chair.OwnerID); err != nil {
if err := tx.Get(owner, `SELECT * FROM owners WHERE id = ?`, chair.OwnerID); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
Expand All @@ -235,6 +248,11 @@ func appGetRides(w http.ResponseWriter, r *http.Request) {
items = append(items, item)
}

if err := tx.Commit(); err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}

writeJSON(w, http.StatusOK, &getAppRidesResponse{
Rides: items,
})
Expand Down

0 comments on commit 4ba5075

Please sign in to comment.