More refactoring for --download
This commit is contained in:
parent
0663ac0a2a
commit
1fad2ec7c1
7 changed files with 63 additions and 33 deletions
|
|
@ -556,13 +556,22 @@ amount_t::value(const bool primary_only,
|
|||
annotation().has_flags(ANNOTATION_PRICE_FIXATED)) {
|
||||
return (*annotation().price * number()).rounded();
|
||||
}
|
||||
else if (optional<price_point_t> point =
|
||||
commodity().find_price(in_terms_of, moment)) {
|
||||
return (point->price * number()).rounded();
|
||||
else {
|
||||
optional<price_point_t> point =
|
||||
commodity().find_price(in_terms_of, moment);
|
||||
|
||||
// Whether a price was found or not, check whether we should attempt
|
||||
// to download a price from the Internet. This is done if (a) no
|
||||
// price was found, or (b) the price is "stale" according to the
|
||||
// setting of --price-exp.
|
||||
point = commodity().check_for_updated_price(point, moment, in_terms_of);
|
||||
if (point)
|
||||
return (point->price * number()).rounded();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw_(amount_error, _("Cannot determine value of an uninitialized amount"));
|
||||
throw_(amount_error,
|
||||
_("Cannot determine value of an uninitialized amount"));
|
||||
}
|
||||
return none;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -346,26 +346,6 @@ optional<price_point_t>
|
|||
" found price " << best.price << " from " << best.when);
|
||||
DEBUG("commodity.download",
|
||||
"found price " << best.price << " from " << best.when);
|
||||
#endif
|
||||
#if 0
|
||||
DEBUG("commodity.download", "leeway = " << download_leeway);
|
||||
datetime_t::sec_type seconds_diff;
|
||||
if (moment) {
|
||||
seconds_diff = (*moment - best.when).total_seconds();
|
||||
DEBUG("commodity.download", "moment = " << *moment);
|
||||
DEBUG("commodity.download", "slip.moment = " << seconds_diff);
|
||||
} else {
|
||||
seconds_diff = (CURRENT_TIME() - best.when).total_seconds();
|
||||
DEBUG("commodity.download", "slip.now = " << seconds_diff);
|
||||
}
|
||||
|
||||
if (download_quotes && ! source.has_flags(COMMODITY_NOMARKET) &&
|
||||
seconds_diff > download_leeway) {
|
||||
DEBUG("commodity.download",
|
||||
"attempting to download a more current quote...");
|
||||
if (optional<price_point_t> quote = source.download_quote(commodity))
|
||||
return quote;
|
||||
}
|
||||
#endif
|
||||
return best;
|
||||
}
|
||||
|
|
@ -398,6 +378,42 @@ optional<commodity_t::base_t::history_t&>
|
|||
return none;
|
||||
}
|
||||
|
||||
optional<price_point_t>
|
||||
commodity_t::check_for_updated_price(const optional<price_point_t>& point,
|
||||
const optional<datetime_t>& moment,
|
||||
const optional<commodity_t&>& in_terms_of)
|
||||
{
|
||||
if (parent().get_quotes && ! has_flags(COMMODITY_NOMARKET)) {
|
||||
bool exceeds_leeway = true;
|
||||
|
||||
if (point) {
|
||||
time_duration_t::sec_type seconds_diff;
|
||||
if (moment) {
|
||||
seconds_diff = (*moment - point->when).total_seconds();
|
||||
DEBUG("commodity.download", "moment = " << *moment);
|
||||
DEBUG("commodity.download", "slip.moment = " << seconds_diff);
|
||||
} else {
|
||||
seconds_diff = (CURRENT_TIME() - point->when).total_seconds();
|
||||
DEBUG("commodity.download", "slip.now = " << seconds_diff);
|
||||
}
|
||||
|
||||
DEBUG("commodity.download", "leeway = " << parent().quote_leeway);
|
||||
if (seconds_diff < parent().quote_leeway)
|
||||
exceeds_leeway = false;
|
||||
}
|
||||
|
||||
if (exceeds_leeway) {
|
||||
DEBUG("commodity.download",
|
||||
"attempting to download a more current quote...");
|
||||
if (optional<price_point_t> quote =
|
||||
parent().get_commodity_quote(*this, in_terms_of)) {
|
||||
return quote;
|
||||
}
|
||||
}
|
||||
}
|
||||
return point;
|
||||
}
|
||||
|
||||
commodity_t::operator bool() const
|
||||
{
|
||||
return this != parent().null_commodity;
|
||||
|
|
|
|||
|
|
@ -309,6 +309,11 @@ public:
|
|||
return none;
|
||||
}
|
||||
|
||||
optional<price_point_t>
|
||||
check_for_updated_price(const optional<price_point_t>& point,
|
||||
const optional<datetime_t>& moment,
|
||||
const optional<commodity_t&>& in_terms_of);
|
||||
|
||||
// Methods related to parsing, reading, writing, etc., the commodity
|
||||
// itself.
|
||||
|
||||
|
|
|
|||
|
|
@ -417,12 +417,12 @@ void global_scope_t::normalize_report_options(const string& verb)
|
|||
report_t& rep(report());
|
||||
|
||||
// jww (2009-02-09): These globals are a hack, but hard to avoid.
|
||||
item_t::use_effective_date = rep.HANDLED(effective);
|
||||
rep.session.commodity_pool->keep_base = rep.HANDLED(base);
|
||||
rep.session.commodity_pool->download_quotes = rep.session.HANDLED(download);
|
||||
item_t::use_effective_date = rep.HANDLED(effective);
|
||||
rep.session.commodity_pool->keep_base = rep.HANDLED(base);
|
||||
rep.session.commodity_pool->get_quotes = rep.session.HANDLED(download);
|
||||
|
||||
if (rep.session.HANDLED(price_exp_))
|
||||
rep.session.commodity_pool->download_leeway =
|
||||
rep.session.commodity_pool->quote_leeway =
|
||||
rep.session.HANDLER(price_exp_).value.as_long();
|
||||
|
||||
if (rep.session.HANDLED(price_db_))
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ namespace ledger {
|
|||
|
||||
commodity_pool_t::commodity_pool_t()
|
||||
: default_commodity(NULL), keep_base(false),
|
||||
download_leeway(86400), download_quotes(false)
|
||||
quote_leeway(86400), get_quotes(false)
|
||||
{
|
||||
TRACE_CTOR(commodity_pool_t, "");
|
||||
null_commodity = create("");
|
||||
|
|
|
|||
|
|
@ -83,12 +83,13 @@ public:
|
|||
bool keep_base; // --base
|
||||
|
||||
optional<path> price_db; // --price-db=
|
||||
long download_leeway; // --leeway=
|
||||
bool download_quotes; // --download
|
||||
long quote_leeway; // --leeway=
|
||||
bool get_quotes; // --download
|
||||
|
||||
public:
|
||||
function<optional<price_point_t>
|
||||
(const optional<commodity_t&>& commodity)> get_commodity_quote;
|
||||
(const commodity_t& commodity,
|
||||
const optional<commodity_t&>& in_terms_of)> get_commodity_quote;
|
||||
|
||||
explicit commodity_pool_t();
|
||||
|
||||
|
|
|
|||
|
|
@ -133,7 +133,6 @@ value_t report_t::fn_market(call_scope_t& scope)
|
|||
optional<datetime_t> moment = (args.has(1) ?
|
||||
args.get<datetime_t>(1) :
|
||||
optional<datetime_t>());
|
||||
|
||||
if (args.has(2))
|
||||
result = args.value_at(0).exchange_commodities(args.get<string>(2),
|
||||
/* add_prices= */ false,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue