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