|
| 1 | +=pod |
| 2 | +Wihlo - Web-based weather logging and display software |
| 3 | +Copyright (C) 2014 A Beverley [email protected] |
| 4 | +
|
| 5 | +This program is free software: you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Affero General Public License as |
| 7 | +published by the Free Software Foundation, either version 3 of the |
| 8 | +License, or (at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Affero General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Affero General Public License |
| 16 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +=cut |
| 18 | + |
| 19 | +package Wihlo::Data; |
| 20 | + |
| 21 | +use DateTime; |
| 22 | +use DateTime::Format::Strptime; |
| 23 | + |
| 24 | +use Moo; |
| 25 | +use MooX::Types::MooseLike::Base qw(:all); |
| 26 | +use MooX::Types::MooseLike::DateTime qw/DateAndTime/; |
| 27 | + |
| 28 | +has schema => ( |
| 29 | + is => 'ro', |
| 30 | + required => 1, |
| 31 | +); |
| 32 | + |
| 33 | +has from => ( |
| 34 | + is => 'rw', |
| 35 | + isa => DateAndTime, |
| 36 | + lazy => 1, |
| 37 | + builder => sub { |
| 38 | + my $self = shift; |
| 39 | + $self->to->clone->subtract(days => 5); |
| 40 | + }, |
| 41 | +); |
| 42 | + |
| 43 | +has to => ( |
| 44 | + is => 'rw', |
| 45 | + isa => DateAndTime, |
| 46 | + lazy => 1, |
| 47 | + builder => sub { DateTime->now }, |
| 48 | +); |
| 49 | + |
| 50 | +has readings => ( |
| 51 | + is => 'lazy', |
| 52 | + isa => ArrayRef, |
| 53 | +); |
| 54 | + |
| 55 | +has readings_graph => ( |
| 56 | + is => 'lazy', |
| 57 | + isa => ArrayRef, |
| 58 | +); |
| 59 | + |
| 60 | +# day, hour, minute. Defaults to sensible value |
| 61 | +# depending on range |
| 62 | +has group_by => ( |
| 63 | + is => 'lazy', |
| 64 | + isa => Str, |
| 65 | +); |
| 66 | + |
| 67 | +# The actual SQL grouping, based on group_by |
| 68 | +has _sql_group_by => ( |
| 69 | + is => 'lazy', |
| 70 | + isa => ArrayRef, |
| 71 | +); |
| 72 | + |
| 73 | +sub _build_readings |
| 74 | +{ my $self = shift; |
| 75 | + |
| 76 | + my $parser = $self->schema->storage->datetime_parser; |
| 77 | + my $readdisp = $self->schema->resultset('Reading')->search( |
| 78 | + { |
| 79 | + datetime => { |
| 80 | + -between => [ |
| 81 | + $parser->format_datetime($self->from), |
| 82 | + $parser->format_datetime($self->to), |
| 83 | + ] |
| 84 | + } |
| 85 | + },{ |
| 86 | + '+select' => [ |
| 87 | + { |
| 88 | + max => 'outtemp', |
| 89 | + -as => 'maxtemp' |
| 90 | + },{ |
| 91 | + min => 'outtemp', |
| 92 | + -as => 'mintemp', |
| 93 | + },{ |
| 94 | + max => 'windgust', |
| 95 | + -as => 'windgust', |
| 96 | + },{ |
| 97 | + sum => 'rain', |
| 98 | + -as => 'rain', |
| 99 | + },{ |
| 100 | + avg => 'barometer', |
| 101 | + -as => 'baramoter', |
| 102 | + } |
| 103 | + ], |
| 104 | + group_by => [ |
| 105 | + $self->_sql_group_by, |
| 106 | + ] |
| 107 | + } |
| 108 | + ); |
| 109 | + |
| 110 | + my $raintot = 0; my $lasthms = 0; |
| 111 | + my @readings; |
| 112 | + while (my $r = $readdisp->next) |
| 113 | + { |
| 114 | + $raintot = 0 |
| 115 | + if ($r->datetime->hms('') <= $lasthms); |
| 116 | + my $rain = $r->rain ? $r->rain : 0; |
| 117 | + $raintot += $rain; |
| 118 | + |
| 119 | + push @readings, { |
| 120 | + datetime => $r->datetime, |
| 121 | + maxtemp => celsius($r->get_column('maxtemp')), |
| 122 | + mintemp => celsius($r->get_column('mintemp')), |
| 123 | + windgust => $r->windgust || 0, |
| 124 | + barometer => $r->barometer || 0, |
| 125 | + raintot => $raintot, |
| 126 | + }; |
| 127 | + $lasthms = $r->datetime->hms(''); |
| 128 | + } |
| 129 | + |
| 130 | + \@readings; |
| 131 | +}; |
| 132 | + |
| 133 | +sub _build_readings_graph |
| 134 | +{ my $self = shift; |
| 135 | + my @readings; |
| 136 | + |
| 137 | + foreach my $reading (@{$self->readings}) |
| 138 | + { |
| 139 | + my $datetime = $reading->{datetime}->datetime; |
| 140 | + push @readings, { |
| 141 | + x => $datetime, |
| 142 | + y => $reading->{maxtemp}, |
| 143 | + group => 2, |
| 144 | + }; |
| 145 | + push @readings, { |
| 146 | + x => $datetime, |
| 147 | + y => $reading->{mintemp}, |
| 148 | + group => 1, |
| 149 | + }; |
| 150 | + push @readings, { |
| 151 | + x => $datetime, |
| 152 | + y => $reading->{windgust}, |
| 153 | + group => 3, |
| 154 | + }; |
| 155 | + push @readings, { |
| 156 | + x => $datetime, |
| 157 | + y => $reading->{barometer}, |
| 158 | + group => 4, |
| 159 | + }; |
| 160 | + push @readings, { |
| 161 | + x => $datetime, |
| 162 | + y => $reading->{raintot}, |
| 163 | + group => 0, |
| 164 | + }; |
| 165 | + } |
| 166 | + \@readings; |
| 167 | +} |
| 168 | + |
| 169 | +sub _build_group_by |
| 170 | +{ my $self = shift; |
| 171 | + my $diff = $self->from->subtract_datetime( $self->to ); |
| 172 | + my $group = !$diff->months && !$diff->years && !$diff->weeks |
| 173 | + ? 'minute' |
| 174 | + : !$diff->months && !$diff->years |
| 175 | + ? 'hour' |
| 176 | + : 'day'; |
| 177 | + $group; |
| 178 | +} |
| 179 | + |
| 180 | +sub _build__sql_group_by |
| 181 | +{ my $self = shift; |
| 182 | + |
| 183 | + my @group = ( |
| 184 | + $self->schema->resultset('Reading')->dt_SQL_pluck({ -ident => '.datetime' }, 'year'), |
| 185 | + $self->schema->resultset('Reading')->dt_SQL_pluck({ -ident => '.datetime' }, 'month'), |
| 186 | + $self->schema->resultset('Reading')->dt_SQL_pluck({ -ident => '.datetime' }, 'day_of_month'), |
| 187 | + ); |
| 188 | + |
| 189 | + push @group, $self->schema->resultset('Reading')->dt_SQL_pluck({ -ident => '.datetime' }, 'hour') |
| 190 | + if $self->group_by eq 'hour' || $self->group_by eq 'minute'; |
| 191 | + push @group, $self->schema->resultset('Reading')->dt_SQL_pluck({ -ident => '.datetime' }, 'minute') |
| 192 | + if $self->group_by eq 'minute'; |
| 193 | + \@group; |
| 194 | +} |
| 195 | + |
| 196 | +sub celsius($) |
| 197 | +{ |
| 198 | + my $f = shift; |
| 199 | + my $c = ($f - 32) * (5/9); |
| 200 | + sprintf("%.2f", $c); |
| 201 | +} |
| 202 | + |
| 203 | +1; |
0 commit comments