App/Chart/Annotation.pm untested

filter: Statementstmt 0.0% 0 / 209 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 345 CC 16 SCAR 25.2
CC
16
Cov
0%
want_alert
72.0
delete
12.0
t
6.0
CRAP 12.5
1 # Copyright 2008, 2009, 2010, 2011, 2015 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17  
18 package App::Chart::Annotation;
19 use 5.010;
20 use strict;
21 use warnings;
22 use Carp 'carp';
23  
24 use App::Chart::Database;
25  
26 use constant DEBUG => 0;
27  
28 sub delete {
29 my ($self) = @_;
30 if (DEBUG) { print "Annotation delete ",$self->table,
31 " symbol ",$self->{'symbol'}," id ",$self->{'id'},"\n"; }
32 my $table = $self->table;
33 my $symbol = $self->{'symbol'};
34 my $id = $self->{'id'};
35  
36 require App::Chart::DBI;
37 my $dbh = App::Chart::DBI->instance;
38 my $rows = $dbh->do ("DELETE FROM $table WHERE symbol=? AND id=?",
39 undef, $symbol, $id);
40 if ($rows != 1) {
41 carp ("Annotation delete: oops, affected $rows\n");
42 }
43 App::Chart::chart_dirbroadcast()->send ('data-changed', { $symbol => 1 });
44 }
45  
46 sub clone {
47 my ($self) = @_;
48 return bless { %$self }, ref ($self);
49 }
50  
51 sub ensure_id {
52 my ($self, $symbol) = @_;
53 return ($self->{'id'} ||= do {
54 my $table = $self->table;
55 my $last_id = App::Chart::Database::read_notes_single
56 ("SELECT id FROM $table WHERE symbol=?
57 ORDER BY id DESC LIMIT 1",
58 $symbol);
59 ($last_id||0) + 1;
60 });
61 }
62  
63 sub next_id {
64 my ($table, $symbol) = @_;
65 my $last_id = App::Chart::Database::read_notes_single
66 ("SELECT id FROM $table WHERE symbol=? ORDER BY id DESC LIMIT 1",
67 $symbol);
68 return ($last_id || 0) + 1;
69 }
70  
71 sub swap_ends {
72 }
73  
74 #------------------------------------------------------------------------------
75  
76 package App::Chart::Annotation::Alert;
77 use strict;
78 use warnings;
79 use base 'App::Chart::Annotation';
80  
81 use constant DEBUG => 0;
82  
83 sub new {
84 my ($class, %self) = @_;
85 return bless \%self, $class;
86 }
87  
88 sub table { return 'alert'; }
89  
90 sub t {
91 my ($self, $new_t) = @_;
92 if (@_ < 2) { return $self->{'t'}||0; }
93 $self->{'t'} = $new_t;
94 }
95  
96 sub price {
97 my ($self, $newprice) = @_;
98 if (@_ < 2) { return $self->{'price'}; }
99 $self->{'price'} = $newprice;
100 }
101  
102 sub write {
103 my ($self) = @_;
104 my $symbol = $self->{'symbol'};
105  
106 require App::Chart::DBI;
107 my $dbh = App::Chart::DBI->instance;
108 App::Chart::Database::call_with_transaction
109 ($dbh, sub {
110 my $id = $self->ensure_id ($symbol);
111  
112 $dbh->do ('INSERT OR REPLACE INTO alert
113 (symbol, id, price, above)
114 VALUES (?,?, ?,?)',
115 undef, # DBI options
116 $symbol,
117 $id,
118 $self->{'price'},
119 $self->{'above'});
120 update_alert ($symbol);
121 });
122 App::Chart::chart_dirbroadcast()->send ('data-changed', { $symbol => 1 });
123 }
124  
125 sub delete {
126 my ($self) = @_;
127 $self->SUPER::delete;
128 App::Chart::Annotation::Alert::update_alert ($self->{'symbol'});
129 }
130  
131 sub draw {
132 my ($self, $graph, $region) = @_;
133 App::Chart::Gtk2::Graph::Plugin::Alerts->draw ($graph, $region, [ $self ]);
134 }
135  
136 sub update_alert {
137 my ($symbol) = @_;
138 require App::Chart::Gtk2::Symlist::Alerts;
139 my $symlist = App::Chart::Gtk2::Symlist::Alerts->instance;
140 if (want_alert ($symbol)) {
141 $symlist->insert_symbol ($symbol);
142 } else {
143 $symlist->delete_symbol ($symbol);
144 }
145 }
146  
147 # return true if $symbol should be in the Alerts list
148 sub want_alert {
149 my ($symbol) = @_;
150 if (DEBUG) { print "want_alert $symbol\n"; }
151  
152 # must be in all list, so not historical and not symbols deleted but with
153 # user notes remaining
154 require App::Chart::Gtk2::Symlist::All;
155 my $symlist = App::Chart::Gtk2::Symlist::All->instance;
156 $symlist->contains_symbol($symbol) or return 0;
157  
158 require App::Chart::DBI;
159 my $dbh = App::Chart::DBI->instance;
160 my $sth = $dbh->prepare_cached ('SELECT price,above FROM alert
161 WHERE symbol=?');
162 my $alert_list = $dbh->selectall_arrayref ($sth, {Slice=>{}}, $symbol);
163 $sth->finish;
164 if (! @$alert_list) { return 0; } # no levels
165  
166 require App::Chart::Latest;
167 my $latest = App::Chart::Latest->get ($symbol);
168  
169 foreach my $alert (@$alert_list) {
170 if ($alert->{'above'}) {
171 # look at bid, but if crossed to offer<bid then use only the offer,
172 # ie. the lesser of the two
173 my $above_price = App::Chart::max_maybe
174 ($latest->{'last'},
175 App::Chart::min_maybe ($latest->{'bid'}, $latest->{'offer'}))
176 // return 0; # if nothing in latest record
177  
178 if (DEBUG) { print " compare $above_price above ",
179 $alert->{'price'},"\n"; }
180 if ($above_price >= $alert->{'price'}) { return 1; }
181  
182 } else {
183 # look at offer, but if crossed to bid>offer then use only the bid,
184 # ie. the greater of the two
185 my $below_price = App::Chart::min_maybe
186 ($latest->{'last'},
187 App::Chart::max_maybe ($latest->{'bid'},
188 $latest->{'offer'}))
189 // return 0; # if nothing in latest record
190  
191 if (DEBUG) { print " compare $below_price below ",
192 $alert->{'price'},"\n"; }
193 if ($below_price <= $alert->{'price'}) { return 1; }
194 }
195 }
196 }
197  
198 package App::Chart::Annotation::Line;
199 use strict;
200 use warnings;
201 use Carp 'croak';
202 use Math::Round;
203 use base 'App::Chart::Annotation';
204  
205 sub table { return 'line'; }
206  
207 sub new_for_graph {
208 my ($class, $graph, $x, $y) = @_;
209 my $series = $graph->get('series_list')->[0] || return;
210 my $symbol = $series->symbol
211 || croak 'Cannot add line to non-database series';
212 my $timebase = $series->timebase;
213 my $t = $graph->x_to_date ($x);
214 my $p = $graph->y_to_value ($y);
215 my $date = $timebase->to_iso ($t);
216 return bless { symbol => $symbol,
217 id => undef,
218 date1 => $date,
219 price1 => $p,
220 date2 => $date,
221 price2 => $p,
222 horizontal => 0,
223  
224 timebase => $timebase,
225 t1 => $t,
226 t2 => $t }, $class;
227 }
228  
229 sub App::Chart::Series::Database::AnnLines_arrayref {
230 my ($series) = @_;
231 return ($series->{__PACKAGE__.'.array'} ||= do {
232 my $symbol = $series->symbol || '';
233 require App::Chart::DBI;
234 my $dbh = App::Chart::DBI->instance;
235  
236 my $sth = $dbh->prepare_cached ('SELECT * FROM line WHERE symbol=?');
237 my $aref = $dbh->selectall_arrayref ($sth, {Slice=>{}}, $symbol);
238 $sth->finish;
239 my $timebase = $series->timebase;
240 foreach my $elem (@$aref) {
241 $elem->{'timebase'} = $timebase;
242 $elem->{'t1'} = $timebase->from_iso_floor ($elem->{'date1'});
243 $elem->{'t2'} = $timebase->from_iso_floor ($elem->{'date2'});
244 bless $elem, __PACKAGE__;
245 }
246 $aref;
247 });
248 }
249  
250 sub t {
251 my ($self, $new_t) = @_;
252 if (@_ < 2) { return $self->{'t1'}; }
253 $new_t = Math::Round::round ($new_t);
254 $self->{'t1'} = $new_t;
255 my $timebase = $self->{'timebase'};
256 $self->{'date1'} = $timebase->to_iso ($new_t);
257 }
258  
259 sub price {
260 my ($self, $newprice) = @_;
261 if (@_ < 2) { return $self->{'price1'}; }
262 $self->{'price1'} = $newprice;
263 }
264  
265 sub swap_ends {
266 my ($self) = @_;
267 swap ($self->{'date1'}, $self->{'date2'});
268  
269 { my $tmp = $self->{'t1'};
270 $self->{'t1'} = $self->{'t2'};
271 $self->{'t2'} = $tmp;
272 }
273 { my $tmp = $self->{'price1'};
274 $self->{'price1'} = $self->{'price2'};
275 $self->{'price2'} = $tmp;
276 }
277 return $self;
278 }
279  
280 sub swap {
281 my $tmp = $_[0]; $_[0] = $_[1]; $_[1] = $tmp;
282 }
283  
284 sub write {
285 my ($self) = @_;
286 my $symbol = $self->{'symbol'};
287  
288 if ($self->{'horizontal'}) {
289 $self->{'price2'} = $self->{'price1'};
290 }
291  
292 require App::Chart::DBI;
293 my $dbh = App::Chart::DBI->instance;
294 App::Chart::Database::call_with_transaction
295 ($dbh, sub {
296 my $id = $self->ensure_id ($symbol);
297  
298 $dbh->do ('INSERT OR REPLACE INTO line
299 (symbol,id, date1,price1,date2,price2,horizontal)
300 VALUES (?,?, ?,?,?,?,?)',
301 undef, # DBI options
302 $symbol,
303 $id,
304 $self->{'date1'},
305 $self->{'price1'},
306 $self->{'date2'},
307 $self->{'price2'},
308 $self->{'horizontal'});
309 });
310 App::Chart::chart_dirbroadcast()->send ('data-changed', { $symbol => 1 });
311 }
312  
313 sub draw {
314 my ($self, $graph, $region) = @_;
315 App::Chart::Gtk2::Graph::Plugin::AnnLines->draw ($graph, $region, [ $self ]);
316 }
317  
318 1;
319 __END__