High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Fix incorrect realtime update of stats on the campaign page.
Kailash Nadh33896f18bcc9
diff --git a/cmd/campaigns.go b/cmd/campaigns.goindex b1019a9e..15637745 100644--- a/cmd/campaigns.go+++ b/cmd/campaigns.go@@ -696,8 +696,13 @@ func (a *App) GetRunningCampaignStats(c *echo.Context) error {return c.JSON(http.StatusOK, okResp{[]struct{}{}})}- // Compute rate.+ // Compute rate and stats.for i, c := range out {+ stats := a.manager.GetCampaignStats(c.ID)+ c.Sent = max(c.Sent, stats.Sent)+ out[i].Sent = c.Sent+ out[i].Rate = stats.SendRate+if c.Started.Valid && c.UpdatedAt.Valid {diff := max(int(c.UpdatedAt.Time.Sub(c.Started.Time).Minutes()), 1)@@ -709,8 +714,6 @@ func (a *App) GetRunningCampaignStats(c *echo.Context) error {// Rate since the starting of the campaign.out[i].NetRate = rate- // Realtime running rate over the last minute.- out[i].Rate = a.manager.GetCampaignStats(c.ID).SendRate}}diff --git a/internal/manager/manager.go b/internal/manager/manager.goindex fb2c99da..e30ad4bc 100644--- a/internal/manager/manager.go+++ b/internal/manager/manager.go@@ -72,6 +72,7 @@ type Messenger interface {// CampStats contains campaign stats like per minute send rate.type CampStats struct {SendRate int+ Sent int}// Manager handles the scheduling, processing, and queuing of campaigns@@ -265,15 +266,16 @@ func (m *Manager) HasRunningCampaigns() bool {// GetCampaignStats returns campaign statistics.func (m *Manager) GetCampaignStats(id int) CampStats {- n := 0+ stats := CampStats{}m.pipesMut.Lock()if c, ok := m.pipes[id]; ok {- n = int(c.rate.Rate())+ stats.SendRate = int(c.rate.Rate())+ stats.Sent = int(c.totalSent.Load())}m.pipesMut.Unlock()- return CampStats{SendRate: n}+ return stats}// Run is a blocking function (that should be invoked as a goroutine)@@ -562,6 +564,7 @@ func (m *Manager) worker() {} else {msg.pipe.rate.Incr(1)msg.pipe.sent.Add(1)+ msg.pipe.totalSent.Add(1)}// Retain failed sends only when stopped, allowing retry on resume.diff --git a/internal/manager/pipe.go b/internal/manager/pipe.goindex c8a49d40..1201ec0b 100644--- a/internal/manager/pipe.go+++ b/internal/manager/pipe.go@@ -11,10 +11,16 @@ import ()type pipe struct {- camp *models.Campaign- rate *ratecounter.RateCounter- wg *sync.WaitGroup- sent atomic.Int64+ camp *models.Campaign+ rate *ratecounter.RateCounter+ wg *sync.WaitGroup++ // Sent is the number of messages sent in the current pipe pending to be+ // synced to the DB. This is periodically flushed to DB and reset to 0.+ sent atomic.Int64++ // This is a running total of all sends used for reporting in the stats API.+ totalSent atomic.Int64errors atomic.Uint64stopped atomic.BoolwithErrors atomic.Bool@@ -60,6 +66,8 @@ func (m *Manager) newPipe(c *models.Campaign) (*pipe, error) {m: m,}+ p.totalSent.Store(int64(c.Sent))+// Increment the waitgroup so that Wait() blocks immediately. This is necessary// as a campaign pipe is created first and subscribers/messages under it are// fetched asynchronolusly later. The messages each add to the wg and thatdiff --git a/static/admin/assets/js/views/campaigns.js b/static/admin/assets/js/views/campaigns.jsindex 49aee759..700542c2 100644--- a/static/admin/assets/js/views/campaigns.js+++ b/static/admin/assets/js/views/campaigns.js@@ -15,11 +15,16 @@ function component() {// Running-campaign stats polling._pollID: null,- _hadRunning: false,init() {- this._poll();- this._pollID = setInterval(() => this._poll(), 1000);+ if (this.$el.querySelector('tr.status-running')) {+ this._pollID = setTimeout(() => this._poll(), 0);+ }+ },++ destroy() {+ clearTimeout(this._pollID);+ this._pollID = null;},// ===============@@ -115,33 +120,34 @@ function component() {// ===============// Live stats polling for running campaigns.async _poll() {- let resp;try {- resp = await fetch(`${urls.api}/campaigns/running/stats`, { headers: { Accept: 'application/json' } });- } catch (e) {- clearInterval(this._pollID);- return;- }- if (!resp.ok) {- clearInterval(this._pollID);- return;- }-- const out = await resp.json().catch(() => ({}));- const data = Array.isArray(out.data) ? out.data : [];+ const resp = await fetch(`${urls.api}/campaigns/running/stats`, {+ headers: { Accept: 'application/json' },+ signal: AbortSignal.timeout(10000),+ });+ if (!resp.ok) {+ if (resp.status === 401 || resp.status === 403) this.destroy();+ return;+ }- // No running campaigns. Stop polling, and if some were running earlier,- // reload to fetch fresh server-rendered stats and statuses.- if (data.length === 0) {- clearInterval(this._pollID);- if (this._hadRunning) {- u.reload();+ const { data } = await resp.json();+ if (this._pollID === null || !Array.isArray(data)) return;++ for (const row of this.$el.querySelectorAll('tr.status-running[data-campaign-id]')) {+ const stats = data.find((c) => c.id === Number(row.dataset.campaignId));+ if (!stats) {+ this.destroy();+ u.reload();+ return;+ }+ this._updateStats(stats);}- return;+ } catch (e) {+ // Retry transient network, timeout, and JSON errors.+ } finally {+ // Schedule after completion so requests cannot overlap.+ if (this._pollID !== null) this._pollID = setTimeout(() => this._poll(), 1000);}-- this._hadRunning = true;- data.forEach((c) => this._updateStats(c));},_updateStats(c) {@@ -157,9 +163,13 @@ function component() {set('sent', u.formatNumber(c.sent));const sentCount = row.querySelector('[data-sent-count]');if (sentCount) {- sentCount.hidden = c.sent === 0;+ sentCount.hidden = false;}set('to_send', u.formatNumber(c.to_send));+ const sendTotal = row.querySelector('[data-send-total]');+ if (sendTotal) {+ sendTotal.hidden = c.to_send === 0;+ }set('rate', Math.round(c.rate || 0));set('percent', `${c.to_send > 0 ? Math.floor((c.sent / c.to_send) * 100) : 0}%`);diff --git a/static/admin/partials/campaigns-table.html b/static/admin/partials/campaigns-table.htmlindex 79cc95f3..a63f5b47 100644--- a/static/admin/partials/campaigns-table.html+++ b/static/admin/partials/campaigns-table.html@@ -146,7 +146,7 @@</td><td class="stats" data-label="{{ $root.L.T "globals.terms.stats" }}"><div class="vstack gap-1">- <div><div data-sent-count{{ if eq .Sent 0 }} hidden{{ end }}><span data-stat="sent">{{ FormatNumber .Sent }}</span>{{ if and .ToSend (not (eq .Status "finished")) }} / <span data-stat="to_send">{{ FormatNumber .ToSend }}</span>{{ end }}</div><span class="text-8 text-light"><span data-stat="percent">{{ if .ToSend }}{{ div (mul .Sent 100) .ToSend }}{{ else }}0{{ end }}%</span> {{ $root.L.T "globals.terms.sent" | lower }}{{ with Duration .StartedAt .UpdatedAt }} · {{ . }}{{ end }}</span></div>+ <div><div data-sent-count{{ if and (eq .Sent 0) (ne .Status "running") }} hidden{{ end }}><span data-stat="sent">{{ FormatNumber .Sent }}</span>{{ if ne .Status "finished" }}<span data-send-total{{ if not .ToSend }} hidden{{ end }}> / <span data-stat="to_send">{{ FormatNumber .ToSend }}</span></span>{{ end }}</div><span class="text-8 text-light"><span data-stat="percent">{{ if .ToSend }}{{ div (mul .Sent 100) .ToSend }}{{ else }}0{{ end }}%</span> {{ $root.L.T "globals.terms.sent" | lower }}{{ with Duration .StartedAt .UpdatedAt }} · {{ . }}{{ end }}</span></div><div><a class="unstyled" href="{{ URI "/admin/subscribers/bounces" }}?campaign_id={{ .ID }}">{{ FormatNumber .Bounces }} <span class="text-8 text-light">{{ $root.L.T "globals.terms.bounces" | lower }}</span></a></div>{{- if eq .Status "running" }}<div class="hstack gap-1 text-8 campaign-progress">