BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Optimize the api for sending email statistics
lotk8629b78ff74c
diff --git a/core/api/batch_mail/batch_mail.go b/core/api/batch_mail/batch_mail.goindex 0dae9042..3e2debb1 100644--- a/core/api/batch_mail/batch_mail.go+++ b/core/api/batch_mail/batch_mail.go@@ -12,6 +12,7 @@ import (type IBatchMailV1 interface {ApiTemplatesList(ctx context.Context, req *v1.ApiTemplatesListReq) (res *v1.ApiTemplatesListRes, err error)+ ApiOverviewStats(ctx context.Context, req *v1.ApiOverviewStatsReq) (res *v1.ApiOverviewStatsRes, err error)ApiTemplatesCreate(ctx context.Context, req *v1.ApiTemplatesCreateReq) (res *v1.ApiTemplatesCreateRes, err error)ApiTemplatesUpdate(ctx context.Context, req *v1.ApiTemplatesUpdateReq) (res *v1.ApiTemplatesUpdateRes, err error)ApiTemplatesDelete(ctx context.Context, req *v1.ApiTemplatesDeleteReq) (res *v1.ApiTemplatesDeleteRes, err error)diff --git a/core/api/batch_mail/v1/api_mail.go b/core/api/batch_mail/v1/api_mail.goindex fc347530..24146755 100644--- a/core/api/batch_mail/v1/api_mail.go+++ b/core/api/batch_mail/v1/api_mail.go@@ -63,20 +63,22 @@ type ApiTemplatesListReq struct {Page int `json:"page" dc:"page"`PageSize int `json:"page_size" dc:"page size"`Keyword string `json:"keyword" dc:"Search Keyword"`+ Active int `json:"active" dc:"active"`+ StartTime int `json:"start_time" dc:"start time"`+ EndTime int `json:"end_time" dc:"end time"`}// 补充结构体 ApiTemplates中增加 SendCount 每个api模板发送次数 成功次数 失败次数 打开率 点击率 退订率type ApiTemplatesInfo struct {ApiTemplates- SendCount int `json:"send_count" dc:"send count"`- SuccessCount int `json:"success_count" dc:"success count"`- FailCount int `json:"fail_count" dc:"fail count"`- OpenRate float64 `json:"open_rate" dc:"open rate"`- ClickRate float64 `json:"click_rate" dc:"click rate"`- DeliveryRate float64 `json:"delivery_rate" dc:"delivery rate"`- BounceRate float64 `json:"bounce_rate" dc:"bounce rate"`-- //UnsubscribeRate float64 `json:"unsubscribe_rate" dc:"unsubscribe rate"`+ SendCount int `json:"send_count" dc:"send count"`+ SuccessCount int `json:"success_count" dc:"success count"`+ FailCount int `json:"fail_count" dc:"fail count"`+ OpenRate float64 `json:"open_rate" dc:"open rate"`+ ClickRate float64 `json:"click_rate" dc:"click rate"`+ DeliveryRate float64 `json:"delivery_rate" dc:"delivery rate"`+ BounceRate float64 `json:"bounce_rate" dc:"bounce rate"`+ UnsubscribeCount int `json:"unsubscribe_count" dc:"unsubscribe count"`}type ApiTemplatesListRes struct {@@ -87,6 +89,29 @@ type ApiTemplatesListRes struct {} `json:"data"`}+type ApiSummaryStats struct {+ TotalSend int `json:"total_send" dc:"total send count"`+ AvgDeliveryRate float64 `json:"avg_delivery_rate" dc:"average delivery rate"`+ AvgOpenRate float64 `json:"avg_open_rate" dc:"average open rate"`+ AvgClickRate float64 `json:"avg_click_rate" dc:"average click rate"`+ AvgBounceRate float64 `json:"avg_bounce_rate" dc:"average bounce rate"`+ AvgUnsubRate float64 `json:"avg_unsub_rate" dc:"average unsubscribe rate"`+ TotalUnsubscribe int `json:"total_unsubscribe" dc:"total unsubscribe count"`+}++// api 总览统计+type ApiOverviewStatsReq struct {+ g.Meta `path:"/batch_mail/api/overview_stats" method:"get" tags:"ApiMail" summary:"api 总览统计"`+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`+ StartTime int `json:"start_time" dc:"start time"`+ EndTime int `json:"end_time" dc:"end time"`+}++type ApiOverviewStatsRes struct {+ api_v1.StandardRes+ Data ApiSummaryStats `json:"data"`+}+// api 配置创建type ApiTemplatesCreateReq struct {g.Meta `path:"/batch_mail/api/create" method:"post" tags:"ApiMail" summary:"api 配置创建"`diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_overview_stats.go b/core/internal/controller/batch_mail/batch_mail_v1_api_overview_stats.gonew file mode 100644index 00000000..a51adcf4--- /dev/null+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_overview_stats.go@@ -0,0 +1,101 @@+package batch_mail++import (+ "billionmail-core/internal/service/public"+ "context"+ "github.com/gogf/gf/v2/frame/g"++ "billionmail-core/api/batch_mail/v1"+)++func (c *ControllerV1) ApiOverviewStats(ctx context.Context, req *v1.ApiOverviewStatsReq) (res *v1.ApiOverviewStatsRes, err error) {+ res = &v1.ApiOverviewStatsRes{}++ // 查询所有API模板+ apiList := []struct {+ Id int+ CreateTime int+ }{}+ err = g.DB().Model("api_templates").Fields("id, create_time").Scan(&apiList)+ if err != nil {+ return nil, err+ }++ var totalSend, totalDelivered, totalBounced, totalOpened, totalClicked, totalUnsub int++ for _, api := range apiList {+ // 统计发送、送达、退信+ query := g.DB().Model("api_mail_logs aml")+ query = query.LeftJoin("mailstat_message_ids mi", "aml.message_id=mi.message_id")+ query = query.LeftJoin("mailstat_send_mails sm", "mi.postfix_message_id=sm.postfix_message_id")+ query = query.Where("aml.api_id", api.Id)+ if req.StartTime > 0 {+ query.Where("sm.log_time_millis > ?", int64(req.StartTime)*1000-1)+ }+ if req.EndTime > 0 {+ query.Where("sm.log_time_millis < ?", int64(req.EndTime)*1000+1)+ }+ query.Fields(+ "count(*) as sends",+ "coalesce(sum(case when sm.status='sent' and sm.dsn like '2.%' then 1 else 0 end), 0) as delivered",+ "coalesce(sum(case when sm.status='bounced' then 1 else 0 end), 0) as bounced",+ )+ result, _ := query.One()+ sends := result["sends"].Int()+ delivered := result["delivered"].Int()+ bounced := result["bounced"].Int()+ totalSend += sends+ totalDelivered += delivered+ totalBounced += bounced++ // 统计打开、点击+ apiCampaignId := api.Id + 1000000000+ openedCount, _ := g.DB().Model("mailstat_opened").+ Where("campaign_id", apiCampaignId).+ WhereGTE("log_time_millis", int64(req.StartTime)*1000).+ WhereLTE("log_time_millis", int64(req.EndTime)*1000).+ Fields("count(distinct postfix_message_id) as opened").+ Value()+ clickedCount, _ := g.DB().Model("mailstat_clicked").+ Where("campaign_id", apiCampaignId).+ WhereGTE("log_time_millis", int64(req.StartTime)*1000).+ WhereLTE("log_time_millis", int64(req.EndTime)*1000).+ Fields("count(distinct postfix_message_id) as clicked").+ Value()+ totalOpened += openedCount.Int()+ totalClicked += clickedCount.Int()++ // 统计退订数量+ recipients := []string{}+ _, _ = g.DB().Model("api_mail_logs").Where("api_id", api.Id).Fields("recipient").Array(&recipients)+ unsubscribeCount := 0+ if len(recipients) > 0 {+ unsubscribeCount, _ = g.DB().Model("bm_contacts").+ Where("email", recipients).+ Where("active", 0).+ WhereGTE("create_time", api.CreateTime).+ Count()+ }+ totalUnsub += unsubscribeCount+ }++ var avgDeliveryRate, avgOpenRate, avgClickRate, avgBounceRate, avgUnsubRate float64+ if totalSend > 0 {+ avgDeliveryRate = public.Round(float64(totalDelivered)/float64(totalSend)*100, 2)+ avgOpenRate = public.Round(float64(totalOpened)/float64(totalSend)*100, 2)+ avgClickRate = public.Round(float64(totalClicked)/float64(totalSend)*100, 2)+ avgBounceRate = public.Round(float64(totalBounced)/float64(totalSend)*100, 2)+ avgUnsubRate = public.Round(float64(totalUnsub)/float64(totalSend)*100, 2)+ }+ res.Data = v1.ApiSummaryStats{+ TotalSend: totalSend,+ AvgDeliveryRate: avgDeliveryRate,+ AvgOpenRate: avgOpenRate,+ AvgClickRate: avgClickRate,+ AvgBounceRate: avgBounceRate,+ AvgUnsubRate: avgUnsubRate,+ TotalUnsubscribe: totalUnsub,+ }+ res.SetSuccess("统计成功")+ return res, nil+}diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.goindex 5f36cb70..3230d7c5 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_templates_list.go@@ -5,6 +5,7 @@ import ("billionmail-core/internal/service/public""context""github.com/gogf/gf/v2/frame/g"+ "time")func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplatesListReq) (res *v1.ApiTemplatesListRes, err error) {@@ -13,10 +14,18 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplate// 构建查询条件model := g.DB().Model("api_templates").Safe()- // 添加搜索条件+ // 添加api_name模糊搜索if req.Keyword != "" {model = model.WhereLike("api_name", "%"+req.Keyword+"%")}+ // active筛选+ if req.Active != -1 {+ model = model.Where("active", req.Active)+ }++ if req.StartTime > 0 && req.EndTime < 0 {+ req.EndTime = int(time.Now().Unix())+ }// 获取总数total, err := model.Count()@@ -41,6 +50,14 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplatequery = query.LeftJoin("mailstat_send_mails sm", "mi.postfix_message_id=sm.postfix_message_id")query = query.Where("aml.api_id", item.Id)+ if req.StartTime > 0 {+ query.Where("sm.log_time_millis > ?", req.StartTime*1000-1)+ }++ if req.EndTime > 0 {+ query.Where("sm.log_time_millis < ?", req.EndTime*1000+1)+ }+// 统计各项数据query.Fields("count(*) as sends",@@ -59,21 +76,23 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplateitem.SuccessCount = result["delivered"].Int()item.FailCount = result["bounced"].Int()- // 3. 统计打开、点击(直接用campaign_id)+ // 统计打开、点击(直接用campaign_id)apiCampaignId := item.Id + 1000000000openedCount, _ := g.DB().Model("mailstat_opened").Where("campaign_id", apiCampaignId).+ WhereGTE("log_time_millis", req.StartTime*1000).+ WhereLTE("log_time_millis", req.EndTime*1000).Fields("count(distinct postfix_message_id) as opened").Value()clickedCount, _ := g.DB().Model("mailstat_clicked").Where("campaign_id", apiCampaignId).+ WhereGTE("log_time_millis", req.StartTime*1000).+ WhereLTE("log_time_millis", req.EndTime*1000).Fields("count(distinct postfix_message_id) as clicked").Value()if item.SendCount > 0 {- // 送达率 = 成功送达数/总发送数item.DeliveryRate = public.Round(float64(item.SuccessCount)/float64(item.SendCount)*100, 2)- // 退信率 = 退信数/总发送数item.BounceRate = public.Round(float64(item.FailCount)/float64(item.SendCount)*100, 2)item.OpenRate = public.Round(float64(openedCount.Int())/float64(item.SendCount)*100, 2)item.ClickRate = public.Round(float64(clickedCount.Int())/float64(item.SendCount)*100, 2)@@ -84,10 +103,50 @@ func (c *ControllerV1) ApiTemplatesList(ctx context.Context, req *v1.ApiTemplateitem.ClickRate = 0}+ // 统计退订数量+ recipients := []string{}+ _, err = g.DB().Model("api_mail_logs").Where("api_id", item.Id).Fields("recipient").Array(&recipients)+ unsubscribeCount := 0+ if len(recipients) > 0 {+ unsubscribeCount, _ = g.DB().Model("bm_contacts").+ Where("email", recipients).+ Where("active", 0).+ WhereGTE("create_time", item.CreateTime).+ Count()+ }+ item.UnsubscribeCount = unsubscribeCount}++ // 汇总全局统计+ //var totalSend, totalDelivered, totalBounced, totalOpened, totalClicked, totalUnsub int+ //for _, item := range list {+ // totalSend += item.SendCount+ // totalDelivered += item.SuccessCount+ // totalBounced += item.FailCount+ // totalOpened += int(item.OpenRate * float64(item.SendCount) / 100)+ // totalClicked += int(item.ClickRate * float64(item.SendCount) / 100)+ // totalUnsub += item.UnsubscribeCount+ //}+ //var avgDeliveryRate, avgOpenRate, avgClickRate, avgBounceRate, avgUnsubRate float64+ //if totalSend > 0 {+ // avgDeliveryRate = public.Round(float64(totalDelivered)/float64(totalSend)*100, 2)+ // avgOpenRate = public.Round(float64(totalOpened)/float64(totalSend)*100, 2)+ // avgClickRate = public.Round(float64(totalClicked)/float64(totalSend)*100, 2)+ // avgBounceRate = public.Round(float64(totalBounced)/float64(totalSend)*100, 2)+ // avgUnsubRate = public.Round(float64(totalUnsub)/float64(totalSend)*100, 2)+ //}+ //res.Data.Summary = map[string]interface{}{+ // "total_send": totalSend,+ // "avg_delivery_rate": avgDeliveryRate,+ // "avg_open_rate": avgOpenRate,+ // "avg_click_rate": avgClickRate,+ // "avg_bounce_rate": avgBounceRate,+ // "avg_unsub_rate": avgUnsubRate,+ // "total_unsubscribe": totalUnsub,+ //}+res.Data.Total = totalres.Data.List = listres.SetSuccess("获取API列表成功")return res, nil-}