2026-01-26 16:04:42 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-09 06:04:31 +02:00
|
|
|
"fmt"
|
2026-01-26 16:04:42 +01:00
|
|
|
"net/http"
|
2026-10-03 06:46:20 +00:00
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2026-01-26 16:04:42 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/warmbly/warmbly/internal/api/middleware"
|
2026-10-03 06:46:20 +00:00
|
|
|
"github.com/warmbly/warmbly/internal/config"
|
2026-01-26 16:04:42 +01:00
|
|
|
"github.com/warmbly/warmbly/internal/errx"
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
"github.com/warmbly/warmbly/internal/models"
|
2026-01-26 16:04:42 +01:00
|
|
|
)
|
|
|
|
|
|
2026-09-16 21:32:15 -07:00
|
|
|
// GetWarmupAnalytics gets warmup statistics for the selected organization.
|
2026-01-26 16:04:42 +01:00
|
|
|
// GET /analytics/warmup
|
|
|
|
|
func (h *Handler) GetWarmupAnalytics(c *gin.Context) {
|
2026-09-16 21:32:15 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse optional email_id filter
|
|
|
|
|
var emailAccountID *uuid.UUID
|
|
|
|
|
if emailIDStr := c.Query("email_id"); emailIDStr != "" {
|
|
|
|
|
if id, err := uuid.Parse(emailIDStr); err == nil {
|
|
|
|
|
emailAccountID = &id
|
2026-10-07 06:11:08 +00:00
|
|
|
} else {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "email_id must be a UUID"))
|
|
|
|
|
return
|
2026-01-26 16:04:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse date range (required)
|
|
|
|
|
fromStr := c.Query("from")
|
|
|
|
|
toStr := c.Query("to")
|
|
|
|
|
|
|
|
|
|
if fromStr == "" || toStr == "" {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "from and to date parameters are required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
from, err := time.Parse("2006-01-02", fromStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid from date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
to, err := time.Parse("2006-01-02", toStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid to date format (expected YYYY-MM-DD)"))
|
2026-10-07 06:11:08 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if to.Before(from) {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "from must be on or before to"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 21:32:15 -07:00
|
|
|
analytics, xerr := h.AnalyticsService.GetWarmupAnalytics(c.Request.Context(), *orgID, emailAccountID, from, to)
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, analytics)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 02:19:46 -07:00
|
|
|
// GetWarmupPlacement reports where warmup mail landed (inbox, category tabs,
|
|
|
|
|
// spam) per day and per recipient provider, for one mailbox or the workspace.
|
|
|
|
|
// GET /analytics/warmup/placement
|
|
|
|
|
func (h *Handler) GetWarmupPlacement(c *gin.Context) {
|
|
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var emailAccountID *uuid.UUID
|
|
|
|
|
if raw := c.Query("email_id"); raw != "" {
|
|
|
|
|
id, err := uuid.Parse(raw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "email_id must be a UUID"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
emailAccountID = &id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
to := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
|
2026-09-24 05:28:50 -07:00
|
|
|
if raw := c.Query("to"); raw != "" {
|
2026-09-24 02:19:46 -07:00
|
|
|
d, err := time.Parse("2006-01-02", raw)
|
|
|
|
|
if err != nil {
|
2026-09-24 05:28:50 -07:00
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid to date format (expected YYYY-MM-DD)"))
|
2026-09-24 02:19:46 -07:00
|
|
|
return
|
|
|
|
|
}
|
2026-09-24 05:28:50 -07:00
|
|
|
to = d
|
2026-09-24 02:19:46 -07:00
|
|
|
}
|
2026-09-24 05:28:50 -07:00
|
|
|
// The default window is the 30 days ending on to, whichever to is.
|
|
|
|
|
from := to.AddDate(0, 0, -29)
|
|
|
|
|
if raw := c.Query("from"); raw != "" {
|
2026-09-24 02:19:46 -07:00
|
|
|
d, err := time.Parse("2006-01-02", raw)
|
|
|
|
|
if err != nil {
|
2026-09-24 05:28:50 -07:00
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid from date format (expected YYYY-MM-DD)"))
|
2026-09-24 02:19:46 -07:00
|
|
|
return
|
|
|
|
|
}
|
2026-09-24 05:28:50 -07:00
|
|
|
from = d
|
2026-09-24 02:19:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
report, xerr := h.AnalyticsService.GetWarmupPlacement(c.Request.Context(), *orgID, emailAccountID, from, to)
|
|
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, report)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 16:04:42 +01:00
|
|
|
// GetCampaignAnalytics gets analytics for a specific campaign
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
// GET /analytics/campaigns/:id[?from=YYYY-MM-DD&to=YYYY-MM-DD]
|
2026-01-26 16:04:42 +01:00
|
|
|
func (h *Handler) GetCampaignAnalytics(c *gin.Context) {
|
2026-09-16 03:31:01 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
campaignIDStr := c.Param("id")
|
|
|
|
|
campaignID, err := uuid.Parse(campaignIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
period, xerr := optionalDayRange(c)
|
|
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
analytics, xerr := h.AnalyticsService.GetCampaignAnalytics(c.Request.Context(), *orgID, campaignID, period)
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, analytics)
|
|
|
|
|
}
|
|
|
|
|
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
// optionalDayRange reads from and to as whole days, both or neither; neither
|
|
|
|
|
// is nil, which callers read as all time.
|
|
|
|
|
func optionalDayRange(c *gin.Context) (*models.DateRange, *errx.Error) {
|
2026-09-26 23:00:30 -07:00
|
|
|
period, err := models.ParseDayRange(c.Query("from"), c.Query("to"))
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
if err != nil {
|
2026-09-26 23:00:30 -07:00
|
|
|
return nil, errx.New(errx.BadRequest, err.Error())
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
}
|
2026-09-26 23:00:30 -07:00
|
|
|
return period, nil
|
feat: scope campaign analytics to an optional from/to send cohort (summary, step performance, engagement and the daily chart read the same UTC days, total_contacts and emails_pending stay campaign-wide, date_range reports the resolved period), count the whole last day in campaign compare and daily stats, add a 7d/30d/90d/all-time/custom period picker to the campaign overview and its share image, take the period in get_campaign_stats, the warmbly and warmblyctl CLIs and the Make and Zapier modules, close a date picker's calendar alone on Escape, and document it in the analytics guide, API reference, MCP table and OpenAPI
2026-09-26 22:42:19 -07:00
|
|
|
}
|
|
|
|
|
|
2026-01-26 16:04:42 +01:00
|
|
|
// GetCampaignDailyStats gets daily statistics for a campaign
|
|
|
|
|
// GET /analytics/campaigns/:id/daily
|
|
|
|
|
func (h *Handler) GetCampaignDailyStats(c *gin.Context) {
|
2026-09-16 03:31:01 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
campaignIDStr := c.Param("id")
|
|
|
|
|
campaignID, err := uuid.Parse(campaignIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse date range
|
|
|
|
|
fromStr := c.Query("from")
|
|
|
|
|
toStr := c.Query("to")
|
|
|
|
|
|
|
|
|
|
if fromStr == "" || toStr == "" {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "from and to date parameters are required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
from, err := time.Parse("2006-01-02", fromStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid from date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
to, err := time.Parse("2006-01-02", toStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid to date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 03:31:01 -07:00
|
|
|
stats, xerr := h.AnalyticsService.GetCampaignDailyStats(c.Request.Context(), *orgID, campaignID, from, to)
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": stats})
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 06:46:20 +00:00
|
|
|
// GetAllAccountStatuses lists email account statuses, one bounded page at a
|
|
|
|
|
// time. A page view asks only for the mailbox ids it shows (email_ids);
|
|
|
|
|
// otherwise the opaque cursor walks the whole inventory. The per-mailbox reads
|
|
|
|
|
// are batched across the page, so a page's cost does not grow with the total
|
|
|
|
|
// inventory, and the overflow beyond the old 1000-row cap is reachable through
|
|
|
|
|
// next_cursor instead of being silently dropped.
|
2026-01-26 16:04:42 +01:00
|
|
|
// GET /analytics/accounts
|
|
|
|
|
func (h *Handler) GetAllAccountStatuses(c *gin.Context) {
|
2026-07-21 17:34:59 +02:00
|
|
|
// Account lookups are org-scoped (emailRepo.Search filters on
|
|
|
|
|
// organization_id), so pass the selected org, not the user id.
|
|
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 06:46:20 +00:00
|
|
|
// email_ids scopes the page to a visible set; invalid or over-limit is a
|
|
|
|
|
// 400, never a silently truncated or ignored filter.
|
|
|
|
|
var emailIDs []uuid.UUID
|
|
|
|
|
if raw := strings.TrimSpace(c.Query("email_ids")); raw != "" {
|
|
|
|
|
parts := strings.Split(raw, ",")
|
|
|
|
|
if len(parts) > config.AccountStatusMaxIDs {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "too many email_ids"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
emailIDs = make([]uuid.UUID, 0, len(parts))
|
|
|
|
|
for _, p := range parts {
|
|
|
|
|
id, err := uuid.Parse(strings.TrimSpace(p))
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid email_ids"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
emailIDs = append(emailIDs, id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
limit := config.AccountStatusLimitDefault
|
|
|
|
|
if raw := c.Query("limit"); raw != "" {
|
|
|
|
|
n, err := strconv.Atoi(raw)
|
|
|
|
|
if err != nil || n < 1 || n > config.AccountStatusLimitMax {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "invalid limit"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
limit = n
|
|
|
|
|
}
|
|
|
|
|
|
feat: hold an API key with allowed_email_accounts to its mailboxes across the unibox (list, thread, message, count and overview reads; compose with auto pick within the list, drafts, agent-draft approve and discard, seen, folder, labels and snooze writes), campaign sender pools, analytics account statuses and GET /analytics/accounts/:id, and store a compose draft's mailbox only when it belongs to the caller's organization
2026-10-04 03:14:32 -07:00
|
|
|
emailIDs, scopeErr := allowedMailboxFilter(c, emailIDs)
|
|
|
|
|
if scopeErr != nil {
|
|
|
|
|
errx.JSON(c, scopeErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 06:46:20 +00:00
|
|
|
result, xerr := h.AnalyticsService.GetAccountStatusesPage(c.Request.Context(), *orgID, emailIDs, c.Query("cursor"), int32(limit))
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 06:46:20 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"data": result.Data, "pagination": result.Pagination})
|
2026-01-26 16:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAccountStatus gets status of a specific email account
|
|
|
|
|
// GET /analytics/accounts/:id
|
|
|
|
|
func (h *Handler) GetAccountStatus(c *gin.Context) {
|
2026-07-21 17:34:59 +02:00
|
|
|
// Org-scoped like the list endpoint above.
|
|
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.JSON(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-26 16:04:42 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accountIDStr := c.Param("id")
|
|
|
|
|
accountID, err := uuid.Parse(accountIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 21:47:36 -07:00
|
|
|
status, xerr := h.AnalyticsService.GetAccountStatusDetail(c.Request.Context(), *orgID, accountID)
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, status)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 21:32:15 -07:00
|
|
|
// GetUsageOverview gets usage overview for the selected organization.
|
2026-01-26 16:04:42 +01:00
|
|
|
// GET /analytics/usage
|
|
|
|
|
func (h *Handler) GetUsageOverview(c *gin.Context) {
|
2026-09-16 21:32:15 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-26 16:04:42 +01:00
|
|
|
userIDStr := middleware.GetUserID(c)
|
|
|
|
|
userID, err := uuid.Parse(userIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrAuth)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
period := c.DefaultQuery("period", "day")
|
|
|
|
|
if period != "day" && period != "week" && period != "month" {
|
|
|
|
|
period = "day"
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 21:32:15 -07:00
|
|
|
overview, xerr := h.AnalyticsService.GetUsageOverview(c.Request.Context(), *orgID, userID, period)
|
2026-01-26 16:04:42 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, overview)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetRealtimeInfo returns WebSocket connection info
|
|
|
|
|
// GET /realtime/info
|
|
|
|
|
func (h *Handler) GetRealtimeInfo(c *gin.Context) {
|
|
|
|
|
userIDStr := middleware.GetUserID(c)
|
|
|
|
|
userID, err := uuid.Parse(userIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrAuth)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get WebSocket host from request or config
|
|
|
|
|
wsHost := c.Request.Header.Get("X-Forwarded-Host")
|
|
|
|
|
if wsHost == "" {
|
|
|
|
|
wsHost = c.Request.Host
|
|
|
|
|
}
|
|
|
|
|
wsHost = "wss://" + wsHost
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"websocket_url": wsHost + "/socket",
|
|
|
|
|
"topics": []string{
|
|
|
|
|
"user:" + userID.String(),
|
|
|
|
|
"campaign:*",
|
|
|
|
|
"account:*",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-01-29 05:59:04 +01:00
|
|
|
|
2026-10-09 06:04:31 +02:00
|
|
|
// maxDashboardScopeIDs bounds each of campaign_ids and folder_ids; a folder
|
|
|
|
|
// stands in for any number of campaigns, so a larger set is never needed.
|
|
|
|
|
const maxDashboardScopeIDs = 100
|
|
|
|
|
|
2026-01-29 05:59:04 +01:00
|
|
|
// GetDashboardAnalytics returns main dashboard analytics overview
|
2026-10-09 06:04:31 +02:00
|
|
|
// GET /analytics/dashboard?period=7d[&campaign_ids=a,b][&folder_ids=c]
|
2026-01-29 05:59:04 +01:00
|
|
|
func (h *Handler) GetDashboardAnalytics(c *gin.Context) {
|
2026-06-11 17:49:39 +02:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-29 05:59:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse period (7d, 30d, 90d)
|
|
|
|
|
period := c.DefaultQuery("period", "7d")
|
|
|
|
|
if period != "7d" && period != "30d" && period != "90d" {
|
|
|
|
|
period = "7d"
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 06:04:31 +02:00
|
|
|
var filter models.DashboardFilter
|
|
|
|
|
var xerr *errx.Error
|
|
|
|
|
if filter.CampaignIDs, xerr = uuidListQuery(c, "campaign_ids", maxDashboardScopeIDs); xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if filter.FolderIDs, xerr = uuidListQuery(c, "folder_ids", maxDashboardScopeIDs); xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-09 13:30:37 +02:00
|
|
|
restricted := middleware.IsScopeRestricted(c)
|
|
|
|
|
if restricted {
|
|
|
|
|
// The filter's echo names what it asked for, so it may only ask for granted ones.
|
|
|
|
|
if !idsWithin(filter.CampaignIDs, middleware.AllowedCampaigns(c)) || !idsWithin(filter.FolderIDs, middleware.AllowedFolders(c)) {
|
|
|
|
|
errx.Handle(c, errx.New(errx.NotFound, "campaign or folder not found"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
filter.AllowedCampaigns = middleware.AllowedCampaigns(c)
|
|
|
|
|
filter.AllowedMailboxes = middleware.AllowedEmailAccounts(c)
|
|
|
|
|
}
|
2026-10-09 06:04:31 +02:00
|
|
|
|
|
|
|
|
analytics, xerr := h.AnalyticsService.GetDashboardAnalytics(c.Request.Context(), *orgID, period, filter)
|
2026-01-29 05:59:04 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-09 13:30:37 +02:00
|
|
|
if restricted {
|
|
|
|
|
// A sender outside the member's mailboxes is not named to them.
|
|
|
|
|
for i := range analytics.RecentActivity {
|
|
|
|
|
if a := &analytics.RecentActivity[i]; a.SenderID != nil && !middleware.EmailAccountAllowed(c, *a.SenderID) {
|
|
|
|
|
a.SenderID, a.SenderEmail = nil, ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
feat: add a Today's sending plan to the campaign overview (GET /campaigns/:id/send-plan, derived through the scheduler's own gates: per-mailbox cap clamps, warmup graduation, health bands, other campaigns on the same mailbox, hours, spacing, window, plan allowance, new-lead cap and leads due, as a waterfall that adds up), feed the sidebar meter and the wizard estimate from the same clamps instead of summing configured caps, floor the campaign chain's next tick at the pool's spacing rather than one mailbox's whole gap, fold the compact Advisor strip to one line, add warmbly campaign plan and warmblyctl campaign plan, and document it (issue #606)
2026-09-19 09:31:46 -07:00
|
|
|
// The sidebar meter's denominator: the mailboxes' day under the
|
|
|
|
|
// scheduler's own clamps, not their caps added up. Best effort; the
|
2026-10-09 13:30:37 +02:00
|
|
|
// dashboard still renders without it. It is the workspace's, so a
|
|
|
|
|
// restricted member is not given it.
|
|
|
|
|
if h.CampaignService != nil && !restricted {
|
feat: add a Today's sending plan to the campaign overview (GET /campaigns/:id/send-plan, derived through the scheduler's own gates: per-mailbox cap clamps, warmup graduation, health bands, other campaigns on the same mailbox, hours, spacing, window, plan allowance, new-lead cap and leads due, as a waterfall that adds up), feed the sidebar meter and the wizard estimate from the same clamps instead of summing configured caps, floor the campaign chain's next tick at the pool's spacing rather than one mailbox's whole gap, fold the compact Advisor strip to one line, add warmbly campaign plan and warmblyctl campaign plan, and document it (issue #606)
2026-09-19 09:31:46 -07:00
|
|
|
if capacity, cerr := h.CampaignService.WorkspaceCapacity(c.Request.Context(), *orgID); cerr == nil {
|
|
|
|
|
analytics.CapacityToday = capacity
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-29 05:59:04 +01:00
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, analytics)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetCampaignHourlyStats returns hourly statistics for a campaign on a specific date
|
|
|
|
|
// GET /analytics/campaigns/:id/hourly?date=2024-01-15
|
|
|
|
|
func (h *Handler) GetCampaignHourlyStats(c *gin.Context) {
|
2026-09-16 03:31:01 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-29 05:59:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
campaignIDStr := c.Param("id")
|
|
|
|
|
campaignID, err := uuid.Parse(campaignIDStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.ErrNotFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse date
|
|
|
|
|
dateStr := c.Query("date")
|
|
|
|
|
if dateStr == "" {
|
|
|
|
|
dateStr = time.Now().Format("2006-01-02")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
date, err := time.Parse("2006-01-02", dateStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 03:31:01 -07:00
|
|
|
stats, xerr := h.AnalyticsService.GetCampaignHourlyStats(c.Request.Context(), *orgID, campaignID, date)
|
2026-01-29 05:59:04 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": stats, "date": dateStr})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CompareCampaigns returns comparison statistics for multiple campaigns
|
|
|
|
|
// GET /analytics/campaigns/compare?ids=uuid1,uuid2&from=2024-01-01&to=2024-01-31
|
|
|
|
|
func (h *Handler) CompareCampaigns(c *gin.Context) {
|
2026-09-16 03:31:01 -07:00
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
2026-01-29 05:59:04 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse campaign IDs
|
|
|
|
|
idsStr := c.Query("ids")
|
|
|
|
|
if idsStr == "" {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "ids parameter is required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var campaignIDs []uuid.UUID
|
|
|
|
|
for _, idStr := range splitAndTrim(idsStr) {
|
|
|
|
|
id, err := uuid.Parse(idStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
campaignIDs = append(campaignIDs, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(campaignIDs) == 0 {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "at least one valid campaign ID is required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-09 13:30:37 +02:00
|
|
|
if !idsWithin(campaignIDs, middleware.AllowedCampaigns(c)) {
|
|
|
|
|
errx.Handle(c, errx.New(errx.NotFound, "campaign not found"))
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-29 05:59:04 +01:00
|
|
|
|
|
|
|
|
// Limit to 10 campaigns
|
|
|
|
|
if len(campaignIDs) > 10 {
|
|
|
|
|
campaignIDs = campaignIDs[:10]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse date range
|
|
|
|
|
fromStr := c.Query("from")
|
|
|
|
|
toStr := c.Query("to")
|
|
|
|
|
|
|
|
|
|
if fromStr == "" || toStr == "" {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "from and to date parameters are required"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
from, err := time.Parse("2006-01-02", fromStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid from date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
to, err := time.Parse("2006-01-02", toStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "Invalid to date format (expected YYYY-MM-DD)"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-16 03:31:01 -07:00
|
|
|
comparison, xerr := h.AnalyticsService.CompareCampaigns(c.Request.Context(), *orgID, campaignIDs, from, to)
|
2026-01-29 05:59:04 +01:00
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, comparison)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 06:04:31 +02:00
|
|
|
// uuidListQuery reads a comma-separated or repeated query parameter of
|
|
|
|
|
// UUIDs, deduplicated in order. A malformed id or more than max is a 400.
|
|
|
|
|
func uuidListQuery(c *gin.Context, key string, max int) ([]uuid.UUID, *errx.Error) {
|
|
|
|
|
var ids []uuid.UUID
|
|
|
|
|
seen := make(map[uuid.UUID]struct{})
|
|
|
|
|
for _, raw := range c.QueryArray(key) {
|
|
|
|
|
for _, part := range splitAndTrim(raw) {
|
|
|
|
|
id, err := uuid.Parse(part)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errx.New(errx.BadRequest, key+" must be a comma-separated list of UUIDs")
|
|
|
|
|
}
|
|
|
|
|
if _, dup := seen[id]; dup {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
seen[id] = struct{}{}
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(ids) > max {
|
|
|
|
|
return nil, errx.New(errx.BadRequest, fmt.Sprintf("%s accepts at most %d ids", key, max))
|
|
|
|
|
}
|
|
|
|
|
return ids, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-09 13:30:37 +02:00
|
|
|
// idsWithin reports whether every id is on allowed; a nil allowed list allows everything.
|
|
|
|
|
func idsWithin(ids, allowed []uuid.UUID) bool {
|
|
|
|
|
if allowed == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
for _, id := range ids {
|
|
|
|
|
ok := false
|
|
|
|
|
for _, a := range allowed {
|
|
|
|
|
if a == id && a != uuid.Nil {
|
|
|
|
|
ok = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 05:59:04 +01:00
|
|
|
// splitAndTrim splits a comma-separated string and trims whitespace
|
|
|
|
|
func splitAndTrim(s string) []string {
|
|
|
|
|
if s == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
parts := make([]string, 0)
|
|
|
|
|
for _, part := range split(s, ',') {
|
|
|
|
|
trimmed := trim(part)
|
|
|
|
|
if trimmed != "" {
|
|
|
|
|
parts = append(parts, trimmed)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return parts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func split(s string, sep rune) []string {
|
|
|
|
|
var result []string
|
|
|
|
|
current := ""
|
|
|
|
|
for _, c := range s {
|
|
|
|
|
if c == sep {
|
|
|
|
|
result = append(result, current)
|
|
|
|
|
current = ""
|
|
|
|
|
} else {
|
|
|
|
|
current += string(c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result = append(result, current)
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func trim(s string) string {
|
|
|
|
|
start := 0
|
|
|
|
|
end := len(s)
|
|
|
|
|
for start < end && (s[start] == ' ' || s[start] == '\t') {
|
|
|
|
|
start++
|
|
|
|
|
}
|
|
|
|
|
for end > start && (s[end-1] == ' ' || s[end-1] == '\t') {
|
|
|
|
|
end--
|
|
|
|
|
}
|
|
|
|
|
return s[start:end]
|
|
|
|
|
}
|
2026-09-17 10:03:27 +00:00
|
|
|
|
|
|
|
|
// GetDirectMailAnalytics returns the hand-written-mail overview: volume and
|
|
|
|
|
// replies from the synced mailbox, plus opens and clicks for the mailboxes that
|
|
|
|
|
// opted into tracking them.
|
|
|
|
|
// GET /analytics/direct?period=7d
|
|
|
|
|
func (h *Handler) GetDirectMailAnalytics(c *gin.Context) {
|
|
|
|
|
orgID := middleware.GetOrganizationID(c)
|
|
|
|
|
if orgID == nil {
|
|
|
|
|
errx.Handle(c, errx.New(errx.BadRequest, "no organization selected"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
period := c.DefaultQuery("period", "7d")
|
|
|
|
|
if period != "7d" && period != "30d" && period != "90d" {
|
|
|
|
|
period = "7d"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
analytics, xerr := h.AnalyticsService.GetDirectMailAnalytics(c.Request.Context(), *orgID, period)
|
|
|
|
|
if xerr != nil {
|
|
|
|
|
errx.Handle(c, xerr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, analytics)
|
|
|
|
|
}
|