• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_discovery / 5c8f78ec-7ed2-40cf-b5a0-80a009270825

17 Jun 2026 07:04PM UTC coverage: 78.246% (-18.2%) from 96.464%
5c8f78ec-7ed2-40cf-b5a0-80a009270825

Pull #951

circleci

leefaisonr
Additional work for withdrawn works landing page
Co-authored-by: Carolyn Cole <carolyncole@users.noreply.github.com>
Pull Request #951: adding withdrawn json with updated metadata

2 of 4 new or added lines in 2 files covered. (50.0%)

150 existing lines in 5 files now uncovered.

2435 of 3112 relevant lines covered (78.25%)

4.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.79
/app/models/plausible.rb
1
# frozen_string_literal: true
2
require 'plausible_api'
1✔
3

4
class Plausible
1✔
5
  PLAUSIBLE_API_URL = 'https://plausible.io/api/v1'
1✔
6

7
  def self.date_period
1✔
8
    "2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}"
3✔
9
  end
10

11
  # Fetches pageview counts from Plausible for a given document id.
12
  def self.pageviews(document_id)
1✔
UNCOV
13
    return 'X' if ENV['PLAUSIBLE_KEY'].nil?
×
14

15
    c = PlausibleApi::Client.new(Rails.configuration.pdc_discovery.plausible_site_id, ENV['PLAUSIBLE_KEY'])
×
16
    page = "/discovery/catalog/#{document_id}"
×
17
    response = c.aggregate({ date: date_period, metrics: 'visitors,pageviews', filters: "event:page==#{page}" })
×
18
    response["pageviews"]["value"]
×
19
  rescue => e
20
    Rails.logger.error "PLAUSIBLE ERROR: (Pageviews for document: #{document_id}) #{e.message}"
×
21
    Honeybadger.notify(e.message)
×
22
    0
×
23
  end
24

25
  # Fetches download counts from Plausible for a given document id.
26
  #
27
  # We store downloads by file name for each document id (page=/catalog/123). We also store downloads from
28
  # Globus in a reserved filename ("globus-download") as part of this data.
29
  #
30
  # This method fetches the breakdown for all downloads by file name for a given document_id and aggregates them.
31
  # We could provide stats by individual file and/or for specific time periods (e.g. period=6mo) in the future.
32
  #
33
  # Notice that this methods goes straight to the Plausible API (without using the PlausbleApi gem)
34
  # because the gem does not support yet the ability to fetch this kind of information.
35
  def self.downloads(document_id)
1✔
36
    return 'X' if ENV['PLAUSIBLE_KEY'].nil?
3✔
37

38
    # Plausible API breakdown API: https://plausible.io/docs/stats-api#get-apiv1statsbreakdown
39
    # Notice that the Plausible API uses "==" to filter: https://plausible.io/docs/stats-api#filtering
40
    # Time periods: https://plausible.io/docs/stats-api#time-periods
41
    site_id = Rails.configuration.pdc_discovery.plausible_site_id
3✔
42
    property = "event:props:filename"
3✔
43
    page = "/discovery/catalog/#{document_id}"
3✔
44
    filters = "event:page==#{page}"
3✔
45
    metrics = "visitors,pageviews"
3✔
46
    period = "custom"
3✔
47
    url = "#{PLAUSIBLE_API_URL}/stats/breakdown?site_id=#{site_id}&property=#{property}&filters=#{filters}&metrics=#{metrics}&period=#{period}&date=#{date_period}"
3✔
48
    authorization = "Bearer #{ENV['PLAUSIBLE_KEY']}"
3✔
49
    response = HTTParty.get(url, headers: { 'Authorization' => authorization })
3✔
50

51
    # retry if the response is an error
52
    if response.code != 200
3✔
53
      Rails.logger.error "PLAUSIBLE ERROR: #{response}"
2✔
54
      sleep(1.0)
2✔
55
      response = HTTParty.get(url, headers: { 'Authorization' => authorization })
2✔
56
    end
57

58
    total_downloads = 0
3✔
59

60
    # retry if the response is an error
61
    if response.code != 200
3✔
62
      Rails.logger.error "PLAUSIBLE ERROR after retry: #{response}"
1✔
63
    else
64
      response["results"].each do |result|
2✔
65
        next if result["filename"] == "(none)" # Skip old test data
6✔
66
        total_downloads += result["visitors"]
4✔
67
      end
68
    end
69

70
    total_downloads
3✔
71
  rescue => e
72
    Rails.logger.error "PLAUSIBLE ERROR: (Downloads for document: #{document_id}) #{e.message}"
×
73
    Honeybadger.notify(e.message)
×
74
    0
×
75
  end
76
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc