| File: | script/bi-extract |
| Coverage: | 89.8% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | #!/usr/bin/env perl | |||||
| 2 | # bi-extract -- build the extracts of accelerated datasets (Database::BI, Phase 7.5) | |||||
| 3 | # | |||||
| 4 | # An accelerated dataset is read from a SQLite copy of itself, an extract, for | |||||
| 5 | # as long as the extract still matches the file it was built from. This script | |||||
| 6 | # marks a dataset accelerated and builds its extract, rebuilds every accelerated | |||||
| 7 | # dataset's extract, or stops accelerating one. It prints what it did. | |||||
| 8 | # | |||||
| 9 | # An extract is a cache under <model_dir>/extracts/, or database-bi-extracts/ | |||||
| 10 | # in the cache directory when one is set (Database::BI::CacheDir). Deleting it | |||||
| 11 | # loses nothing: the application reads each file again until this is rerun. | |||||
| 12 | # Nothing is ever written under data_dir. | |||||
| 13 | # | |||||
| 14 | # Usage: | |||||
| 15 | # bi-extract --spec <l-spec> [--spec <l-spec> ...] | |||||
| 16 | # bi-extract --all | |||||
| 17 | # bi-extract --remove --spec <l-spec> | |||||
| 18 | # bi-extract --list | |||||
| 19 | # options: [--data-dir <dir>] [--model-dir <dir>] | |||||
| 20 | # | |||||
| 21 | # Exit codes: 0 = ok | |||||
| 22 | # 1 = an extract could not be built (the others still are) | |||||
| 23 | # 2 = bad arguments | |||||
| 24 | # | |||||
| 25 | # Suitable for cron, after the data files are refreshed: | |||||
| 26 | # 15 2 * * * cd /opt/myapp && ./script/bi-extract --all | |||||
| 27 | ||||||
| 28 | 21 21 21 | 32468 14 213 | use strict; | |||
| 29 | 21 21 21 | 14 6 335 | use warnings; | |||
| 30 | ||||||
| 31 | 21 21 21 | 1762 6359 843 | use FindBin qw($Bin); | |||
| 32 | 21 21 21 | 1729 3764 33 | use lib "$Bin/../lib"; | |||
| 33 | ||||||
| 34 | 21 21 21 | 4549 63099 30 | use Getopt::Long qw(GetOptions :config no_ignore_case); | |||
| 35 | 21 21 21 | 3760 20 397 | use Database::BI::Util qw(minute_stamp); | |||
| 36 | 21 21 21 | 1932 15 12928 | use Database::BI::I18N (); | |||
| 37 | ||||||
| 38 | # Messages in the language the locale names: LANG=en_US.UTF-8 is American | |||||
| 39 | # English, anything else British (CGI::Lingua chooses). | |||||
| 40 | 21 | 941513 | $Database::BI::I18N::LANGUAGE = Database::BI::I18N::from_locale(); | |||
| 41 | ||||||
| 42 | 21 | 213 | my (@opt_spec, $opt_all, $opt_remove, $opt_list, $opt_data_dir, $opt_model_dir, $opt_help); | |||
| 43 | ||||||
| 44 | GetOptions( | |||||
| 45 | 'spec|s=s' => \@opt_spec, | |||||
| 46 | 'all|a' => \$opt_all, | |||||
| 47 | 'remove|r' => \$opt_remove, | |||||
| 48 | 'list|l' => \$opt_list, | |||||
| 49 | 'data-dir|d=s' => \$opt_data_dir, | |||||
| 50 | 'model-dir|m=s' => \$opt_model_dir, | |||||
| 51 | 'help|h' => \$opt_help, | |||||
| 52 | 21 0 0 | 80 0 0 | ) or do { _usage(); exit 2 }; | |||
| 53 | ||||||
| 54 | 21 1 1 | 8443 1 17 | if ($opt_help) { _usage(); exit 0 } | |||
| 55 | ||||||
| 56 | 20 80 | 40 44 | my $modes = grep { $_ } (scalar(@opt_spec) && !$opt_remove), $opt_all, $opt_remove, $opt_list; | |||
| 57 | 20 | 70 | if ($modes != 1 || ($opt_remove && !@opt_spec) || ($opt_all && @opt_spec)) { | |||
| 58 | 3 | 113 | print STDERR "bi-extract: give --spec, --all, --remove --spec or --list\n"; | |||
| 59 | 3 | 6 | _usage(); | |||
| 60 | 3 | 53 | exit 2; | |||
| 61 | } | |||||
| 62 | 17 | 19 | if (@ARGV) { | |||
| 63 | 0 | 0 | print STDERR "bi-extract: unexpected argument '$ARGV[0]'\n"; | |||
| 64 | 0 | 0 | exit 2; | |||
| 65 | } | |||||
| 66 | ||||||
| 67 | # The application itself, so a dataset is read exactly as the pages read it. | |||||
| 68 | 17 | 1894 | require Mojo::Server; | |||
| 69 | 17 | 814206 | my $app = Mojo::Server->new->build_app('Database::BI'); | |||
| 70 | 17 | 724 | $app->log->level('fatal'); | |||
| 71 | 17 | 172 | $app->config->{data_dir} = $opt_data_dir if defined $opt_data_dir; | |||
| 72 | 17 | 149 | $app->config->{model_dir} = $opt_model_dir if defined $opt_model_dir; | |||
| 73 | ||||||
| 74 | 17 | 73 | require Database::BI::Controller::Dashboard; | |||
| 75 | # A controller holds its transaction weakly, so this file keeps it. | |||||
| 76 | 17 | 26 | require Mojo::Transaction::HTTP; | |||
| 77 | 17 | 119 | my $tx = Mojo::Transaction::HTTP->new; | |||
| 78 | 17 | 95 | my $c = Database::BI::Controller::Dashboard->new(app => $app, tx => $tx); | |||
| 79 | ||||||
| 80 | 17 | 513 | if ($opt_list) { | |||
| 81 | 3 | 5 | my @specs = Database::BI::Controller::Dashboard::accelerated_specs($c); | |||
| 82 | 3 | 10 | print "No dataset is accelerated.\n" unless @specs; | |||
| 83 | 3 | 12 | print "$_\n" for @specs; | |||
| 84 | 3 | 431 | exit 0; | |||
| 85 | } | |||||
| 86 | ||||||
| 87 | 14 | 17 | if ($opt_remove) { | |||
| 88 | 3 | 4 | for my $spec (@opt_spec) { | |||
| 89 | 4 | 5 | my $was = Database::BI::Controller::Dashboard::unaccelerate($c, $spec); | |||
| 90 | 4 | 14 | print $was ? "$spec: no longer accelerated; its extract is removed\n" | |||
| 91 | : "$spec: was not accelerated\n"; | |||||
| 92 | } | |||||
| 93 | 3 | 849 | exit 0; | |||
| 94 | } | |||||
| 95 | ||||||
| 96 | 11 | 29 | my @specs = $opt_all ? Database::BI::Controller::Dashboard::accelerated_specs($c) : @opt_spec; | |||
| 97 | 11 | 13 | unless (@specs) { | |||
| 98 | 1 | 7 | print "No dataset is accelerated; name one with --spec.\n"; | |||
| 99 | 1 | 323 | exit 0; | |||
| 100 | } | |||||
| 101 | ||||||
| 102 | 10 | 9 | my $failed = 0; | |||
| 103 | 10 | 11 | for my $spec (@specs) { | |||
| 104 | 10 10 | 5 17 | my $built = eval { Database::BI::Controller::Dashboard::accelerate($c, $spec) }; | |||
| 105 | 10 | 820 | if ($built) { | |||
| 106 | printf "%s: %d row%s, %d column%s -> %s (built %s)\n", | |||||
| 107 | $spec, $built->{rows}, ($built->{rows} == 1 ? '' : 's'), | |||||
| 108 | $built->{columns}, ($built->{columns} == 1 ? '' : 's'), | |||||
| 109 | 6 | 92 | $built->{path}, minute_stamp(localtime $built->{built}); | |||
| 110 | } else { | |||||
| 111 | 4 | 31 | (my $e = $@ // 'unknown error') =~ s/(?:\s+at \S.*? line \d+\.?)+\s*\z//; | |||
| 112 | 4 | 139 | print STDERR "bi-extract: $e\n"; | |||
| 113 | 4 | 7 | $failed = 1; | |||
| 114 | } | |||||
| 115 | } | |||||
| 116 | 10 | 1746 | exit $failed; | |||
| 117 | ||||||
| 118 | sub _usage { | |||||
| 119 | 4 | 46 | print STDERR <<'USAGE'; | |||
| 120 | Usage: bi-extract --spec <spec> [--spec <spec> ...] | --all | --remove --spec <spec> | --list | |||||
| 121 | ||||||
| 122 | --spec, -s <spec> Accelerate a dataset and build its extract: table:<name> | |||||
| 123 | in data_dir, or path:</abs/file>. Repeatable. | |||||
| 124 | --all, -a Rebuild the extract of every accelerated dataset | |||||
| 125 | --remove, -r With --spec: stop accelerating it, and remove its extract | |||||
| 126 | --list, -l Print the accelerated datasets | |||||
| 127 | ||||||
| 128 | Options: | |||||
| 129 | --data-dir, -d <dir> Data directory (default: the application's, usually data) | |||||
| 130 | --model-dir, -m <dir> Model store directory (default: the application's, usually .bi) | |||||
| 131 | --help, -h Show this message | |||||
| 132 | ||||||
| 133 | Exit codes: | |||||
| 134 | 0 = success | |||||
| 135 | 1 = an extract could not be built | |||||
| 136 | 2 = bad arguments | |||||
| 137 | USAGE | |||||
| 138 | } | |||||
| 139 | ||||||