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

add reports to detect existence of cocina property in dros and collections #4245

Merged
merged 1 commit into from
Sep 28, 2022
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
44 changes: 44 additions & 0 deletions app/reports/property_existence_collections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# Report collection objects with occurences of a property.

# Invoke via:
# bin/rails r -e production "PropertyExistenceCollections.report"
class PropertyExistenceCollections
# 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
#
# > The .** accessor can lead to surprising results when using the lax mode.
# > ... This happens because the .** accessor selects both the segments array
# > and each of its elements, while the .HR accessor automatically unwraps
# > arrays when using the lax mode. To avoid surprising results, we recommend
# > using the .** accessor only in the strict mode.
JSONB_PATH = 'strict $.**.groupedValue ? (@.size() > 0)' # when property is array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... report on any non-empty grouped values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... report on any non-empty grouped values?

yes - that's what this report is doing - reporting on non-empty occurrences of groupedValue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome.

# JSONB_PATH = 'strict $.**.contributor.type' # when property is a string - maybe keep size check to avoid empty values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cruft?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no - when property is an array vs when property is a string

SQL = <<~SQL.squish.freeze
SELECT external_identifier as collection_druid,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catkey
FROM "collections" WHERE
jsonb_path_exists(collections.description, '#{JSONB_PATH}')
SQL

def self.report
puts "collection_druid,catkey,collection_name\n"
rows(SQL).compact.each { |row| puts row }
end

def self.rows(sql_query)
sql_result_rows = ActiveRecord::Base.connection.execute(sql_query).to_a

sql_result_rows.map do |row|
collection_druid = row['collection_druid']
collection_name = Collection.find_by(external_identifier: collection_druid)&.label

[
collection_druid,
row['catkey'],
"\"#{collection_name}\""
].join(',')
end
end
end
46 changes: 46 additions & 0 deletions app/reports/property_existence_dros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# Report dro objects with occurences of a property.

# Invoke via:
# bin/rails r -e production "PropertyExistenceDros.report"
class PropertyExistenceDros
# 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
#
# > The .** accessor can lead to surprising results when using the lax mode.
# > ... This happens because the .** accessor selects both the segments array
# > and each of its elements, while the .HR accessor automatically unwraps
# > arrays when using the lax mode. To avoid surprising results, we recommend
# > using the .** accessor only in the strict mode.
JSONB_PATH = 'strict $.**.contributor.parallelContributor ? (@.size() > 0)' # when property is array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to square the names of the reports of this PR with these JSON paths. This seems narrower than "report on occurrences of a property." Intended or oversight or a misunderstanding on my part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a generic name for the report so Arcadia knows what the report does in the abstract so she will use it when she wants to do a run.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk

# JSONB_PATH = 'strict $.**.contributor.type' # when property is a string - maybe keep size check to avoid empty values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cruft?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above: one for array property, one for string.

SQL = <<~SQL.squish.freeze
SELECT external_identifier as item_druid,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catkey,
jsonb_path_query(structural, '$.isMemberOf') ->> 0 as collection_id
FROM "dros" WHERE
jsonb_path_exists(dros.description, '#{JSONB_PATH}')
SQL

def self.report
puts "item_druid,catkey,collection_druid,collection_name\n"
rows(SQL).compact.each { |row| puts row }
end

def self.rows(sql_query)
sql_result_rows = ActiveRecord::Base.connection.execute(sql_query).to_a

sql_result_rows.map do |row|
collection_druid = row['collection_id']
collection_name = Collection.find_by(external_identifier: collection_druid)&.label

[
row['item_druid'],
row['catkey'],
collection_druid,
"\"#{collection_name}\""
].join(',')
end
end
end