One more default sorting order ("grouping").
[usenet/newsstats.git] / bin / groupstats.pl
index f1e566b..bcb2b61 100755 (executable)
@@ -4,12 +4,12 @@
 #
 # This script will get statistical data on newgroup usage
 # from a database.
 #
 # This script will get statistical data on newgroup usage
 # from a database.
-# 
+#
 # It is part of the NewsStats package.
 #
 # Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
 #
 # It is part of the NewsStats package.
 #
 # Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net>
 #
-# It can be redistributed and/or modified under the same terms under 
+# It can be redistributed and/or modified under the same terms under
 # which Perl itself is published.
 
 BEGIN {
 # which Perl itself is published.
 
 BEGIN {
@@ -32,7 +32,7 @@ Getopt::Long::config ('bundling');
 ### read commandline options
 my ($OptBoundType,$OptCaptions,$OptCheckgroupsFile,$OptComments,
     $OptFileTemplate,$OptFormat,$OptGroupBy,$OptGroupsDB,$LowBound,$OptMonth,
 ### read commandline options
 my ($OptBoundType,$OptCaptions,$OptCheckgroupsFile,$OptComments,
     $OptFileTemplate,$OptFormat,$OptGroupBy,$OptGroupsDB,$LowBound,$OptMonth,
-    $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound);
+    $OptNewsgroups,$OptOrderBy,$OptReportType,$OptSums,$UppBound,$OptConfFile);
 GetOptions ('b|boundary=s'   => \$OptBoundType,
             'c|captions!'    => \$OptCaptions,
             'checkgroups=s'  => \$OptCheckgroupsFile,
 GetOptions ('b|boundary=s'   => \$OptBoundType,
             'c|captions!'    => \$OptCaptions,
             'checkgroups=s'  => \$OptCheckgroupsFile,
@@ -48,6 +48,7 @@ GetOptions ('b|boundary=s'   => \$OptBoundType,
             'r|report=s'     => \$OptReportType,
             's|sums!'        => \$OptSums,
             'u|upper=i'      => \$UppBound,
             'r|report=s'     => \$OptReportType,
             's|sums!'        => \$OptSums,
             'u|upper=i'      => \$UppBound,
+            'conffile=s'     => \$OptConfFile,
             'h|help'         => \&ShowPOD,
             'V|version'      => \&ShowVersion) or exit 1;
 # parse parameters
             'h|help'         => \&ShowPOD,
             'V|version'      => \&ShowVersion) or exit 1;
 # parse parameters
@@ -82,7 +83,7 @@ if ($OptReportType) {
 my $ValidGroups = &ReadGroupList($OptCheckgroupsFile) if $OptCheckgroupsFile;
 
 ### read configuration
 my $ValidGroups = &ReadGroupList($OptCheckgroupsFile) if $OptCheckgroupsFile;
 
 ### read configuration
-my %Conf = %{ReadConfig('')};
+my %Conf = %{ReadConfig($OptConfFile)};
 
 ### override configuration via commandline options
 my %ConfOverride;
 
 ### override configuration via commandline options
 my %ConfOverride;
@@ -125,12 +126,17 @@ if ($OptBoundType and $OptBoundType ne 'default') {
 }
 
 ### get sort order and build SQL 'ORDER BY' clause
 }
 
 ### get sort order and build SQL 'ORDER BY' clause
+# force to 'month' for $OptReportType 'average' or 'sum'
+$OptGroupBy = 'month' if ($OptReportType and $OptReportType ne 'default');
 # default to 'newsgroup' for $OptBoundType 'level' or 'average'
 $OptGroupBy = 'newsgroup' if (!$OptGroupBy and
                               $OptBoundType and $OptBoundType ne 'default');
 # default to 'newsgroup' for $OptBoundType 'level' or 'average'
 $OptGroupBy = 'newsgroup' if (!$OptGroupBy and
                               $OptBoundType and $OptBoundType ne 'default');
-# force to 'month' for $OptReportType 'average' or 'sum'
-$OptGroupBy = 'month' if ($OptReportType and $OptReportType ne 'default');
+# default to 'newsgroup' if $OptGroupBy is not set and
+# just one newsgroup is requested, but more than one month
+$OptGroupBy = 'newsgroup' if (!$OptGroupBy and
+                              $OptMonth =~ /:/ and $OptNewsgroups !~ /[:*%]/);
 # parse $OptGroupBy to $GroupBy, create ORDER BY clause $SQLOrderClause
 # parse $OptGroupBy to $GroupBy, create ORDER BY clause $SQLOrderClause
+# if $OptGroupBy is still not set, SQLSortOrder() will default to 'month'
 my ($GroupBy,$SQLOrderClause) = SQLSortOrder($OptGroupBy, $OptOrderBy);
 # $GroupBy will contain 'month' or 'newsgroup' (parsed result of $OptGroupBy)
 # set it to 'month' or 'key' for OutputData()
 my ($GroupBy,$SQLOrderClause) = SQLSortOrder($OptGroupBy, $OptOrderBy);
 # $GroupBy will contain 'month' or 'newsgroup' (parsed result of $OptGroupBy)
 # set it to 'month' or 'key' for OutputData()
@@ -245,7 +251,7 @@ if ($OptCaptions && $OptComments) {
          ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '',
          ($OptOrderBy and $OptOrderBy =~ /-?desc$/i) ? 'descending' : 'ascending');
 }
          ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '',
          ($OptOrderBy and $OptOrderBy =~ /-?desc$/i) ? 'descending' : 'ascending');
 }
