From 7ac429de584d5fa8def554c576f9505e3727eb4a Mon Sep 17 00:00:00 2001 From: sunnavy Date: Tue, 29 Mar 2022 22:27:11 +0800 Subject: [PATCH 01/11] Refactor chart code to avoid hard coded class and group by This is the preparation work to support transaction charts. --- lib/RT/Report/Tickets.pm | 6 ++++- lib/RT/SearchBuilder.pm | 16 ++++++++++++++ share/html/Elements/ShowSearch | 1 + share/html/Search/Chart | 7 ++++-- share/html/Search/Chart.html | 22 ++++++++++++++----- share/html/Search/Elements/Chart | 7 ++++-- share/html/Search/Elements/ChartTable | 1 + share/html/Search/Elements/EditSearches | 2 +- .../html/Search/Elements/SelectChartFunction | 7 ++++-- share/html/Search/Elements/SelectGroupBy | 9 +++++--- .../Search/Elements/SelectSearchesForObjects | 1 + share/html/Search/JSChart | 11 +++++----- share/html/Widgets/SavedSearch | 2 ++ 13 files changed, 70 insertions(+), 22 deletions(-) diff --git a/lib/RT/Report/Tickets.pm b/lib/RT/Report/Tickets.pm index 45af2a26685..9532acedb9c 100644 --- a/lib/RT/Report/Tickets.pm +++ b/lib/RT/Report/Tickets.pm @@ -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 ) { @@ -1838,6 +1838,10 @@ sub _GroupingType { return $GROUPINGS{$key}; } +sub DefaultGroupBy { + return 'Status'; +} + RT::Base->_ImportOverlays(); 1; diff --git a/lib/RT/SearchBuilder.pm b/lib/RT/SearchBuilder.pm index c134f176016..798e223c39b 100644 --- a/lib/RT/SearchBuilder.pm +++ b/lib/RT/SearchBuilder.pm @@ -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 diff --git a/share/html/Elements/ShowSearch b/share/html/Elements/ShowSearch index a86701b48e2..2202f40406d 100644 --- a/share/html/Elements/ShowSearch +++ b/share/html/Elements/ShowSearch @@ -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 diff --git a/share/html/Search/Chart b/share/html/Search/Chart index d5cead18243..5b3a19afcca 100644 --- a/share/html/Search/Chart +++ b/share/html/Search/Chart @@ -54,6 +54,7 @@ $ChartStyle => 'bar+table+sql' @ChartFunction => 'COUNT' $Width => undef $Height => undef +$Class => 'RT::Tickets' <%init> use GD; @@ -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 } ) { diff --git a/share/html/Search/Chart.html b/share/html/Search/Chart.html index cd15b50fa7f..a106fc508e4 100644 --- a/share/html/Search/Chart.html +++ b/share/html/Search/Chart.html @@ -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'], }; @@ -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], @@ -147,6 +152,7 @@
+ % if ( $query{ExtraQueryParams} ) { % for my $input ( ref $query{ExtraQueryParams} eq 'ARRAY' ? @{$query{ExtraQueryParams}} : $query{ExtraQueryParams} ) { @@ -165,6 +171,7 @@ Default => $query{'GroupBy'}[0], Stacked => $query{'GroupBy'}[0] eq ($query{StackedGroupBy} // '') ? 1 : 0, StackedId => 'StackedGroupBy-1', + Class => $Class, &>
<% loc('and then') %> @@ -175,6 +182,7 @@ ShowEmpty => 1, Stacked => $query{'GroupBy'}[1] && ($query{'GroupBy'}[1] eq ($query{StackedGroupBy} // '')) ? 1 : 0, StackedId => 'StackedGroupBy-2', + Class => $Class, &>
<% loc('and then') %> @@ -185,19 +193,20 @@ ShowEmpty => 1, Stacked => $query{'GroupBy'}[2] && ($query{'GroupBy'}[2] eq ($query{StackedGroupBy} // '')) ? 1 : 0, StackedId => 'StackedGroupBy-3', + Class => $Class, &>
<&| /Widgets/TitleBox, title => loc("Calculate"), class => "chart-calculate" &>
<% loc('Calculate values of') %> - <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[0] &> + <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[0], Class => $Class, &>
<% loc('and then') %> - <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[1] // q{}, ShowEmpty => 1 &> + <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[1] // q{}, ShowEmpty => 1, Class => $Class, &>
<% loc('and then') %> - <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[2] // q{}, ShowEmpty => 1 &> + <& Elements/SelectChartFunction, Default => $query{'ChartFunction'}[2] // q{}, ShowEmpty => 1, Class => $Class, &>
@@ -330,7 +339,7 @@
@@ -340,4 +349,5 @@ <%ARGS> @ExtraQueryParams => () +$Class => 'RT::Tickets' diff --git a/share/html/Search/Elements/Chart b/share/html/Search/Elements/Chart index a6cd9e0ada9..7079551846a 100644 --- a/share/html/Search/Elements/Chart +++ b/share/html/Search/Elements/Chart @@ -50,11 +50,14 @@ $Query => "id > 0" @GroupBy => () $ChartStyle => 'bar+table+sql' @ChartFunction => 'COUNT' +$Class => 'RT::Tickets' <%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, diff --git a/share/html/Search/Elements/ChartTable b/share/html/Search/Elements/ChartTable index 694d297b02e..175a3c56809 100644 --- a/share/html/Search/Elements/ChartTable +++ b/share/html/Search/Elements/ChartTable @@ -48,6 +48,7 @@ <%ARGS> %Table => () $Query => 'id > 0' +$Class => 'RT::Tickets' <%INIT> diff --git a/share/html/Search/Elements/EditSearches b/share/html/Search/Elements/EditSearches index c9db81f1b9e..7068315ec6b 100644 --- a/share/html/Search/Elements/EditSearches +++ b/share/html/Search/Elements/EditSearches @@ -123,7 +123,7 @@
<&|/l&>Load saved search:
-<& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@LoadObjects, SearchType => $Type &> +<& SelectSearchesForObjects, Name => 'SavedSearchLoad', Objects => \@LoadObjects, SearchType => $Type, Class => $Class &>
diff --git a/share/html/Search/Elements/SelectChartFunction b/share/html/Search/Elements/SelectChartFunction index 7a794fea4c3..ee1aeab4b44 100644 --- a/share/html/Search/Elements/SelectChartFunction +++ b/share/html/Search/Elements/SelectChartFunction @@ -72,8 +72,11 @@ while ( my ($value, $display) = splice @functions, 0, 2 ) { $Name => 'ChartFunction' $Default => 'COUNT' $ShowEmpty => 0 +$Class => $Class <%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; diff --git a/share/html/Search/Elements/SelectGroupBy b/share/html/Search/Elements/SelectGroupBy index e5c7e77c130..c35512dbc07 100644 --- a/share/html/Search/Elements/SelectGroupBy +++ b/share/html/Search/Elements/SelectGroupBy @@ -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'