| 1 | # Download cost recording and calculations. | ||
| 2 | |||
| 3 | # Copyright 2007, 2008, 2009, 2010, 2011 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 | |||
| 20 | package App::Chart::DownloadCost; | ||
| 21 | use 5.010; | ||
| 22 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use Carp; | ||
| 25 | use List::Util qw(min max); | ||
| 26 | |||
| 27 | use App::Chart; | ||
| 28 | use App::Chart::Database; | ||
| 29 | use App::Chart::Download; | ||
| 30 | |||
| 31 | |||
| 32 | sub cost_store_h { | ||
| 33 | my ($h) = @_; | ||
| 34 | my $key = $h->{'cost_key'}; | ||
| 35 | my $value = $h->{'cost_value'}; | ||
| 36 | if (! defined $value && defined $h->{'resp'}) { | ||
| 37 | $value = length ($h->{'resp'}->content); | ||
| 38 | } | ||
| 39 | if (! defined $value) { | ||
| 40 | croak "DownloadCost: missing cost_value or resp for cost_key '$key'"; | ||
| 41 | } | ||
| 42 | cost_store ($key, $value); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub cost_store { | ||
| 46 | my ($key, $value) = @_; | ||
| 47 | App::Chart::Database->write_extra ('', $key, $value); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub cost_get { | ||
| 51 | my ($key, $default) = @_; | ||
| 52 | my $cost = App::Chart::Database->read_extra ('', $key); | ||
| 53 | return (defined $cost ? $cost : $default); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | # going by day or by symbol | ||
| 58 | # | ||
| 59 | # Decide whether to use daily downloads or individual symbol downloads to | ||
| 60 | # update all of SYMBOL-LIST. Download will be from the | ||
| 61 | # `download-start-tdate' of each symbol, up to AVAIL-TDATE. | ||
| 62 | # | ||
| 63 | # DAILY-COST is the cost (in bytes) to download a day. | ||
| 64 | # (SYMBOL-COST-PROC tdate) should return the cost of downloading from TDATE | ||
| 65 | # to now (ie. AVAIL-TDATE) for a symbol. | ||
| 66 | # Both costs will get `http-get-cost' added to them automatically. | ||
| 67 | # | ||
| 68 | # The return is two values (USE-SYMBOL-LIST TDATE), which is a list of | ||
| 69 | # symbols to do individually, and a tdate to start from for daily | ||
| 70 | # downloads. That tdate is (1+ avail-tdate) when no dailies should be | ||
| 71 | # done. | ||
| 72 | # | ||
| 73 | # As an example, if there's a few symbols quite a bit behind but the rest | ||
| 74 | # needing just a day or two then the result can be to do the laggards | ||
| 75 | # individually, then whole days for the most recent. | ||
| 76 | # | ||
| 77 | |||
| 78 | sub by_day_or_by_symbol { | ||
| 79 | my (%param) = @_; | ||
| 80 | |||
| 81 | if (! exists $param{'available_tdate'}) { croak 'missing available_tdate'; } | ||
| 82 | my $avail = $param{'available_tdate'}; | ||
| 83 | |||
| 84 | if (! exists $param{'symbol_list'}) { croak 'missing symbol_list'; } | ||
| 85 | my $symbol_list = $param{'symbol_list'}; | ||
| 86 | |||
| 87 | my $whole_cost; | ||
| 88 | if (exists $param{'whole_cost'}) { | ||
| 89 | $whole_cost = $param{'whole_cost'}; | ||
| 90 | } elsif (exists $param{'whole_cost_key'}) { | ||
| 91 | $whole_cost = cost_get ($param{'whole_cost_key'}, | ||
| 92 | $param{'whole_cost_default'}); | ||
| 93 | } else { | ||
| 94 | croak 'no whole day cost method'; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $indiv_cost_proc; | ||
| 98 | if (exists $param{'indiv_cost_proc'}) { | ||
| 99 | $indiv_cost_proc = $param{'indiv_cost_proc'}; | ||
| 100 | } elsif (exists $param{'indiv_cost_key'}) { | ||
| 101 | my $cost = cost_get ($param{'indiv_cost_key'}, | ||
| 102 | $param{'indiv_cost_default'}); | ||
| 103 | $indiv_cost_proc = sub { return $cost; }; | ||
| 104 | } elsif (exists $param{'indiv_cost_fixed'}) { | ||
| 105 | my $cost = $param{'indiv_cost_fixed'}; | ||
| 106 | $indiv_cost_proc = sub { return $cost; }; | ||
| 107 | } elsif (exists $param{'indiv_cost_daily'}) { | ||
| 108 | my $indiv_cost_daily = $param{'indiv_cost_daily'}; | ||
| 109 | $indiv_cost_proc = sub { my ($lo) = @_; | ||
| 110 | return $indiv_cost_daily * ($avail - $lo + 1); }; | ||
| 111 | } else { | ||
| 112 | croak 'no indiv cost method'; | ||
| 113 | } | ||
| 114 | |||
| 115 | $whole_cost += $App::Chart::option{http_get_cost}; | ||
| 116 | |||
| 117 | # tdate => [ symbol, symbol, ... ] | ||
| 118 | my %hash = (); | ||
| 119 | foreach my $symbol (@$symbol_list) { | ||
| 120 | my $start = App::Chart::Download::start_tdate_for_update ($symbol); | ||
| 121 | my $aref = ($hash{$start} ||= []); | ||
| 122 | push @$aref, $symbol; | ||
| 123 | } | ||
| 124 | |||
| 125 | my $best_whole_tdate = min (keys %hash); | ||
| 126 | my $best_cost = $whole_cost * ($avail - $best_whole_tdate + 1); | ||
| 127 | my @best_indiv_list = (); | ||
| 128 | ### whole ... | ||
| 129 | ### $best_whole_tdate | ||
| 130 | ### $avail | ||
| 131 | ### none indiv ... | ||
| 132 | ### $best_cost | ||
| 133 | ### whole_cost per day: $whole_cost | ||
| 134 | |||
| 135 | my @indiv_list = (); | ||
| 136 | my $indiv_cost = 0; | ||
| 137 | |||
| 138 | while (%hash) { | ||
| 139 | my $new_tdate = min (keys %hash); | ||
| 140 | my $new_list = delete $hash{$new_tdate}; | ||
| 141 | my $new_cost = scalar (@$new_list) | ||
| 142 | * (($indiv_cost_proc->($new_tdate)) + $App::Chart::option{http_get_cost}); | ||
| 143 | push @indiv_list, @$new_list; | ||
| 144 | $indiv_cost += $new_cost; | ||
| 145 | |||
| 146 | my $whole_tdate = min (keys %hash); | ||
| 147 | if (! defined $whole_tdate) { $whole_tdate = $avail + 1; } | ||
| 148 | my $cost = $whole_cost * ($avail - $whole_tdate + 1) + $indiv_cost; | ||
| 149 | # if (DEBUG) { print " [",scalar(%hash), | ||
| 150 | # "] whole $whole_tdate indiv ",scalar(@indiv_list), | ||
| 151 | # ": $cost (indiv $indiv_cost)\n"; } | ||
| 152 | |||
| 153 | if ($cost < $best_cost) { | ||
| 154 | ### is new best ... | ||
| 155 | $best_cost = $cost; | ||
| 156 | @best_indiv_list = @indiv_list; | ||
| 157 | $best_whole_tdate = $whole_tdate; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | # # same order as original symbol-list | ||
| 162 | # (set! best-symbol-list (lset-intersection string=? | ||
| 163 | # symbol-list best-symbol-list)) | ||
| 164 | |||
| 165 | return $best_whole_tdate, @best_indiv_list; | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | 1; | ||
| 170 | __END__ |