Skip to content

Commit

Permalink
Support to sort/limit axis labels in search charts
Browse files Browse the repository at this point in the history
Previously charts were always sorted by labels in ascending order, this
commit enhances it to support descending order too, as well as sorting by
values(i.e. asset/ticket/transaction count).

Limiting the number of results is also implemented in this commit, with
which users can easily get a more friendly chart that contains significant
data only.
  • Loading branch information
sunnavy committed Mar 28, 2024
1 parent 54ab3ed commit 0c8ef9f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/RT/Interface/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ use RT::Interface::Web::ReportsRegistry;

our @SHORTENER_SEARCH_FIELDS
= qw/Class ObjectType BaseQuery Query Format RowsPerPage Order OrderBy ExtraQueryParams ResultPage/;
our @SHORTENER_CHART_FIELDS = qw/Width Height ChartStyle GroupBy ChartFunction StackedGroupBy/;
our @SHORTENER_CHART_FIELDS = qw/Width Height ChartStyle GroupBy ChartFunction StackedGroupBy ChartOrderBy ChartOrder ChartLimit/;

=head2 SquishedCSS $style
Expand Down
23 changes: 19 additions & 4 deletions lib/RT/Report.pm
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,12 @@ sub _FieldToFunction {

sub SortEntries {
my $self = shift;
my %args = (
ChartOrderBy => 'label',
ChartOrder => 'ASC',
ChartLimit => undef,
@_,
);

$self->_DoSearch if $self->{'must_redo_search'};
return unless $self->{'items'} && @{ $self->{'items'} };
Expand All @@ -772,16 +778,21 @@ sub SortEntries {
my @SORT_OPS;
my $by_multiple = sub ($$) {
for my $f ( @SORT_OPS ) {
my $r = $f->($_[0], $_[1]);
my $r = uc $f->($args{ChartOrder} eq 'ASC' ? ( $_[0], $_[1] ) : ( $_[1], $_[0] ) );
return $r if $r;
}
};

my @data = map [$_], @{ $self->{'items'} };

for ( my $i = 0; $i < @groups; $i++ ) {
my $group_by = $groups[$i];
my $idx = $i+1;
if ( $args{ChartOrderBy} eq 'value' && grep { $_ eq 'id' } $self->ColumnsList ) {
$_->[1] = $_->[0]->RawValue('id') for @data;
push @SORT_OPS, sub { $_[0][1] <=> $_[1][1] };
}

# Even if charts are sorted by value, it's still good to sort by labels next, to sort items with the same value.
for my $group_by ( @groups ) {
my $idx = @SORT_OPS + 1;
my $order = $group_by->{'META'}{Sort} || 'label';
my $method = $order =~ /label$/ ? 'LabelValue' : 'RawValue';

Expand Down Expand Up @@ -834,6 +845,10 @@ sub SortEntries {
map $_->[0],
sort $by_multiple @data
];

if ( $args{ChartLimit} ) {
$self->{'items'} = [ splice @{ $self->{'items'} }, 0, $args{ChartLimit} ];
}
}

sub PostProcessRecords {
Expand Down
2 changes: 1 addition & 1 deletion share/html/Search/Chart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ if ( $Cache and my $data = delete $session{'charts_cache'}{ $Cache } ) {
Function => \@ChartFunction,
);

$report->SortEntries;
$report->SortEntries( map { $_ => $ARGS{$_} } grep { $ARGS{$_} } qw(ChartOrderBy ChartOrder ChartLimit) );
}

my @data = ([],[]);
Expand Down
36 changes: 35 additions & 1 deletion share/html/Search/Chart.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@
GroupBy => [ $report->DefaultGroupBy ],
ChartStyle => 'bar+table+sql',
ChartFunction => ['COUNT'],
ChartOrderBy => 'label',
ChartOrder => 'ASC',
ChartLimit => '',
};

$m->callback( ARGSRef => \%ARGS, CallbackName => 'Initial' );

my $title = loc( "Grouped search results");

my @search_fields = ( qw(Query GroupBy StackedGroupBy ChartStyle ChartFunction Width Height Class ExtraQueryParams), grep $_, @ExtraQueryParams );
my @search_fields = ( qw(Query GroupBy StackedGroupBy ChartStyle ChartFunction Width Height Class ExtraQueryParams ChartOrderBy ChartOrder ChartLimit), grep $_, @ExtraQueryParams );
my $saved_search = $m->comp( '/Widgets/SavedSearch:new',
SearchType => 'Chart',
SearchFields => [@search_fields],
Expand Down Expand Up @@ -273,6 +276,37 @@
<input type="checkbox" id="ChartStyleIncludeSQL" name="ChartStyleIncludeSQL" class="custom-control-input" <% $query{ChartStyle} =~ /\bsql\b/ ? 'checked="checked"' : '' |n %>>
<label class="custom-control-label" for="ChartStyleIncludeSQL"><&|/l&>Include TicketSQL query</&></label>
</div>

<div class="form-row sorting">
<div class="label col-auto">
<span class="far fa-question-circle icon-helper" data-toggle="tooltip" data-placement="top" data-original-title="<&|/l&>Value only works for count calculations</&>"></span>
<&|/l&>Order by</&>:
</div>
<div class="value col-auto">
<select name="ChartOrderBy" class="form-control selectpicker">
<option value="label" <% $query{ChartOrderBy} eq 'label' ? 'selected="selected"' : '' |n %>><&|/l&>Label</&></option>
<option value="value" <% $query{ChartOrderBy} eq 'value' ? 'selected="selected"' : '' |n %>><&|/l&>Value</&></option>
</select>
</div>
<div class="value col-auto">
<select name="ChartOrder" class="form-control selectpicker">
<option value="ASC" <% $query{ChartOrder} eq 'ASC' ? 'selected="selected"' : '' |n %>><&|/l&>ASC</&></option>
<option value="DESC" <% $query{ChartOrder} eq 'DESC' ? 'selected="selected"' : '' |n %>><&|/l&>DESC</&></option>
</select>
</div>
<div class="label col-auto"><&|/l&>Limit</&>:</div>
<div class="value col-auto">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><&|/l&>top</&></span>
</div>
<input name="ChartLimit" size="3" class="form-control" value="<% $query{ChartLimit} // '' %>" />
<div class="input-group-append">
<span class="input-group-text"><&|/l&>items</&></span>
</div>
</div>
</div>
</div>
</&>

<script type="text/javascript">
Expand Down
2 changes: 1 addition & 1 deletion share/html/Search/Elements/Chart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ my %columns = $report->SetupGroupings(
Function => \@ChartFunction,
);

$report->SortEntries;
$report->SortEntries(map { $_ => $ARGS{$_} } grep { $ARGS{$_} } qw(ChartOrderBy ChartOrder ChartLimit));

my $query_string = $m->comp('/Elements/QueryString', %ARGS, GroupBy => \@GroupBy );

Expand Down
2 changes: 1 addition & 1 deletion share/html/Search/JSChart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ if ( $Cache and my $data = delete $session{'charts_cache'}{ $Cache } ) {
Function => \@ChartFunction,
);

$report->SortEntries;
$report->SortEntries( map { $_ => $ARGS{$_} } grep { $ARGS{$_} } qw(ChartOrderBy ChartOrder ChartLimit) );
}

my @data = ([],[]);
Expand Down

0 comments on commit 0c8ef9f

Please sign in to comment.