feat(reports): align inbox label matrix filters

This commit is contained in:
2026-06-07 01:32:57 +08:00
parent 1232e974ac
commit f3fda02a9a
6 changed files with 166 additions and 15 deletions
+50 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/csv"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
@@ -498,8 +499,12 @@ func (h *AnalyticsHandler) InboxLabelMatrix(c *gin.Context) {
if !ok {
return
}
filter, ok := parseInboxLabelMatrixFilter(c)
if !ok {
return
}
result, err := h.svc.GetInboxLabelMatrix(c.Request.Context(), accountID)
result, err := h.svc.GetInboxLabelMatrix(c.Request.Context(), accountID, filter)
if err != nil {
applogger.L().Errorf("Inbox label matrix report: %v", err)
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to generate inbox label matrix")
@@ -509,6 +514,50 @@ func (h *AnalyticsHandler) InboxLabelMatrix(c *gin.Context) {
c.JSON(http.StatusOK, result)
}
func parseInboxLabelMatrixFilter(c *gin.Context) (service.InboxLabelMatrixFilter, bool) {
filter := service.InboxLabelMatrixFilter{
InboxIDs: parseReportUintList(c, "inbox_ids"),
LabelIDs: parseReportUintList(c, "label_ids"),
}
sinceRaw := c.Query("since")
untilRaw := c.Query("until")
if sinceRaw == "" || untilRaw == "" {
return filter, true
}
since, err := parseChatwootReportTime(sinceRaw)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid since date format")
return service.InboxLabelMatrixFilter{}, false
}
until, err := parseChatwootReportTime(untilRaw)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid until date format")
return service.InboxLabelMatrixFilter{}, false
}
filter.Since = since
filter.Until = until
return filter, true
}
func parseReportUintList(c *gin.Context, key string) []uint {
values := append([]string{}, c.QueryArray(key)...)
values = append(values, c.QueryArray(key+"[]")...)
ids := make([]uint, 0, len(values))
for _, value := range values {
for _, part := range strings.Split(value, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
parsed, err := strconv.ParseUint(part, 10, 64)
if err == nil && parsed > 0 {
ids = append(ids, uint(parsed))
}
}
}
return ids
}
// FirstResponseTimeDistribution returns first response time distribution.
// GET /api/v1/accounts/:account_id/reports/first_response_time_distribution
// Reference: Chatwoot reports#first_response_time_distribution