Skip to content

Commit

Permalink
Adding a second try for plausible downloads (#722)
Browse files Browse the repository at this point in the history
fixes #594
  • Loading branch information
carolyncole authored Nov 19, 2024
1 parent 06b2e0e commit 02e7896
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
21 changes: 18 additions & 3 deletions app/models/plausible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,26 @@ def self.downloads(document_id)
url = "#{PLAUSIBLE_API_URL}/stats/breakdown?site_id=#{site_id}&property=#{property}&filters=#{filters}&metrics=#{metrics}&period=#{period}&date=#{date_period}"
authorization = "Bearer #{ENV['PLAUSIBLE_KEY']}"
response = HTTParty.get(url, headers: { 'Authorization' => authorization })

# retry if the response is an error
if response.code != 200
Rails.logger.error "PLAUSIBLE ERROR: #{response}"
sleep(1.0)
response = HTTParty.get(url, headers: { 'Authorization' => authorization })
end

total_downloads = 0
response["results"].each do |result|
next if result["filename"] == "(none)" # Skip old test data
total_downloads += result["visitors"]

# retry if the response is an error
if response.code != 200
Rails.logger.error "PLAUSIBLE ERROR after retry: #{response}"
else
response["results"].each do |result|
next if result["filename"] == "(none)" # Skip old test data
total_downloads += result["visitors"]
end
end

total_downloads
rescue => e
Rails.logger.error "PLAUSIBLE ERROR: (Downloads for document: #{document_id}) #{e.message}"
Expand Down
66 changes: 64 additions & 2 deletions spec/models/plausible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

# rubocop:disable Layout/LineLength
RSpec.describe Plausible do
before do
let(:url) do
plausible = "https://plausible.io/api/v1"
date_period = "2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}"
url = "#{plausible}/stats/breakdown?filters=event:page==/discovery/catalog/88163&metrics=visitors,pageviews&property=event:props:filename&site_id=pdc-discovery-staging.princeton.edu&period=custom&date=#{date_period}"
"#{plausible}/stats/breakdown?filters=event:page==/discovery/catalog/88163&metrics=visitors,pageviews&property=event:props:filename&site_id=pdc-discovery-staging.princeton.edu&period=custom&date=#{date_period}"
end

before do
stub_request(:get, url)
.with(
headers: {
Expand All @@ -30,6 +33,65 @@
expect(described_class.downloads('88163')).to eq 6
ENV['PLAUSIBLE_KEY'] = nil
end

context "a lasting error" do
before do
stub_request(:get, url)
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer no-key-for-testing',
'User-Agent' => 'Ruby'
}
)
.to_return(
status: 500,
body: { "error" => "an error" }.to_json,
headers: { 'content-type' => 'application/json; charset=utf-8' }
)
end

it "returns zero" do
allow(Honeybadger).to receive(:notify)
ENV['PLAUSIBLE_KEY'] = 'no-key-for-testing'
expect(described_class.downloads('88163')).to eq 0
expect(Honeybadger).not_to have_received(:notify)
ENV['PLAUSIBLE_KEY'] = nil
end
end

context "a intermittent error" do
before do
stub_request(:get, url)
.with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer no-key-for-testing',
'User-Agent' => 'Ruby'
}
)
.to_return({
status: 500,
body: { "error" => "an error" }.to_json,
headers: { 'content-type' => 'application/json; charset=utf-8' }
},
{
status: 200,
body: file_fixture("plausible_breakdown_catalog_88163.json"),
headers: { 'content-type' => 'application/json; charset=utf-8' }
})
end

it "rolls up downloads" do
allow(Honeybadger).to receive(:notify)
ENV['PLAUSIBLE_KEY'] = 'no-key-for-testing'
expect(described_class.downloads('88163')).to eq 6
expect(Honeybadger).not_to have_received(:notify)
ENV['PLAUSIBLE_KEY'] = nil
end
end
end
end
# rubocop:enable Layout/LineLength

0 comments on commit 02e7896

Please sign in to comment.