From 29d76eb6719ca1b9e5c8dc39ccc503f6a14979b5 Mon Sep 17 00:00:00 2001 From: j-corry Date: Tue, 17 Sep 2024 11:18:37 +0100 Subject: [PATCH] - Add new helper method to remove (currently only) the year filter when adding a new filter. This is used to allow drill-down behaviour for the date facets. - Add new helper methods to assess date spread of results, allowing year-month filters to be displayed automatically if the data is already limited to within a single year. - De-dupe applied search filters before displaying them --- app/helpers/application_helper.rb | 6 + app/models/search_data.rb | 26 +- app/models/solr_search.rb | 11 +- app/views/search/_applied_filters.html.erb | 2 +- .../results/facets/_date_facet.html.erb | 21 +- coverage/.last_run.json | 2 +- coverage/.resultset.json | 129 ++- coverage/index.html | 997 ++++++++++++------ spec/models/search_data_spec.rb | 88 +- 9 files changed, 862 insertions(+), 420 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d9e8fe6..fb7c8f2 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -138,6 +138,12 @@ def apply_filter_url(params, filter_name, filter_value) params.except(:page).merge(filter: params.dig(:filter).nil? ? { filter_name => [filter_value] } : params.dig(:filter).merge(filter_name => [filter_value, params.dig(:filter, filter_name)].compact.flatten)) end + def replace_filter_url(params, filter_name, filter_value) + # used for year-month filters; performs the additional step of removing year filters first + adjusted_params = params.except(:filter, :year) + apply_filter_url(adjusted_params, filter_name, filter_value) + end + def year_filter params.dig(:filter, :year) end diff --git a/app/models/search_data.rb b/app/models/search_data.rb index a4869e7..85fb624 100644 --- a/app/models/search_data.rb +++ b/app/models/search_data.rb @@ -188,19 +188,17 @@ def facet_ses_ids end def years - # extract years from returned facet data year_buckets = search&.dig(:data, "facets", "year", "buckets") - return if year_buckets.blank? + return [] if year_buckets.blank? year_buckets.sort_by { |h| h["val"] }.uniq.reverse end - def months - # extract months from returned facet data - month_buckets = search.dig(:data, "facets", "month", "buckets") - return if month_buckets.blank? + def months(year_string) + month_buckets = search&.dig(:data, "facets", "month", "buckets") + return [] if month_buckets.blank? - month_buckets.sort_by { |h| h["val"] }.uniq + month_buckets.select { |m| m['val'].first(4) == year_string }.sort_by { |h| h["val"] }.uniq end def hierarchy_data @@ -273,4 +271,18 @@ def session_facets def sort_facets(facet_field) facet_field.sort_by { |h| h["count"] }.reverse end + + def single_data_year? + data_years.size == 1 + end + + def data_years + return [] unless search + + date_facet = search.dig(:data, 'facets', 'date_dt') + + return [] if date_facet.blank? + + date_facet.dig("buckets").map { |b| b.dig("val").to_date.strftime("%Y") }.uniq + end end \ No newline at end of file diff --git a/app/models/solr_search.rb b/app/models/solr_search.rb index ccac8d3..6a131c5 100644 --- a/app/models/solr_search.rb +++ b/app/models/solr_search.rb @@ -247,12 +247,21 @@ def facet_hash ret['year'] = { "type": "range", "field": "date_dt", - "start": "1500-01-01T00:00:00Z", + # "start": "1500-01-01T00:00:00Z", "end": "#{Date.today.strftime("%Y-%m-%d")}T23:59:59Z", "gap": "+1YEAR", "mincount": 1, "limit": 100 } + ret['month'] = { + "type": "range", + "field": "date_dt", + # "start": "1500-01-01T00:00:00Z", + "end": "#{Date.today.strftime("%Y-%m-%d")}T23:59:59Z", + "gap": "+1MONTH", + "mincount": 0, + "domain": { excludeTags: 'month' } + } end SolrSearch.sessions.each do |session| diff --git a/app/views/search/_applied_filters.html.erb b/app/views/search/_applied_filters.html.erb index a491bcc..b3a0b7b 100644 --- a/app/views/search/_applied_filters.html.erb +++ b/app/views/search/_applied_filters.html.erb @@ -1,7 +1,7 @@ <% unless request.params.dig(:filter).blank? %>
- <% request.params.dig(:filter).sort.each do |filter| %> + <% request.params.dig(:filter).sort.uniq.each do |filter| %> <% filter.last.sort.each do |filter_value| %>
diff --git a/app/views/search/results/facets/_date_facet.html.erb b/app/views/search/results/facets/_date_facet.html.erb index cd42078..bed51d2 100644 --- a/app/views/search/results/facets/_date_facet.html.erb +++ b/app/views/search/results/facets/_date_facet.html.erb @@ -6,9 +6,26 @@ Date
- <% @search_data.months.each_with_index do |month, index| %> + <% @search_data.months(year).each_with_index do |month, index| %>
- <%= link_to("#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(apply_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %> + <%= link_to_unless(request.params.dig(:filter, "month")&.include?(month['val'].to_date.strftime("%Y-%-m")), "#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(replace_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %> +
+ <% end %> +
+
+
+ <% end %> +<% elsif @search_data.single_data_year? %> + <% @search_data.data_years.each do |year| %> +
+
+
+ Date +
+
+ <% @search_data.months(year).each_with_index do |month, index| %> +
+ <%= link_to_unless(request.params.dig(:filter, "month")&.include?(month['val'].to_date.strftime("%Y-%-m")), "#{month['val'].to_date.strftime("%B")} #{year} (#{month['count']})", url_for(replace_filter_url(request.params, "month", year + "-#{index + 1}")), class: "menu-link modifiable-link") %>
<% end %>
diff --git a/coverage/.last_run.json b/coverage/.last_run.json index ca574ee..2cabc52 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 81.33 + "line": 81.51 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 92b14d1..f59cde9 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -166,6 +166,12 @@ null, null, 1, + null, + 0, + 0, + null, + null, + 1, 3, null, null @@ -588,16 +594,16 @@ 1, null, 1, - 1107, + 1122, null, null, 1, null, null, - 135, - 135, + 150, + 150, null, - 135, + 150, null, null, 1, @@ -622,9 +628,9 @@ null, null, 1, - 274, - 274, - 274, + 289, + 289, + 289, null, null, 1, @@ -837,7 +843,7 @@ null, null, 1, - 632, + 647, null, null, 1, @@ -983,7 +989,7 @@ null, null, 1, - 641, + 656, null, 92, null, @@ -1031,7 +1037,7 @@ null, null, 1, - 135, + 150, null, 0, null, @@ -1129,7 +1135,7 @@ null, 1, null, - 54, + 69, null, null, null, @@ -1230,40 +1236,40 @@ "lines": [ 1, 1, - 248, + 257, null, null, 1, - 249, + 258, null, null, 1, - 521, + 545, null, null, 1, - 245, - 245, + 254, + 254, null, 33, null, null, 1, - 243, + 252, null, - 243, + 252, null, - 243, + 252, null, - 243, - 243, + 252, + 252, 4, null, null, - 243, - 243, + 252, + 252, null, - 243, + 252, null, null ] @@ -1994,11 +2000,11 @@ 1, null, 1, - 35, + 44, null, null, 1, - 35, + 44, null, null, 1, @@ -2171,11 +2177,11 @@ 1, null, 1, - 247, + 256, null, null, 1, - 245, + 254, null, 33, null, @@ -2204,7 +2210,7 @@ null, 1, null, - 245, + 254, null, null, null @@ -2779,13 +2785,13 @@ null, 1, null, - 35, - 35, - 35, + 44, + 44, + 44, null, null, 1, - 35, + 44, null, null, 1, @@ -2822,17 +2828,17 @@ null, null, 1, - 75, + 89, null, - 74, + 84, null, null, 1, - 43, + 52, null, - 27, - 108, - 27, + 32, + 128, + 32, null, null, 1, @@ -2926,12 +2932,12 @@ null, null, 1, - 18, - 18, + 6, + 6, null, null, 1, - 35, + 44, null, null, 1, @@ -2963,19 +2969,17 @@ null, null, 1, + 5, + 5, null, 3, - 3, - null, - 0, null, null, 1, + 2, + 2, null, - 0, - 0, - null, - 0, + 4, null, null, 1, @@ -3048,6 +3052,20 @@ 1, 54, null, + null, + 1, + 6, + null, + null, + 1, + 8, + null, + 6, + null, + 6, + null, + 9, + null, null ] }, @@ -3059,7 +3077,7 @@ 1, null, 1, - 1461, + 1470, null, null, 1, @@ -3535,6 +3553,15 @@ null, null, null, + 0, + null, + null, + null, + null, + null, + null, + null, + null, null, null, 0, @@ -3888,6 +3915,6 @@ ] } }, - "timestamp": 1726139945 + "timestamp": 1726567988 } } diff --git a/coverage/index.html b/coverage/index.html index 5da11f3..f695a1c 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -14,7 +14,7 @@ loading
-
Generated 2024-09-12T12:19:05+01:00
+
Generated 2024-09-17T11:13:08+01:00
    @@ -23,14 +23,14 @@

    All Files ( - 81.33% + 81.51% covered at - 87.95 + 87.66 hits/line ) @@ -43,11 +43,11 @@

    - 1682 relevant lines, - 1368 lines covered and - 314 lines missed. + 1693 relevant lines, + 1380 lines covered and + 313 lines missed. ( - 81.33% + 81.51% )
    @@ -138,12 +138,12 @@

    app/helpers/application_helper.rb - 82.35 % - 144 - 51 - 42 - 9 - 26.39 + 79.63 % + 150 + 54 + 43 + 11 + 24.94 @@ -253,7 +253,7 @@

    21 21 0 - 166.43 + 172.71 @@ -308,7 +308,7 @@

    277 268 9 - 288.33 + 288.92 @@ -440,7 +440,7 @@

    33 27 6 - 4.52 + 5.06 @@ -495,7 +495,7 @@

    23 23 0 - 51.83 + 53.00 @@ -721,12 +721,12 @@

    app/models/search_data.rb - 87.14 % - 276 - 140 - 122 - 18 - 8.70 + 90.48 % + 288 + 147 + 133 + 14 + 9.21 @@ -737,7 +737,7 @@

    66 47 19 - 23.76 + 23.89 @@ -776,12 +776,12 @@

    app/models/solr_search.rb - 37.98 % - 354 - 129 + 37.69 % + 363 + 130 49 - 80 - 2.61 + 81 + 2.59 @@ -2378,8 +2378,8 @@

    app/helpers/application_helper.rb

    - - 82.35% + + 79.63% lines covered @@ -2388,9 +2388,9 @@

    - 51 relevant lines. - 42 lines covered and - 9 lines missed. + 54 relevant lines. + 43 lines covered and + 11 lines missed.
    @@ -3947,12 +3947,78 @@

    + def replace_filter_url(params, filter_name, filter_value) + +

    + +
    +
  • + + + + + + # used for year-month filters; performs the additional step of removing year filters first +
  • +
    + +
    +
  • + + + + + + adjusted_params = params.except(:filter, :year) +
  • +
    + +
    +
  • + + + + + + apply_filter_url(adjusted_params, filter_name, filter_value) +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + def year_filter
  • -
  • +
  • 3 @@ -3963,7 +4029,7 @@

  • -
  • +
  • @@ -3974,7 +4040,7 @@

  • -
  • +
  • @@ -7793,8 +7859,8 @@

  • -
  • - 248 +
  • + 257 @@ -7837,8 +7903,8 @@

  • -
  • - 249 +
  • + 258 @@ -7881,8 +7947,8 @@

  • -
  • - 521 +
  • + 545 @@ -7925,8 +7991,8 @@

  • -
  • - 245 +
  • + 254 @@ -7936,8 +8002,8 @@

  • -
  • - 245 +
  • + 254 @@ -8002,8 +8068,8 @@

  • -
  • - 243 +
  • + 252 @@ -8024,8 +8090,8 @@

  • -
  • - 243 +
  • + 252 @@ -8046,8 +8112,8 @@

  • -
  • - 243 +
  • + 252 @@ -8068,8 +8134,8 @@

  • -
  • - 243 +
  • + 252 @@ -8079,8 +8145,8 @@

  • -
  • - 243 +
  • + 252 @@ -8123,8 +8189,8 @@

  • -
  • - 243 +
  • + 252 @@ -8134,8 +8200,8 @@

  • -
  • - 243 +
  • + 252 @@ -8156,8 +8222,8 @@

  • -
  • - 243 +
  • + 252 @@ -9444,8 +9510,8 @@

  • -
  • - 1107 +
  • + 1122 @@ -9510,8 +9576,8 @@

  • -
  • - 135 +
  • + 150 @@ -9521,8 +9587,8 @@

  • -
  • - 135 +
  • + 150 @@ -9543,8 +9609,8 @@

  • -
  • - 135 +
  • + 150 @@ -9818,8 +9884,8 @@

  • -
  • - 274 +
  • + 289 @@ -9829,8 +9895,8 @@

  • -
  • - 274 +
  • + 289 @@ -9840,8 +9906,8 @@

  • -
  • - 274 +
  • + 289 @@ -12183,8 +12249,8 @@

  • -
  • - 632 +
  • + 647 @@ -13789,8 +13855,8 @@

  • -
  • - 641 +
  • + 656 @@ -14317,8 +14383,8 @@

  • -
  • - 135 +
  • + 150 @@ -15395,8 +15461,8 @@

  • -
  • - 54 +
  • + 69 @@ -20343,8 +20409,8 @@

  • -
  • - 35 +
  • + 44 @@ -20387,8 +20453,8 @@

  • -
  • - 35 +
  • + 44 @@ -22225,8 +22291,8 @@

  • -
  • - 247 +
  • + 256 @@ -22269,8 +22335,8 @@

  • -
  • - 245 +
  • + 254 @@ -22588,8 +22654,8 @@

  • -
  • - 245 +
  • + 254 @@ -30575,8 +30641,8 @@

    app/models/search_data.rb

    - - 87.14% + + 90.48% lines covered @@ -30585,9 +30651,9 @@

    - 140 relevant lines. - 122 lines covered and - 18 lines missed. + 147 relevant lines. + 133 lines covered and + 14 lines missed.
    @@ -30664,8 +30730,8 @@

    -
  • - 35 +
  • + 44 @@ -30675,8 +30741,8 @@

  • -
  • - 35 +
  • + 44 @@ -30686,8 +30752,8 @@

  • -
  • - 35 +
  • + 44 @@ -30730,8 +30796,8 @@

  • -
  • - 35 +
  • + 44 @@ -31137,8 +31203,8 @@

  • -
  • - 75 +
  • + 89 @@ -31159,8 +31225,8 @@

  • -
  • - 74 +
  • + 84 @@ -31203,8 +31269,8 @@

  • -
  • - 43 +
  • + 52 @@ -31225,8 +31291,8 @@

  • -
  • - 27 +
  • + 32 @@ -31236,8 +31302,8 @@

  • -
  • - 108 +
  • + 128 @@ -31247,8 +31313,8 @@

  • -
  • - 27 +
  • + 32 @@ -32281,8 +32347,8 @@

  • -
  • - 18 +
  • + 6 @@ -32292,8 +32358,8 @@

  • -
  • - 18 +
  • + 6 @@ -32336,8 +32402,8 @@

  • -
  • - 35 +
  • + 44 @@ -32688,19 +32754,8 @@

  • -
  • - - - - - - # extract years from returned facet data -
  • -
    - -
    -
  • - 3 +
  • + 5 @@ -32710,18 +32765,18 @@

  • -
  • - 3 +
  • + 5 - return if year_buckets.blank? + return [] if year_buckets.blank?
  • -
  • +
  • @@ -32732,8 +32787,8 @@

  • -
  • - +
  • + 3 @@ -32743,7 +32798,7 @@

  • -
  • +
  • @@ -32754,7 +32809,7 @@

  • -
  • +
  • @@ -32765,51 +32820,40 @@

  • -
  • +
  • 1 - def months + def months(year_string)
  • -
  • - - - - - - # extract months from returned facet data -
  • -
    - -
    -
  • - +
  • + 2 - month_buckets = search.dig(:data, "facets", "month", "buckets") + month_buckets = search&.dig(:data, "facets", "month", "buckets")
  • -
  • - +
  • + 2 - return if month_buckets.blank? + return [] if month_buckets.blank?
  • -
  • +
  • @@ -32820,18 +32864,18 @@

  • -
  • - +
  • + 4 - month_buckets.sort_by { |h| h["val"] }.uniq + month_buckets.select { |m| m['val'].first(4) == year_string }.sort_by { |h| h["val"] }.uniq
  • -
  • +
  • @@ -32842,7 +32886,7 @@

  • -
  • +
  • @@ -32853,7 +32897,7 @@

  • -
  • +
  • 1 @@ -32864,7 +32908,7 @@

  • -
  • +
  • 4 @@ -32875,7 +32919,7 @@

  • -
  • +
  • @@ -32886,7 +32930,7 @@

  • -
  • +
  • @@ -32897,7 +32941,7 @@

  • -
  • +
  • 1 @@ -32908,7 +32952,7 @@

  • -
  • +
  • @@ -32919,7 +32963,7 @@

  • -
  • +
  • @@ -32930,7 +32974,7 @@

  • -
  • +
  • @@ -32941,7 +32985,7 @@

  • -
  • +
  • 3 @@ -32952,7 +32996,7 @@

  • -
  • +
  • @@ -32963,7 +33007,7 @@

  • -
  • +
  • @@ -32974,7 +33018,7 @@

  • -
  • +
  • 1 @@ -32985,7 +33029,7 @@

  • -
  • +
  • @@ -32996,7 +33040,7 @@

  • -
  • +
  • @@ -33007,7 +33051,7 @@

  • -
  • +
  • @@ -33018,7 +33062,7 @@

  • -
  • +
  • 1 @@ -33029,7 +33073,7 @@

  • -
  • +
  • @@ -33040,7 +33084,7 @@

  • -
  • +
  • @@ -33051,7 +33095,7 @@

  • -
  • +
  • @@ -33062,7 +33106,7 @@

  • -
  • +
  • @@ -33073,7 +33117,7 @@

  • -
  • +
  • 3 @@ -33084,7 +33128,7 @@

  • -
  • +
  • 3 @@ -33095,7 +33139,7 @@

  • -
  • +
  • 3 @@ -33106,7 +33150,7 @@

  • -
  • +
  • @@ -33117,7 +33161,7 @@

  • -
  • +
  • 3 @@ -33128,7 +33172,7 @@

  • -
  • +
  • @@ -33139,7 +33183,7 @@

  • -
  • +
  • @@ -33150,7 +33194,7 @@

  • -
  • +
  • 1 @@ -33161,7 +33205,7 @@

  • -
  • +
  • 2 @@ -33172,7 +33216,7 @@

  • -
  • +
  • @@ -33183,7 +33227,7 @@

  • -
  • +
  • 1 @@ -33194,7 +33238,7 @@

  • -
  • +
  • 1 @@ -33205,7 +33249,7 @@

  • -
  • +
  • @@ -33216,7 +33260,7 @@

  • -
  • +
  • 1 @@ -33227,7 +33271,7 @@

  • -
  • +
  • @@ -33238,7 +33282,7 @@

  • -
  • +
  • @@ -33249,7 +33293,7 @@

  • -
  • +
  • 1 @@ -33260,7 +33304,7 @@

  • -
  • +
  • 17 @@ -33271,7 +33315,7 @@

  • -
  • +
  • @@ -33282,7 +33326,7 @@

  • -
  • +
  • @@ -33293,7 +33337,7 @@

  • -
  • +
  • 1 @@ -33304,7 +33348,7 @@

  • -
  • +
  • 15 @@ -33315,7 +33359,7 @@

  • -
  • +
  • @@ -33326,7 +33370,7 @@

  • -
  • +
  • 14 @@ -33337,7 +33381,7 @@

  • -
  • +
  • 14 @@ -33348,7 +33392,7 @@

  • -
  • +
  • @@ -33359,18 +33403,18 @@

  • -
  • +
  • 14 - facet_field_data.slice("type_sesrollup", "publisher_ses", "legislature_ses", "date_dt", "department_ses", + facet_field_data.slice("type_sesrollup", "legislature_ses", "date_dt", "department_ses",
  • -
  • +
  • @@ -33381,7 +33425,7 @@

  • -
  • +
  • @@ -33392,18 +33436,18 @@

  • -
  • +
  • 18 - "topic_ses", "year").map { |k, v| { field_name: k, facets: sort_facets(v['buckets']) } } + "topic_ses", "year", "publisher_ses").map { |k, v| { field_name: k, facets: sort_facets(v['buckets']) } }
  • -
  • +
  • @@ -33414,7 +33458,7 @@

  • -
  • +
  • @@ -33425,7 +33469,7 @@

  • -
  • +
  • 1 @@ -33436,7 +33480,7 @@

  • -
  • +
  • 4 @@ -33447,7 +33491,7 @@

  • -
  • +
  • 16 @@ -33458,7 +33502,7 @@

  • -
  • +
  • 4 @@ -33469,7 +33513,7 @@

  • -
  • +
  • @@ -33480,7 +33524,7 @@

  • -
  • +
  • @@ -33491,7 +33535,7 @@

  • -
  • +
  • 1 @@ -33502,7 +33546,7 @@

  • -
  • +
  • 3 @@ -33513,7 +33557,7 @@

  • -
  • +
  • @@ -33524,7 +33568,7 @@

  • -
  • +
  • 3 @@ -33535,7 +33579,7 @@

  • -
  • +
  • 3 @@ -33546,7 +33590,7 @@

  • -
  • +
  • @@ -33557,7 +33601,7 @@

  • -
  • +
  • 9 @@ -33568,7 +33612,7 @@

  • -
  • +
  • @@ -33579,7 +33623,7 @@

  • -
  • +
  • @@ -33590,7 +33634,7 @@

  • -
  • +
  • 1 @@ -33601,7 +33645,7 @@

  • -
  • +
  • 54 @@ -33612,7 +33656,7 @@

  • -
  • +
  • @@ -33623,7 +33667,161 @@

  • -
  • +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + + def single_data_year? +
  • +
    + +
    +
  • + 6 + + + + + data_years.size == 1 +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 1 + + + + + def data_years +
  • +
    + +
    +
  • + 8 + + + + + return [] unless search +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 6 + + + + + date_facet = search.dig(:data, 'facets', 'date_dt') +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 6 + + + + + return [] if date_facet.blank? +
  • +
    + +
    +
  • + + + + + + +
  • +
    + +
    +
  • + 9 + + + + + date_facet.dig("buckets").map { |b| b.dig("val").to_date.strftime("%Y") }.uniq +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • @@ -33731,8 +33929,8 @@

  • -
  • - 1461 +
  • + 1470 @@ -36088,7 +36286,7 @@

    app/models/solr_search.rb

    - 37.98% + 37.69% lines covered @@ -36097,9 +36295,9 @@

    - 129 relevant lines. + 130 relevant lines. 49 lines covered and - 80 lines missed. + 81 lines missed.
    @@ -38855,7 +39053,7 @@

    - "start": "1500-01-01T00:00:00Z", + # "start": "1500-01-01T00:00:00Z",

  • @@ -38915,13 +39113,13 @@

  • -
  • +
  • - end + ret['month'] = {
  • @@ -38932,12 +39130,111 @@

    + "type": "range", + +

    + +
    +
  • + + + + + + "field": "date_dt", +
  • +
    + +
    +
  • + + + + + + # "start": "1500-01-01T00:00:00Z", +
  • +
    + +
    +
  • + + + + + + "end": "#{Date.today.strftime("%Y-%m-%d")}T23:59:59Z", +
  • +
    + +
    +
  • + + + + + + "gap": "+1MONTH", +
  • +
    + +
    +
  • + + + + + + "mincount": 0, +
  • +
    + +
    +
  • + + + + + + "domain": { excludeTags: 'month' } +
  • +
    + +
    +
  • + + + + + + } +
  • +
    + +
    +
  • + + + + + + end +
  • +
    + +
    +
  • + + + + +
  • -
  • +
  • @@ -38948,7 +39245,7 @@

  • -
  • +
  • @@ -38959,7 +39256,7 @@

  • -
  • +
  • @@ -38970,7 +39267,7 @@

  • -
  • +
  • @@ -38981,7 +39278,7 @@

  • -
  • +
  • @@ -38992,7 +39289,7 @@

  • -
  • +
  • @@ -39003,7 +39300,7 @@

  • -
  • +
  • @@ -39014,7 +39311,7 @@

  • -
  • +
  • @@ -39025,7 +39322,7 @@

  • -
  • +
  • @@ -39036,7 +39333,7 @@

  • -
  • +
  • @@ -39047,7 +39344,7 @@

  • -
  • +
  • @@ -39058,7 +39355,7 @@

  • -
  • +
  • 1 @@ -39069,7 +39366,7 @@

  • -
  • +
  • @@ -39080,7 +39377,7 @@

  • -
  • +
  • @@ -39091,7 +39388,7 @@

  • -
  • +
  • @@ -39102,7 +39399,7 @@

  • -
  • +
  • @@ -39113,7 +39410,7 @@

  • -
  • +
  • @@ -39124,7 +39421,7 @@

  • -
  • +
  • @@ -39135,7 +39432,7 @@

  • -
  • +
  • 1 @@ -39146,7 +39443,7 @@

  • -
  • +
  • @@ -39157,7 +39454,7 @@

  • -
  • +
  • @@ -39168,7 +39465,7 @@

  • -
  • +
  • @@ -39179,7 +39476,7 @@

  • -
  • +
  • @@ -39190,7 +39487,7 @@

  • -
  • +
  • @@ -39201,7 +39498,7 @@

  • -
  • +
  • @@ -39212,7 +39509,7 @@

  • -
  • +
  • @@ -39223,7 +39520,7 @@

  • -
  • +
  • @@ -39234,7 +39531,7 @@

  • -
  • +
  • 1 @@ -39245,7 +39542,7 @@

  • -
  • +
  • @@ -39256,7 +39553,7 @@

  • -
  • +
  • @@ -39267,7 +39564,7 @@

  • -
  • +
  • @@ -39278,7 +39575,7 @@

  • -
  • +
  • @@ -39289,7 +39586,7 @@

  • -
  • +
  • @@ -39300,7 +39597,7 @@

  • -
  • +
  • @@ -39311,7 +39608,7 @@

  • -
  • +
  • 1 @@ -39322,7 +39619,7 @@

  • -
  • +
  • @@ -39333,7 +39630,7 @@

  • -
  • +
  • 9 @@ -39344,7 +39641,7 @@

  • -
  • +
  • @@ -39355,7 +39652,7 @@

  • -
  • +
  • @@ -39366,7 +39663,7 @@

  • -
  • +
  • @@ -39377,7 +39674,7 @@

  • -
  • +
  • @@ -39388,7 +39685,7 @@

  • -
  • +
  • @@ -39399,7 +39696,7 @@

  • -
  • +
  • @@ -39410,7 +39707,7 @@

  • -
  • +
  • @@ -39421,7 +39718,7 @@

  • -
  • +
  • @@ -39432,7 +39729,7 @@

  • -
  • +
  • @@ -39443,7 +39740,7 @@

  • -
  • +
  • @@ -39454,7 +39751,7 @@

  • -
  • +
  • @@ -39465,7 +39762,7 @@

  • -
  • +
  • @@ -39476,7 +39773,7 @@

  • -
  • +
  • @@ -39487,7 +39784,7 @@

  • -
  • +
  • @@ -39498,7 +39795,7 @@

  • -
  • +
  • @@ -39509,7 +39806,7 @@

  • -
  • +
  • @@ -39520,7 +39817,7 @@

  • -
  • +
  • @@ -39531,7 +39828,7 @@

  • -
  • +
  • @@ -39542,7 +39839,7 @@

  • -
  • +
  • @@ -39553,7 +39850,7 @@

  • -
  • +
  • @@ -39564,7 +39861,7 @@

  • -
  • +
  • @@ -39575,7 +39872,7 @@

  • -
  • +
  • @@ -39586,7 +39883,7 @@

  • -
  • +
  • @@ -39597,7 +39894,7 @@

  • -
  • +
  • @@ -39608,7 +39905,7 @@

  • -
  • +
  • @@ -39619,7 +39916,7 @@

  • -
  • +
  • @@ -39630,7 +39927,7 @@

  • -
  • +
  • @@ -39641,7 +39938,7 @@

  • -
  • +
  • @@ -39652,7 +39949,7 @@

  • -
  • +
  • @@ -39663,7 +39960,7 @@

  • -
  • +
  • @@ -39674,7 +39971,7 @@

  • -
  • +
  • @@ -39685,7 +39982,7 @@

  • -
  • +
  • @@ -39696,7 +39993,7 @@

  • -
  • +
  • @@ -39707,7 +40004,7 @@

  • -
  • +
  • @@ -39718,7 +40015,7 @@

  • -
  • +
  • @@ -39729,7 +40026,7 @@

  • -
  • +
  • @@ -39740,7 +40037,7 @@

  • -
  • +
  • @@ -39751,7 +40048,7 @@

  • -
  • +
  • @@ -39762,7 +40059,7 @@

  • -
  • +
  • @@ -39773,7 +40070,7 @@

  • -
  • +
  • @@ -39784,7 +40081,7 @@

  • -
  • +
  • @@ -39795,7 +40092,7 @@

  • -
  • +
  • @@ -39806,7 +40103,7 @@

  • -
  • +
  • @@ -39817,7 +40114,7 @@

  • -
  • +
  • @@ -39828,7 +40125,7 @@

  • -
  • +
  • @@ -39839,7 +40136,7 @@

  • -
  • +
  • @@ -39850,7 +40147,7 @@

  • -
  • +
  • 1 @@ -39861,7 +40158,7 @@

  • -
  • +
  • @@ -39872,7 +40169,7 @@

  • -
  • +
  • 1 @@ -39883,7 +40180,7 @@

  • -
  • +
  • @@ -39894,7 +40191,7 @@

  • -
  • +
  • @@ -39905,7 +40202,7 @@

  • -
  • +
  • @@ -39916,7 +40213,7 @@

  • -
  • +
  • @@ -39927,7 +40224,7 @@

  • -
  • +
  • @@ -39938,7 +40235,7 @@

  • -
  • +
  • @@ -39949,7 +40246,7 @@

  • -
  • +
  • @@ -39960,7 +40257,7 @@

  • -
  • +
  • @@ -39971,7 +40268,7 @@

  • -
  • +
  • @@ -39982,7 +40279,7 @@

  • -
  • +
  • @@ -39993,7 +40290,7 @@

  • -
  • +
  • diff --git a/spec/models/search_data_spec.rb b/spec/models/search_data_spec.rb index 592cfdf..e0232b6 100644 --- a/spec/models/search_data_spec.rb +++ b/spec/models/search_data_spec.rb @@ -2,6 +2,11 @@ RSpec.describe SearchData, type: :model do let(:search_data) { SearchData.new(search_output) } + let!(:facet_data) { { + "count" => 1234, + "topic_ses" => { "buckets" => [{ "val" => 90996, "count" => 123 }, { "val" => 90995, "count" => 234 }] }, + "subject_ses" => { "buckets" => [{ "val" => 123456, "count" => 455 }, { "val" => 234556, "count" => 66 }] } + } } let!(:search_output) { { search_parameters: { filter: ['topic_ses:12345'], query: 'horse' }, data: { "responseHeader" => { @@ -19,14 +24,9 @@ ] }, "highlighting" => { "test_url" => {} }, - "facets" => { - "count" => 1234, - "topic_ses" => { "buckets" => [{ "val" => 90996, "count" => 123 }, { "val" => 90995, "count" => 234 }] }, - "subject_ses" => { "buckets" => [{ "val" => 123456, "count" => 455 }, { "val" => 234556, "count" => 66 }] } - } + "facets" => facet_data }, - } - } + } } let!(:hierarchy_test_response) { { [92424, "Personal statements"] => [{ "typeId" => "1", "qty" => "1", "name" => "Broader Term", "abbr" => "BT", "fields" => [{ "field" => { "name" => "Oral statements", "id" => "350073", "zid" => "52566919", "class" => "CTP", "freq" => "0", "facets" => [{ "id" => "346696", "name" => "Content type" }] } }] }] } } before do @@ -281,4 +281,78 @@ end end + describe 'data years' do + context 'where search is nil' do + let(:search_data) { SearchData.new(nil) } + + it 'returns an empty array' do + expect(search_data.data_years).to eq([]) + end + end + context 'where search is not nil' do + let!(:facet_data) { { "date_dt" => { "buckets" => [{ "val" => "2020-10-22T23:00:00Z", "count" => 12 }, { "val" => "2020-07-06T23:00:00Z", "count" => 9 }] } } } + + it 'extracts unique years from the date facet' do + expect(search_data.data_years).to eq(["2020"]) + end + end + end + + describe 'single_data_year?' do + context 'where search is nil' do + let(:search_data) { SearchData.new(nil) } + it 'returns false' do + expect(search_data.single_data_year?).to be false + end + end + + context 'where search is not nil' do + context 'where all data falls within a single year' do + let!(:facet_data) { { "date_dt" => { "buckets" => [{ "val" => "2020-10-22T23:00:00Z", "count" => 12 }, { "val" => "2020-07-06T23:00:00Z", "count" => 9 }] } } } + + it 'returns true' do + expect(search_data.single_data_year?).to be true + end + end + + context 'where there are multiple years represented by the data' do + let!(:facet_data) { { "date_dt" => { "buckets" => [{ "val" => "2017-10-22T23:00:00Z", "count" => 12 }, { "val" => "2020-07-06T23:00:00Z", "count" => 9 }] } } } + + it 'returns false' do + expect(search_data.single_data_year?).to be false + end + end + end + end + + describe 'years' do + context 'where search is nil' do + let(:search_data) { SearchData.new(nil) } + it 'returns an empty array' do + expect(search_data.years).to eq([]) + end + end + context 'where search is not nil' do + let!(:facet_data) { { "year" => { "buckets" => [{ "val" => "2017-01-01T00:00:00Z", "count" => 12 }, { "val" => "2020-01-01T00:00:00Z", "count" => 9 }] } } } + it 'returns years as date strings' do + expect(search_data.years).to eq([{ "count" => 9, "val" => "2020-01-01T00:00:00Z" }, { "count" => 12, "val" => "2017-01-01T00:00:00Z" }]) + end + end + end + + describe 'months' do + context 'where search is nil' do + let(:search_data) { SearchData.new(nil) } + it 'returns an empty array' do + expect(search_data.months("2017")).to eq([]) + end + end + context 'where search is not nil' do + let!(:facet_data) { { "month" => { "buckets" => [{ "val" => "2017-01-03T00:00:00Z", "count" => 12 }, { "val" => "2020-01-03T00:00:00Z", "count" => 9 }] } } } + it 'returns months for the given year as date strings' do + expect(search_data.months("2017")).to eq([{ "val" => "2017-01-03T00:00:00Z", "count" => 12 }]) + end + end + end + end