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);
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 'h|help' => \&ShowPOD,
53 'V|version' => \&ShowVersion) or exit 1;
55 ### read configuration
56 my %Conf = %{ReadConfig('')};
58 ### override configuration via commandline options
60 $ConfOverride{'DBTableRaw'} = $OptRawDB if $OptRawDB;
61 $ConfOverride{'DBTableGrps'} = $OptGroupsDB if $OptGroupsDB;
62 $ConfOverride{'DBTableClnts'} = $OptClientsDB if $OptClientsDB;
63 $ConfOverride{'DBTableHosts'} = $OptHostsDB if $OptHostsDB;
64 $ConfOverride{'TLH'} = $OptTLH if $OptTLH;
65 &OverrideConfig(\%Conf,\%ConfOverride);
67 ### get type of information to gather, defaulting to 'all'
68 $OptStatsType = 'all' if !$OptStatsType;
69 &Bleat(2, sprintf("Unknown type '%s'!", $OptStatsType))
70 if !exists($LegalStats{$OptStatsType});
72 ### get time period from --month
73 # get verbal description of time period, drop SQL code
74 my ($Period) = &GetTimePeriod($OptMonth);
75 &Bleat(2,"--month option has an invalid format - please use 'YYYY-MM' or ".
76 "'YYYY-MM:YYYY-MM'!") if (!$Period or $Period eq 'all time');
78 ### reformat $Conf{'TLH'}
81 # $Conf{'TLH'} is parsed as an array by Config::Auto;
82 # make a flat list again, separated by :
83 if (ref($Conf{'TLH'}) eq 'ARRAY') {
84 $TLH = join(':',@{$Conf{'TLH'}});
90 # add trailing dots if none are present yet
91 # (using negative look-behind assertions)
92 $TLH =~ s/(?<!\.):/.:/g;
93 $TLH =~ s/(?<!\.)$/./;
94 # check for illegal characters
95 &Bleat(2,'Config error - illegal characters in TLH definition!')
96 if ($TLH !~ /^[a-zA-Z0-9:+.-]+$/);
100 # reformat $TLH from a:b to (a)|(b),
101 # e.g. replace ':' by ')|('
103 $TLH = '(' . $TLH . ')';
108 my $DBHandle = InitDB(\%Conf,1);
110 ### get data for each month
111 &Bleat(1,'Test mode. Database is not updated.') if $OptTest;
112 foreach my $Month (&ListMonth($Period)) {
114 print "---------- $Month ----------\n" if $OptDebug;
116 if ($OptStatsType eq 'all' or $OptStatsType eq 'groups') {
117 # read list of newsgroups from --checkgroups
119 my %ValidGroups = %{ReadGroupList(sprintf('%s-%s',$OptCheckgroupsFile,$Month))}
120 if $OptCheckgroupsFile;
122 ### ----------------------------------------------
123 ### get groups data (number of postings per group)
124 # get groups data from raw table for given month
125 my $DBQuery = $DBHandle->prepare(sprintf("SELECT newsgroups FROM %s.%s ".
126 "WHERE day LIKE ? AND NOT disregard",
128 $Conf{'DBTableRaw'}));
129 $DBQuery->execute($Month.'-%')
130 or &Bleat(2,sprintf("Can't get groups data for %s from %s.%s: ".
131 "$DBI::errstr\n",$Month,
132 $Conf{'DBDatabase'},$Conf{'DBTableRaw'}));
134 # count postings per group
136 while (($_) = $DBQuery->fetchrow_array) {
137 # get list of newsgroups and hierarchies from Newsgroups:
138 my %Newsgroups = ListNewsgroups($_,$TLH,
139 $OptCheckgroupsFile ? \%ValidGroups : '');
140 # count each newsgroup and hierarchy once
141 foreach (sort keys %Newsgroups) {
146 # add valid but empty groups if --checkgroups is set
148 foreach (sort keys %ValidGroups) {
149 if (!defined($Postings{$_})) {
150 # add current newsgroup as empty group
152 warn (sprintf("ADDED: %s as empty group\n",$_));
153 # add empty hierarchies for current newsgroup as needed
154 foreach (ParseHierarchies($_)) {
155 my $Hierarchy = $_ . '.ALL';
156 if (!defined($Postings{$Hierarchy})) {
157 $Postings{$Hierarchy} = 0;
158 warn (sprintf("ADDED: %s as empty group\n",$Hierarchy));
165 # delete old data for that month
167 $DBQuery = $DBHandle->do(sprintf("DELETE FROM %s.%s WHERE month = ?",
168 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}),
170 or &Bleat(2,sprintf("Can't delete old groups data for %s from %s.%s: ".
171 "$DBI::errstr\n",$Month,
172 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}));
175 print "----- GroupStats -----\n" if $OptDebug;
176 foreach my $Newsgroup (sort keys %Postings) {
177 print "$Newsgroup => $Postings{$Newsgroup}\n" if $OptDebug;
180 $DBQuery = $DBHandle->prepare(sprintf("INSERT INTO %s.%s ".
181 "(month,newsgroup,postings) ".
184 $Conf{'DBTableGrps'}));
185 $DBQuery->execute($Month, $Newsgroup, $Postings{$Newsgroup})
186 or &Bleat(2,sprintf("Can't write groups data for %s/%s to %s.%s: ".
187 "$DBI::errstr\n",$Month,$Newsgroup,
188 $Conf{'DBDatabase'},$Conf{'DBTableGrps'}));
193 # other types of information go here - later on
198 $DBHandle->disconnect;
202 ################################ Documentation #################################
206 gatherstats - process statistical data from a raw source
210 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>]
218 This script will extract and process statistical information from a
219 database table which is fed from F<feedlog.pl> for a given time period
220 and write its results to (an)other database table(s). Entries marked
221 with I<'disregard'> in the database will be ignored; currently, you
222 have to set this flag yourself, using your database management tools.
223 You can exclude erroneous entries that way (e.g. automatic reposts
224 (think of cancels flood and resurrectors); spam; ...).
226 The time period to act on defaults to last month; you can assign
227 another time period or a single month via the B<--month> option (see
230 By default B<gatherstats> will process all types of information; you
231 can change that using the B<--stats> option and assigning the type of
232 information to process. Currently that doesn't matter yet as only
233 processing of the number of postings per group per month is
236 Possible information types include:
240 =item B<groups> (postings per group per month)
242 B<gatherstats> will examine Newsgroups: headers. Crosspostings will be
243 counted for each single group they appear in. Groups not in I<TLH>
246 B<gatherstats> will also add up the number of postings for each
247 hierarchy level, but only count each posting once. A posting to
248 de.alt.test will be counted for de.alt.test, de.alt.ALL and de.ALL,
249 respectively. A crossposting to de.alt.test and de.alt.admin, on the
250 other hand, will be counted for de.alt.test and de.alt.admin each, but
251 only once for de.alt.ALL and de.ALL.
253 Data is written to I<DBTableGrps> (see L<doc/INSTALL>); you can
254 override that default through the B<--groupsdb> option.
260 B<gatherstats> will read its configuration from F<newsstats.conf>
261 which should be present in the same directory via Config::Auto.
263 See L<doc/INSTALL> for an overview of possible configuration options.
265 You can override configuration options via the B<--hierarchy>,
266 B<--rawdb>, B<--groupsdb>, B<--clientsdb> and B<--hostsdb> options,
273 =item B<-V>, B<--version>
275 Print out version and copyright information and exit.
277 =item B<-h>, B<--help>
279 Print this man page and exit.
281 =item B<-d>, B<--debug>
283 Output debugging information to STDOUT while processing (number of
286 =item B<-t>, B<--test>
288 Do not write results to database. You should use B<--debug> in
289 conjunction with B<--test> ... everything else seems a bit pointless.
291 =item B<-m>, B<--month> I<YYYY-MM[:YYYY-MM]>
293 Set processing period to a single month in YYYY-MM format or to a time
294 period between two month in YYYY-MM:YYYY-MM format (two month, separated
297 =item B<-s>, B<--stats> I<type>
299 Set processing type to one of I<all> and I<groups>. Defaults to all
300 (and is currently rather pointless as only I<groups> has been
303 =item B<-c>, B<--checkgroups> I<filename template>
305 Check each group against a list of valid newsgroups read from a file,
306 one group on each line and ignoring everything after the first
307 whitespace (so you can use a file in checkgroups format or (part of)
308 your INN active file).
310 The filename is taken from I<filename template>, amended by each
311 B<--month> B<gatherstats> is processing in the form of I<template-YYYY-MM>,
314 gatherstats -m 2010-01:2010-12 -c checkgroups
316 will check against F<checkgroups-2010-01> for January 2010, against
317 F<checkgroups-2010-02> for February 2010 and so on.
319 Newsgroups not found in the checkgroups file will be dropped (and
320 logged to STDERR), and newsgroups found there but having no postings
321 will be added with a count of 0 (and logged to STDERR).
323 =item B<--hierarchy> I<TLH> (newsgroup hierarchy)
325 Override I<TLH> from F<newsstats.conf>.
327 =item B<--rawdb> I<table> (raw data table)
329 Override I<DBTableRaw> from F<newsstats.conf>.
331 =item B<--groupsdb> I<table> (postings per group table)
333 Override I<DBTableGrps> from F<newsstats.conf>.
335 =item B<--clientsdb> I<table> (client data table)
337 Override I<DBTableClnts> from F<newsstats.conf>.
339 =item B<--hostsdb> I<table> (host data table)
341 Override I<DBTableHosts> from F<newsstats.conf>.
351 Process all types of information for lasth month:
355 Do a dry run, showing results of processing:
357 gatherstats --debug --test
359 Process all types of information for January of 2010:
361 gatherstats --month 2010-01
363 Process only number of postings for the year of 2010,
364 checking against checkgroups-*:
366 gatherstats -m 2010-01:2010-12 -s groups -c checkgroups
372 =item F<bin/gatherstats.pl>
376 =item F<lib/NewsStats.pm>
378 Library functions for the NewsStats package.
380 =item F<etc/newsstats.conf>
382 Runtime configuration file.
388 Please report any bugs or feature requests to the author or use the
389 bug tracker at L<http://bugs.th-h.de/>!
405 This script is part of the B<NewsStats> package.
409 Thomas Hochstein <thh@inter.net>
411 =head1 COPYRIGHT AND LICENSE
413 Copyright (c) 2010-2012 Thomas Hochstein <thh@inter.net>
415 This program is free software; you may redistribute it and/or modify it
416 under the same terms as Perl itself.