Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

"Previous period" with "Match day of week" picks the later weekday, so the comparison overlaps the selected range

未关闭 适合新手
#6,802 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
elixir
领域
analytics, backend

调研方向

从 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:

  1. Open https://plausible.io/plausible.io?period=custom&from=2026-10-01&to=2026-10-12 (Thu 1 Oct to Mon 12 Oct).
  2. Open the date menu and click "Compare". Leave "Previous period" and "Match day of week" as they are.
  3. 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:

https://github.com/plausible/analytics/blob/57f734e3dbc5e96d868766dc1e61064984ee3a80/lib/plausible/stats/comparisons.ex#L241-L250

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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

plausible/analytics 的其他 Issue

查看 plausible/analytics 的全部 Issue

相似的 Issue

更多 Elixir Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。