Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weekly report time range #951

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.3] - Unreleased
## Unreleased

### Fixed
- Fix weekly report time range plausible/analytics#951


## [1.3] - 2021-04-14

### Added
- Stats API [currently in beta] plausible/analytics#679
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible_web/templates/email/weekly_report.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ body {
<!--[if mso]><table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-right: 5px; padding-left: 5px; padding-top: 0px; padding-bottom: 5px; font-family: Arial, sans-serif"><![endif]-->
<div style="color:#555555;font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;line-height:120%;padding-top:0px;padding-right:5px;padding-bottom:5px;padding-left:5px;">
<div style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 12px; line-height: 14px; color: #555555;">
<p style="font-size: 12px; line-height: 24px; text-align: left; margin: 0;"><span style="font-size: 20px;"><strong><span style="line-height: 24px; font-size: 20px;"><%= PlausibleWeb.StatsView.large_number_format(@unique_visitors) %></span></strong></span></p>
<p style="font-size: 12px; line-height: 24px; text-align: left; margin: 0;"><span style="font-size: 20px;"><strong><span id="visitors" style="line-height: 24px; font-size: 20px;"><%= PlausibleWeb.StatsView.large_number_format(@unique_visitors) %></span></strong></span></p>
</div>
</div>
<!--[if mso]></td></tr></table><![endif]-->
Expand Down
4 changes: 3 additions & 1 deletion lib/workers/send_email_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ defmodule Plausible.Workers.SendEmailReport do
@impl Oban.Worker
def perform(%{"interval" => "weekly", "site_id" => site_id}, _job) do
site = Repo.get(Plausible.Site, site_id) |> Repo.preload(:weekly_report)
query = Query.from(site.timezone, %{"period" => "7d"})
today = Timex.now(site.timezone) |> DateTime.to_date()
date = Timex.shift(today, weeks: -1) |> Timex.end_of_week() |> Date.to_iso8601()
query = Query.from(site.timezone, %{"period" => "7d", "date" => date})

for email <- site.weekly_report.recipients do
unsubscribe_link =
Expand Down
39 changes: 39 additions & 0 deletions test/workers/send_email_report_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Plausible.Workers.SendEmailReportTest do
import Plausible.TestUtils
use Plausible.DataCase
use Bamboo.Test
alias Plausible.Workers.SendEmailReport
alias Timex.Timezone

defp perform(args) do
SendEmailReport.new(args) |> Oban.insert!()
Expand All @@ -25,6 +27,43 @@ defmodule Plausible.Workers.SendEmailReportTest do
to: [nil: "[email protected]"]
)
end

test "calculates timezone correctly" do
site = insert(:site, timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])

now = Timex.now(site.timezone)
last_monday = Timex.shift(now, weeks: -1) |> Timex.beginning_of_week()
last_sunday = Timex.shift(now, weeks: -1) |> Timex.end_of_week()
sunday_before_last = Timex.shift(last_monday, minutes: -1)
this_monday = Timex.beginning_of_week(now)

create_pageviews([
# Sunday before last, not counted
%{domain: site.domain, timestamp: Timezone.convert(sunday_before_last, "UTC")},
# Sunday before last, not counted
%{domain: site.domain, timestamp: Timezone.convert(sunday_before_last, "UTC")},
# Last monday, counted
%{domain: site.domain, timestamp: Timezone.convert(last_monday, "UTC")},
# Last sunday, counted
%{domain: site.domain, timestamp: Timezone.convert(last_sunday, "UTC")},
# This monday, not counted
%{domain: site.domain, timestamp: Timezone.convert(this_monday, "UTC")},
# This monday, not counted
%{domain: site.domain, timestamp: Timezone.convert(this_monday, "UTC")}
])

perform(%{"site_id" => site.id, "interval" => "weekly"})

assert_delivered_email_matches(%{
to: [nil: "[email protected]"],
html_body: html_body
})

# Should find 2 visiors
assert html_body =~
~s(<span id="visitors" style="line-height: 24px; font-size: 20px;">2</span>)
end
end

describe "monthly_reports" do
Expand Down