-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrom.pl
More file actions
executable file
·315 lines (261 loc) · 7.4 KB
/
strom.pl
File metadata and controls
executable file
·315 lines (261 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env perl
# StromEntry Class
package StromEntry;
use Moose;
use v5.32;
use Types::Standard qw(Any Int Str InstanceOf);
use Type::Params qw(compile);
use namespace::clean;
use overload '""' => 'to_string';
has 'date' => ( is => 'ro', required => 1, isa => InstanceOf['DateTime'] );
has 'kwh' => ( is => 'ro', required => 1, isa => Int );
has 'comment' => ( is => 'ro', required => 1, isa => Str );
sub to_string {
my ( $self ) = @_;
return sprintf("[Date: %s; Kwh: %d; Comment: %s]",
$self->date->datetime,
$self->kwh,
$self->comment
);
}
my $StromEntry = InstanceOf['StromEntry'];
# Calculates days between two entries
sub days_delta {
state $check = compile(Any, $StromEntry, $StromEntry);
my ( $class, $entry1, $entry2 ) = &$check;
return $entry1->date->delta_days($entry2->date)->in_units('days');
}
__PACKAGE__->meta->make_immutable;
# Strom Class
package Strom;
use Moose;
use v5.32;
use List::Util qw(reduce);
use Scalar::Util qw(refaddr);
use Types::Standard qw(Any CodeRef ArrayRef Object InstanceOf);
use Type::Params qw(compile);
use namespace::clean -except => 'before';
my sub Strom { InstanceOf['Strom'] }
my sub Entry { InstanceOf['StromEntry'] }
has 'start' => (
is => 'ro',
isa => Entry,
lazy => 1,
builder => '_start',
);
has 'start_date' => (
is => 'ro',
isa => InstanceOf['DateTime'],
required => 1,
);
has 'entries' => (
is => 'ro',
isa => ArrayRef[Entry],
default => sub { [] },
traits => ['Array'],
handles => {
get => 'get',
elements => 'elements',
has_entries => 'count',
count => 'count',
map => 'map',
filter => 'grep',
foreach => 'natatime',
}
);
sub _start {
my ( $self ) = @_;
return StromEntry->new(
date => $self->start_date,
kwh => 0,
comment => "",
);
}
no Moose;
sub BUILD {
my ( $self ) = @_;
# Check if dates are in ascending order
for my $entry ( $self->elements ) {
my $before = $self->before($entry);
if ( $entry->date < $before->date ) {
die sprintf(
"Datum %s muss größer sein als %s\n",
$entry->date->dmy('.'),
$before->date->dmy('.'),
);
}
}
}
sub add {
state $check = compile(Strom, Entry);
my ( $self, $entry ) = &$check;
return Strom->new(
start_date => $self->start_date,
entries => [ $self->elements, $entry ],
);
}
sub last {
state $check = compile(Strom);
my ( $self ) = &$check;
return $self->get(-1);
}
sub fold {
state $check = compile(Strom, Any, CodeRef);
my ( $self, $init, $code ) = &$check;
return reduce { $code->($a,$b) } $init, $self->elements;
}
sub days_since_start {
state $check = compile(Strom, Entry);
my ( $self, $entry ) = &$check;
return StromEntry->days_delta($entry, $self->start);
}
# search and returns the StromEntry exactly before entry
sub before {
state $check = compile(Strom, Entry);
my ( $self, $current ) = &$check;
my @elements = $self->elements;
my $before = shift @elements;
for my $entry ( @elements ) {
if ( refaddr $entry == refaddr $current ) {
return $before;
}
$before = $entry;
}
return $self->start;
}
sub average_since_start {
state $check = compile(Strom, Entry);
my ( $self, $entry ) = &$check;
my $days = $self->days_since_start($entry);
my $average = $entry->kwh / $days;
return $average;
}
sub average_before {
state $check = compile(Strom, Entry);
my ( $self, $entry ) = &$check;
my $before = $self->before($entry);
my $days = StromEntry->days_delta($entry, $before);
if ( $days == 0 ) {
return 0;
}
return ($entry->kwh - $before->kwh) / $days;
}
__PACKAGE__->meta->make_immutable;
# Main Program
package main;
use strict;
use warnings;
use v5.32;
use Data::Dump qw(dump dd);
use Data::Dump::Filtered qw(add_dump_filter);
use Carp qw(croak);
use DateTime;
use List::Util qw(max);
use Chart::Clicker;
# Dump DateTime as String
add_dump_filter(sub{
my ($ctx, $obj) = @_;
if ( $ctx->class eq 'DateTime' ) {
return { dump => $obj->datetime };
}
return;
});
# Parsing
my @data;
open my $fh, '<', 'strom.txt' or die "Cannot open file: $!\n";
while ( my $line = <$fh> ) {
if ( $line =~ m/\A \s* (\d\d)\.(\d\d)\.(\d\d\d\d) \s+ (\d+) \s* ([^\r\n]*) \Z/xms ) {
my $dt = DateTime->new(
day => $1,
month => $2,
year => $3,
);
my $kwh = $4;
my $comment = $5;
push @data, [$dt, $kwh, $comment];
}
}
close $fh;
# Create Strom
my $strom = (sub {
my $first = shift @data;
my $init_kwh = $first->[1];
my @entries = map {
StromEntry->new(
date => $_->[0],
kwh => $_->[1] - $init_kwh,
comment => $_->[2],
)
} @data;
return Strom->new(
start_date => $first->[0],
entries => \@entries,
);
})->();
# Insgesamt Ausgabe
printf "Insgesamt:\n";
for my $entry ( $strom->elements ) {
my $days = $strom->days_since_start($entry);
my $average = $strom->average_since_start($entry);
my $comment = $entry->comment ? "-- " . $entry->comment : "";
printf "Datum: %s | Days: %3d | Kwh/Day: %.2f | 365-Total: %4d %s\n",
$entry->date->dmy('.'),
$days,
$average,
$average * 365,
$comment;
}
# Delta Ausgabe
printf "\nDelta:\n";
for my $entry ( $strom->elements ) {
my $before = $strom->before($entry);
my $days = StromEntry->days_delta($entry, $before);
my $average = $strom->average_before($entry);
my $change = $average - $strom->average_before($before);
my $comment = $entry->comment ? "-- " . $entry->comment : "";
printf "Datum: %s | Days: %3d | Kwh/Day: %.2f | Change: %+.2f kwh %s\n",
$entry->date->dmy('.'),
$days,
$average,
$change,
$comment;
}
# Generate a Chart
{
my $cc = Chart::Clicker->new(width => 1500, height => 1000, format => 'png');
my $days = [ $strom->map(sub{ $strom->days_since_start($_) }) ];
my $insgesamt = Chart::Clicker::Data::Series->new(
name => 'Insgesamt',
keys => $days,
values => [$strom->map(sub{ $strom->average_since_start($_) })],
);
my $deltas = Chart::Clicker::Data::Series->new(
name => 'Delta',
keys => $days,
values => [$strom->map(sub{ $strom->average_before($_) })],
);
my $ds = Chart::Clicker::Data::DataSet->new(
series => [ $insgesamt, $deltas ]
);
$cc->title->text('Stromverbrauch');
$cc->title->padding->bottom(5);
$cc->add_to_datasets($ds);
my $defctx = $cc->get_context('default');
$defctx->renderer->shape(
Geometry::Primitive::Circle->new({
radius => 5,
})
);
$defctx->renderer->shape_brush(
Graphics::Primitive::Brush->new(
width => 2,
color => Graphics::Color::RGB->new(red => 1, green => 1, blue => 1)
)
);
$defctx->domain_axis->label('Tage');
$defctx->range_axis->label('Kwh pro Tag');
$defctx->range_axis->range->min(2);
$defctx->range_axis->range->max(5);
$defctx->renderer->brush->width(2);
$cc->write_output('strom_pl.png');
}