5 # This script will gather statistical information from a database
6 # containing headers and other information from a INN feed.
8 # It is part of the NewsStats package.
10 # Copyright (c) 2010-2013 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 # we're in .../bin, so our module is in ../lib
19 push(@INC, dirname($0).'/../lib');
24 use NewsStats qw(:DEFAULT :TimePeriods ListNewsgroups ParseHierarchies ReadGroupList);
27 use Getopt::Long qw(GetOptions);
28 Getopt::Long::config ('bundling');
30 ################################# Definitions ##################################
32 # define types of information that can be gathered
33 # all / groups (/ clients / hosts)
35 @LegalStats{('all','groups')} = ();
37 ################################# Main program #################################
39 ### read commandline options
40 my ($OptCheckgroupsFile,$OptClientsDB,$OptDebug,$OptGroupsDB,$OptTLH,
41 $OptHostsDB,$OptMonth,$OptRawDB,$OptStatsType,$OptTest,$OptConfFile);
42 GetOptions ('c|checkgroups=s' => \$OptCheckgroupsFile,
43 'clientsdb=s' => \$OptClientsDB,
44 'd|debug!' => \$OptDebug,
45 'groupsdb=s' => \$OptGroupsDB,
46 'hierarchy=s' => \$OptTLH,
47 'hostsdb=s' => \$OptHostsDB,
48 'm|month=s' => \$OptMonth,
49 'rawdb=s' => \$OptRawDB,
50 's|stats=s' => \$OptStatsType,
51 't|test!' => \$OptTest,
52 'conffile=s' => \$OptConfFile,
53 'h|help' => \&ShowPOD,
54 'V|version' => \&ShowVersion) or exit 1;
56 ### read configuration
57 my %Conf = %{ReadConfig($OptConfFile)};
59 ### override configuration via commandline options
61 $ConfOverride{'DBTableRaw'} = $OptRawDB if $OptRawDB;
62 $ConfOverride{'DBTableGrps'} = $OptGroupsDB if $OptGroupsDB;
63 $ConfOverride{'DBTableClnts'} = $OptClientsDB if $OptClientsDB;
64 $ConfOverride{'DBTableHosts'} = $OptHostsDB if $OptHostsDB;
65 $ConfOverride{'TLH'} = $OptTLH if $OptTLH;
66 &OverrideConfig(\%Conf,\%ConfOverride);
68 ### get type of information to gather, defaulting to 'all'
69 $OptStatsType = 'all' if !$OptStatsType;
70 &Bleat(2, sprintf("Unknown type '%s'!", $OptStatsType))
71 if !exists($LegalStats{$OptStatsType});
73 ### get time period from --month
74 # get verbal description of time period, drop SQL code
75 my ($Period) = &GetTimePeriod($OptMonth);
76 &Bleat(2,"--month option has an invalid format - please use 'YYYY-MM' or ".
77 "'YYYY-MM:YYYY-MM'!") if (!$Period or $Period eq 'all time');
79 ### reformat $Conf{'TLH'}
82 # $Conf{'TLH'} is parsed as an array by Config::Auto;
83 # make a flat list again, separated by :
84 if (ref($Conf{'TLH'}) eq 'ARRAY') {
85 $TLH = join(':',@{$Conf{'TLH'}});
91 # add trailing dots if none are present yet
92 # (using negative look-behind assertions)
93 $TLH =~ s/(?<!\.):/.:/g;
94 $TLH =~ s/(?<!\.)$/./;
95 # check for illegal characters
96 &Bleat(2,'Config error - illegal characters in TLH definition!')
97 if ($TLH !~ /^[a-zA-Z0-9:+.-]+$/);
101 # reformat $TLH from a:b to (a)|(b),
102 # e.g. replace ':' by ')|('
104 $TLH = '(' . $TLH . ')';
109 my $DBHandle = InitDB(\%Conf,1);
111 ### get data for each month
112 &Bleat(1,'Test mode. Database is not updated.') if $OptTest;
113 foreach my $Month (&ListMonth($Period)) {
115 print "---------- $Month ----------\n" if $OptDebug;
117 if ($OptStatsType eq 'all' or $OptStatsType eq 'groups') {
118 # read list of newsgroups from --checkgroups
120 my %ValidGroups = %{ReadGroupList(sprintf('%s-%s',$OptCheckgroupsFile,$Month))}
121 if $OptCheckgroupsFile;
123 ### ----------------------------------------------
124 ### get groups data (number of postings per group)
125 # get groups data from raw table for given month
126 my $DBQuery = $DBHandle->prepare(sprintf("SELECT newsgroups FROM %s.%s ".
127 "WHERE day LIKE ? AND NOT disregard",
129 $Conf{'DBTableRaw'}));
130 $DBQuery->execute($Month.'-%')
131 or &Bleat(2,sprintf("Can't get groups data for %s from %s.%s: ".
132 "$DBI::errstr\n",$Month,
133 $Conf{'DBDatabase'},$Conf{'DBTableRaw'}));
135 # count postings per group
137 while (($_) = $DBQuery->fetchrow_array) {
138 # get list of newsgroups and hierarchies from Newsgroups:
139 my %Newsgroups = ListNewsgroups($_,$TLH,
140 $OptCheckgroupsFile ? \%ValidGroups : '');
141 # count each newsgroup and hierarchy once
142 foreach (sort keys %Newsgroups) {
147 # add valid but empty groups if --checkgroups is set
149 foreach (sort keys %ValidGroups) {
150 if (!defined($Postings{$_})) {
151 # add current newsgroup as empty group
153 warn (sprintf("ADDED: %s as empty group\n",$_));
154 # add empty hierarchies for current newsgroup as needed
155 foreach (ParseHierarchies($_)) {
156 my $Hierarchy = $_ . '.ALL';
157 if (!defined($Postings{$Hierarchy})) {
158 $Postings{$Hierarchy} = 0;
159 warn (sprintf("ADDED: %s as empty group\n",$Hierarchy));
166 # delete old data for that month
168 $DBQuery = $DBHandle->do(sprintf("DELETE FROM %s.%s WHERE month = ?",
169 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}),
171 or &Bleat(2,sprintf("Can't delete old groups data for %s from %s.%s: ".
172 "$DBI::errstr\n",$Month,
173 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}));
176 print "----- GroupStats -----\n" if $OptDebug;
177 foreach my $Newsgroup (sort keys %Postings) {
178 print "$Newsgroup => $Postings{$Newsgroup}\n" if $OptDebug;
181 $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s ".
182 "(month,newsgroup,postings) ".
185 $Conf{'DBTableGrps'}));
186 $DBQuery->execute($Month, $Newsgroup, $Postings{$Newsgroup})
187 or &Bleat(2,sprintf("Can't write groups data for %s/%s to %s.%s: ".
188 "$DBI::errstr\n",$Month,$Newsgroup,
189 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}));
194 # other types of information go here - later on
199 $DBHandle->disconnect;
203 ################################ Documentation #################################
207 gatherstats - process statistical data from a raw source
211 B<gatherstats> [B<-Vhdt>] [B<-m> I<YYYY-MM> | I<YYYY-MM:YYYY-MM>] [B<-s> I<stats>] [B<-c> I<filename template>]] [B<--hierarchy> I<TLH>] [B<--rawdb> I<database table>] [B<-groupsdb> I<database table>] [B<--clientsdb> I<database table>] [B<--hostsdb> I<database table>] [--conffile I<filename>]
219 This script will extract and process statistical information from a
220 database table which is fed from F<feedlog.pl> for a given time period
221 and write its results to (an)other database table(s). Entries marked
222 with I<'disregard'> in the database will be ignored; currently, you
223 have to set this flag yourself, using your database management tools.
224 You can exclude erroneous entries that way (e.g. automatic reposts
225 (think of cancels flood and resurrectors); spam; ...).
227 The time period to act on defaults to last month; you can assign
228 another time period or a single month via the B<--month> option (see
231 By default B<gatherstats> will process all types of information; you
232 can change that using the B<--stats> option and assigning the type of
233 information to process. Currently that doesn't matter yet as only
234 processing of the number of postings per group per month is
237 Possible information types include:
241 =item B<groups> (postings per group per month)
243 B<gatherstats> will examine Newsgroups: headers. Crosspostings will be
244 counted for each single group they appear in. Groups not in I<TLH>
247 B<gatherstats> will also add up the number of postings for each
248 hierarchy level, but only count each posting once. A posting to
249 de.alt.test will be counted for de.alt.test, de.alt.ALL and de.ALL,
250 respectively. A crossposting to de.alt.test and de.alt.admin, on the
251 other hand, will be counted for de.alt.test and de.alt.admin each, but
252 only once for de.alt.ALL and de.ALL.
254 Data is written to I<DBTableGrps> (see L<doc/INSTALL>); you can
255 override that default through the B<--groupsdb> option.
261 B<gatherstats> will read its configuration from F<newsstats.conf>
262 which should be present in the same directory via Config::Auto.
264 See L<doc/INSTALL> for an overview of possible configuration options.
266 You can override configuration options via the B<--hierarchy>,
267 B<--rawdb>, B<--groupsdb>, B<--clientsdb> and B<--hostsdb> options,
274 =item B<-V>, B<--version>
276 Print out version and copyright information and exit.
278 =item B<-h>, B<--help>
280 Print this man page and exit.
282 =item B<-d>, B<--debug>
284 Output debugging information to STDOUT while processing (number of
287 =item B<-t>, B<--test>
289 Do not write results to database. You should use B<--debug> in
290 conjunction with B<--test> ... everything else seems a bit pointless.
292 =item B<-m>, B<--month> I<YYYY-MM[:YYYY-MM]>
294 Set processing period to a single month in YYYY-MM format or to a time
295 period between two month in YYYY-MM:YYYY-MM format (two month, separated
298 =item B<-s>, B<--stats> I<type>
300 Set processing type to one of I<all> and I<groups>. Defaults to all
301 (and is currently rather pointless as only I<groups> has been
304 =item B<-c>, B<--checkgroups> I<filename template>
306 Check each group against a list of valid newsgroups read from a file,
307 one group on each line and ignoring everything after the first
308 whitespace (so you can use a file in checkgroups format or (part of)
309 your INN active file).
311 The filename is taken from I<filename template>, amended by each
312 B<--month> B<gatherstats> is processing in the form of I<template-YYYY-MM>,
315 gatherstats -m 2010-01:2010-12 -c checkgroups
317 will check against F<checkgroups-2010-01> for January 2010, against
318 F<checkgroups-2010-02> for February 2010 and so on.
320 Newsgroups not found in the checkgroups file will be dropped (and
321 logged to STDERR), and newsgroups found there but having no postings
322 will be added with a count of 0 (and logged to STDERR).
324 =item B<--hierarchy> I<TLH> (newsgroup hierarchy)
326 Override I<TLH> from F<newsstats.conf>.
328 =item B<--rawdb> I<table> (raw data table)
330 Override I<DBTableRaw> from F<newsstats.conf>.
332 =item B<--groupsdb> I<table> (postings per group table)
334 Override I<DBTableGrps> from F<newsstats.conf>.
336 =item B<--clientsdb> I<table> (client data table)
338 Override I<DBTableClnts> from F<newsstats.conf>.
340 =item B<--hostsdb> I<table> (host data table)
342 Override I<DBTableHosts> from F<newsstats.conf>.
344 =item B<--conffile> I<filename>
346 Load configuration from I<filename> instead of F<newsstats.conf>.
356 Process all types of information for lasth month:
360 Do a dry run, showing results of processing:
362 gatherstats --debug --test
364 Process all types of information for January of 2010:
366 gatherstats --month 2010-01
368 Process only number of postings for the year of 2010,
369 checking against checkgroups-*:
371 gatherstats -m 2010-01:2010-12 -s groups -c checkgroups
377 =item F<bin/gatherstats.pl>
381 =item F<lib/NewsStats.pm>
383 Library functions for the NewsStats package.
385 =item F<etc/newsstats.conf>
387 Runtime configuration file.
393 Please report any bugs or feature requests to the author or use the
394 bug tracker at L<http://bugs.th-h.de/>!
410 This script is part of the B<NewsStats> package.
414 Thomas Hochstein <thh@inter.net>
416 =head1 COPYRIGHT AND LICENSE
418 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
420 This program is free software; you may redistribute it and/or modify it
421 under the same terms as Perl itself.