Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.0/txn search chart #373

Merged
merged 11 commits into from
Dec 15, 2023
Next Next commit
Refactor chart code to avoid hard coded class and group by
This is the preparation work to support transaction charts.
  • Loading branch information
sunnavy committed Dec 5, 2023
commit 7ac429de584d5fa8def554c576f9505e3727eb4a
6 changes: 5 additions & 1 deletion lib/RT/Report/Tickets.pm
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ sub SetupGroupings {

my @group_by = grep defined && length,
ref( $args{'GroupBy'} )? @{ $args{'GroupBy'} } : ($args{'GroupBy'});
@group_by = ('Status') unless @group_by;
@group_by = $self->DefaultGroupBy unless @group_by;

my $distinct_results = 1;
foreach my $e ( splice @group_by ) {
Expand Down Expand Up @@ -1838,6 +1838,10 @@ sub _GroupingType {
return $GROUPINGS{$key};
}

sub DefaultGroupBy {
return 'Status';
}

RT::Base->_ImportOverlays();

1;
16 changes: 16 additions & 0 deletions lib/RT/SearchBuilder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,22 @@ sub RecordClass {
$_[0]->_SingularClass
}

=head2 ReportClass

Returns report class name of this collection. E.g. report class of RT::Tickets
is RT::Report::Tickets

=cut

sub ReportClass {
my $self = shift;
my $class = ref($self) || $self;
if ( $class =~ s/(?<=^RT::)/Report::/ ) {
return $class;
}
return undef;
}

=head2 RegisterCustomFieldJoin

Takes a pair of arguments, the first a class name and the second a callback
Expand Down
1 change: 1 addition & 0 deletions share/html/Elements/ShowSearch
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ if ($SavedSearch) {

if ( $SearchArg->{'SearchType'} eq 'Chart' ) {
$SearchArg->{'SavedChartSearchId'} ||= $SavedSearch;
$class = $SearchArg->{Class} if $SearchArg->{Class};
}

# XXX: dispatch to different handler here
Expand Down
7 changes: 5 additions & 2 deletions share/html/Search/Chart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ $ChartStyle => 'bar+table+sql'
@ChartFunction => 'COUNT'
$Width => undef
$Height => undef
$Class => 'RT::Tickets'
</%args>
<%init>
use GD;
Expand Down Expand Up @@ -101,8 +102,10 @@ my $plot_error = sub {
$m->comp( 'SELF:Plot', plot => $plot, %ARGS );
};

use RT::Report::Tickets;
my $report = RT::Report::Tickets->new( $session{'CurrentUser'} );
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my $report = $report_class->new( $session{'CurrentUser'} );

my %columns;
if ( $Cache and my $data = delete $session{'charts_cache'}{ $Cache } ) {
Expand Down
22 changes: 16 additions & 6 deletions share/html/Search/Chart.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@
%#
%# END BPS TAGGED BLOCK }}}
<%init>
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my $report = $report_class->new( $session{'CurrentUser'} );

my $default_value = {
Query => 'id > 0',
GroupBy => ['Status'],
GroupBy => [ $report->DefaultGroupBy ],
ChartStyle => 'bar+table+sql',
ChartFunction => ['COUNT'],
};
Expand All @@ -57,7 +62,7 @@

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

my @search_fields = ( qw(Query GroupBy StackedGroupBy ChartStyle ChartFunction Width Height ExtraQueryParams), grep $_, @ExtraQueryParams );
my @search_fields = ( qw(Query GroupBy StackedGroupBy ChartStyle ChartFunction Width Height Class ExtraQueryParams), grep $_, @ExtraQueryParams );
my $saved_search = $m->comp( '/Widgets/SavedSearch:new',
SearchType => 'Chart',
SearchFields => [@search_fields],
Expand Down Expand Up @@ -147,6 +152,7 @@
<form method="POST" action="<% RT->Config->Get('WebPath') %>/Search/Chart.html">
<input type="hidden" class="hidden" name="Query" value="<% $query{Query} %>" />
<input type="hidden" class="hidden" name="SavedChartSearchId" value="<% $saved_search->{SearchId} || 'new' %>" />
<input type="hidden" class="hidden" name="Class" value="<% $Class %>" />

% if ( $query{ExtraQueryParams} ) {
% for my $input ( ref $query{ExtraQueryParams} eq 'ARRAY' ? @{$query{ExtraQueryParams}} : $query{ExtraQueryParams} ) {
Expand All @@ -165,6 +171,7 @@
Default => $query{'GroupBy'}[0],
Stacked => $query{'GroupBy'}[0] eq ($query{StackedGroupBy} // '') ? 1 : 0,
StackedId => 'StackedGroupBy-1',
Class => $Class,
&>
</fieldset>
<fieldset><legend><% loc('and then') %></legend>
Expand All @@ -175,6 +182,7 @@
ShowEmpty => 1,
Stacked => $query{'GroupBy'}[1] && ($query{'GroupBy'}[1] eq ($query{StackedGroupBy} // '')) ? 1 : 0,
StackedId => 'StackedGroupBy-2',
Class => $Class,
&>
</fieldset>
<fieldset><legend><% loc('and then') %></legend>
Expand All @@ -185,19 +193,20 @@
ShowEmpty => 1,
Stacked => $query{'GroupBy'}[2] && ($query{'GroupBy'}[2] eq ($query{StackedGroupBy} // '')) ? 1 : 0,
StackedId => 'StackedGroupBy-3',
Class => $Class,
&>
</fieldset>
</&>

<&| /Widgets/TitleBox, title => loc("Calculate"), class => "chart-calculate" &>
<fieldset><legend><% loc('Calculate values of') %></legend>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[0] &>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[0], Class => $Class, &>
</fieldset>
<fieldset><legend><% loc('and then') %></legend>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[1] // q{}, ShowEmpty => 1 &>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[1] // q{}, ShowEmpty => 1, Class => $Class, &>
</fieldset>
<fieldset><legend><% loc('and then') %></legend>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[2] // q{}, ShowEmpty => 1 &>
<& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[2] // q{}, ShowEmpty => 1, Class => $Class, &>
</fieldset>
</&>

Expand Down Expand Up @@ -330,7 +339,7 @@

<div class="col-xl-6">
<div class="saved-search">
<& /Widgets/SavedSearch:show, %ARGS, Action => 'Chart.html', self => $saved_search, Title => loc('Saved charts'), AllowCopy => 1 &>
<& /Widgets/SavedSearch:show, Class => $Class, %ARGS, Action => 'Chart.html', self => $saved_search, Title => loc('Saved charts'), AllowCopy => 1 &>
</div>
</div>
</div>
Expand All @@ -340,4 +349,5 @@

<%ARGS>
@ExtraQueryParams => ()
$Class => 'RT::Tickets'
</%ARGS>
7 changes: 5 additions & 2 deletions share/html/Search/Elements/Chart
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ $Query => "id > 0"
@GroupBy => ()
$ChartStyle => 'bar+table+sql'
@ChartFunction => 'COUNT'
$Class => 'RT::Tickets'
</%args>
<%init>
use RT::Report::Tickets;

my $report = RT::Report::Tickets->new( $session{'CurrentUser'} );
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my $report = $report_class->new( $session{'CurrentUser'} );

my %columns = $report->SetupGroupings(
Query => $Query,
Expand Down
1 change: 1 addition & 0 deletions share/html/Search/Elements/ChartTable
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<%ARGS>
%Table => ()
$Query => 'id > 0'
$Class => 'RT::Tickets'
</%ARGS>
<%INIT>

Expand Down
2 changes: 1 addition & 1 deletion share/html/Search/Elements/EditSearches
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<div class="form-row">
<div class="label col-4"><&|/l&>Load saved search</&>:</div>
<div class="col-8 input-group">
<& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@LoadObjects, SearchType => $Type &>
<& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@LoadObjects, SearchType => $Type, Class => $Class &>
<input type="submit" class="button btn btn-primary" value="<% loc('Load') %>" id="SavedSearchLoadSubmit" name="SavedSearchLoadSubmit" />
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions share/html/Search/Elements/SelectChartFunction
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ while ( my ($value, $display) = splice @functions, 0, 2 ) {
$Name => 'ChartFunction'
$Default => 'COUNT'
$ShowEmpty => 0
$Class => $Class
</%ARGS>
<%INIT>
my @functions = RT::Report::Tickets->Statistics;
$Default = '' unless defined $Default;
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my @functions = $report_class->Statistics;
</%INIT>
9 changes: 6 additions & 3 deletions share/html/Search/Elements/SelectGroupBy
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@
%# END BPS TAGGED BLOCK }}}
<%args>
$Name => 'GroupBy'
$Default => 'Status'
$Default => ''
$Query => ''
$ShowEmpty => 0
$Stacked => 0
$StackedId => "Stacked$Name"
$Class => 'RT::Tickets'
</%args>
<select name="<% $Name %>" class="cascade-by-optgroup">
% if ( $ShowEmpty ) {
Expand Down Expand Up @@ -85,7 +86,9 @@ while ( my ($label, $value) = splice @options, 0, 2 ) {
</span>

<%init>
use RT::Report::Tickets;
my $report = RT::Report::Tickets->new( $session{'CurrentUser'} );
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my $report = $report_class->new( $session{'CurrentUser'} );
my @options = $report->Groupings( Query => $Query );
</%init>
1 change: 1 addition & 0 deletions share/html/Search/Elements/SelectSearchesForObjects
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ $SearchType => $Class eq 'RT::Transactions' ? 'Transaction' : $Class eq 'RT::Ass
% next if ($search->SubValue('SearchType')
% && $search->SubValue('SearchType') ne $SearchType);
% next if ($search->SubValue('SearchType') // '') eq 'RT::Transactions' && ($search->SubValue('ObjectType') // '') ne $ObjectType;
% next if $SearchType eq 'Chart' && ( $search->SubValue('Class') || 'RT::Tickets' ) ne ( $Class || 'RT::Tickets' );
<option value="<%ref($object)%>-<%$object->id%>-SavedSearch-<%$search->Id%>"><%$search->Description||loc('Unnamed search')%></option>
% }
</optgroup>
Expand Down
11 changes: 6 additions & 5 deletions share/html/Search/JSChart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ $Width => undef
$Height => undef
$SavedSearchId => ''
$StackedGroupBy => undef
$Class => 'RT::Tickets'
</%args>

% my $id = join '-', 'search-chart', $SavedSearchId || ();
Expand Down Expand Up @@ -188,12 +189,12 @@ $Width ||= ($ChartStyle =~ /\bpie\b/ ? 400 : 600);
$Height ||= ($ChartStyle =~ /\bpie\b/ ? $Width : 400);
$Height = $Width if $ChartStyle =~ /\bpie\b/;

use RT::Report::Tickets;
my $report = RT::Report::Tickets->new( $session{'CurrentUser'} );
my $report_class = ( $Class || 'RT::Tickets' )->ReportClass
or Abort( loc( "Couldn't find ReportClass for [_1]", $Class || 'RT::Tickets' ) );
RT::StaticUtil::RequireModule($report_class) or Abort( loc("Couldn't load [_1]", $report_class) );
my $report = $report_class->new( $session{'CurrentUser'} );

# Default GroupBy we use in RT::Report::Tickets, we also need it here to
# generate sub queries.
@GroupBy = 'Status' unless @GroupBy;
@GroupBy = $report_class->DefaultGroupBy unless @GroupBy;

my %columns;
if ( $Cache and my $data = delete $session{'charts_cache'}{ $Cache } ) {
Expand Down
2 changes: 2 additions & 0 deletions share/html/Widgets/SavedSearch
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ $defaults => {}
$self->{CurrentSearch}{Object} ?
( Object => $self->{CurrentSearch}{Object},
Description => $self->{CurrentSearch}{Object}->Description, ) : (),
Class => $Class,
&><br />
<%PERL>
foreach my $field ( @{$self->{SearchFields}} ) {
Expand All @@ -197,6 +198,7 @@ $self => undef
$Action => ''
$Title => loc('Saved searches')
$AllowCopy => 0
$Class => 'RT::Tickets'
</%ARGS>
<%init>
</%init>
Expand Down