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

Move the logic for the default ES and Solr server definitions from server AND client side to just client side #505

Merged
merged 9 commits into from
May 8, 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
6 changes: 6 additions & 0 deletions app/assets/javascripts/controllers/queryParamsHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ angular.module('QuepidApp')
function($scope, $uibModal, flash, caseTryNavSvc) {
var urlsIveSeen = {};

// This method trys to group search urls into names.
// we use this to do some colour grouping in the history view by engine
// and we use it to name the engine. Eventually we will name our engines
// and this can go away.
// There is a known bug that after running the wizard, we have TWO tries, one
// with a null url, and one that is actually from what you defined in the Wizard.
$scope.urlBucket = function(url, numBuckets) {
urlsIveSeen[url] = 0;
var allUrls = Object.keys(urlsIveSeen);
Expand Down
69 changes: 54 additions & 15 deletions app/assets/javascripts/controllers/wizardModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ angular.module('QuepidApp')


// I don't think we need this as we look it up from the Try we create by default on the server side!
$scope.pendingWizardSettings = angular.copy(settingsSvc.defaults.solr);
// Except that it's also defined in settingsSvc. Sigh.
var defaultSettings = settingsSvc.defaults['solr'];

// if we have restarted the wizard, then grab the searchUrl, searchEngine,
// and caseName from the params and override the default values.
// We should pass this stuff in externally, not do it here.
if (angular.isDefined($location.search().searchEngine)){
$scope.pendingWizardSettings.searchEngine = $location.search().searchEngine;
$scope.pendingWizardSettings = angular.copy(defaultSettings);
var quepidStartsWithHttps = $location.protocol() === 'https';
if ($scope.pendingWizardSettings.searchEngine === 'es' ){
$scope.pendingWizardSettings.searchUrl = defaultSettings.searchUrl;
}
if (angular.isDefined($location.search().searchUrl)){
$scope.pendingWizardSettings.searchUrl = $location.search().searchUrl;
else if (quepidStartsWithHttps === true){
$scope.pendingWizardSettings.searchUrl = defaultSettings.secureSearchUrl;
}
if (angular.isDefined($location.search().caseName)){
$scope.pendingWizardSettings.caseName = $location.search().caseName;
else {
$scope.pendingWizardSettings.searchUrl = defaultSettings.insecureSearchUrl;
}

$scope.wizardSettingsModel.settingsId = function() {
Expand All @@ -46,10 +46,19 @@ angular.module('QuepidApp')
$scope.pendingWizardSettings.idField = settings.idField;
$scope.pendingWizardSettings.searchEngine = settings.searchEngine;
$scope.pendingWizardSettings.apiMethod = settings.apiMethod;
$scope.pendingWizardSettings.searchUrl = settings.searchUrl;
$scope.pendingWizardSettings.selectedTry.queryParams = settings.queryParams;
$scope.pendingWizardSettings.queryParams = settings.queryParams;
$scope.pendingWizardSettings.titleField = settings.titleField;
$scope.pendingWizardSettings.urlFormat = settings.urlFormat;
var quepidStartsWithHttps = $location.protocol() === 'https';
if ($scope.pendingWizardSettings.searchEngine === 'es' ){
$scope.pendingWizardSettings.searchUrl = settings.searchUrl;
}
else if (quepidStartsWithHttps === true){
$scope.pendingWizardSettings.searchUrl = settings.secureSearchUrl;
}
else {
$scope.pendingWizardSettings.searchUrl = settings.insecureSearchUrl;
}
$scope.reset();
};

Expand All @@ -60,9 +69,22 @@ angular.module('QuepidApp')
$scope.reset = reset;
$scope.resetUrlValid = resetUrlValid;
$scope.checkTLSForSearchEngineUrl = checkTLSForSearchEngineUrl;
$scope.reset();
$scope.updateSettingsDefaults();
$scope.searchFields = [];

// if we have restarted the wizard, then grab the searchUrl, searchEngine,
// and caseName from the params and override the default values.
// We should pass this stuff in externally, not do it here.
if (angular.isDefined($location.search().searchEngine)){
$scope.pendingWizardSettings.searchEngine = $location.search().searchEngine;
}
if (angular.isDefined($location.search().searchUrl)){
$scope.pendingWizardSettings.searchUrl = $location.search().searchUrl;
}
if (angular.isDefined($location.search().caseName)){
$scope.pendingWizardSettings.caseName = $location.search().caseName;
}

$scope.extractSolrConfigApiUrl = function(searchUrl) {
return searchUrl.substring(0, searchUrl.lastIndexOf('/')) + '/config';
};
Expand Down Expand Up @@ -166,9 +188,20 @@ angular.module('QuepidApp')
// if the URL is still set as the default
var searchEngine = $scope.pendingWizardSettings.searchEngine;
var defaults = settingsSvc.defaults[searchEngine];
var defaultUrl = defaults.searchUrl;
var newUrl = $scope.pendingWizardSettings.searchUrl;
if ( newUrl === defaultUrl ) {
var useDefaultSettings = false;
if ($scope.pendingWizardSettings.searchEngine === 'solr'){
if (newUrl === defaults.insecureSearchUrl || newUrl === defaults.secureSearchUrl ){
useDefaultSettings = true;
}
}
else {
if (newUrl === defaults.searchUrl) {
useDefaultSettings = true;
}
}

if ( useDefaultSettings ) {
$scope.pendingWizardSettings.idField = defaults.idField;
$scope.pendingWizardSettings.titleField = defaults.titleField;
$scope.pendingWizardSettings.additionalFields = defaults.additionalFields;
Expand Down Expand Up @@ -272,7 +305,13 @@ angular.module('QuepidApp')

$scope.$watch('wizardSettingsModel.settingsId()', function() {
// Reinit our pending settings from the service
var tempSearchUrl = $scope.pendingWizardSettings.searchUrl;
var tempApiMethod = $scope.pendingWizardSettings.apiMethod;
var tempQueryParams = $scope.pendingWizardSettings.queryParams;
angular.merge($scope.pendingWizardSettings, settingsSvc.editableSettings());
$scope.pendingWizardSettings.searchUrl = tempSearchUrl;
$scope.pendingWizardSettings.apiMethod = tempApiMethod;
$scope.pendingWizardSettings.queryParams = tempQueryParams;
$scope.pendingWizardSettings.newQueries = [];

if(userSvc.getUser().completedCaseWizard===false){
Expand Down
14 changes: 13 additions & 1 deletion app/assets/javascripts/factories/TryFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@
// This method converts the response from the API to angular objects.
var self = this;

// check if the backend has populated these fields or not?
if ( angular.isUndefined(data.search_engine) ) {
$log.info('We have an undefined data.search_engine so setting to Solr, should this ever happen?');
data.search_engine = 'solr';
}

if ( data.query_params === null ) {
// this app
if (data.search_engine === 'solr'){
data.query_params = '';
}
else {
data.query_params = '{}';
}
}


// Attributes
// Note, if you are changing these, then you probably to fix the
// var tmp = new TryFactory method in queryParams.js as well.
Expand All @@ -40,6 +51,7 @@
self.searchUrl = data.search_url;
self.tryNo = data.try_number;


// transform curator vars to be more angular friendly
var ngFriendlyCuratorVars = [];
angular.forEach(data.curatorVars, function(varValue, varName) {
Expand Down
11 changes: 6 additions & 5 deletions app/assets/javascripts/services/settingsSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ angular.module('QuepidApp')
TryFactory, SettingsFactory,
broadcastSvc
) {
// many of these defaults like apiMethod are defined server side in
// the Try.rb set_defaults method

// Used by the wizard
this.defaults = {
solr: {
queryParams: [
'q=#$query##',
'&defType=edismax',
'&qf=text_all',
'&indent=on',
'&q=#$query##',
'&tie=1.0',
].join('\n'),

Expand All @@ -37,7 +36,8 @@ angular.module('QuepidApp')
additionalFields: ['overview','cast','thumb:poster_path'],
numberOfRows: 10,
searchEngine: 'solr',
searchUrl: 'http://quepid-solr.dev.o19s.com:8985/solr/tmdb/select',
insecureSearchUrl:'http://quepid-solr.dev.o19s.com:8985/solr/tmdb/select',
secureSearchUrl: 'https://quepid-solr.dev.o19s.com/solr/tmdb/select',
urlFormat: 'http(s?)://yourdomain.com:8983/<index>/select',
},
es: {
Expand Down Expand Up @@ -227,6 +227,7 @@ angular.module('QuepidApp')

// Not sure why we have this. Handoff from wizard requires it though.
settingsToSave.selectedTry.apiMethod = settingsToSave.apiMethod;
settingsToSave.selectedTry.queryParams = settingsToSave.queryParams;

// post up
// (1) searchUrl
Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/views/wizardModal.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ <h2>Solr or Elasticsearch?</h2>

<div class="row">
<input style="width: 100%" type="text" ng-change="reset()" ng-model="pendingWizardSettings.searchUrl" class="form-control" />
<small title="Manually validates Quepid can get retrieve search results from your URL before continuing" class="pull-right"><a ng-click="validate(true)">ping it</a></small>
<small title="Manually validates Quepid can get retrieve search results from your URL before continuing" class="pull-right"><button type="button" class="btn btn-link btn-sm" ng-click="validate(true)" ng-disabled="showTLSChangeWarning">ping it</button></small>
<p class="help-block">Unsure? <a href="http://quepid.com/how-quepid-works/" target="_blank">Learn More</a> about Quepid's light touch with your search engine &amp; data.

<p class="help-block tip">
Expand Down
11 changes: 4 additions & 7 deletions app/controllers/core_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def set_case_or_bootstrap
# Similarily we may have only HTTPS set up for Quepid, and therefore need to stay on HTTPS,
# so this method is only conditionally called if force_ssl is false.
#
# rubocop:disable Layout/LineLength
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
Expand All @@ -54,18 +55,13 @@ def redirect_to_correct_tls
# Deal with front end UI changes to search engine being stored in backend
if params[:searchEngine].present?
# Reset the default queries
if @try.search_engine != params[:searchEngine]
@try.search_engine = params[:searchEngine]
@try.query_params = Try::DEFAULTS[@try.search_engine.to_sym][:query_params]
@try.field_spec = Try::DEFAULTS[@try.search_engine.to_sym][:field_spec]

end
@try.search_engine = params[:searchEngine] if @try.search_engine != params[:searchEngine]
@try.search_url = params[:searchUrl]
end
@try.save
end

search_engine_starts_with_https = @try.present? ? @try.search_url.starts_with?('https') : false
search_engine_starts_with_https = @try.present? && @try.search_url.present? ? @try.search_url.starts_with?('https') : false

if search_engine_starts_with_https && !request.ssl? # redirect to SSL
original_url = request.original_url
Expand All @@ -81,6 +77,7 @@ def redirect_to_correct_tls
true
end
end
# rubocop:enable Layout/LineLength
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
Expand Down
39 changes: 0 additions & 39 deletions app/models/try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,6 @@ class Try < ApplicationRecord
# Scopes
scope :latest, -> { order(id: :desc).first } # The try created the most recently

# Defaults for defining a search engine. These defaults
# are duplicated in the Angular SPA layer too ;-(
DEFAULTS = {
search_engine: 'solr',
solr: {
query_params: 'q=#$query##',
search_url: 'http://quepid-solr.dev.o19s.com:8985/solr/tmdb/select',
field_spec: 'id:id title:title',
api_method: 'JSONP',
},
es: {
query_params:
'{
"query": {
"multi_match": {
"query": "#$query##",
"type": "best_fields",
"fields": [
"title^10",
"overview",
"cast"
]
}
}
}',
search_url: 'http://quepid-elasticsearch.dev.o19s.com:9206/tmdb/_search',
field_spec: 'id:_id, title:title',
api_method: 'POST',
},
}.freeze

# Associations
belongs_to :case, optional: true # shouldn't be optional, but was in rails 4

Expand Down Expand Up @@ -146,15 +115,7 @@ def index_name_from_search_url

private

# rubocop:disable Metrics/AbcSize
def set_defaults
self.name = "Try #{try_number}" if name.blank?

self.search_engine = DEFAULTS[:search_engine] if search_engine.blank?
self.api_method = DEFAULTS[search_engine.to_sym][:api_method] if api_method.blank?
self.field_spec = DEFAULTS[search_engine.to_sym][:field_spec] if field_spec.blank?
self.query_params = DEFAULTS[search_engine.to_sym][:query_params] if query_params.blank?
self.search_url = DEFAULTS[search_engine.to_sym][:search_url] if search_url.blank?
end
# rubocop:enable Metrics/AbcSize
end
1 change: 1 addition & 0 deletions lib/es_arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module EsArgParser
def self.parse query_string, vars = {}
query_string = '{}' if query_string.nil?
# Remove new line characters
json_string = query_string.gsub(/\\n/, '').gsub(/\\r/, '').gsub(/%/, '%%')

Expand Down
1 change: 1 addition & 0 deletions lib/solr_arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module SolrArgParser
def self.parse query_string, vars = {}
# join lines, remove extraneous whitespace
# rubocop:disable Style/RedundantArgument
query_string = '' if query_string.nil?
query_string = query_string.lines.map(&:strip).join('')
# rubocop:enable Style/RedundantArgument

Expand Down
40 changes: 12 additions & 28 deletions test/controllers/api/v1/tries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,17 @@ def assert_curator_vars_equal vars, response_vars
assert_match( /#{created_try.try_number}/, created_try.name )

assert_not_nil created_try.search_engine
assert_not_nil created_try.field_spec
assert_not_nil created_try.search_url
assert_not_nil created_try.query_params
assert_not_nil created_try.escape_query

assert_equal created_try.search_engine, Try::DEFAULTS[:search_engine]
assert_equal created_try.field_spec, Try::DEFAULTS[:solr][:field_spec]
assert_equal created_try.search_url, Try::DEFAULTS[:solr][:search_url]
assert_equal created_try.escape_query, true
assert_equal created_try.api_method, Try::DEFAULTS[:solr][:api_method]
assert_equal created_try.number_of_rows, 10
assert_nil created_try.field_spec
assert_nil created_try.search_url
assert_nil created_try.query_params
assert created_try.escape_query

# assert_equal created_try.search_engine, Try::DEFAULTS[:search_engine]
# assert_equal created_try.field_spec, Try::DEFAULTS[:solr][:field_spec]
# assert_equal created_try.search_url, Try::DEFAULTS[:solr][:search_url]
# assert_equal created_try.escape_query, true
# assert_equal created_try.api_method, Try::DEFAULTS[:solr][:api_method]
assert_equal created_try.number_of_rows, 10
end

describe 'analytics' do
Expand Down Expand Up @@ -401,16 +401,7 @@ def assert_curator_vars_equal vars, response_vars
assert_match( /#{created_try.try_number}/, created_try.name )

assert_not_nil created_try.search_engine
assert_not_nil created_try.field_spec
assert_not_nil created_try.search_url
assert_not_nil created_try.query_params
assert_not_nil created_try.escape_query

assert_equal created_try.search_engine, Try::DEFAULTS[:search_engine]
assert_equal created_try.field_spec, Try::DEFAULTS[:solr][:field_spec]
assert_equal created_try.search_url, Try::DEFAULTS[:solr][:search_url]
assert_equal created_try.query_params, Try::DEFAULTS[:solr][:query_params]
assert_equal created_try.escape_query, true
assert_equal created_try.escape_query, true
end
end

Expand All @@ -430,16 +421,9 @@ def assert_curator_vars_equal vars, response_vars
assert_match( /#{created_try.try_number}/, created_try.name )

assert_not_nil created_try.search_engine
assert_not_nil created_try.field_spec
assert_not_nil created_try.search_url
assert_not_nil created_try.query_params
assert_not_nil created_try.escape_query

assert_equal created_try.search_engine, 'es'
assert_equal created_try.field_spec, Try::DEFAULTS[:es][:field_spec]
assert_equal created_try.search_url, Try::DEFAULTS[:es][:search_url]
assert_equal created_try.api_method, Try::DEFAULTS[:es][:api_method]
assert_equal created_try.query_params, Try::DEFAULTS[:es][:query_params]
assert_equal created_try.escape_query, true
end

Expand Down
6 changes: 6 additions & 0 deletions test/lib/es_arg_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require 'test_helper'

class EsArgParserTest < ActiveSupport::TestCase
test 'parses nil value' do
params = nil
result = EsArgParser.parse(params)
assert result.empty?
end

test 'parses basic case' do
params = '{ "foo": 1234 }'
result = EsArgParser.parse(params)
Expand Down
Loading