]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-updatesqlcache
we want paged results even while using --quick in updatesqlcache
[debbugs.git] / bin / debbugs-updatesqlcache
1 #! /usr/bin/perl
2 # debbugs-updatesqlcache is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2016 by Don Armstrong <don@donarmstrong.com>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long qw(:config no_ignore_case);
12 use Pod::Usage;
13
14 =head1 NAME
15
16 debbugs-updatesqlcache -- Update Debbugs SQL Cache
17
18 =head1 SYNOPSIS
19
20 debbugs-updatesqlcache [options]
21
22  Options:
23   --quick, -q only load changed bugs
24   --progress Show progress bar
25   --service, -s service name
26   --sysconfdir, -c postgresql service config dir
27   --spool-dir debbugs spool directory
28   --debug, -d debugging level (Default 0)
29   --help, -h display this help
30   --man, -m display manual
31
32 =head1 SUBCOMMANDS
33
34 =head2 help
35
36 Display this manual
37
38 =head2 update
39
40 Update SQL cache
41
42 =head1 OPTIONS
43
44 =over
45
46 =item B<--quick, -q>
47
48 Only update things which may have changed
49
50 =item B<--progress>
51
52 Show progress bar (requires Term::ProgressBar)
53
54 =item B<--service,-s>
55
56 Postgreql service to use; defaults to debbugs
57
58 =item B<--sysconfdir,-c>
59
60 System configuration directory to use; if not set, defaults to the
61 postgresql default. [Operates by setting PGSYSCONFDIR]
62
63 =item B<--spool-dir>
64
65 Debbugs spool directory; defaults to the value configured in the
66 debbugs configuration file.
67
68 =item B<--verbose>
69
70 Output more information about what is happening. Probably not useful
71 if you also set --progress.
72
73 =item B<--debug, -d>
74
75 Debug verbosity.
76
77 =item B<--help, -h>
78
79 Display brief useage information.
80
81 =item B<--man, -m>
82
83 Display this manual.
84
85 =back
86
87
88 =cut
89
90
91 use vars qw($DEBUG);
92
93 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list getsourcemaintainers);
94 use Debbugs::Config qw(:config);
95 use Debbugs::Status qw(bug_presence read_bug);
96 use Debbugs::DB;
97 use DateTime;
98 use File::stat;
99
100
101 my %options =
102     (debug           => 0,
103      help            => 0,
104      man             => 0,
105      verbose         => 0,
106      quiet           => 0,
107      quick           => 0,
108      service         => $config{debbugs_db},
109      progress        => 0,
110     );
111
112 Getopt::Long::Configure('pass_through');
113 GetOptions(\%options,
114            'quick|q!',
115            'service|s=s',
116            'sysconfdir|c=s',
117            'progress!',
118            'spool_dir|spool-dir=s',
119            'verbose|v+',
120            'quiet+',
121            'debug|d+','help|h|?','man|m');
122 Getopt::Long::Configure('default');
123
124 pod2usage() if $options{help};
125 pod2usage({verbose=>2}) if $options{man};
126
127 $DEBUG = $options{debug};
128
129 my %subcommands =
130     ('update' => {function => \&update_cache,
131                },
132      'help' => {function => sub {pod2usage({verbose => 2});}}
133     );
134
135 my @USAGE_ERRORS;
136 $options{verbose} = $options{verbose} - $options{quiet};
137
138 if ($options{progress}) {
139     eval "use Term::ProgressBar";
140     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
141 }
142
143
144 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
145
146 if (exists $options{sysconfdir}) {
147     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
148         delete $ENV{PGSYSCONFDIR};
149     } else {
150         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
151     }
152 }
153
154 if (exists $options{spool_dir} and defined $options{spool_dir}) {
155     $config{spool_dir} = $options{spool_dir};
156 }
157
158 my $prog_bar;
159 if ($options{progress}) {
160     $prog_bar = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
161     warn "Unable to initialize progress bar: $@" if not $prog_bar;
162 }
163
164
165 my ($subcommand) = shift @ARGV;
166 if (not defined $subcommand) {
167     $subcommand = 'help';
168     print STDERR "You must provide a subcommand; displaying usage.\n";
169     pod2usage();
170 } elsif (not exists $subcommands{$subcommand}) {
171     print STDERR "$subcommand is not a valid subcommand; displaying usage.\n";
172     pod2usage();
173 }
174
175 my $opts =
176     handle_subcommand_arguments(\@ARGV,$subcommands{$subcommand}{arguments});
177 $subcommands{$subcommand}{function}->(\%options,$opts,$prog_bar,\%config,\@ARGV);
178
179 sub update_cache {
180     my ($options,$opts,$p,$config,$argv) = @_;
181
182     my $verbose = $options->{debug};
183     # select bugs to update
184
185     # basically, if this is a quick run, we want any bug which has
186     # been modified or any bug which belongs to a package which has a
187     # new version; otherwise, walk every bug
188     my $s = db_connect($options);
189
190     # get all of the possible architectures that we might care about
191     # select distinct s.codename,a.arch from bin_associations ba join bin_ver bv on ba.bin=bv.id join suite s on ba.suite=s.id join arch a on bv.arch=a.id;
192
193     my @suites =
194         $s->resultset('Suite')->
195         search_rs({active => 1,
196                   },
197                  {result_class => 'DBIx::Class::ResultClass::HashRefInflator'}
198                  )->all();
199     my $bugs;
200     if ($options->{quick}) {
201         # identify the last time that we ran this query
202         my $last_query_time =
203             $s->resultset('BugStatusCache')->
204             search_rs(undef,
205                      {rows => 1,
206                       order_by => { -desc => 'asof' },
207                       columns => [qw(asof)],
208                      }
209                      )->first();
210         my $dtf = $s->storage->datetime_parser;
211         if (defined $last_query_time) {
212             $last_query_time = $last_query_time->asof();
213         } else {
214             $last_query_time = DateTime->from_epoch(0);
215         }
216         # select last status update
217         $last_query_time = $dtf->format_datetime($last_query_time);
218         $bugs = $s->resultset('Bug')->
219             search_rs({-or => {'bin_associations.modified' => {'>=',$last_query_time},
220                                'src_associations.modified' => {'>=',$last_query_time},
221                                'me.log_modified' => {'>=',$last_query_time},
222                                'me.last_modified' => {'>=',$last_query_time},
223                               }
224                       },
225                      {join => [{bug_binpackages =>
226                                {bin_pkg =>
227                                {bin_vers =>
228                                 'bin_associations'}}},
229                               {bug_srcpackages =>
230                               {src_pkg =>
231                               {src_vers =>
232                                'src_associations'}}},
233                               ],
234                       columns => [qw(id)],
235                       distinct => 1,
236                       rows => 100,
237                       page => 1,
238                      },
239                      );
240
241         # select bugs which have been modified since the last updatex
242     } else {
243         $bugs = $s->resultset('Bug')->
244             search_rs(undef,
245                      {rows => 100,
246                       page => 1,
247                       columns => [qw(id)],
248                      });
249     }
250     my $update_bug =
251         sub {
252             my @bugs = @_;
253             for my $bug (@bugs) {
254                 my $status = read_bug(bug => $bug->id);
255                 next unless defined $status;
256                 for my $suite (@suites) {
257                     my $presence =
258                         bug_presence(bug => $bug->id,
259                                      status => $status,
260                                      dist => $suite->{suite_name},
261                                     );
262                     $s->resultset('BugStatusCache')->
263                         update_bug_status($bug->id,
264                                           $suite->{id},
265                                           undef,
266                                           $presence,
267                                          );
268                 }
269             }
270         };
271     my $last_page = 1;
272     if ($bugs->is_paged) {
273         $last_page = 
274             $bugs->pager->last_page;
275     }
276     $p->target($last_page) if defined $p;
277     for my $page (1..$last_page) {
278         my $bugs_on_page = $bugs;
279         if ($bugs->is_paged) {
280             $bugs_on_page = $bugs->page($page);
281         }
282         $s->txn_do($update_bug,
283                    $bugs_on_page->all());
284         $p->update($page) if defined $p;
285     }
286 }
287
288
289 sub handle_subcommand_arguments {
290     my ($argv,$args) = @_;
291     my $subopt = {};
292     Getopt::Long::GetOptionsFromArray($argv,
293                               $subopt,
294                               keys %{$args},
295                              );
296     my @usage_errors;
297     for my $arg  (keys %{$args}) {
298         next unless $args->{$arg};
299         my $r_arg = $arg; # real argument name
300         $r_arg =~ s/[=\|].+//g;
301         if (not defined $subopt->{$r_arg}) {
302             push @usage_errors, "You must give a $r_arg option";
303         }
304     }
305     pod2usage(join("\n",@usage_errors)) if @usage_errors;
306     return $subopt;
307 }
308
309 sub get_lock{
310     my ($subcommand,$config,$options) = @_;
311     if (not lockpid($config->{spool_dir}.'/lock/debbugs-updatesqlcache-$subcommand')) {
312         if ($options->{quick}) {
313             # If this is a quick run, just exit
314             print STDERR "Another debbugs-updatesqlcache is running; stopping\n" if $options->{verbose};
315             exit 0;
316         }
317         print STDERR "Another debbugs-updatesqlcache is running; stopping\n";
318         exit 1;
319     }
320 }
321
322 sub db_connect {
323     my ($options) = @_;
324     # connect to the database; figure out how to handle errors
325     # properly here.
326     my $s = Debbugs::DB->connect('dbi:Pg:service='.$options->{service}) or
327         die "Unable to connect to database: ";
328 }
329
330 sub walk_bugs {
331     my ($dirs,$p,$what,$verbose,$sub) = @_;
332     my @dirs = @{$dirs};
333     my $tot_dirs = @dirs;
334     my $done_dirs = 0;
335     my $avg_subfiles = 0;
336     my $completed_files = 0;
337     while (my $dir = shift @dirs) {
338         printf "Doing dir %s ...\n", $dir if $verbose;
339
340         opendir(DIR, "$dir/.") or die "opendir $dir: $!";
341         my @subdirs = readdir(DIR);
342         closedir(DIR);
343
344         my @list = map { m/^(\d+)\.$what$/?($1):() } @subdirs;
345         $tot_dirs -= @dirs;
346         push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
347         $tot_dirs += @dirs;
348         if ($avg_subfiles == 0) {
349             $avg_subfiles = @list;
350         }
351
352         $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
353         $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
354         $done_dirs += 1;
355
356         for my $bug (@list) {
357             $completed_files++;
358             $p->update($completed_files) if $p;
359             print "Up to $completed_files bugs...\n" if ($completed_files % 100 == 0 && $verbose);
360             $sub->($bug);
361         }
362     }
363     $p->remove() if $p;
364 }
365
366
367
368 __END__