-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 6126-deactivated-leases
- Loading branch information
Showing
15 changed files
with
296 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/search_builders/hyrax/valkyrie_abstract_type_relation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hyrax | ||
class ValkyrieAbstractTypeRelation | ||
def initialize(allowable_types: nil, _opts: {}) | ||
@allowable_types = allowable_types | ||
end | ||
|
||
def allowable_types | ||
@allowable_types.present? || | ||
raise(NotImplementedException, "Implement allowable_types in a subclass") | ||
end | ||
|
||
def equivalent_class?(klass) | ||
allowable_types.include?(klass) | ||
end | ||
|
||
def count | ||
Hyrax.query_service.custom_queries.find_count_by(models: allowable_types) | ||
end | ||
|
||
def where(hash) | ||
Hyrax.query_service.find_references_by(resource: hash.values.first, property: hash.keys.first) | ||
end | ||
|
||
def ==(other) | ||
case other | ||
when Relation | ||
other.where_values == where_values | ||
when Array | ||
to_a == other | ||
end | ||
end | ||
|
||
delegate :inspect, to: :to_a | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hyrax | ||
class ValkyrieWorkRelation < ValkyrieAbstractTypeRelation | ||
def allowable_types | ||
Hyrax.config.curation_concerns | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module CustomQueries | ||
## | ||
# @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries | ||
class FindByDateRange | ||
def self.queries | ||
[:find_by_date_range] | ||
end | ||
|
||
def initialize(query_service:) | ||
@query_service = query_service | ||
end | ||
|
||
attr_reader :query_service | ||
delegate :resource_factory, to: :query_service | ||
delegate :orm_class, to: :resource_factory | ||
|
||
## | ||
# @note this is an unoptimized default implementation of this custom | ||
# query. it's Hyrax's policy to provide such implementations of custom | ||
# queries in use for cross-compatibility of Valkyrie query services. | ||
# it's advisable to provide an optimized query for the specific adapter. | ||
# | ||
# @param models [Array] | ||
# @param start_datetime [DateTime] | ||
# @param end_datetime [DateTime] | ||
def find_by_date_range(start_datetime:, end_datetime: nil, models: nil) | ||
end_datetime = 1.second.since(Time.zone.now) if end_datetime.blank? | ||
if models.present? | ||
query_service.run_query(find_models_by_date_range_query, start_datetime.to_s, end_datetime.to_s, models) | ||
else | ||
query_service.run_query(find_by_date_range_query, start_datetime.to_s, end_datetime.to_s) | ||
end | ||
end | ||
|
||
def find_models_by_date_range_query | ||
<<-SQL | ||
SELECT * FROM orm_resources | ||
WHERE created_at >= ? | ||
AND created_at <= ? | ||
AND internal_resource IN (?); | ||
SQL | ||
end | ||
|
||
def find_by_date_range_query | ||
<<-SQL | ||
SELECT * FROM orm_resources | ||
WHERE created_at >= ? | ||
AND created_at <= ?; | ||
SQL | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module CustomQueries | ||
## | ||
# @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries | ||
class FindCountBy | ||
def self.queries | ||
[:find_count_by] | ||
end | ||
|
||
def initialize(query_service:) | ||
@query_service = query_service | ||
end | ||
|
||
attr_reader :query_service | ||
delegate :resource_factory, to: :query_service | ||
delegate :orm_class, to: :resource_factory | ||
|
||
## | ||
# @note this is an unoptimized default implementation of this custom | ||
# query. it's Hyrax's policy to provide such implementations of custom | ||
# queries in use for cross-compatibility of Valkyrie query services. | ||
# it's advisable to provide an optimized query for the specific adapter. | ||
# | ||
# @param hash [Hash] the hash representation of the query | ||
def find_count_by(hash = {}, models: nil) | ||
return nil if models.empty? && hash.blank? | ||
|
||
internal_array = ["{ #{hash.map { |k, v| "\"#{k}\": #{v}" }.join(', ')} }"] if hash.present? | ||
if models.empty? | ||
query_service.orm_class.count_by_sql(([find_count_by_properties_query] + internal_array)) | ||
elsif hash.blank? | ||
query_service.orm_class.count_by_sql([find_count_by_models_query] + [models]) | ||
else | ||
query_service.orm_class.count_by_sql(([find_count_by_properties_and_models_query] + internal_array + [models])) | ||
end | ||
end | ||
|
||
def find_count_by_properties_and_models_query | ||
<<-SQL | ||
SELECT count(*) FROM orm_resources | ||
WHERE metadata @> ? | ||
AND internal_resource IN (?); | ||
SQL | ||
end | ||
|
||
def find_count_by_models_query | ||
<<-SQL | ||
SELECT count(*) FROM orm_resources | ||
WHERE internal_resource IN (?); | ||
SQL | ||
end | ||
|
||
def find_count_by_properties_query | ||
<<-SQL | ||
SELECT count(*) FROM orm_resources | ||
WHERE metadata @> ?; | ||
SQL | ||
end | ||
end | ||
end | ||
end |
59 changes: 59 additions & 0 deletions
59
app/services/hyrax/custom_queries/find_models_by_access.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module CustomQueries | ||
## | ||
# @see https://github.com/samvera/valkyrie/wiki/Queries#custom-queries | ||
class FindModelsByAccess | ||
def self.queries | ||
[:find_models_by_access] | ||
end | ||
|
||
def initialize(query_service:) | ||
@query_service = query_service | ||
end | ||
|
||
attr_reader :query_service | ||
delegate :resource_factory, to: :query_service | ||
delegate :orm_class, to: :resource_factory | ||
|
||
## | ||
# @note this is an unoptimized default implementation of this custom | ||
# query. it's Hyrax's policy to provide such implementations of custom | ||
# queries in use for cross-compatibility of Valkyrie query services. | ||
# it's advisable to provide an optimized query for the specific adapter. | ||
# | ||
# @param model [Class] | ||
# @param ids [Enumerable<#to_s>, Symbol] | ||
# | ||
def find_models_by_access(mode:, models: nil, agent:, group: nil) | ||
agent = "group/#{agent}" if group.present? | ||
internal_array = "{\"permissions\": [{\"mode\": \"#{mode}\", \"agent\": \"#{agent}\"}]}" | ||
if models.present? | ||
query_service.run_query(find_models_by_access_query, internal_array, models) | ||
else | ||
query_service.run_query(find_by_access_query, internal_array) | ||
end | ||
end | ||
|
||
def find_models_by_access_query | ||
<<-SQL | ||
SELECT * FROM orm_resources | ||
WHERE id IN ( | ||
SELECT uuid(metadata::json#>'{access_to,0}'->>'id') FROM orm_resources | ||
WHERE metadata @> ? | ||
) AND internal_resource IN (?); | ||
SQL | ||
end | ||
|
||
def find_by_access_query | ||
<<-SQL | ||
SELECT * FROM orm_resources | ||
WHERE id IN ( | ||
SELECT uuid(metadata::json#>'{access_to,0}'->>'id') FROM orm_resources | ||
WHERE metadata @> ? | ||
); | ||
SQL | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
module Hyrax | ||
module Statistics | ||
class ValkyrieQueryService < QueryService | ||
# query to find works created during the time range | ||
# @param [DateTime] start_datetime starting date time for range query | ||
# @param [DateTime] end_datetime ending date time for range query | ||
def find_by_date_created(start_datetime, end_datetime = nil) | ||
return [] if start_datetime.blank? # no date just return nothing | ||
return super unless non_wings_valkyire? | ||
|
||
Hyrax.query_service.custom_queries.find_by_date_range(start_datetime: start_datetime, | ||
end_datetime: end_datetime, | ||
models: relation.allowable_types).to_a | ||
end | ||
|
||
def find_registered_in_date_range(start_datetime, end_datetime = nil) | ||
return super unless non_wings_valkyire? | ||
find_by_date_created(start_datetime, end_datetime) & where_registered.to_a | ||
end | ||
|
||
def find_public_in_date_range(start_datetime, end_datetime = nil) | ||
return super unless non_wings_valkyire? | ||
find_by_date_created(start_datetime, end_datetime) & where_public.to_a | ||
end | ||
|
||
def relation | ||
return super unless non_wings_valkyire? | ||
Hyrax::ValkyrieWorkRelation.new | ||
end | ||
|
||
private | ||
|
||
def where_access_is(access_level) | ||
# returns all works where the access level is public | ||
return super unless non_wings_valkyire? | ||
|
||
Hyrax.custom_queries.find_models_by_access(mode: 'read', | ||
models: relation.allowable_types, | ||
group: true, | ||
agent: access_level) | ||
end | ||
|
||
def non_wings_valkyire? | ||
Hyrax.config.use_valkyrie? && (!defined?(Wings) || (defined?(Wings) && Hyrax.config.disable_wings)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.