109 lines
2.5 KiB
Go
109 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
|
|
"task/internal/sqlite"
|
|
"task/internal/ticket"
|
|
)
|
|
|
|
func (s *Server) handleProjectReport(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if _, ok, code := s.requireProjectRole(id, sqlite.RoleViewer, r); !ok {
|
|
writeError(w, code, "forbidden")
|
|
return
|
|
}
|
|
groupBy := ticket.GroupBy(r.URL.Query().Get("groupBy"))
|
|
if groupBy == "" {
|
|
groupBy = ticket.GroupByTicket
|
|
}
|
|
valid := false
|
|
for _, g := range ticket.AllGroupBys {
|
|
if g == groupBy {
|
|
valid = true
|
|
break
|
|
}
|
|
}
|
|
if !valid {
|
|
writeError(w, http.StatusBadRequest, "invalid groupBy")
|
|
return
|
|
}
|
|
|
|
from, err := ticket.ParseReportDay(r.URL.Query().Get("from"))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
toDay, err := ticket.ParseReportDay(r.URL.Query().Get("to"))
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
var toBound = toDay
|
|
if !toDay.IsZero() {
|
|
toBound = ticket.EndOfReportDay(toDay)
|
|
}
|
|
filter := ticket.TimeFilter{
|
|
From: from,
|
|
To: toBound,
|
|
TicketID: r.URL.Query().Get("ticketId"),
|
|
}
|
|
|
|
entries, err := s.Times.ListByProject(id)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
entries = ticket.FilterEntries(entries, filter)
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
ai, aj := entries[i].Start, entries[j].Start
|
|
if ai.IsZero() {
|
|
ai = entries[i].End
|
|
}
|
|
if aj.IsZero() {
|
|
aj = entries[j].End
|
|
}
|
|
if !ai.Equal(aj) {
|
|
return ai.After(aj)
|
|
}
|
|
return entries[i].ID > entries[j].ID
|
|
})
|
|
|
|
tickets, err := s.Tickets.List(id)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
byID := make(map[string]ticket.Ticket, len(tickets))
|
|
for _, t := range tickets {
|
|
byID[t.ID] = t
|
|
}
|
|
rows := ticket.Aggregate(entries, byID, groupBy)
|
|
total := ticket.TotalDuration(entries)
|
|
|
|
type rowJSON struct {
|
|
Key string `json:"key"`
|
|
Label string `json:"label"`
|
|
DurationNs int64 `json:"durationNs"`
|
|
}
|
|
out := make([]rowJSON, 0, len(rows))
|
|
for _, row := range rows {
|
|
out = append(out, rowJSON{
|
|
Key: row.Key,
|
|
Label: row.Label,
|
|
DurationNs: int64(row.Duration),
|
|
})
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"groupBy": groupBy,
|
|
"totalDurationNs": int64(total),
|
|
"rows": out,
|
|
"entryCount": len(entries),
|
|
"entries": entries,
|
|
"from": r.URL.Query().Get("from"),
|
|
"to": r.URL.Query().Get("to"),
|
|
"ticketId": filter.TicketID,
|
|
})
|
|
}
|