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

[Hold] Report on cocina contributors and form where there is a missing value #4542

Closed
wants to merge 4 commits into from
Closed
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
59 changes: 59 additions & 0 deletions app/reports/contributor_without_name_collections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

# Report on contributors with no name value that have role information

# Invoke via:
# bin/rails r -e production "ContributorWithoutNameCollections.report"
class ContributorWithoutNameCollections
# 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.
# NOTE: I never figured out why the following line didn't work as expected
# JSONB_PATH = 'strict $.**.contributor[*] ? (!(exists(@.name.**.value))) ? ((exists(@.role.code)) || (exists(@.role.value)) || (exists(@.role.uri)))'
# NOTE: I ran each of the following four lines as separate reports
JSONB_PATH = 'strict $.contributor[*] ? ( !exists(@.name.**.value) )' # top level
# JSONB_PATH = 'strict $.**.event.contributor[*] ? ( !exists(@.name.**.value) )'
# JSONB_PATH = 'strict $.**.adminMetadata.contributor[*] ? ( !exists(@.name.**.value) )'
# JSONB_PATH = 'strict $.**.relatedResource.contributor[*] ? ( !exists(@.name.**.value) )'
SQL = <<~SQL.squish.freeze
SELECT collections.external_identifier as collection_druid,
desc_value->'role'->'code' as role_code,
desc_value->'role'->'value' as role_value,
desc_value->'role'->'uri' as role_uri,
desc_value->'value' as name_value,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catalog_record_id
FROM "collections",
jsonb_path_query(collections.description, '#{JSONB_PATH}') desc_value
WHERE
jsonb_path_exists(collections.description, '#{JSONB_PATH}')
SQL

def self.report
puts "collection_druid,catalog_record_id,collection_name,name_value,role_code,role_value,role_uri\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['catalog_record_id'],
"\"#{collection_name}\"",
row['name_value'],
row['role_code'],
row['role_value'],
row['role_uri']
].join(',')
end
end
end
65 changes: 65 additions & 0 deletions app/reports/contributor_without_name_dros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

# Report on contributors with no name value that have role information

# Invoke via:
# bin/rails r -e production "ContributorWithoutNameDros.report"
class ContributorWithoutNameDros
# 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.
# NOTE: I never figured out why the following three lines didn't work as expected
# JSONB_PATH = 'strict $.**.contributor[*] ? (!(exists(@.name.**.value))) ? ((exists(@.role.code)) || (exists(@.role.value)) || (exists(@.role.uri)))'
# JSONB_PATH = 'strict $.**.contributor[*] ? ( !exists(@.name.**.value) && ( exists(@.role.code) || exists(@.role.value) || exists(@.role.uri) ) )'
# JSONB_PATH = 'strict $.**.contributor[*] ? ( ( exists(@.role.code) ) && ( !exists(@.name.**.value) ) )'
# NOTE: I ended up doing the below because the above was not working as expected
# JSONB_PATH = 'strict $.**.contributor[*] ? ( !exists(@.name.**.value) )' # works, but not so useful
# NOTE: so I just ran each of the following four lines as separate reports
# JSONB_PATH = 'strict $.contributor[*] ? ( !exists(@.name.**.value) )' # top level
# JSONB_PATH = 'strict $.**.event.contributor[*] ? ( !exists(@.name.**.value) )'
JSONB_PATH = 'strict $.**.adminMetadata.contributor[*] ? ( !exists(@.name.**.value) )'
# JSONB_PATH = 'strict $.**.relatedResource.contributor[*] ? ( !exists(@.name.**.value) )'
SQL = <<~SQL.squish.freeze
SELECT dros.external_identifier as item_druid,
desc_value->'role'->'code' as role_code,
desc_value->'role'->'value' as role_value,
desc_value->'role'->'uri' as role_uri,
desc_value->'value' as name_value,
jsonb_path_query(dros.structural, '$.isMemberOf') ->> 0 as collection_druid,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catalog_record_id
FROM "dros",
jsonb_path_query(dros.description, '#{JSONB_PATH}') desc_value
WHERE
jsonb_path_exists(dros.description, '#{JSONB_PATH}')
SQL

def self.report
puts "item_druid,catalog_record_id,collection_druid,collection_name,name_value,role_code,role_value,role_uri\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

[
row['item_druid'],
row['catalog_record_id'],
collection_druid,
"\"#{collection_name}\"",
row['name_value'],
row['role_code'],
row['role_value'],
row['role_uri']
].join(',')
end
end
end
54 changes: 54 additions & 0 deletions app/reports/form_without_value_collections.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

# Report on form with no value that have source and/or type information

# Invoke via:
# bin/rails r -e production "FormWithoutValueCollections.report"
class FormWithoutValueCollections
# 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 $.**.form.**.value[*] ? (!(exists(@.value))) ? ((exists(@.type)) || (exists(@.source.code)) || (exists(@.source.value)) || (exists(@.source.uri)))'
SQL = <<~SQL.squish.freeze
SELECT collections.external_identifier as collection_druid,
desc_value->'value' as form_value,
desc_value->'type' as form_type,
desc_value->'source'->'code' as source_code,
desc_value->'source'->'value' as source_value,
desc_value->'source'->'uri' as source_uri,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catalog_record_id
FROM "collections",
jsonb_path_query(collections.description, '#{JSONB_PATH}') desc_value
WHERE
jsonb_path_exists(collections.description, '#{JSONB_PATH}')
SQL

def self.report
puts "collection_druid,catalog_record_id,collection_name,form_value,form_type,source_code,source_value,source_uri\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_name = Collection.find_by(external_identifier: collection_druid)&.label

[
collection_druid,
row['catalog_record_id'],
"\"#{collection_name}\"",
row['form_value'],
row['form_type'],
row['source_code'],
row['source_value'],
row['source_uri']
].join(',')
end
end
end
57 changes: 57 additions & 0 deletions app/reports/form_without_value_dros.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

# Report on form with no value that have source and/or type information

# Invoke via:
# bin/rails r -e production "FormWithoutValueDros.report"
class FormWithoutValueDros
# 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 $.**.form.**.value[*] ? (!(exists(@.value))) ? ((exists(@.type)) || (exists(@.source.code)) || (exists(@.source.value)) || (exists(@.source.uri)))'
SQL = <<~SQL.squish.freeze
SELECT dros.external_identifier as item_druid,
desc_value->'value' as form_value,
desc_value->'type' as form_type,
desc_value->'source'->'code' as source_code,
desc_value->'source'->'value' as source_value,
desc_value->'source'->'uri' as source_uri,
jsonb_path_query(dros.structural, '$.isMemberOf') ->> 0 as collection_druid,
jsonb_path_query(identification, '$.catalogLinks[*] ? (@.catalog == "symphony").catalogRecordId') ->> 0 as catalog_record_id
FROM "dros",
jsonb_path_query(dros.description, '#{JSONB_PATH}') desc_value
WHERE
jsonb_path_exists(dros.description, '#{JSONB_PATH}')
SQL

def self.report
puts "item_druid,catalog_record_id,collection_druid,collection_name,form_value,form_type,source_code,source_value,source_uri\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

[
row['item_druid'],
row['catalog_record_id'],
collection_druid,
"\"#{collection_name}\"",
row['form_value'],
row['form_type'],
row['source_code'],
row['source_value'],
row['source_uri']
].join(',')
end
end
end