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

pulibrary / pdc_discovery / 5af13607-7741-41ac-b110-c94c8a3ee0f0

pending completion
5af13607-7741-41ac-b110-c94c8a3ee0f0

Pull #441

circleci

hectorcorrea
Better error handling
Pull Request #441: Indexing to a new collection

99 of 99 new or added lines in 3 files covered. (100.0%)

1816 of 2220 relevant lines covered (81.8%)

95.45 hits per line

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

59.26
/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
  # Fetches pageview counts from Plausible for a given document id.
8
  def self.pageviews(document_id)
1✔
9
    return 'X' if ENV['PLAUSIBLE_KEY'].nil?
×
10

11
    c = PlausibleApi::Client.new(Rails.configuration.pdc_discovery.plausible_site_id, ENV['PLAUSIBLE_KEY'])
×
12
    page = "/discovery/catalog/#{document_id}"
×
13
    response = c.aggregate({ date: "2021-01-01,#{Time.zone.today.strftime('%Y-%m-%d')}", metrics: 'visitors,pageviews', filters: "event:page==#{page}" })
×
14
    response["pageviews"]["value"]
×
15
  rescue => e
16
    Rails.logger.error "PLAUSIBLE ERROR: (Pageviews for document: #{document_id}) #{e.message}"
×
17
    Honeybadger.notify(e.message)
×
18
    0
×
19
  end
20

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

34
    site_id = Rails.configuration.pdc_discovery.plausible_site_id
1✔
35
    page = "/discovery/catalog/#{document_id}"
1✔
36
    url = "#{PLAUSIBLE_API_URL}/stats/breakdown?site_id=#{site_id}&property=event:props:filename&filters=event:page==#{page}&metrics=visitors,pageviews"
1✔
37
    authorization = "Bearer #{ENV['PLAUSIBLE_KEY']}"
1✔
38
    response = HTTParty.get(url, headers: { 'Authorization' => authorization })
1✔
39
    total_downloads = 0
1✔
40
    response["results"].each do |result|
1✔
41
      next if result["filename"] == "(none)" # Skip old test data
3✔
42
      total_downloads += result["visitors"]
2✔
43
    end
44
    total_downloads
1✔
45
  rescue => e
46
    Rails.logger.error "PLAUSIBLE ERROR: (Downloads for document: #{document_id}) #{e.message}"
×
47
    Honeybadger.notify(e.message)
×
48
    0
×
49
  end
50
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

© 2025 Coveralls, Inc