App-Chart
view release on metacpan or search on metacpan
lib/App/Chart/DownloadHandler.pm view on Meta::CPAN
# Download data handlers.
# Copyright 2007, 2008, 2009, 2010, 2011, 2014, 2016, 2024 Kevin Ryde
# This file is part of Chart.
#
# Chart is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# Chart is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Chart. If not, see <http://www.gnu.org/licenses/>.
package App::Chart::DownloadHandler;
use 5.010;
use strict;
use warnings;
use sort 'stable'; # lexical in 5.10
use Carp;
use Encode;
use Encode::Locale; # for coding system "locale"
use List::Util qw(min max);
use List::MoreUtils;
use POSIX::Wide;
use Locale::TextDomain ('App-Chart');
use App::Chart;
use App::Chart::Database;
use constant DEBUG => 0;
#------------------------------------------------------------------------------
our @handler_list = ();
sub new {
my ($class, %self) = @_;
$self{'pred'} or croak __PACKAGE__,": missing pred";
$self{'proc'} or croak __PACKAGE__,": missing proc";
App::Chart::Sympred::validate ($self{'pred'});
my $self = bless \%self, $class;
push @handler_list, $self;
$self{'name'} ||= do {
my ($package,$filename,$line) = caller();
"$package:" . Glib::filename_to_unicode($filename) . ":$line" };
# highest priority first and 'stable' above for order added for equals
@handler_list
= sort { ($b->{'priority'}||0) <=> ($a->{'priority'}||0) }
@handler_list;
return $self;
}
sub name {
my ($self) = @_;
return $self->{'name'};
}
#------------------------------------------------------------------------------
sub handlers_for_symbol {
my ($class, $symbol) = @_;
App::Chart::symbol_setups ($symbol);
if (DEBUG) { print "total ", scalar(@handler_list), " handlers\n"; }
return grep { $_->match($symbol) } @handler_list;
}
#------------------------------------------------------------------------------
sub match {
my ($self, $symbol) = @_;
return $self->{'pred'}->match($symbol);
}
#------------------------------------------------------------------------------
sub download {
my ($self, $symbol_list) = @_;
if (DEBUG) { print "Download ",@$symbol_list,"\n"; }
if (! @$symbol_list) { return; }
if (my $key = $self->{'recheck_key'}) {
my $recheck_seconds = 86400 * $self->{'recheck_days'};
my ($timestamp_lo, $timestamp_hi)
= App::Chart::Download::timestamp_range ($recheck_seconds);
my $min_timestamp = '9999';
my $any_wanted = 0;
foreach my $symbol (@$symbol_list) {
my $symbol_timestamp = App::Chart::Database->read_extra ($symbol, $key);
if (defined $symbol_timestamp
&& $symbol_timestamp ge $timestamp_lo
&& $symbol_timestamp le $timestamp_hi) {
if (DEBUG) { print "recheck not for $symbol: $symbol_timestamp\n"; }
$min_timestamp = List::Util::minstr($min_timestamp, $symbol_timestamp);
} else {
if (DEBUG) { print "recheck want $symbol: $symbol_timestamp\n"; }
( run in 1.112 second using v1.01-cache-2.11-cpan-39bf76dae61 )