diff --git a/bin/cliservstats.pl b/bin/cliservstats.pl index 5f9b3f4..b367a1f 100644 --- a/bin/cliservstats.pl +++ b/bin/cliservstats.pl @@ -50,11 +50,8 @@ GetOptions ('c|captions!' => \$OptCaptions, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; # parse parameters -# TODO: $OptSums is currently a no-op -# $OptComments defaults to TRUE -$OptComments = 1 if (!defined($OptComments)); -# force --nocomments when --filetemplate is used -$OptComments = 0 if ($OptFileTemplate); +# $OptComments defaults to TRUE if --filetemplate is not used +$OptComments = 1 if (!$OptFileTemplate && !defined($OptComments)); # parse $OptType if ($OptType) { if ($OptType =~ /(host|server)s?/i) { @@ -166,24 +163,25 @@ $DBQuery->execute(@SQLBindNames) # set default to 'pretty' $OptFormat = 'pretty' if !$OptFormat; # print captions if --caption is set +my $LeadIn; if ($OptCaptions && $OptComments) { # print time period with report type my $CaptionReportType = '(number of postings for each month)'; if ($OptReportType and $OptReportType ne 'default') { $CaptionReportType = '(number of all postings for that time period)'; } - printf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); + $LeadIn .= sprintf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); # print name list if --names is set - printf("# ----- Names: %s\n",join(',',split(/:/,$OptNames))) + $LeadIn .= sprintf("# ----- Names: %s\n",join(',',split(/:/,$OptNames))) if $OptNames; # print boundaries, if set my $CaptionBoundary= '(counting only month fulfilling this condition)'; - printf("# ----- Threshold: %s %s x %s %s %s\n", + $LeadIn .= sprintf("# ----- Threshold: %s %s x %s %s %s\n", $LowBound ? $LowBound : '',$LowBound ? '=>' : '', $UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary) if ($LowBound or $UppBound); # print primary and secondary sort order - printf("# ----- Grouped by %s (%s), sorted %s%s\n", + $LeadIn .= sprintf("# ----- Grouped by %s (%s), sorted %s%s\n", ($GroupBy eq 'month') ? 'Months' : 'Names', ($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending', ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', @@ -191,7 +189,7 @@ if ($OptCaptions && $OptComments) { } # output data -&OutputData($OptFormat,$OptComments,$GroupBy,$Precision,'', +&OutputData($OptFormat,$OptComments,$GroupBy,$Precision,'',$LeadIn, $OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength); ### close handles @@ -261,7 +259,6 @@ using B<--nocomments>. Last but not least you can redirect all output to a number of files, e.g. one for each month, by submitting the B<--filetemplate> option, see below. -Captions and comments are automatically disabled in this case. =head2 Configuration @@ -429,10 +426,11 @@ False by default. =item B<--comments|--nocomments> -Add comments (group headers) to I and I output. True by default. +Add comments (group headers) to I and I output. True by default +as logn as B<--filetemplate> is not set. -Use I<--nocomments> to suppress anything except newsgroup names/months and -numbers of postings. This is enforced when using B<--filetemplate>, see below. +Use I<--nocomments> to suppress anything except host/client names or months and +numbers of postings. =item B<--filetemplate> I @@ -446,8 +444,6 @@ example with B<--filetemplate> I: stats-2012-02 ... and so on -B<--nocomments> is enforced, see above. - =item B<--db> I Override I or I from F. diff --git a/bin/groupstats.pl b/bin/groupstats.pl index f463a6e..cc13550 100755 --- a/bin/groupstats.pl +++ b/bin/groupstats.pl @@ -51,10 +51,8 @@ GetOptions ('b|boundary=s' => \$OptBoundType, 'h|help' => \&ShowPOD, 'V|version' => \&ShowVersion) or exit 1; # parse parameters -# $OptComments defaults to TRUE -$OptComments = 1 if (!defined($OptComments)); -# force --nocomments when --filetemplate is used -$OptComments = 0 if ($OptFileTemplate); +# $OptComments defaults to TRUE if --filetemplate is not used +$OptComments = 1 if (!$OptFileTemplate && !defined($OptComments)); # parse $OptBoundType if ($OptBoundType) { if ($OptBoundType =~ /level/i) { @@ -226,6 +224,7 @@ $DBQuery->execute(@SQLBindNewsgroups) # set default to 'pretty' $OptFormat = 'pretty' if !$OptFormat; # print captions if --caption is set +my $LeadIn; if ($OptCaptions && $OptComments) { # print time period with report type my $CaptionReportType= '(number of postings for each month)'; @@ -235,9 +234,9 @@ if ($OptCaptions && $OptComments) { $CaptionReportType= '(number of all postings for that time period)' if $OptReportType eq 'sum'; } - printf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); + $LeadIn .= sprintf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); # print newsgroup list if --newsgroups is set - printf("# ----- Newsgroups: %s\n",join(',',split(/:/,$OptNewsgroups))) + $LeadIn .= sprintf("# ----- Newsgroups: %s\n",join(',',split(/:/,$OptNewsgroups))) if $OptNewsgroups; # print boundaries, if set my $CaptionBoundary= '(counting only month fulfilling this condition)'; @@ -246,12 +245,12 @@ if ($OptCaptions && $OptComments) { $CaptionBoundary= '(on average)' if $OptBoundType eq 'average'; $CaptionBoundary= '(all month summed up)' if $OptBoundType eq 'sum'; } - printf("# ----- Threshold: %s %s x %s %s %s\n", + $LeadIn .= sprintf("# ----- Threshold: %s %s x %s %s %s\n", $LowBound ? $LowBound : '',$LowBound ? '=>' : '', $UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary) if ($LowBound or $UppBound); # print primary and secondary sort order - printf("# ----- Grouped by %s (%s), sorted %s%s\n", + $LeadIn .= sprintf("# ----- Grouped by %s (%s), sorted %s%s\n", ($GroupBy eq 'month') ? 'Months' : 'Newsgroups', ($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending', ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', @@ -260,7 +259,7 @@ if ($OptCaptions && $OptComments) { # output data &OutputData($OptFormat,$OptComments,$GroupBy,$Precision, - $OptCheckgroupsFile ? $ValidGroups : '', + $OptCheckgroupsFile ? $ValidGroups : '',$LeadIn, $OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength); ### close handles @@ -336,7 +335,6 @@ using B<--nocomments>. Last but not least you can redirect all output to a number of files, e.g. one for each month, by submitting the B<--filetemplate> option, see below. -Captions and comments are automatically disabled in this case. =head2 Configuration @@ -589,10 +587,11 @@ False by default. =item B<--comments|--nocomments> -Add comments (group headers) to I and I output. True by default. +Add comments (group headers) to I and I output. True by default +as logn as B<--filetemplate> is not set. Use I<--nocomments> to suppress anything except newsgroup names/months and -numbers of postings. This is enforced when using B<--filetemplate>, see below. +numbers of postings. =item B<--filetemplate> I @@ -606,8 +605,6 @@ example with B<--filetemplate> I: stats-2012-02 ... and so on -B<--nocomments> is enforced, see above. - =item B<--groupsdb> I Override I from F. diff --git a/doc/ChangeLog b/doc/ChangeLog index 379fa6b..9dd4129 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -16,6 +16,8 @@ NewsStats 0.3.0 (unreleased) - Fallback to last month if no month is given. - Add option handling, import VERSION, add POD. * Update README, INSTALL and ChangeLog. + * Don't enforce --nocomment for --filetemplate, just default to it. + Change caption handling, update documentation accordingly. NewsStats 0.2.0 (2025-05-10) * Redo directory structure: diff --git a/lib/NewsStats.pm b/lib/NewsStats.pm index cb376c0..d16965b 100644 --- a/lib/NewsStats.pm +++ b/lib/NewsStats.pm @@ -430,12 +430,13 @@ sub OutputData { ### $GroupBy : primary sorting order (month or key) ### $Precision: number of digits right of decimal point (0 or 2) ### $ValidKeys: reference to a hash containing all valid keys +### $LeadIn : print at start of output ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM ### $DBQuery : database query handle with executed query, ### containing $Month, $Key, $Value ### $PadField : padding length for key field (optional) for 'pretty' ### $PadValue : padding length for value field (optional) for 'pretty' - my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl, + my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $LeadIn, $FileTempl, $DBQuery, $PadField, $PadValue) = @_; my %ValidKeys = %{$ValidKeys} if $ValidKeys; my ($FileName, $Handle, $OUT); @@ -478,8 +479,8 @@ sub OutputData { $FileName)); $Handle = $OUT; }; - print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value, - $Precision, $PadField, $PadValue); + print $Handle &FormatOutput($Format, $Comments, $LeadIn, $Caption, + $Key, $Value, $Precision, $PadField, $PadValue); $LastIteration = $Caption; }; close $OUT if ($FileTempl); @@ -498,7 +499,7 @@ sub FormatOutput { ### $PadField : padding length for key field (optional) for 'pretty' ### $PadValue : padding length for value field (optional) for 'pretty' ### OUT: $Output: formatted output - my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadField, + my ($Format, $Comments, $LeadIn, $Caption, $Key, $Value, $Precision, $PadField, $PadValue) = @_; my ($Output); # keep last caption in mind @@ -514,8 +515,10 @@ sub FormatOutput { $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value); } elsif ($Format eq 'pretty') { # output as a table - $Output = sprintf ("# ----- %s:\n",$Caption) - if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration)); + if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration)) { + $Output = $LeadIn; + $Output .= sprintf ("# ----- %s:\n",$Caption); + } # increase $PadValue for numbers with decimal point $PadValue += $Precision+1 if $Precision; # add padding if $PadField is set; $PadValue HAS to be set then