reports: fix error when report is empty.
Fix an exception when a report is empty, for example when performing a tag search that returns no results: * https://betabooru.donmai.us/reports/posts?search[group]=uploader&search[tags]=does_not_exist
This commit is contained in:
@@ -18,7 +18,7 @@ class TimeSeriesComponent < ApplicationComponent
|
||||
|
||||
def chart_height
|
||||
if x_axis != "date"
|
||||
dataframe[x_axis].size * 30
|
||||
dataframe[x_axis]&.size.to_i * 30
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<% if mode == :chart && x_axis.present? %>
|
||||
<% if dataframe.empty? %>
|
||||
<p>No results found. Try modifying your search or expanding your time range.</p>
|
||||
<% elsif mode == :chart && x_axis.present? %>
|
||||
<div class="line-chart" style="width: 100%; height: max(90vh, <%= chart_height.to_i %>px);"></div>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
@@ -147,7 +147,7 @@ class ReportsController < ApplicationController
|
||||
@x_axis = @group
|
||||
end
|
||||
|
||||
@dataframe[@group] = @dataframe[@group].map(&:pretty_name) if @group.in?(%w[creator updater uploader banner approver user])
|
||||
@dataframe[@group] = @dataframe[@group].map(&:pretty_name) if @group.in?(%w[creator updater uploader banner approver user]) && @dataframe.names.include?(@group)
|
||||
@dataframe["date"] = @dataframe["date"].map(&:to_date) if @dataframe["date"]
|
||||
@dataframe = @dataframe.crosstab("date", @group) if @group && @period.present?
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
module Danbooru
|
||||
class DataFrame
|
||||
attr_reader :df
|
||||
delegate :head, :shape, :types, :rename, :each_row, :[], :[]=, to: :df
|
||||
delegate :head, :shape, :names, :types, :rename, :empty?, :each_row, :[], :[]=, to: :df
|
||||
|
||||
def initialize(...)
|
||||
@df = Rover::DataFrame.new(...)
|
||||
@@ -17,6 +17,8 @@ module Danbooru
|
||||
foreign_key = association.foreign_key
|
||||
name = association.name.to_s
|
||||
|
||||
next table if !table.names.include?(foreign_key)
|
||||
|
||||
ids = table[foreign_key].to_a.uniq.compact_blank
|
||||
records = association.klass.where(primary_key => ids).index_by(&primary_key.to_sym)
|
||||
|
||||
@@ -27,6 +29,8 @@ module Danbooru
|
||||
end
|
||||
|
||||
def crosstab(index, pivot)
|
||||
return self if empty?
|
||||
|
||||
new_df = DataFrame.new(index => df[index].uniq)
|
||||
|
||||
df[pivot].uniq.to_a.each do |value|
|
||||
|
||||
@@ -26,6 +26,18 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "work with the period and group params" do
|
||||
get report_path("posts", search: { period: "month", group: "uploader" })
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "work with the group param when the dataset is empty" do
|
||||
get report_path("posts", search: { tags: "does_not_exist", group: "uploader" })
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "work when filtering by a nested association" do
|
||||
get report_path("posts", search: { uploader: { name: @post.uploader.name }})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user