Skip to content

Commit

Permalink
feat: track an empty result page as an error
Browse files Browse the repository at this point in the history
  • Loading branch information
danirod committed Sep 28, 2023
1 parent 5a3004e commit 7fe3608
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions app/models/search_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Table name: search_requests
#
# id :bigint not null, primary key
# count :integer default(0), not null
# error :string
# filters :jsonb not null
# page :integer default(1)
Expand All @@ -21,4 +22,8 @@ class SearchRequest < ApplicationRecord
def full_criteria
filters.merge(page:)
end

def empty_page?
count.zero?
end
end
4 changes: 2 additions & 2 deletions app/services/video_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def initialize(query, page: 1, filters: {})
end

def videos
Video.visible.includes(playlist: :topic).search(@query, meilisearch_filters).tap do
search_request.save
Video.visible.includes(playlist: :topic).search(@query, meilisearch_filters).tap do |v|
search_request.tap { |s| s.count = v.length }.save
end
rescue MeiliSearch::CommunicationError => e
search_request.update(error: e.message)
Expand Down
10 changes: 9 additions & 1 deletion app/views/dashboard/searches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<tr>
<th>Búsqueda</th>
<th>Filtros</th>
<th>Error</th>
<th colspan="2">Error</th>
<th>Fecha</th>
</tr>
</thead>
Expand All @@ -28,6 +28,14 @@
title: search.error) %>
<% end %>
</td>
<td>
<% if search.empty_page? %>
<%= tag(:i,
data: { 'bs-toggle': 'tooltip' },
class: 'bi bi-database-x',
title: t('.no_results')) %>
<% end %>
</td>
<td><%= search.created_at %></td>
</tr>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/locales/dashboard.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ es:
show:
searches: Búsquedas
dashboard: Panel de control
no_results: No proporcionó resultados
pending:
tags:
show:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddResultCountToSearchRequest < ActiveRecord::Migration[7.0]
def change
add_column :search_requests, :count, :integer, default: 0, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_09_28_082907) do
ActiveRecord::Schema[7.0].define(version: 2023_09_28_104909) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -130,6 +130,7 @@
t.integer "page", default: 1
t.jsonb "filters", default: "{}", null: false
t.string "error"
t.integer "count", default: 0, null: false
t.index ["filters"], name: "index_search_requests_on_filters", using: :gin
t.index ["query"], name: "index_search_requests_on_query"
end
Expand Down

0 comments on commit 7fe3608

Please sign in to comment.