5 # This script will get statistical data on newgroup usage
8 # It is part of the NewsStats package.
10 # Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
12 # It can be redistributed and/or modified under the same terms under
13 # which Perl itself is published.
16 our $VERSION = "0.01";
18 push(@INC, dirname($0));
22 use NewsStats qw(:DEFAULT :TimePeriods :Output :SQLHelper);
26 ################################# Main program #################################
28 ### read commandline options
29 my %Options = &ReadOptions('m:p:n:o:t:l:b:iscqdg:');
31 ### read configuration
32 my %Conf = %{ReadConfig('newsstats.conf')};
34 ### override configuration via commandline options
36 $ConfOverride{'DBTableGrps'} = $Options{'g'} if $Options{'g'};
37 &OverrideConfig(\%Conf,\%ConfOverride);
39 ### check for incompatible command line options
40 # you can't mix '-t', '-b' and '-l'
41 # -b/-l take preference over -t, and -b takes preference over -l
42 if ($Options{'b'} or $Options{'l'}) {
45 warn ("$MySelf: W: You cannot combine thresholds (-t) and top lists (-b) or levels (-l). Threshold '-t $Options{'t'}' was ignored.\n");
48 if ($Options{'b'} and $Options{'l'}) {
50 warn ("$MySelf: W: You cannot combine top lists (-b) and levels (-l). Level '-l $Options{'l'}' was ignored.\n");
53 # -q/-d don't work with -b or -l
54 warn ("$MySelf: W: Sorting by number of postings (-q) ignored due to top list mode (-b) / levels (-l).\n") if $Options{'q'};
55 warn ("$MySelf: W: Reverse sorting (-d) ignored due to top list mode (-b) / levels (-l).\n") if $Options{'d'};
59 # default output type to 'dump'
60 $Options{'o'} = 'dump' if !$Options{'o'};
61 # fail if more than one newsgroup is combined with 'dumpgroup' type
62 die ("$MySelf: E: You cannot combine newsgroup lists (-n) with more than one group with '-o dumpgroup'!\n") if ($Options{'o'} eq 'dumpgroup' and defined($Options{'n'}) and $Options{'n'} =~ /:|\*/);
63 # accept 'dumpgroup' only with -n
64 if ($Options{'o'} eq 'dumpgroup' and !defined($Options{'n'})) {
65 $Options{'o'} = 'dump';
66 warn ("$MySelf: W: You must submit exactly one newsgroup ('-n news.group') for '-o dumpgroup'. Output type was set to 'dump'.\n");
68 # set output type to 'pretty' for -l
70 $Options{'o'} = 'pretty';
71 warn ("$MySelf: W: Output type forced to '-o pretty' due to usage of '-l'.\n");
75 my ($StartMonth,$EndMonth) = &GetTimePeriod($Options{'m'},$Options{'p'});
76 # reset to one month for 'dump' output type
77 if ($Options{'o'} eq 'dump' and $Options{'p'}) {
78 warn ("$MySelf: W: You cannot combine time periods (-p) with '-o dump', changing output type to '-o pretty'.\n");
79 $Options{'o'} = 'pretty';
83 my $DBHandle = InitDB(\%Conf,1);
86 # get list of newsgroups (-n)
87 my ($QueryGroupList,$QueryThreshold,@GroupList,@Params);
88 my $Newsgroups = $Options{'n'};
90 # explode list of newsgroups for WHERE clause
91 ($QueryGroupList,@GroupList) = &SQLGroupList($Newsgroups);
93 # set to dummy value (always true)
98 if (defined($Options{'t'})) {
100 # -i: list groups below threshold
101 $QueryThreshold .= ' postings < ?';
103 # default: list groups above threshold
104 $QueryThreshold .= ' postings > ?';
106 # push threshold to Params
107 push @Params,$Options{'t'};
109 # set to dummy value (always true)
113 # construct WHERE clause
114 # $QueryGroupList is "list of newsgroup" (or 1),
115 # $QueryThreshold is threshold definition (or 1),
116 # &SQLHierarchies() takes care of the exclusion of hierarchy levels (.ALL)
117 # according to setting of -s
118 my $WhereClause = sprintf('month BETWEEN ? AND ? AND %s AND %s %s',$QueryGroupList,$QueryThreshold,&SQLHierarchies($Options{'s'}));
120 # get lenght of longest newsgroup delivered by query for formatting purposes
122 my $MaxLength = &GetMaxLenght($DBHandle,$Conf{'DBTableGrps'},'newsgroup',$WhereClause,$StartMonth,$EndMonth,(@GroupList,@Params));
124 my ($OrderClause,$DBQuery);
125 # -b (best of / top list) defined?
126 if (!defined($Options{'b'}) and !defined($Options{'l'})) {
127 # default: neither -b nor -l
128 # set ordering (ORDER BY) to "newsgroups" or "postings", "ASC" or "DESC"
129 # according to -q and -d
130 $OrderClause = 'newsgroup';
131 $OrderClause = 'postings' if $Options{'q'};
132 $OrderClause .= ' DESC' if $Options{'d'};
133 # prepare query: get number of postings per group from groups table for given months and newsgroups
134 $DBQuery = $DBHandle->prepare(sprintf("SELECT month,newsgroup,postings FROM %s.%s WHERE %s ORDER BY month,%s",$Conf{'DBDatabase'},$Conf{'DBTableGrps'},$WhereClause,$OrderClause));
135 } elsif ($Options{'b'}) {
136 # -b is set (then -l can't be!)
137 # set sorting order (-i)
139 $OrderClause = 'postings';
141 $OrderClause = 'postings DESC';
143 # set -b to 10 if < 1 (Top 10)
144 $Options{'b'} = 10 if $Options{'b'} !~ /^\d*$/ or $Options{'b'} < 1;
145 # push LIMIT to Params
146 push @Params,$Options{'b'};
147 # prepare query: get sum of postings per group from groups table for given months and newsgroups with LIMIT
148 $DBQuery = $DBHandle->prepare(sprintf("SELECT newsgroup,SUM(postings) AS postings FROM %s.%s WHERE %s GROUP BY newsgroup ORDER BY %s,newsgroup LIMIT ?",$Conf{'DBDatabase'},$Conf{'DBTableGrps'},$WhereClause,$OrderClause));
150 # -l must be set now, as all other cases have been taken care of
151 # set sorting order (-i)
158 # push level and $StartMonth,$EndMonth - again - to Params
159 # FIXME -- together with the query (see below)
160 push @Params,$Options{'l'};
161 push @Params,$StartMonth,$EndMonth;
162 # prepare query: get number of postings per group from groups table for given months and
163 # FIXME -- this query is ... in dire need of impromevent
164 $DBQuery = $DBHandle->prepare(sprintf("SELECT month,newsgroup,postings FROM %s.%s WHERE newsgroup IN (SELECT newsgroup FROM %s.%s WHERE %s GROUP BY newsgroup HAVING MAX(postings) %s ?) AND %s ORDER BY newsgroup,month",$Conf{'DBDatabase'},$Conf{'DBTableGrps'},$Conf{'DBDatabase'},$Conf{'DBTableGrps'},$WhereClause,$Level,$WhereClause));
168 $DBQuery->execute($StartMonth,$EndMonth,@GroupList,@Params)
169 or die sprintf("$MySelf: E: Can't get groups data for %s to %s from %s.%s: %s\n",$StartMonth,$EndMonth,$Conf{'DBDatabase'},$Conf{'DBTableGrps'},$DBI::errstr);
172 # print caption (-c) with time period if -m or -p is set
173 # FIXME - month or period should handled differently
174 printf ("----- Report from %s to %s\n",$StartMonth,$EndMonth) if $Options{'c'} and ($Options{'m'} or $Options{'p'});
175 # print caption (-c) with newsgroup list if -n is set
176 printf ("----- Newsgroups: %s\n",join(',',split(/:/,$Newsgroups))) if $Options{'c'} and $Options{'n'};
177 # print caption (-c) with threshold if -t is set, taking -i in account
178 printf ("----- Threshold: %s %u\n",$Options{'i'} ? '<' : '>',$Options{'t'}) if $Options{'c'} and $Options{'t'};
179 if (!defined($Options{'b'}) and !defined($Options{'l'})) {
180 # default: neither -b nor -l
181 &OutputData($Options{'o'},$DBQuery,$MaxLength);
182 } elsif ($Options{'b'}) {
183 # -b is set (then -l can't be!)
184 # we have to read in the query results ourselves, as they do not have standard layout
185 while (my ($Newsgroup,$Postings) = $DBQuery->fetchrow_array) {
186 # we just assign "top x" or "bottom x" instead of a month for the caption
188 print &FormatOutput($Options{'o'}, ($Options{'i'} ? 'Bottom ' : 'Top ').$Options{'b'}, $Newsgroup, $Postings, $MaxLength);
191 # -l must be set now, as all other cases have been taken care of
192 # we have to read in the query results ourselves, as they do not have standard layout
193 while (my ($Month,$Newsgroup,$Postings) = $DBQuery->fetchrow_array) {
194 # we just switch $Newsgroups and $Month for output generation
196 print &FormatOutput($Options{'o'}, $Newsgroup, $Month, $Postings, 7);
201 $DBHandle->disconnect;
205 ################################ Documentation #################################
209 groupstats - create reports on newsgroup usage
213 B<groupstats> [B<-Vhiscqd>] [B<-m> I<YYYY-MM>] [B<-p> I<YYYY-MM:YYYY-MM>] [B<-n> I<newsgroup(s)>] [B<-t> I<threshold>] [B<-l> I<level>] [B<-b> I<number>] [B<-o> I<output type>] [B<-g> I<database table>]
217 See doc/README: Perl 5.8.x itself and the following modules from CPAN:
233 This script create reports on newsgroup usage (number of postings per
234 group per month) taken from result tables created by
237 The time period to act on defaults to last month; you can assign
238 another month via the B<-m> switch or a time period via the B<-p>
239 switch; the latter takes preference.
241 B<groupstats> will process all newsgroups by default; you can limit
242 that to only some newsgroups by supplying a list of those groups via
243 B<-n> (see below). You can include hierarchy levels in the output by
244 adding the B<-s> switch (see below).
246 Furthermore you can set a threshold via B<-t> so that only newsgroups
247 with more postings per month will be included in the report. You can
248 invert that by the B<-i> switch so only newsgroups with less than
249 I<threshold> postings per month will be included.
251 You can sort the output by number of postings per month instead of the
252 default (alphabetical list of newsgroups) by using B<-q>; you can
253 reverse the sorting order (from highest to lowest or in reversed
254 alphabetical order) by using B<-d>.
256 Furthermore, you can create a list of newsgroups that had consistently
257 more (or less) than x postings per month during the whole report
258 period by using B<-l> (together with B<i> as needed).
260 Last but not least you can create a "best of" list of the top x
261 newsgroups via B<-b> (or a "worst of" list by adding B<i>).
263 By default, B<groupstats> will dump a very simple alphabetical list of
264 newsgroups, one per line, followed by the number of postings in that
265 month. This output format of course cannot sensibly be combined with
266 time periods, so you can set the output format by using B<-o> (see
267 below). Captions can be added by setting the B<-c> switch.
271 F<groupstats.pl> will read its configuration from F<newsstats.conf>
272 which should be present in the same directory via Config::Auto.
274 See doc/INSTALL for an overview of possible configuration options.
276 You can override configuration options via the B<-g> switch.
282 =item B<-V> (version)
284 Print out version and copyright information on B<yapfaq> and exit.
288 Print this man page and exit.
290 =item B<-m> I<YYYY-MM> (month)
292 Set processing period to a month in YYYY-MM format. Ignored if B<-p>
295 =item B<-p> I<YYYY-MM:YYYY-MM> (period)
297 Set processing period to a time period between two month, each in
298 YYYY-MM format, separated by a colon. Overrides B<-m>.
300 =item B<-n> I<newsgroup(s)> (newsgroups)
302 Limit processing to a certain set of newsgroups. I<newsgroup(s)> can
303 be a single newsgroup name (de.alt.test), a newsgroup hierarchy
304 (de.alt.*) or a list of either of these, separated by colons, for
307 de.test:de.alt.test:de.newusers.*
309 =item B<-t> I<threshold> (threshold)
311 Only include newsgroups with more than I<threshold> postings per
312 month. Can be inverted by the B<-i> switch so that only newsgroups
313 with less than I<threshold> postings will be included.
315 This setting will be ignored if B<-l> or B<-b> is set.
317 =item B<-l> I<level> (level)
319 Only include newsgroups with more than I<level> postings per
320 month, every month during the whole reporting period. Can be inverted
321 by the B<-i> switch so that only newsgroups with less than I<level>
322 postings every single month will be included. Output will be ordered
323 by newsgroup name, followed by month.
325 This setting will be ignored if B<-b> is set. Overrides B<-t> and
326 can't be used together with B<-q> or B<-d>.
328 =item B<-b> I<n> (best of)
330 Create a list of the I<n> newsgroups with the most postings over the
331 whole reporting period. Can be inverted by the B<-i> switch so that a
332 list of the I<n> newsgroups with the least postings over the whole
333 period is generated. Output will be ordered by sum of postings.
335 Overrides B<-t> and B<-l> and can't be used together with B<-q> or
336 B<-d>. Output format is set to I<pretty> (see below).
340 Used in conjunction with B<-t>, B<-l> or B<-b> to set a lower
341 threshold or level or generate a "bottom list" instead of a top list.
343 =item B<-s> (sum per hierarchy level)
345 Include "virtual" groups for every hierarchy level in output, for
352 See the B<gatherstats> man page for details.
354 =item B<-o> I<output type> (output format)
356 Set output format. Default is I<dump>, consisting of an alphabetical
357 list of newsgroups, each on a new line, followed by the number of
358 postings in that month. This default format can't be used with time
359 periods of more than one month.
361 I<list> format is like I<dump>, but will print the month in front of
364 I<dumpgroup> format can only be use with a group list (see B<-n>) of
365 exactly one newsgroup and is like I<dump>, but will output months,
366 followed by the number of postings.
368 If you don't need easily parsable output, you'll mostly use I<pretty>
369 format, which will print a header for each new month and try to align
370 newsgroup names and posting counts. Usage of B<-b> will force this
373 =item B<-c> (captions)
375 Add captions to output (reporting period, newsgroups list, threshold).
377 =item B<-q> (quantity of postings)
379 Sort by number of postings instead of by newsgroup names.
381 Cannot be used with B<-l> or B<-b>.
383 =item B<-d> (descending)
385 Change sort order to descending.
387 Cannot be used with B<-l> or B<-b>.
389 =item B<-g> I<table> (postings per group table)
391 Override I<DBTableGrps> from F<newsstats.conf>.
401 Show number of postings per group for lasth month in I<dump> format:
405 Show that report for January of 2010 and de.alt.* plus de.test,
406 including display of hierarchy levels:
408 groupstats -m 2010-01 -n de.alt.*:de.test -s
410 Show that report for the year of 2010 in I<pretty> format:
412 groupstats -p 2010-01:2010-12 -o pretty
414 Only show newsgroups with less than 30 postings last month, ordered
415 by number of postings, descending, in I<pretty> format:
417 groupstats -iqdt 30 -o pretty
419 Show top 10 for the first half-year of of 2010 in I<pretty> format:
421 groupstats -p 2010-01:2010-06 -b 10 -o pretty
423 Report all groups that had less than 30 postings every singele month
424 in the year of 2010 (I<pretty> format is forced)
426 groupstats -p 2010-01:2010-12 -il 30
432 =item F<groupstats.pl>
436 =item F<NewsStats.pm>
438 Library functions for the NewsStats package.
440 =item F<newsstats.conf>
442 Runtime configuration file for B<yapfaq>.
448 Please report any bugs or feature requests to the author or use the
449 bug tracker at L<http://bugs.th-h.de/>!
469 This script is part of the B<NewsStats> package.
473 Thomas Hochstein <thh@inter.net>
475 =head1 COPYRIGHT AND LICENSE
477 Copyright (c) 2010 Thomas Hochstein <thh@inter.net>
479 This program is free software; you may redistribute it and/or modify it
480 under the same terms as Perl itself.