Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(httpapi): extract businessReportFilter from business domain report (#613) (#616)
fx-kurumib0ceea2d49d8
diff --git a/internal/httpapi/business_domains.go b/internal/httpapi/business_domains.goindex e4e31db8..412b7623 100644--- a/internal/httpapi/business_domains.go+++ b/internal/httpapi/business_domains.go@@ -9,6 +9,7 @@ import ("github.com/gin-gonic/gin""gorm.io/gorm""net/http"+ "net/url""reflect""regexp""slices"@@ -147,55 +148,63 @@ func (h *businessDomainsHandlers) report(c *gin.Context) {return}}- filter := domain.AnalyticsFilter{ProjectIDs: []string{}, Grain: strings.TrimSpace(c.Query("grain"))}- filter.TimeZone = strings.TrimSpace(c.Query("timeZone"))+ filter, err := businessReportFilter(c.Request.URL.Query())+ if err != nil {+ c.JSON(400, gin.H{"error": err.Error()})+ return+ }+ projects := h.repo.Projects()+ for _, p := range projects {+ selected := requested == "all" || requested == p.BusinessDomainID || (requested == "unclassified" && p.BusinessDomainID == "")+ allowed := admin || visible[p.BusinessDomainID] || (p.BusinessDomainID == "" && uid > 0 && p.OwnerID == uid)+ if selected && allowed {+ filter.ProjectIDs = append(filter.ProjectIDs, p.ID)+ }+ }+ result, err := h.cachedBusinessReport(businessReportCacheKey(requested, filter, projects, domains), func() (domain.BusinessDomainReport, error) {+ return h.repo.BusinessDomainReport(filter, projects, domains)+ })+ if err != nil {+ respond(c, err)+ return+ }+ c.JSON(200, result)+}++// businessReportFilter 解析业务域汇总的查询参数:时区默认 UTC 且须为 IANA 时区,+// 粒度默认 day 且只能是 day/week/month,from/to 为可选的 RFC3339 时间且不能倒序。+// 返回的 ProjectIDs 为空切片,由调用方按可见范围填充。+func businessReportFilter(query url.Values) (domain.AnalyticsFilter, error) {+ filter := domain.AnalyticsFilter{ProjectIDs: []string{}, Grain: strings.TrimSpace(query.Get("grain"))}+ filter.TimeZone = strings.TrimSpace(query.Get("timeZone"))if filter.TimeZone == "" {filter.TimeZone = "UTC"}if _, err := time.LoadLocation(filter.TimeZone); err != nil || filter.TimeZone == "Local" {- c.JSON(400, gin.H{"error": "timeZone 须为有效的 IANA 时区"})- return+ return filter, errors.New("timeZone 须为有效的 IANA 时区")}if filter.Grain == "" {filter.Grain = "day"}if filter.Grain != "day" && filter.Grain != "week" && filter.Grain != "month" {- c.JSON(400, gin.H{"error": "grain 必须为 day、week 或 month"})- return+ return filter, errors.New("grain 必须为 day、week 或 month")}for _, target := range []struct {key stringvalue *time.Time}{{"from", &filter.From}, {"to", &filter.To}} {- if raw := c.Query(target.key); raw != "" {+ if raw := query.Get(target.key); raw != "" {at, err := time.Parse(time.RFC3339, raw)if err != nil {- c.JSON(400, gin.H{"error": "时间须使用 RFC3339 格式"})- return+ return filter, errors.New("时间须使用 RFC3339 格式")}*target.value = at}}if !filter.From.IsZero() && !filter.To.IsZero() && filter.From.After(filter.To) {- c.JSON(400, gin.H{"error": "开始时间不能晚于结束时间"})- return- }- projects := h.repo.Projects()- for _, p := range projects {- selected := requested == "all" || requested == p.BusinessDomainID || (requested == "unclassified" && p.BusinessDomainID == "")- allowed := admin || visible[p.BusinessDomainID] || (p.BusinessDomainID == "" && uid > 0 && p.OwnerID == uid)- if selected && allowed {- filter.ProjectIDs = append(filter.ProjectIDs, p.ID)- }- }- result, err := h.cachedBusinessReport(businessReportCacheKey(requested, filter, projects, domains), func() (domain.BusinessDomainReport, error) {- return h.repo.BusinessDomainReport(filter, projects, domains)- })- if err != nil {- respond(c, err)- return+ return filter, errors.New("开始时间不能晚于结束时间")}- c.JSON(200, result)+ return filter, nil}// businessReportCacheTTL 只用于合并同一瞬间的重复打开:报表本身仍由同一个diff --git a/internal/httpapi/business_report_filter_test.go b/internal/httpapi/business_report_filter_test.gonew file mode 100644index 00000000..2592f783--- /dev/null+++ b/internal/httpapi/business_report_filter_test.go@@ -0,0 +1,75 @@+package httpapi++import (+ "net/url"+ "testing"+ "time"+)++func TestBusinessReportFilterDefaults(t *testing.T) {+ filter, err := businessReportFilter(url.Values{})+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if filter.TimeZone != "UTC" || filter.Grain != "day" {+ t.Fatalf("defaults = %q/%q, want UTC/day", filter.TimeZone, filter.Grain)+ }+ if filter.ProjectIDs == nil || len(filter.ProjectIDs) != 0 {+ t.Fatalf("ProjectIDs = %#v, want empty non-nil slice", filter.ProjectIDs)+ }+ if !filter.From.IsZero() || !filter.To.IsZero() {+ t.Fatalf("from/to should stay zero, got %v/%v", filter.From, filter.To)+ }+}++func TestBusinessReportFilterParsesValues(t *testing.T) {+ filter, err := businessReportFilter(url.Values{+ "timeZone": {" Asia/Shanghai "},+ "grain": {" week "},+ "from": {"2026-01-01T00:00:00Z"},+ "to": {"2026-01-31T00:00:00+08:00"},+ })+ if err != nil {+ t.Fatalf("unexpected error: %v", err)+ }+ if filter.TimeZone != "Asia/Shanghai" || filter.Grain != "week" {+ t.Fatalf("got %q/%q, want Asia/Shanghai/week", filter.TimeZone, filter.Grain)+ }+ if want := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC); !filter.From.Equal(want) {+ t.Fatalf("from = %v, want %v", filter.From, want)+ }+ if want := time.Date(2026, 1, 30, 16, 0, 0, 0, time.UTC); !filter.To.Equal(want) {+ t.Fatalf("to = %v, want %v", filter.To, want)+ }+}++func TestBusinessReportFilterSameFromTo(t *testing.T) {+ at := "2026-03-01T00:00:00Z"+ if _, err := businessReportFilter(url.Values{"from": {at}, "to": {at}, "grain": {"month"}}); err != nil {+ t.Fatalf("equal from/to should be accepted: %v", err)+ }+}++func TestBusinessReportFilterRejectsInvalidInput(t *testing.T) {+ cases := []struct {+ name string+ query url.Values+ want string+ }{+ {"unknown time zone", url.Values{"timeZone": {"invalid"}}, "timeZone 须为有效的 IANA 时区"},+ {"local time zone", url.Values{"timeZone": {"Local"}}, "timeZone 须为有效的 IANA 时区"},+ {"time zone checked before grain", url.Values{"timeZone": {"invalid"}, "grain": {"year"}}, "timeZone 须为有效的 IANA 时区"},+ {"unknown grain", url.Values{"grain": {"year"}}, "grain 必须为 day、week 或 month"},+ {"bad from", url.Values{"from": {"2026-01-01"}}, "时间须使用 RFC3339 格式"},+ {"bad to", url.Values{"to": {"yesterday"}}, "时间须使用 RFC3339 格式"},+ {"from after to", url.Values{"from": {"2026-02-01T00:00:00Z"}, "to": {"2026-01-01T00:00:00Z"}}, "开始时间不能晚于结束时间"},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ _, err := businessReportFilter(tc.query)+ if err == nil || err.Error() != tc.want {+ t.Fatalf("err = %v, want %q", err, tc.want)+ }+ })+ }+}