Initial commit
This commit is contained in:
48
server/internal/domain/eta.go
Normal file
48
server/internal/domain/eta.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ETAInput struct {
|
||||
PeopleAhead int
|
||||
IntervalPerNumber time.Duration
|
||||
Running bool
|
||||
}
|
||||
|
||||
type ETAResult struct {
|
||||
Available bool `json:"available"`
|
||||
EstimateMinutes int `json:"estimate_minutes,omitempty"`
|
||||
MinMinutes int `json:"min_minutes,omitempty"`
|
||||
MaxMinutes int `json:"max_minutes,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
func CalculateETA(input ETAInput) (ETAResult, error) {
|
||||
if !input.Running {
|
||||
return ETAResult{Available: false, Reason: "queue_not_running"}, nil
|
||||
}
|
||||
if input.PeopleAhead < 0 {
|
||||
return ETAResult{}, errors.New("people ahead cannot be negative")
|
||||
}
|
||||
if input.IntervalPerNumber <= 0 {
|
||||
return ETAResult{Available: false, Reason: "missing_interval_configuration"}, nil
|
||||
}
|
||||
rawMinutes := float64(input.PeopleAhead+1) * input.IntervalPerNumber.Minutes()
|
||||
estimate := roundUpFive(rawMinutes)
|
||||
return ETAResult{
|
||||
Available: true,
|
||||
EstimateMinutes: estimate,
|
||||
MinMinutes: estimate,
|
||||
MaxMinutes: estimate,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func roundUpFive(value float64) int {
|
||||
if value <= 0 {
|
||||
return 0
|
||||
}
|
||||
return int(math.Ceil(value/5) * 5)
|
||||
}
|
||||
Reference in New Issue
Block a user