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
Fixed overview getting incorrect data. Optimized overview module to enhance reusability. Optimized campaign to use the overview module to get analysis data.
Jackc4663f1ab603
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_task_stat_chart.go b/core/internal/controller/batch_mail/batch_mail_v1_task_stat_chart.goindex 032b3186..a5395153 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_task_stat_chart.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_task_stat_chart.go@@ -2,8 +2,11 @@ package batch_mailimport ("billionmail-core/internal/service/batch_mail"+ "billionmail-core/internal/service/maillog_stat""billionmail-core/internal/service/public""context"+ "fmt"+ "github.com/gogf/gf/v2/util/gconv""github.com/gogf/gf/v2/errors/gerror"@@ -26,16 +29,27 @@ func (c *ControllerV1) TaskStatChart(ctx context.Context, req *v1.TaskStatChartRreturn}- statService := batch_mail.NewTaskStatService()+ // reuse the maillog_stat service to get the overview+ overview := maillog_stat.NewOverview()+ overviewMap := overview.Overview(req.TaskId, req.Domain, req.StartTime, req.EndTime)- chartData := statService.GetTaskStatChart(req.TaskId, req.Domain, req.StartTime, req.EndTime)+ err = gconv.Struct(overviewMap, &res.Data)- res.Data.Dashboard = chartData["dashboard"]- res.Data.MailProviders = chartData["mail_providers"]- res.Data.SendMailChart = chartData["send_mail_chart"]- res.Data.BounceRateChart = chartData["bounce_rate_chart"]- res.Data.OpenRateChart = chartData["open_rate_chart"]- res.Data.ClickRateChart = chartData["click_rate_chart"]+ if err != nil {+ err = fmt.Errorf("failed to convert overview data: %v", err)+ return+ }++ //statService := batch_mail.NewTaskStatService()+ //+ //chartData := statService.GetTaskStatChart(req.TaskId, req.Domain, req.StartTime, req.EndTime)+ //+ //res.Data.Dashboard = chartData["dashboard"]+ //res.Data.MailProviders = chartData["mail_providers"]+ //res.Data.SendMailChart = chartData["send_mail_chart"]+ //res.Data.BounceRateChart = chartData["bounce_rate_chart"]+ //res.Data.OpenRateChart = chartData["open_rate_chart"]+ //res.Data.ClickRateChart = chartData["click_rate_chart"]res.SetSuccess(public.LangCtx(ctx, "Task statistics retrieved successfully"))returndiff --git a/core/internal/service/maillog_stat/overview.go b/core/internal/service/maillog_stat/overview.goindex e2493534..66b61551 100644--- a/core/internal/service/maillog_stat/overview.go+++ b/core/internal/service/maillog_stat/overview.go@@ -37,6 +37,12 @@ func (o *Overview) buildBaseQuery(campaignID int64, domain string, startTime, enquery.LeftJoin("mailstat_senders s", "sm.postfix_message_id=s.postfix_message_id")query.Where("s.postfix_message_id is not null")+ if campaignID > 0 {+ query.InnerJoin("mailstat_message_ids mi", "sm.postfix_message_id=mi.postfix_message_id")+ query.InnerJoin("recipient_info r", "mi.message_id=r.message_id")+ query.Where("r.task_id = ?", campaignID)+ }+if domain != "" {query.Where("s.sender LIKE ?", "%@"+domain)}@@ -70,13 +76,24 @@ func (o *Overview) Overview(campaignID int64, domain string, startTime, endTimefunc (o *Overview) overviewDashboard(campaignID int64, domain string, startTime, endTime int64) map[string]interface{} {query := o.buildBaseQuery(campaignID, domain, startTime, endTime)- query.LeftJoin("mailstat_opened o", "sm.postfix_message_id=o.postfix_message_id")- query.LeftJoin("mailstat_clicked c", "sm.postfix_message_id=c.postfix_message_id")+ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_opened+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as o`, "true")++ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_clicked+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as c`, "true")query.Fields("count(*) as sends")query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")- query.Fields("count(distinct o.postfix_message_id) as opened")- query.Fields("count(distinct c.postfix_message_id) as clicked")+ query.Fields("count(o.id) as opened")+ query.Fields("count(c.id) as clicked")query.Fields("coalesce(sum(case when status='bounced' then 1 else 0 end), 0) as bounced")aggregate := map[string]interface{}{@@ -102,15 +119,11 @@ func (o *Overview) overviewDashboard(campaignID int64, domain string, startTime,if aggregate["sends"].(int) > 0 {aggregate["delivery_rate"] = public.Round(float64(aggregate["delivered"].(int))/float64(aggregate["sends"].(int))*100, 2)- } else {- aggregate["delivery_rate"] = 0- }-- if aggregate["sends"].(int) > 0 {aggregate["bounce_rate"] = public.Round(float64(aggregate["bounced"].(int))/float64(aggregate["sends"].(int))*100, 2)aggregate["open_rate"] = public.Round(float64(aggregate["opened"].(int))/float64(aggregate["sends"].(int))*100, 2)aggregate["click_rate"] = public.Round(float64(aggregate["clicked"].(int))/float64(aggregate["sends"].(int))*100, 2)} else {+ aggregate["delivery_rate"] = 0aggregate["bounce_rate"] = 0aggregate["open_rate"] = 0aggregate["click_rate"] = 0@@ -125,17 +138,28 @@ func (o *Overview) overviewProviders(campaignID int64, domain string, startTime,query := o.buildBaseQuery(campaignID, domain, startTime, endTime)- query = query.LeftJoin("mailstat_opened o", "sm.postfix_message_id=o.postfix_message_id")- query = query.LeftJoin("mailstat_clicked c", "sm.postfix_message_id=c.postfix_message_id")-- query = query.Fields("sm.mail_provider")- query = query.Fields("count(*) as sends")- query = query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")- query = query.Fields("count(distinct o.postfix_message_id) as opened")- query = query.Fields("count(distinct c.postfix_message_id) as clicked")- query = query.Fields("coalesce(sum(case when status='bounced' then 1 else 0 end), 0) as bounced")+ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_opened+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as o`, "true")++ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_clicked+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as c`, "true")++ query.Fields("sm.mail_provider")+ query.Fields("count(*) as sends")+ query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")+ query.Fields("count(o.id) as opened")+ query.Fields("count(c.id) as clicked")+ query.Fields("coalesce(sum(case when status='bounced' then 1 else 0 end), 0) as bounced")- query = query.Group("sm.mail_provider")+ query.Group("sm.mail_provider")lst := make([]map[string]interface{}, 0)@@ -282,9 +306,9 @@ func (o *Overview) sendMailDashboard(campaignID int64, domain string, startTime,query := o.buildBaseQuery(campaignID, domain, startTime, endTime)- query = query.Fields("count(*) as sends")- query = query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")- query = query.Fields("count(*) - coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as failed")+ query.Fields("count(*) as sends")+ query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")+ query.Fields("count(*) - coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as failed")aggregate := map[string]interface{}{"sends": 0,@@ -316,11 +340,11 @@ func (o *Overview) sendMailDashboard(campaignID int64, domain string, startTime,func (o *Overview) prepareChartData(startTime, endTime int64) (string, string) {columnType := "daily"secs := endTime - startTime- xAxisField := "to_char(to_timestamp(sm.log_time_millis / 1000), 'YYYY-MM-DD') as x"+ xAxisField := "TO_CHAR(TO_TIMESTAMP(sm.log_time_millis / 1000), 'YYYY-MM-DD') as x"if secs < 86400 {columnType = "hourly"- xAxisField = "to_char(to_timestamp(sm.log_time_millis / 1000), 'HH24')::integer as x"+ xAxisField = "TO_CHAR(TO_TIMESTAMP(sm.log_time_millis / 1000), 'HH24')::integer as x"}return columnType, xAxisField@@ -333,13 +357,13 @@ func (o *Overview) chartSendMail(campaignID int64, domain string, startTime, endcolumnType, xAxisField := o.prepareChartData(startTime, endTime)- query = query.Fields(xAxisField)- query = query.Fields("count(*) as sends")- query = query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")- query = query.Fields("count(*) - coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as failed")+ query.Fields(xAxisField)+ query.Fields("count(*) as sends")+ query.Fields("coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as delivered")+ query.Fields("count(*) - coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) as failed")- query = query.Group("x")- query = query.Order("x")+ query.Group("x")+ query.Order("x")rs, err := query.All()if err != nil {@@ -372,11 +396,11 @@ func (o *Overview) chartBounceRate(campaignID int64, domain string, startTime, ecolumnType, xAxisField := o.prepareChartData(startTime, endTime)- query = query.Fields(xAxisField)- query = query.Fields(gdb.Raw("case when count(*) > 0 then round(1.0 * coalesce(sum(case when status='bounced' then 1 else 0 end), 0) / count(*) * 100.0, 2) else 0.0 end as bounce_rate"))+ query.Fields(xAxisField)+ query.Fields(gdb.Raw("case when count(*) > 0 then round(1.0 * coalesce(sum(case when status='bounced' then 1 else 0 end), 0) / count(*) * 100.0, 2) else 0.0 end as bounce_rate"))- query = query.Group("x")- query = query.Order("x")+ query.Group("x")+ query.Order("x")rs, err := query.All()if err != nil {@@ -406,12 +430,17 @@ func (o *Overview) chartOpenRate(campaignID int64, domain string, startTime, endcolumnType, xAxisField := o.prepareChartData(startTime, endTime)- query = query.LeftJoin("mailstat_opened o", "sm.postfix_message_id=o.postfix_message_id")+ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_opened+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as o`, "true")- query = query.Fields(xAxisField)- query = query.Fields("case when coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) > 0 then round(1.0 * count(distinct o.postfix_message_id) / coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) * 100, 2) else 0.0 end as open_rate")+ query.Fields(xAxisField)+ query.Fields("case when coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) > 0 then round(1.0 * count(o.id) / coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) * 100, 2) else 0.0 end as open_rate")- query = query.Group("x")+ query.Group("x")rs, err := query.All()if err != nil {@@ -441,12 +470,17 @@ func (o *Overview) chartClickRate(campaignID int64, domain string, startTime, encolumnType, xAxisField := o.prepareChartData(startTime, endTime)- query = query.LeftJoin("mailstat_clicked c", "sm.postfix_message_id=c.postfix_message_id")+ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_clicked+ WHERE sm.postfix_message_id = postfix_message_id+ LIMIT 1+) as c`, "true")- query = query.Fields(xAxisField)- query = query.Fields("case when coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) > 0 then round(1.0 * count(distinct c.postfix_message_id) / coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) * 100, 2) else 0.0 end as click_rate")+ query.Fields(xAxisField)+ query.Fields("case when coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) > 0 then round(1.0 * count(c.id) / coalesce(sum(case when status='sent' and dsn like '2.%' then 1 else 0 end), 0) * 100, 2) else 0.0 end as click_rate")- query = query.Group("x")+ query.Group("x")rs, err := query.All()if err != nil {@@ -477,6 +511,14 @@ func (o *Overview) FailedList(campaignID int64, domain string, startTime, endTimquery.LeftJoin("mailstat_deferred_mails d", "sm.postfix_message_id=d.postfix_message_id")+ query.LeftJoin(`LATERAL(+ SELECT id+ FROM mailstat_deferred_mails+ WHERE sm.postfix_message_id = postfix_message_id+ ORDER BY id DESC+ LIMIT 1+) as d`, "true")+query.Fields("sm.postfix_message_id")query.Fields("s.sender")query.Fields("sm.recipient")diff --git a/core/run_dev.sh b/core/run_dev.shindex 6c8f9200..5ef3a7e2 100644--- a/core/run_dev.sh+++ b/core/run_dev.sh@@ -2,24 +2,27 @@echo "Compiling billionmail..."-# Using the alpine image to compile the Go application-docker exec p-g-alpine sh -c "cd /opt/core && sh ./go-build.sh"--echo "Copying billionmail to billionmail-core-billionmail-1 container..."-# Determine the architecture and set the binary name accordinglyARCH=$(uname -m)+PLATFORMS="all"if [[ "$ARCH" == "x86_64" ]]; thenBINARY="billionmail-amd64"+ PLATFORMS="x86"echo "Detect x86_64 architecture, using amd64 binary"elif [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; thenBINARY="billionmail-arm64"+ PLATFORMS="arm"echo "Detect arm64/aarch64 architecture, using arm64 binary"elseecho "Unsupported architecture: $ARCH"exit 1fi+# Using the alpine image to compile the Go application+docker exec p-g-alpine sh -c "cd /opt/core && sh ./go-build.sh $PLATFORMS"++echo "Copying billionmail to billionmail-core-billionmail-1 container..."+echo "Copying the compiled binary from p-g-alpine to billionmail-core-billionmail-1..."# Copy the compiled binary to the billionmail-core-billionmail-1 container