App/Chart/DownloadHandler.pm

filter: Statementstmt 94.2% 33 / 35 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 95.7% 45 / 47 CC 1 SCAR 0.2
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/Chart/Download.pm ↑ Summary App/Chart.pm »
1 # Download data handlers.
2  
3 # Copyright 2007, 2008, 2009, 2010, 2011, 2014, 2016, 2024 Kevin Ryde
4  
5 # This file is part of Chart.
6 #
7 # Chart is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3, or (at your option) any later version.
10 #
11 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Chart. If not, see <http://www.gnu.org/licenses/>.
18  
19 package App::Chart::DownloadHandler;
20 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use sort 'stable'; # lexical in 5.10
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Encode::Locale; # for coding system "locale"
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use List::MoreUtils;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use POSIX::Wide;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Locale::TextDomain ('App-Chart');
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 0 ▶use App::Chart;
Subroutine BEGIN: called CC=1 SCAR=2.6
33 use App::Chart::Database;
34  
35 use constant DEBUG => 0;
36  
37  
38 #------------------------------------------------------------------------------
39  
40 our @handler_list = ();
41  
42 sub new {
43 my ($class, %self) = @_;
44 $self{'pred'} or croak __PACKAGE__,": missing pred";
45 $self{'proc'} or croak __PACKAGE__,": missing proc";
46 App::Chart::Sympred::validate ($self{'pred'});
47  
48 my $self = bless \%self, $class;
49 push @handler_list, $self;
50  
51 $self{'name'} ||= do {
52 my ($package,$filename,$line) = caller();
53 "$package:" . Glib::filename_to_unicode($filename) . ":$line" };
54  
55 # highest priority first and 'stable' above for order added for equals
56 @handler_list
57 = sort { ($b->{'priority'}||0) <=> ($a->{'priority'}||0) }
58 @handler_list;
59  
60 return $self;
61 }
62  
63 sub name {
64 my ($self) = @_;
65 return $self->{'name'};
66 }
67  
68 #------------------------------------------------------------------------------
69  
70 sub handlers_for_symbol {
71 my ($class, $symbol) = @_;
72 App::Chart::symbol_setups ($symbol);
73 if (DEBUG) { print "total ", scalar(@handler_list), " handlers\n"; }
74 return grep { $_->match($symbol) } @handler_list;
75 }
76  
77 #------------------------------------------------------------------------------
78  
79 sub match {
80 my ($self, $symbol) = @_;
81 return $self->{'pred'}->match($symbol);
82 }
83  
84 #------------------------------------------------------------------------------
85  
86 sub download {
87 my ($self, $symbol_list) = @_;
88 if (DEBUG) { print "Download ",@$symbol_list,"\n"; }
89 if (! @$symbol_list) { return; }
90  
91 if (my $key = $self->{'recheck_key'}) {
92 my $recheck_seconds = 86400 * $self->{'recheck_days'};
93 my ($timestamp_lo, $timestamp_hi)
94 = App::Chart::Download::timestamp_range ($recheck_seconds);
95  
96 my $min_timestamp = '9999';
97 my $any_wanted = 0;
98 foreach my $symbol (@$symbol_list) {
99 my $symbol_timestamp = App::Chart::Database->read_extra ($symbol, $key);
100 if (defined $symbol_timestamp
101 && $symbol_timestamp ge $timestamp_lo
102 && $symbol_timestamp le $timestamp_hi) {
103 if (DEBUG) { print "recheck not for $symbol: $symbol_timestamp\n"; }
104 $min_timestamp = List::Util::minstr($min_timestamp, $symbol_timestamp);
105 } else {
106 if (DEBUG) { print "recheck want $symbol: $symbol_timestamp\n"; }
107 $any_wanted = 1;
108 last;
109 }
110 }
111  
112 if (! $any_wanted) {
113 my $t = App::Chart::Download::timestamp_to_timet ($min_timestamp)
114 + $recheck_seconds;
115 my $fmt = $App::Chart::option{'d_fmt'} . ' %H:%M';
116 my $datetime = POSIX::Wide::strftime ($fmt, localtime($t));
117 App::Chart::Download::verbose_message
118 (__x("{name}: next check {datetime}",
119 name => $self->name,
120 datetime => $datetime));
121 return;
122 }
123 }
124  
125 App::Chart::Download::verbose_message ($self->name, @$symbol_list);
126 App::Chart::Download::status ($self->name);
127  
128 my $tsproc = $self->{'available_tdate_by_symbol'};
129 {
130 my $avail;
131 if (my $tproc = $self->{'available_tdate'}) {
132 $avail = $tproc->();
133 }
134 if (my $tproc = $self->{'available_date_time'}) {
135 my ($iso_date, $time) = $tproc->();
136 $avail = App::Chart::Download::iso_to_tdate_floor ($iso_date);
137 }
138 if (defined $avail) {
139 $tsproc = sub { return $avail };
140 }
141 }
142 if ($tsproc) {
143 my @new;
144 foreach my $symbol (@$symbol_list) {
145 my $avail = $tsproc->($symbol);
146 my $start = App::Chart::Download::start_tdate_for_update ($symbol);
147 if ($avail >= $start) {
148 push @new, $symbol;
149 } else {
150 App::Chart::Download::verbose_message
151 (__x('{symbol} already got data to {date}',
152 symbol => $symbol,
153 date => App::Chart::Download::tdate_range_string ($avail)));
154 }
155 }
156 $symbol_list = \@new;
157 }
158 if (! @$symbol_list) { return; }
159  
160 my $proc = $self->{'proc'};
161 my @symbol_groups;
162  
163 if ($self->{'by_commodity'}) {
164 require Tie::IxHash;
165 my %byc;
166 tie %byc, 'Tie::IxHash';
167 foreach my $symbol (@$symbol_list) {
168 my $commodity = App::Chart::symbol_commodity ($symbol);
169 push @{$byc{$commodity}}, $symbol;
170 }
171 @symbol_groups = [ values %byc ];
172  
173 } elsif (my $max_symbols = $self->{'max_symbols'}) {
174 my $i = 0;
175 @symbol_groups = List::MoreUtils::part
176 {int(($i++) / $max_symbols)} @$symbol_list;
177  
178 } else {
179 @symbol_groups = ($symbol_list);
180 }
181  
182 if (! eval {
183 foreach my $symbol_group (@symbol_groups) {
184 if ($self->{'proc_with_self'}) {
185 $proc->($self, $symbol_group);
186 } else {
187 $proc->($symbol_group);
188 }
189 }
190 1;
191 }) {
192 my $err = $@;
193 unless (utf8::is_utf8($err)) { $err = Encode::decode('locale',$err); }
194 $err = App::Chart::collapse_whitespace ($err);
195 App::Chart::Download::download_message ("Download error: $err\n");
196 return 0;
197 }
198 return 1;
199 }
200  
201 sub available_tdate_for_symbol {
202 my ($self, $symbol) = @_;
203  
204 if (my $tsproc = $self->{'available_tdate_by_symbol'}) {
205 return $tsproc->($symbol);
206  
207 } elsif (my $tproc = $self->{'available_tdate'}) {
208 return $tproc->();
209  
210 } else {
211 return undef;
212 }
213 }
214  
215 1;
216 __END__
« App/Chart/Download.pm ↑ Summary App/Chart.pm »