Skip to content

Commit

Permalink
Merge pull request #5247 from sul-dlss/example-count-report
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Jan 10, 2025
2 parents 3e900cc + 32beb75 commit fba0269
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/reports/related_resource_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

# Invoke via:
# bin/rails r -e production "RelatedResourceTypes.report" > related_resource_types.csv
class RelatedResourceTypes
# NOTE: Prefer strict JSON querying over lax when using the `.**` operator, per
# https://www.postgresql.org/docs/14/functions-json.html#STRICT-AND-LAX-MODES
# relatedResource may be within a relatedResource, so we need to use .**
JSON_PATH = 'strict $.**.relatedResource[*].type'
SQL = <<~SQL.squish.freeze
SELECT jsonb_path_query_array(rov.description, '#{JSON_PATH}') as values
FROM repository_objects AS ro, repository_object_versions AS rov WHERE
ro.head_version_id = rov.id
AND ro.object_type = 'dro'
SQL

def self.report
@types = Hash.new(0) # default value of 0
puts "value,count\n"
rows(SQL)

@types.each do |value, count|
puts "#{value},#{count}"
end
end

def self.rows(sql)
result = ActiveRecord::Base.connection.execute(sql)

result.to_a.map do |row|
JSON.parse(row['values']).each do |value|
@types[value] += 1
end
end
end
end

0 comments on commit fba0269

Please sign in to comment.