Skip to content

Commit

Permalink
Fix temperature queries with available options
Browse files Browse the repository at this point in the history
  • Loading branch information
beque committed Oct 17, 2024
1 parent b8abaed commit ac6c491
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
9 changes: 5 additions & 4 deletions app/packs/src/components/searchModal/forms/DetailSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,14 @@ const DetailSearch = () => {
}

const availableOptionsForTemperature = (searchValue, startValue, startUnit) => {
startValue = startValue.replace(/,/g, '.');
startValue = startValue.slice(-1) === '.' ? `${startValue}0` : startValue;
startValue = startValue.match(/^-?\d+(\.\d+)?$/g);

if (startValue === null || isNaN(Number(startValue))) { return searchValue; }

searchValue.available_options = [];
searchValue.available_options.push({ value: startValue, unit: startUnit });
searchValue.available_options.push({ value: startValue[0], unit: startUnit });

let [convertedValue, convertedUnit] = convertTemperature(startValue, startUnit);
let [convertedValue, convertedUnit] = convertTemperature(startValue[0], startUnit);
searchValue.available_options.push({ value: convertedValue.trim(), unit: convertedUnit });

[convertedValue, convertedUnit] = convertTemperature(convertedValue, convertedUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const searchValuesByFilters = (store) => {
let table = val.field.table || val.table;
let value = val.value;
table = table.charAt(0).toUpperCase() + table.slice(1, -1).replace('_', ' ');
value = value != true ? value.replace(/[\n\r]/g, ' OR ') : value;
value = value && value !== true ? value.replace(/[\n\r]/g, ' OR ') : value;

if (val.field.sub_fields && val.field.sub_fields.length >= 1 && val.sub_values.length >= 1) {
let values = searchValuesBySubFields(val, table);
Expand Down
27 changes: 15 additions & 12 deletions app/usecases/search/conditions_for_advanced_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,11 @@ def special_non_generic_field_options(filter)
when 'temperature'
regex_number = "'^-{0,1}\\d+(\\.\\d+){0,1}\\Z'"
is_data_valid = "(#{@table}.temperature ->> 'userText' ~ #{regex_number})"

@conditions[:field] =
"CASE WHEN #{is_data_valid} THEN (#{@table}.temperature ->> 'userText')::FLOAT ELSE -30000 END "

@conditions[:first_condition] += " (#{@table}.temperature ->> 'valueUnit')::TEXT != '' AND "
@conditions[:additional_condition] = "AND (#{@table}.temperature ->> 'valueUnit')::TEXT = '#{filter['unit']}'"
field = filter['field']['column']
@conditions[:field] = "(#{@table}.temperature ->> 'userText')::FLOAT"
@conditions[:first_condition] +=
" (#{@table}.temperature ->> 'valueUnit')::TEXT != '' AND #{is_data_valid} AND "
unit_and_available_options_conditions(filter, "#{@table}.temperature", field, 'userText', 'valueUnit')
@conditions[:condition_table] = ''
when 'duration'
time_divisor = duration_interval_by_unit(filter['unit'])
Expand Down Expand Up @@ -380,7 +379,7 @@ def dataset_tab_options(filter, number)
@conditions[:additional_condition] = "AND (#{prop} ->> 'field')::TEXT = '#{field}'"

if filter['unit'].present? || filter['available_options'].present?
unit_and_available_options_conditions(filter, prop, field)
unit_and_available_options_conditions(filter, prop, field, 'value', 'value_system')
end
end
end
Expand All @@ -389,10 +388,14 @@ def remove_degree_from_unit(filter)
filter['unit'].remove('°').remove(/ \(.*\)/).tr('/', '_')
end

def unit_and_available_options_conditions(filter, prop, field)
def remove_degree_from_property(prop, unit)
"LOWER(replace((#{prop} ->> '#{unit}')::TEXT, '°', ''))"
end

def unit_and_available_options_conditions(filter, prop, field, number, unit)
if filter['unit'].present?
@conditions[:additional_condition] +=
" AND LOWER((#{prop} ->> 'value_system')::TEXT) = LOWER('#{remove_degree_from_unit(filter)}')"
" AND #{remove_degree_from_property(prop, unit)} = LOWER('#{remove_degree_from_unit(filter)}')"
end

return if filter['available_options'].blank?
Expand All @@ -403,10 +406,10 @@ def unit_and_available_options_conditions(filter, prop, field)
next if option[:unit] == filter['unit']

@conditions[:additional_condition] +=
" OR ((#{prop} ->> 'value')::TEXT >= '#{option[:value]}'
AND LOWER((#{prop} ->> 'value_system')::TEXT) = LOWER('#{remove_degree_from_unit(filter)}'))"
" OR ((#{prop} ->> '#{number}')::TEXT >= '#{option[:value]}'
AND #{remove_degree_from_property(prop, unit)} = LOWER('#{remove_degree_from_unit(option)}'))"
else
conditions += " AND (#{prop} ->> 'value')::TEXT NOT ILIKE '%#{option[:value]}%'"
conditions += " AND (#{prop} ->> '#{number}')::TEXT NOT ILIKE '%#{option[:value]}%'"
end
end

Expand Down

0 comments on commit ac6c491

Please sign in to comment.