| 1 | package Weather::MOSMIX::Writer; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use Moo 2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | require POSIX; | |||||||||
| 7 | 2 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Weather::MOSMIX::Writer - Write MOSMIX weather forecast data to a DBI handle | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | my $w = Weather::MOSMIX::Writer->new( | ||||||||||
| 16 | dbh => { | ||||||||||
| 17 | dsn => 'dbi:SQLite:dbname=db/forecast.sqlite', | ||||||||||
| 18 | }, | ||||||||||
| 19 | ); | ||||||||||
| 20 | my $r = Weather::MOSMIX::Reader->new( | ||||||||||
| 21 | writer => $w, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | for my $file (@files) { | ||||||||||
| 25 | status("Importing $file\n"); | ||||||||||
| 26 | $r->read_zip( $file ); | ||||||||||
| 27 | }; | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | 2 | our $VERSION = '0.04'; | |||||||||
| 32 | |||||||||||
| 33 | # This should be MooX::Role::DBConnection | ||||||||||
| 34 | 2 | with 'MooX::Role::DBIConnection'; | |||||||||
| 35 | |||||||||||
| 36 | 2 | has 'insert_location_sth' => ( | |||||||||
| 37 | is => 'lazy', | ||||||||||
| 38 | default => \&_prepare_location_sth, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 2 | has 'insert_forecast_sth' => ( | |||||||||
| 42 | is => 'lazy', | ||||||||||
| 43 | default => \&_prepare_forecast_sth, | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | has 'json' => ( | ||||||||||
| 47 | is => 'lazy', | ||||||||||
| 48 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 49 | 0 | JSON->new() | |||||||||
| 50 | }, | ||||||||||
| 51 | 2 | ); | |||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub _prepare_location_sth( $self ) { | ||||||||
|
Subroutine
_prepare_location_sth: not called CC=1 SCAR=6.9
| |||||||||||
| 54 | 0 | $self->dbh->prepare(<<'SQL'); | |||||||||
| 55 | insert or replace into forecast_location ( | ||||||||||
| 56 | name, description, latitude, longitude, elevation | ||||||||||
| 57 | ) values ( | ||||||||||
| 58 | ?, ?, ?, ?, ? | ||||||||||
| 59 | ) | ||||||||||
| 60 | SQL | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | sub _prepare_forecast_sth( $self ) { | ||||||||
|
Subroutine
_prepare_forecast_sth: not called CC=1 SCAR=6.9
| |||||||||||
| 64 | 0 | $self->dbh->prepare(<<'SQL'); | |||||||||
| 65 | insert into forecast ( | ||||||||||
| 66 | name, forecasts, expiry, issuetime | ||||||||||
| 67 | ) values ( | ||||||||||
| 68 | ?, ?, ?, ? | ||||||||||
| 69 | ) | ||||||||||
| 70 | SQL | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub purge_expired_records( $self, $date = POSIX::strftime('%Y-%m-%d %H:%M:%SZ', gmtime()) ) { | ||||||||
|
Subroutine
purge_expired_records: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 74 | 0 | $self->dbh->do(<<'SQL', $date); | |||||||||
| 75 | delete from forecast | ||||||||||
| 76 | where expiry <= ? | ||||||||||
| 77 | SQL | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub purge_outdated_expired_records( $self ) { | ||||||||
|
Subroutine
purge_outdated_expired_records: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 81 | 0 | $self->dbh->do(<<'SQL'); | |||||||||
| 82 | delete from forecast | ||||||||||
| 83 | where expiry < (select max(expiry) from forecast) | ||||||||||
| 84 | SQL | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | sub start( $self ) { | ||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | 1 | my $dbh = $self->dbh; | |||||||||
| 89 | 1 | local $dbh->{PrintError} = 0; | |||||||||
| 90 | 1 | local $dbh->{RaiseError} = 0; | |||||||||
| 91 | 1 | $dbh->do('PRAGMA synchronous = OFF'); | |||||||||
| 92 | 1 | $dbh->do('PRAGMA journal_mode = MEMORY'); | |||||||||
| 93 | 1 | $dbh->{AutoCommit} = 0; | |||||||||
| 94 | }; | ||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub insert( $self, $expiry, @records ) { | ||||||||
|
Subroutine
insert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 97 | 0 | my $i = 0; | |||||||||
| 98 | 0 | ▶ | $self->insert_location_sth->execute_for_fetch(sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 99 | 0 | my $rec = $records[$i++]; | |||||||||
| 100 | 0 | ▶ | return if ! $rec; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | [@{$rec}{qw(name description latitude longitude elevation)}] | |||||||||
| 102 | 0 | }); | |||||||||
| 103 | 0 | $i = 0; | |||||||||
| 104 | 0 | ▶ | $self->insert_forecast_sth->execute_for_fetch(sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 105 | 0 | my $rec = $records[$i++]; | |||||||||
| 106 | 0 | ▶ | return if ! $rec; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | my $f = $self->json->encode( $rec->{forecasts} ); | |||||||||
| 108 | 0 | [$rec->{name}, $f, $expiry, $rec->{issuetime}] | |||||||||
| 109 | 0 | }); | |||||||||
| 110 | }; | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | sub commit( $self ) { | ||||||||
|
Subroutine
commit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 113 | 0 | $self->dbh->commit; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =head2 C<< Weather::MOSMIX::Writer->create_db >> | ||||||||||
| 117 | |||||||||||
| 118 | $w->create_db( | ||||||||||
| 119 | dsn => 'dbi:SQLite:dbname=db/forecast.sqlite', | ||||||||||
| 120 | ); | ||||||||||
| 121 | |||||||||||
| 122 | Shorthand to create the database file. If no dbh is already set, this sets | ||||||||||
| 123 | the active database handle. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||
| 127 | 1 | ▶ | sub create_db( $self, %options ) { | ||||||||
|
Subroutine
create_db: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 128 | 1 | require DBIx::RunSQL; | |||||||||
| 129 | 1 | require File::ShareDir; | |||||||||
| 130 | 1 | my $dbh = DBIx::RunSQL->run( | |||||||||
| 131 | sql => File::ShareDir::dist_file('Weather-MOSMIX', 'create.sql'), | ||||||||||
| 132 | %options, | ||||||||||
| 133 | ); | ||||||||||
| 134 | 1 | ▶ | if( ref $self ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | $self->{_dbh} = $dbh # our dbh is generally read-only | ||||||||||
| 136 | 1 | ▶ | unless $self->{_dbh}; # we look directly so the lazy builder doesn't kick in | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | }; | ||||||||||
| 138 | 1 | return $dbh | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 2 | 1; | |||||||||
| 142 | |||||||||||
| 143 | =head1 REPOSITORY | ||||||||||
| 144 | |||||||||||
| 145 | The public repository of this module is | ||||||||||
| 146 | L<https://github.com/Corion/weather-mosmix>. | ||||||||||
| 147 | |||||||||||
| 148 | =head1 SUPPORT | ||||||||||
| 149 | |||||||||||
| 150 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||
| 151 | |||||||||||
| 152 | =head1 BUG TRACKER | ||||||||||
| 153 | |||||||||||
| 154 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||
| 155 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Weather-MOSMIX> | ||||||||||
| 156 | or via mail to L<www-Weather-MOSMIX@rt.cpan.org|mailto:Weather-MOSMIX@rt.cpan.org>. | ||||||||||
| 157 | |||||||||||
| 158 | =head1 AUTHOR | ||||||||||
| 159 | |||||||||||
| 160 | Max Maischein C<corion@cpan.org> | ||||||||||
| 161 | |||||||||||
| 162 | =head1 COPYRIGHT (c) | ||||||||||
| 163 | |||||||||||
| 164 | Copyright 2019-2020 by Max Maischein C<corion@cpan.org>. | ||||||||||
| 165 | |||||||||||
| 166 | =head1 LICENSE | ||||||||||
| 167 | |||||||||||
| 168 | This module is released under the same terms as Perl itself. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||