feat: add party-size queueing and call modes
This commit is contained in:
@@ -35,6 +35,45 @@ func (s *Server) publicStatus(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, view)
|
||||
}
|
||||
|
||||
func (s *Server) publicProjects(w http.ResponseWriter, r *http.Request) {
|
||||
var projects []model.Project
|
||||
if err := s.db.WithContext(r.Context()).
|
||||
Where("status = ?", model.ProjectRunning).
|
||||
Order("name ASC").Find(&projects).Error; err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
views := make([]map[string]any, 0, len(projects))
|
||||
for _, project := range projects {
|
||||
views = append(views, map[string]any{
|
||||
"id": project.ID, "name": project.Name, "status": project.Status,
|
||||
"visitor_notice": project.VisitorNotice,
|
||||
"min_party_size": project.MinPartySize, "max_party_size": project.MaxPartySize,
|
||||
})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"projects": views})
|
||||
}
|
||||
|
||||
func (s *Server) publicCreateTicket(w http.ResponseWriter, r *http.Request) {
|
||||
projectID := r.PathValue("id")
|
||||
if err := validateUUID(projectID); err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
if s.publicTicketLimiter != nil {
|
||||
if allowed, retry := s.publicTicketLimiter.allow("ip:" + publicQueryClientKey(r)); !allowed {
|
||||
writePublicTicketRateLimit(w, retry)
|
||||
return
|
||||
}
|
||||
}
|
||||
var actor model.User
|
||||
if err := s.db.WithContext(r.Context()).Where("username = ?", model.PublicVisitorUsername).First(&actor).Error; err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
s.createTicketForActor(w, r, actor.ID, true)
|
||||
}
|
||||
|
||||
type publicPhoneQueryRequest struct {
|
||||
Phone string `json:"phone"`
|
||||
}
|
||||
@@ -114,6 +153,15 @@ func writePublicQueryRateLimit(w http.ResponseWriter, retry time.Duration) {
|
||||
writeError(w, &apiError{Status: http.StatusTooManyRequests, Code: "PUBLIC_QUERY_RATE_LIMITED", Message: "查询次数过多,请稍后再试"})
|
||||
}
|
||||
|
||||
func writePublicTicketRateLimit(w http.ResponseWriter, retry time.Duration) {
|
||||
seconds := int((retry + time.Second - 1) / time.Second)
|
||||
if seconds < 1 {
|
||||
seconds = 1
|
||||
}
|
||||
w.Header().Set("Retry-After", fmt.Sprintf("%d", seconds))
|
||||
writeError(w, &apiError{Status: http.StatusTooManyRequests, Code: "PUBLIC_TICKET_RATE_LIMITED", Message: "取号次数过多,请稍后再试"})
|
||||
}
|
||||
|
||||
func (s *Server) publicStatusView(ctx context.Context, ticket model.QueueTicket) (map[string]any, error) {
|
||||
var project model.Project
|
||||
var session model.QueueSession
|
||||
@@ -123,19 +171,26 @@ func (s *Server) publicStatusView(ctx context.Context, ticket model.QueueTicket)
|
||||
if err := s.db.WithContext(ctx).First(&session, "id = ? AND project_id = ?", ticket.QueueSessionID, ticket.ProjectID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
experiencedPeople, err := s.displayedExperiencedPeople(ctx, project, &session)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
phoneSuffix, err := s.ticketPhoneLast4(ticket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ticketsAhead := 0
|
||||
peopleAhead := 0
|
||||
if ticket.Status == model.TicketWaiting {
|
||||
var count int64
|
||||
var totals ticketPeopleTotals
|
||||
if err := s.db.WithContext(ctx).Model(&model.QueueTicket{}).
|
||||
Select("count(*) AS ticket_count, COALESCE(sum(party_size), 0) AS people_count").
|
||||
Where("project_id = ? AND queue_session_id = ? AND status = ? AND ticket_number < ?",
|
||||
ticket.ProjectID, ticket.QueueSessionID, model.TicketWaiting, ticket.TicketNumber).Count(&count).Error; err != nil {
|
||||
ticket.ProjectID, ticket.QueueSessionID, model.TicketWaiting, ticket.TicketNumber).Scan(&totals).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
peopleAhead = int(count)
|
||||
ticketsAhead = int(totals.TicketCount)
|
||||
peopleAhead = int(totals.PeopleCount)
|
||||
}
|
||||
var latestCalledTicket struct {
|
||||
DisplayNumber string `gorm:"column:display_number"`
|
||||
@@ -150,7 +205,7 @@ func (s *Server) publicStatusView(ctx context.Context, ticket model.QueueTicket)
|
||||
latestCalledNumber = latestCalledTicket.DisplayNumber
|
||||
}
|
||||
eta, err := domain.CalculateETA(domain.ETAInput{
|
||||
PeopleAhead: peopleAhead, IntervalPerNumber: time.Duration(project.ETAIntervalSeconds) * time.Second,
|
||||
PeopleAhead: peopleAhead, IntervalPerPerson: time.Duration(project.ETAIntervalSeconds) * time.Second,
|
||||
Running: project.Status == model.ProjectRunning && session.Status == "RUNNING" && ticket.Status == model.TicketWaiting,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -161,25 +216,26 @@ func (s *Server) publicStatusView(ctx context.Context, ticket model.QueueTicket)
|
||||
}
|
||||
return map[string]any{
|
||||
"ticket_number": ticket.DisplayNumber, "display_number": ticket.DisplayNumber,
|
||||
"project_name": project.Name, "status": ticket.Status, "phone_last4": phoneSuffix, "estimated_wait": eta,
|
||||
"visitor_notice": project.VisitorNotice,
|
||||
"last_updated_at": s.now(), "called_at": ticket.CalledAt,
|
||||
"project_name": project.Name, "status": ticket.Status, "party_size": ticket.PartySize, "phone_last4": phoneSuffix, "estimated_wait": eta,
|
||||
"visitor_notice": project.VisitorNotice,
|
||||
"experienced_people": experiencedPeople,
|
||||
"last_updated_at": s.now(), "called_at": ticket.CalledAt,
|
||||
"ticket": map[string]any{
|
||||
"display_number": ticket.DisplayNumber, "status": ticket.Status, "joined_at": ticket.JoinedAt,
|
||||
"display_number": ticket.DisplayNumber, "status": ticket.Status, "party_size": ticket.PartySize, "joined_at": ticket.JoinedAt,
|
||||
"called_at": ticket.CalledAt, "arrived_at": ticket.ArrivedAt, "completed_at": ticket.CompletedAt, "missed_at": ticket.MissedAt,
|
||||
},
|
||||
"project": map[string]any{"id": project.ID, "name": project.Name, "status": project.Status},
|
||||
"people_ahead": peopleAhead, "queue_position": queuePosition(ticket.Status, peopleAhead),
|
||||
"project": map[string]any{"id": project.ID, "name": project.Name, "status": project.Status},
|
||||
"tickets_ahead": ticketsAhead, "people_ahead": peopleAhead, "queue_position": queuePosition(ticket.Status, ticketsAhead),
|
||||
"latest_called_number": latestCalledNumber,
|
||||
"eta": eta, "revision": session.Revision, "server_time": s.now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func queuePosition(status string, peopleAhead int) any {
|
||||
func queuePosition(status string, ticketsAhead int) any {
|
||||
if status != model.TicketWaiting {
|
||||
return nil
|
||||
}
|
||||
return peopleAhead + 1
|
||||
return ticketsAhead + 1
|
||||
}
|
||||
|
||||
// displayTicketDTO is the complete public ticket shape for a display. Keeping
|
||||
@@ -189,14 +245,19 @@ type displayTicketDTO struct {
|
||||
TicketNumber string `json:"ticket_number"`
|
||||
DisplayNumber string `json:"display_number"`
|
||||
Status string `json:"status"`
|
||||
PartySize int `json:"party_size"`
|
||||
}
|
||||
|
||||
type displayBatchDTO struct {
|
||||
BatchNumber int `json:"batch_number"`
|
||||
Sequence int `json:"sequence"`
|
||||
Status string `json:"status"`
|
||||
CalledAt time.Time `json:"called_at"`
|
||||
Tickets []displayTicketDTO `json:"tickets"`
|
||||
BatchNumber int `json:"batch_number"`
|
||||
Sequence int `json:"sequence"`
|
||||
Status string `json:"status"`
|
||||
CallMode string `json:"call_mode"`
|
||||
RequestedCount int `json:"requested_count"`
|
||||
TicketCount int `json:"ticket_count"`
|
||||
PeopleCount int `json:"people_count"`
|
||||
CalledAt time.Time `json:"called_at"`
|
||||
Tickets []displayTicketDTO `json:"tickets"`
|
||||
}
|
||||
|
||||
type displayProjectDTO struct {
|
||||
@@ -206,16 +267,19 @@ type displayProjectDTO struct {
|
||||
}
|
||||
|
||||
type displaySnapshotDTO struct {
|
||||
ProjectName string `json:"project_name"`
|
||||
Status string `json:"status"`
|
||||
Project displayProjectDTO `json:"project"`
|
||||
Revision int64 `json:"revision"`
|
||||
WaitingCount int64 `json:"waiting_count"`
|
||||
CurrentBatch *displayBatchDTO `json:"current_batch"`
|
||||
RecentBatches []displayBatchDTO `json:"recent_batches"`
|
||||
EstimatedWait domain.ETAResult `json:"estimated_wait"`
|
||||
ServerTime time.Time `json:"server_time"`
|
||||
LastUpdatedAt time.Time `json:"last_updated_at"`
|
||||
ProjectName string `json:"project_name"`
|
||||
Status string `json:"status"`
|
||||
Project displayProjectDTO `json:"project"`
|
||||
Revision int64 `json:"revision"`
|
||||
WaitingCount int64 `json:"waiting_count"`
|
||||
WaitingTicketCount int64 `json:"waiting_ticket_count"`
|
||||
WaitingPeopleCount int64 `json:"waiting_people_count"`
|
||||
ExperiencedPeople int64 `json:"experienced_people"`
|
||||
CurrentBatch *displayBatchDTO `json:"current_batch"`
|
||||
RecentBatches []displayBatchDTO `json:"recent_batches"`
|
||||
EstimatedWait domain.ETAResult `json:"estimated_wait"`
|
||||
ServerTime time.Time `json:"server_time"`
|
||||
LastUpdatedAt time.Time `json:"last_updated_at"`
|
||||
}
|
||||
|
||||
func (s *Server) displaySnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -234,10 +298,15 @@ func (s *Server) displaySnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
err = s.db.WithContext(r.Context()).Where("project_id = ? AND status IN ?", project.ID, []string{"RUNNING", "PAUSED"}).
|
||||
Order("business_date DESC").First(&session).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
experiencedPeople, metricErr := s.displayedExperiencedPeople(r.Context(), project, nil)
|
||||
if metricErr != nil {
|
||||
writeError(w, metricErr)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, displaySnapshotDTO{
|
||||
ProjectName: project.Name, Status: project.Status,
|
||||
Project: displayProjectDTO{ID: project.ID, Name: project.Name, Status: project.Status},
|
||||
RecentBatches: []displayBatchDTO{}, EstimatedWait: domain.ETAResult{Available: false, Reason: "queue_not_running"},
|
||||
Project: displayProjectDTO{ID: project.ID, Name: project.Name, Status: project.Status},
|
||||
ExperiencedPeople: experiencedPeople, RecentBatches: []displayBatchDTO{}, EstimatedWait: domain.ETAResult{Available: false, Reason: "queue_not_running"},
|
||||
ServerTime: s.now(), LastUpdatedAt: project.UpdatedAt,
|
||||
})
|
||||
return
|
||||
@@ -246,10 +315,8 @@ func (s *Server) displaySnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
var waitingCount int64
|
||||
if err := s.db.WithContext(r.Context()).Model(&model.QueueTicket{}).
|
||||
Where("project_id = ? AND queue_session_id = ? AND status = ?", project.ID, session.ID, model.TicketWaiting).
|
||||
Count(&waitingCount).Error; err != nil {
|
||||
waiting, err := queueTotals(s.db.WithContext(r.Context()), project.ID, session.ID, model.TicketWaiting)
|
||||
if err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -263,15 +330,31 @@ func (s *Server) displaySnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
peopleAhead := max(0, int(waitingCount)-1)
|
||||
var lastWaiting model.QueueTicket
|
||||
lastWaitingErr := s.db.WithContext(r.Context()).Select("id", "party_size").
|
||||
Where("project_id = ? AND queue_session_id = ? AND status = ?", project.ID, session.ID, model.TicketWaiting).
|
||||
Order("ticket_number DESC").First(&lastWaiting).Error
|
||||
if lastWaitingErr != nil && !errors.Is(lastWaitingErr, gorm.ErrRecordNotFound) {
|
||||
writeError(w, lastWaitingErr)
|
||||
return
|
||||
}
|
||||
peopleAhead := int(waiting.PeopleCount)
|
||||
if lastWaitingErr == nil {
|
||||
peopleAhead = max(0, peopleAhead-lastWaiting.PartySize)
|
||||
}
|
||||
estimatedWait, err := domain.CalculateETA(domain.ETAInput{
|
||||
PeopleAhead: peopleAhead, IntervalPerNumber: time.Duration(project.ETAIntervalSeconds) * time.Second,
|
||||
Running: project.Status == model.ProjectRunning && session.Status == "RUNNING" && waitingCount > 0,
|
||||
PeopleAhead: peopleAhead, IntervalPerPerson: time.Duration(project.ETAIntervalSeconds) * time.Second,
|
||||
Running: project.Status == model.ProjectRunning && session.Status == "RUNNING" && waiting.TicketCount > 0,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
experiencedPeople, err := s.displayedExperiencedPeople(r.Context(), project, &session)
|
||||
if err != nil {
|
||||
writeError(w, err)
|
||||
return
|
||||
}
|
||||
lastUpdated := project.UpdatedAt
|
||||
if session.UpdatedAt.After(lastUpdated) {
|
||||
lastUpdated = session.UpdatedAt
|
||||
@@ -279,7 +362,9 @@ func (s *Server) displaySnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, displaySnapshotDTO{
|
||||
ProjectName: project.Name, Status: project.Status,
|
||||
Project: displayProjectDTO{ID: project.ID, Name: project.Name, Status: project.Status},
|
||||
Revision: session.Revision, WaitingCount: waitingCount, CurrentBatch: batch, RecentBatches: recentBatches,
|
||||
Revision: session.Revision, WaitingCount: waiting.TicketCount,
|
||||
WaitingTicketCount: waiting.TicketCount, WaitingPeopleCount: waiting.PeopleCount,
|
||||
ExperiencedPeople: experiencedPeople, CurrentBatch: batch, RecentBatches: recentBatches,
|
||||
EstimatedWait: estimatedWait, ServerTime: s.now(), LastUpdatedAt: lastUpdated,
|
||||
})
|
||||
}
|
||||
@@ -321,7 +406,7 @@ func (s *Server) recentDisplayBatches(r *http.Request, projectID, sessionID stri
|
||||
|
||||
func displayBatchTickets(db *gorm.DB, batchID, projectID string, calledOnly bool) ([]displayTicketDTO, error) {
|
||||
query := db.Table("queue_tickets").
|
||||
Select("queue_tickets.display_number, queue_tickets.status").
|
||||
Select("queue_tickets.display_number, queue_tickets.status, queue_tickets.party_size").
|
||||
Joins("JOIN call_batch_tickets cbt ON cbt.ticket_id = queue_tickets.id AND cbt.project_id = queue_tickets.project_id").
|
||||
Where("cbt.call_batch_id = ? AND cbt.project_id = ?", batchID, projectID).
|
||||
Order("cbt.position ASC")
|
||||
@@ -331,6 +416,7 @@ func displayBatchTickets(db *gorm.DB, batchID, projectID string, calledOnly bool
|
||||
type row struct {
|
||||
DisplayNumber string
|
||||
Status string
|
||||
PartySize int
|
||||
}
|
||||
var rows []row
|
||||
if err := query.Scan(&rows).Error; err != nil {
|
||||
@@ -339,7 +425,7 @@ func displayBatchTickets(db *gorm.DB, batchID, projectID string, calledOnly bool
|
||||
tickets := make([]displayTicketDTO, 0, len(rows))
|
||||
for _, item := range rows {
|
||||
tickets = append(tickets, displayTicketDTO{
|
||||
TicketNumber: item.DisplayNumber, DisplayNumber: item.DisplayNumber, Status: item.Status,
|
||||
TicketNumber: item.DisplayNumber, DisplayNumber: item.DisplayNumber, Status: item.Status, PartySize: item.PartySize,
|
||||
})
|
||||
}
|
||||
return tickets, nil
|
||||
@@ -348,6 +434,8 @@ func displayBatchTickets(db *gorm.DB, batchID, projectID string, calledOnly bool
|
||||
func newDisplayBatchDTO(batch model.CallBatch, tickets []displayTicketDTO) displayBatchDTO {
|
||||
return displayBatchDTO{
|
||||
BatchNumber: batch.BatchSequence, Sequence: batch.BatchSequence, Status: batch.Status,
|
||||
CallMode: batch.CallMode, RequestedCount: batch.RequestedCount,
|
||||
TicketCount: batch.TicketCount, PeopleCount: batch.PeopleCount,
|
||||
CalledAt: batch.CalledAt, Tickets: tickets,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user