Repository navigation
Description
Description
A brand can receive two Daily Pulse emails within the same hour whose bodies describe the identical tracking window.
It happened in production this morning for one large brand (~1.8k results/day): two mails, ~05:03 and ~06:19 local, both reporting the same totalResults because both were computed from the same previous-day tracking run. The second mail was effectively a stripped copy of the first — its warnings were empty only because the 7-day warning cooldown had already consumed them an hour earlier.
sent_pulses rows for that brand:
| created | pulse_date |
totalResults |
warnings | trigger |
|---|---|---|---|---|
| 05:03 | previous day | 1785 | 3 | catch-up sweep |
| 06:19 | today | 1785 | 0 | end-of-run pulse |
Two independent defects combine:
1. The catch-up sweep recovers a missed pulse with current data.
runPulseCatchUp() in server/src/lib/pulse/engine.js passes only pulseDate into generatePulseForBrand(); the metrics are still computed with now. So a pulse "recovered" for yesterday actually reports whatever window is current at sweep time. Because the sweep runs at the start of the daily cycle (wired into runDailyTracking in server/src/server.js), the anchor has not moved yet — it is still yesterday's run, i.e. exactly the window today's own pulse will use an hour later.
2. Dedupe is keyed on pulse_date, not on the data.
The sent_pulses (brand_id, pulse_date) unique key cannot see that two rows with different dates describe the same window. computePulseMetrics() anchors the daily KPI window to the latest completed run (latestCompletedRunTimes), so whenever that anchor has not advanced, every pulse fired that day repeats the previous mail.
Condition to trigger: a brand misses a pulse (process death, DB timeout, deploy) and its run anchor has not moved by the time the next cycle's own pulse fires. The second half held today because the brand's run was never stamped — filed separately.
Steps to reproduce
- Pick a brand with a completed tracking run and delete its
sent_pulsesrow for that run's day (simulates the lost pulse). - Make sure no newer completed run exists for that brand (so the KPI anchor stays on that run).
- Start the daily cycle so
runPulseCatchUp()runs — a recovery mail goes out, dated to the missed day. - Let the brand's own tracking job finish without stamping a newer run.
- A second mail goes out with the same numbers, dated today.
Expected behavior
At most one pulse email per brand per tracking window. A recovered pulse should either describe the window it claims to recover, or be suppressed when the brand is about to produce a pulse for the same window.
Actual behavior
Two emails an hour apart with identical KPIs, and the second one silently loses its warnings to the cooldown, so it reads as a degraded duplicate.
Deployment mode
Cloud (ansvisor.com)
Environment
Server (Node), production daily cycle.
Proposed fix
Both parts are small and independent:
- Anchor-aware dedupe (primary). Record the KPI window anchor (
curTo, i.e. the latest completed run used bycomputePulseMetrics) in thesent_pulsespayload, and havegeneratePulseForBrand()skip generation when a pulse for the same brand already exists with the same anchor. This kills identical-content mails regardless of which path fired them, and also protects against re-runs and manual triggers. - Move the sweep out of the cycle start. At cycle start every eligible brand is about to produce its own pulse, so the sweep is racing them by design. Either run it at the end of the cycle, or skip brands that have a tracking job queued/running for the current cycle.
Optionally, if the recovery mail is meant to represent the missed day, generatePulseForBrand() should accept a now override so the metrics match the pulseDate it is filed under — otherwise the label is misleading even when no duplicate occurs.
Relevant code:
- https://github.com/ansvisor/ansvisor/blob/main/server/src/lib/pulse/engine.js
- https://github.com/ansvisor/ansvisor/blob/main/server/src/lib/pulse/metrics.js
- https://github.com/ansvisor/ansvisor/blob/main/server/src/server.js
Introduced by #654.
Activity
fix(pulse): never send two Daily Pulse emails for the same tracking w…