class Qa::Authorities::LinkedData::SearchQuery

Attributes

access_time_s[R]
filtered_graph[R]
full_graph[R]
language[R]
normalize_time_s[R]
request[R]
request_header[R]
request_id[R]
search_config[R]
subauthority[R]

Public Class Methods

new(search_config) click to toggle source

@param [SearchConfig] search_config The search portion of the config

# File lib/qa/authorities/linked_data/search_query.rb, line 14
def initialize(search_config)
  @search_config = search_config
end

Public Instance Methods

Private Instance Methods

append_data_outside_results(results) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 188
def append_data_outside_results(results)
  return results unless performance_data? || response_header?
  full_results = {}
  full_results[:results] = results
  full_results[:performance] = performance(results) if performance_data?
  full_results[:response_header] = response_header(results) if response_header?
  full_results
end
build_request_header(language:, replacements:, subauth:, context:, performance_data:) click to toggle source

This is providing support for calling build_url with individual parameters instead of the request_header. This is deprecated and will be removed in the next major release.

# File lib/qa/authorities/linked_data/search_query.rb, line 208
def build_request_header(language:, replacements:, subauth:, context:, performance_data:) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  unless language.blank? && replacements.blank? && subauth.blank? && !context && !performance_data
    Qa.deprecation_warning(
      in_msg: 'Qa::Authorities::LinkedData::SearchQuery',
      msg: "individual attributes for options (e.g. replacements, subauth, language) are deprecated; use request_header instead"
    )
  end
  request_header = {}
  request_header[:replacements] = replacements || {}
  request_header[:subauthority] = subauth || nil
  request_header[:language] = language || nil
  request_header[:context] = context
  request_header[:performance_data] = performance_data
  request_header
end
context?() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 109
def context?
  @context == true
end
context_map() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 105
def context_map
  context? ? search_config.context_map : nil
end
convert_result_to_json(result) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 163
def convert_result_to_json(result)
  json_result = {}
  json_result[:uri] = result[:uri].first.to_s
  json_result[:id] = result[:id].any? ? result[:id].first.to_s : ""
  json_result[:label] = full_label(result[:label], result[:altlabel])
  json_result[:context] = result[:context] if context?
  json_result
end
convert_results_to_json(results) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 157
def convert_results_to_json(results)
  json_results = []
  results.each { |result| json_results << convert_result_to_json(result) }
  json_results
end
full_label(label = [], altlabel = []) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 172
def full_label(label = [], altlabel = [])
  label = language_sort_service.new(label, language).sort
  altlabel = language_sort_service.new(altlabel, language).sort
  lbl = wrap_labels(label)
  lbl += " (#{altlabel.join(', ')})" if altlabel.present?
  lbl = lbl.slice(0..95) + '...' if lbl.length > 98
  lbl.strip
end
id_ldpath() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 131
def id_ldpath
  @id_ldpath ||= search_config.results_id_ldpath
end
id_predicate() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 149
def id_predicate
  @id_predicate ||= search_config.results_id_predicate
end
load_graph(url:) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 49
def load_graph(url:)
  access_start_dt = Time.now.utc

  @full_graph = graph_service.load_graph(url: url)

  access_end_dt = Time.now.utc
  @access_time_s = access_end_dt - access_start_dt
  Rails.logger.info("Time to receive data from authority: #{access_time_s}s")
end
map_results() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 73
def map_results
  predicate_map = preds_for_search
  ldpath_map = ldpaths_for_search

  raise Qa::InvalidConfiguration, "do not specify results using both predicates and ldpath in search configuration for linked data authority #{authority_name} (ldpath is preferred)" if predicate_map.present? && ldpath_map.present? # rubocop:disable Layout/LineLength
  raise Qa::InvalidConfiguration, "must specify label_ldpath or label_predicate in search configuration for linked data authority #{authority_name} (label_ldpath is preferred)" unless ldpath_map.key?(:label) || predicate_map.key?(:label) # rubocop:disable Layout/LineLength

  if predicate_map.present?
    Qa.deprecation_warning(
      in_msg: 'Qa::Authorities::LinkedData::SearchQuery',
      msg: "defining results using predicates in search config is deprecated; update to define using ldpaths (authority: #{authority_name})"
    )
  end

  results_mapper_service.map_values(graph: filtered_graph, prefixes: prefixes, ldpath_map: ldpath_map,
                                    predicate_map: predicate_map, sort_key: :sort,
                                    preferred_language: language, context_map: context_map)
end
normalize_results() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 59
def normalize_results
  normalize_start_dt = Time.now.utc

  @filtered_graph = graph_service.filter(graph: full_graph, language: language)
  results = map_results
  json = convert_results_to_json(results)

  normalize_end_dt = Time.now.utc
  @normalize_time_s = normalize_end_dt - normalize_start_dt
  Rails.logger.info("Time to normalize data: #{normalize_time_s}s")
  json = append_data_outside_results(json)
  json
end
performance(results) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 197
def performance(results)
  Qa::LinkedData::PerformanceDataService.performance_data(access_time_s: access_time_s, normalize_time_s: normalize_time_s,
                                                          fetched_data_graph: full_graph, normalized_data: results)
end
performance_data?() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 113
def performance_data?
  @performance_data == true
end
response_header(results) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 202
def response_header(results)
  Qa::LinkedData::ResponseHeaderService.new(results: results, request_header: request_header, config: search_config, graph: full_graph).search_header
end
response_header?() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 117
def response_header?
  @response_header == true
end
sort_ldpath() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 135
def sort_ldpath
  @sort_ldpath ||= search_config.results_sort_ldpath
end
sort_predicate() click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 153
def sort_predicate
  @sort_predicate ||= search_config.results_sort_predicate
end
unpack_request_header(request_header) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 92
def unpack_request_header(request_header)
  @request_header = request_header
  @request = request_header.fetch(:request, nil)
  @request_id = request_header.fetch(:request_id, 'UNASSIGNED')
  @subauthority = request_header.fetch(:subauthority, nil)
  @context = request_header.fetch(:context, false)
  @performance_data = request_header.fetch(:performance_data, false)
  @response_header = request_header.fetch(:response_header, false)
  @language = language_service.preferred_language(user_language: request_header.fetch(:user_language, nil),
                                                  authority_language: search_config.language)
  request_header[:language] = Array(@language)
end
wrap_labels(labels) click to toggle source
# File lib/qa/authorities/linked_data/search_query.rb, line 181
def wrap_labels(labels)
  lbl = "" if labels.nil? || labels.size.zero?
  lbl = labels.join(', ') if labels.size.positive?
  lbl = '[' + lbl + ']' if labels.size > 1
  lbl
end