+
 # output data
 &OutputData($OptFormat,$OptComments,$GroupBy,$Precision,
             $OptCheckgroupsFile ? $ValidGroups : '',
 # output data
 &OutputData($OptFormat,$OptComments,$GroupBy,$Precision,
             $OptCheckgroupsFile ? $ValidGroups : '',
@@ -264,7 +270,7 @@ groupstats - create reports on newsgroup usage
 
 =head1 SYNOPSIS
 
 
 =head1 SYNOPSIS
 
-B<groupstats> [B<-Vhcs> B<--comments>] [B<-m> I<YYYY-MM>[:I<YYYY-MM>] | I<all>] [B<-n> I<newsgroup(s)>] [B<--checkgroups> I<checkgroups file>] [B<-r> I<report type>] [B<-l> I<lower boundary>] [B<-u> I<upper boundary>] [B<-b> I<boundary type>] [B<-g> I<group by>] [B<-o> I<order by>] [B<-f> I<output format>] [B<--filetemplate> I<filename template>] [B<--groupsdb> I<database table>]
+B<groupstats> [B<-Vhcs> B<--comments>] [B<-m> I<YYYY-MM>[:I<YYYY-MM>] | I<all>] [B<-n> I<newsgroup(s)>] [B<--checkgroups> I<checkgroups file>] [B<-r> I<report type>] [B<-l> I<lower boundary>] [B<-u> I<upper boundary>] [B<-b> I<boundary type>] [B<-g> I<group by>] [B<-o> I<order by>] [B<-f> I<output format>] [B<--filetemplate> I<filename template>] [B<--groupsdb> I<database table>] [--conffile I<filename>]
 
 =head1 REQUIREMENTS
 
 
 =head1 REQUIREMENTS
 
@@ -347,7 +353,7 @@ Print out version and copyright information and exit.
 
 Print this man page and exit.
 
 
 Print this man page and exit.
 
-=item B<-m>, B<--month> I<YYYY-MM[:YYYY-MM]|all> 
+=item B<-m>, B<--month> I<YYYY-MM[:YYYY-MM]|all>
 
 Set processing period to a single month in YYYY-MM format or to a time
 period between two month in YYYY-MM:YYYY-MM format (two month, separated
 
 Set processing period to a single month in YYYY-MM format or to a time
 period between two month in YYYY-MM:YYYY-MM format (two month, separated
@@ -593,6 +599,10 @@ B<--nocomments> is enforced, see above.
 
 Override I<DBTableGrps> from F<newsstats.conf>.
 
 
 Override I<DBTableGrps> from F<newsstats.conf>.
 
+=item B<--conffile> I<filename>
+
+Load configuration from I<filename> instead of F<newsstats.conf>.
+
 =back
 
 =head1 INSTALLATION
 =back
 
 =head1 INSTALLATION
This page took 0.012586 seconds and 4 git commands to generate.