"Previous period" with "Match day of week" picks the later weekday, so the comparison overlaps the selected range
维护者通常 2 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 lib/plausible/stats/comparisons.ex 中的 shift_to_nearest/3 入手,并阅读 test/plausible/stats/comparisons_test.exs 中现有的星期几匹配用例。报告指出带符号的日期差是原因,并建议比较绝对距离。添加一个较早的匹配星期几距离更近的测试用例,然后运行 comparisons 测试;完成标准是比较范围仍在所选时段之前,且现有用例仍然通过。
由索引模型根据 Issue 内容生成。
描述
Watch in BugReels (the date menu opens at 0:04, Compare is clicked at 0:13, and every top stat shows "24 Sep - 5 Oct" under "1 Oct - 12 Oct" from 0:13)
Past Issues Searched
- I have searched open and closed issues to make sure that the bug has not yet been reported
Issue is a Bug Report
- This is a bug report and not a feature request, nor asking for self-hosted support
Using official Plausible Cloud hosting or self-hosting?
Plausible Cloud from plausible.io
Describe the bug
With Compare set to "Previous period" and "Match day of week" (the default), the comparison range is moved to the next matching weekday even when the previous one is closer. When the period length leaves a remainder of 4 to 6 days after dividing by 7, the comparison moves forward by 4 to 6 days and overlaps the selected range.
Reproduced on the public demo dashboard https://plausible.io/plausible.io on 2026-10-26. The code is the same on master (57f734e).
Steps:
- Open https://plausible.io/plausible.io?period=custom&from=2026-10-01&to=2026-10-12 (Thu 1 Oct to Mon 12 Oct).
- Open the date menu and click "Compare". Leave "Previous period" and "Match day of week" as they are.
- Read the date lines under the top stats.
Actual: every top stat compares "1 Oct - 12 Oct" with "24 Sep - 5 Oct", so 1 to 5 Oct is counted in both periods. The query API returns the same range:
POST /api/stats/plausible.io/query
{"date_range":["2026-10-01","2026-10-12"],"metrics":["visitors"],"include":{"compare":"previous_period","compare_match_day_of_week":true}}
-> "comparison_date_range":["2026-09-24T00:00:00+00:00","2026-10-05T23:59:59+00:00"]
Built-in periods hit it on some days. On 2026-10-26, "Month to Date" compares 1 Oct - 26 Oct with 10 Sep - 5 Oct, and "Year to Date" compares 1 Jan - 26 Oct with 13 Mar 2025 - 5 Jan 2026. The dashboard also sends compare: previous_period with compare_match_day_of_week: true for the top stats when Compare is off, so the percent change arrows use this range too. In the recording, Unique visitors shows "309k" and "25%" before Compare is turned on, which is 309k against the 247k of 24 Sep - 5 Oct.
Expected behavior
The unshifted previous period is Sat 19 Sep - Wed 30 Sep. The nearest Thursday to 19 Sep is 17 Sep (2 days back, against 5 days forward), so the comparison should be 17 Sep - 28 Sep. The docs describe Previous period as "the period immediately before your selected date range". With the same rule, Month to Date on 2026-10-26 would compare with 3 Sep - 28 Sep and Year to Date with 6 Mar 2025 - 29 Dec 2025.
Cause
shift_to_nearest/3 sorts the two candidate dates by the signed Date.diff:
Date.diff(date, next) is always negative and Date.diff(date, previous) is always positive, so the next occurrence always sorts first. For 19 Sep and a Thursday target it compares -5 (24 Sep) with 2 (17 Sep) and picks 24 Sep. This sort has been there since #2822, which introduced the feature to shift the comparison "to start on the same day of the week as the source query".
Sorting by abs(Date.diff(date, &1)) would pick the nearest day. I checked this in a Python model of previous_period, year_over_year and shift_to_nearest, not by running the Elixir test suite. The model reproduces the current ranges above, gives the expected ranges with the change, and gives unchanged results for the match-day-of-week cases in test/plausible/stats/comparisons_test.exs (March 2023 with the rejected start date, February 2023, January 2023, and year over year to 1 March 2023). Those tests only cover cases where the later weekday is also the nearest one.
Screenshots
The replay link above shows the dashboard.
Environment
- OS: Linux
- Browser: Chrome
- Browser Version: 151
This report was drafted with Claude Code (Claude Opus 5.5). The bug was reproduced on the demo, and the cause was checked against the linked source and a Python model of the function.
- 主要语言
- Elixir
- 星标
- 29.7k
- 派生
- 1.9k
- 平均合并
- 3 天 22 小时
- 30 天内合并 PR
- 50
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
plausible/analytics 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 2 天内回复
-
ObanErrorReporter purges imported data when the cleanup worker crashes — silent loss of completed CSV imports可能已有人在做 @EmmS21 于 95 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
plausible/analytics#5201 · 1 条评论 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
plausible/analytics#5200 · 2 条评论 ·
维护者通常 2 天内回复
-
[Tracker] Scroll-depth tracking still forces layout on large pages after #6174可能已有人在做 @trotzig 于 21 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 18/100
维护者通常 2 天内回复
查看 plausible/analytics 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
taskforcesh/bullmq#5038 ·
维护者通常 1 天内回复
-
pip: same package pinned twice with different casing in pyproject.toml, only the first pin is updated可能已有人在做 @v-abhishekbhaskar 今天认领。 未关闭L: python L: ruby:bundler T: bug 🐞
难度 2/5 1-3 小时 新手友好度 69/100
dependabot/dependabot-core#16756 · 已指派 1 人 ·
维护者通常 1 天内回复
-
d: medium
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1 小时以内 新手友好度 85/100