fixes to how commodities are looked up using quotes.cc

This commit is contained in:
John Wiegley 2004-08-21 03:54:27 -04:00
parent 9350433499
commit b030416982
6 changed files with 36 additions and 25 deletions

View file

@ -980,7 +980,8 @@ amount_t commodity_t::value(const std::time_t moment)
} }
if (updater) if (updater)
(*updater)(this, moment, age, price); (*updater)(this, moment, age,
history.size() > 0 ? (*history.rbegin()).first : 0, price);
return price; return price;
} }

View file

@ -192,8 +192,7 @@ std::ostream& operator<<(std::ostream& out, const amount_t& amt);
#define COMMODITY_STYLE_SEPARATED 0x0002 #define COMMODITY_STYLE_SEPARATED 0x0002
#define COMMODITY_STYLE_EUROPEAN 0x0004 #define COMMODITY_STYLE_EUROPEAN 0x0004
#define COMMODITY_STYLE_THOUSANDS 0x0008 #define COMMODITY_STYLE_THOUSANDS 0x0008
#define COMMODITY_STYLE_CONSULTED 0x0010 #define COMMODITY_STYLE_NOMARKET 0x0010
#define COMMODITY_STYLE_NOMARKET 0x0020
typedef std::map<const std::time_t, amount_t> history_map; typedef std::map<const std::time_t, amount_t> history_map;
typedef std::pair<const std::time_t, amount_t> history_pair; typedef std::pair<const std::time_t, amount_t> history_pair;
@ -210,6 +209,7 @@ class commodity_t
virtual void operator()(commodity_t * commodity, virtual void operator()(commodity_t * commodity,
const std::time_t moment, const std::time_t moment,
const std::time_t date, const std::time_t date,
const std::time_t last,
amount_t& price) = 0; amount_t& price) = 0;
}; };
@ -222,6 +222,7 @@ class commodity_t
unsigned short precision; unsigned short precision;
unsigned short flags; unsigned short flags;
history_map history; history_map history;
std::time_t last_lookup;
amount_t conversion; amount_t conversion;
ident_t ident; ident_t ident;
@ -254,7 +255,8 @@ class commodity_t
commodity_t(const std::string& _symbol = "", commodity_t(const std::string& _symbol = "",
unsigned int _precision = 0, unsigned int _precision = 0,
unsigned int _flags = COMMODITY_STYLE_DEFAULTS) unsigned int _flags = COMMODITY_STYLE_DEFAULTS)
: symbol(_symbol), quote(false), precision(_precision), flags(_flags) { : symbol(_symbol), quote(false), precision(_precision),
flags(_flags), last_lookup(0) {
check_symbol(); check_symbol();
} }

View file

@ -9,7 +9,7 @@
namespace ledger { namespace ledger {
const unsigned long binary_magic_number = 0xFFEED765; const unsigned long binary_magic_number = 0xFFEED765;
static const unsigned long format_version = 0x0002000c; static const unsigned long format_version = 0x0002000e;
bool binary_parser_t::test(std::istream& in) const bool binary_parser_t::test(std::istream& in) const
{ {
@ -151,6 +151,7 @@ commodity_t * read_binary_commodity(std::istream& in)
read_binary_amount(in, amt); read_binary_amount(in, amt);
commodity->history.insert(history_pair(when, amt)); commodity->history.insert(history_pair(when, amt));
} }
read_binary_number(in, commodity->last_lookup);
read_binary_amount(in, commodity->conversion); read_binary_amount(in, commodity->conversion);
@ -351,6 +352,7 @@ void write_binary_commodity(std::ostream& out, commodity_t * commodity)
write_binary_number(out, (*i).first); write_binary_number(out, (*i).first);
write_binary_amount(out, (*i).second); write_binary_amount(out, (*i).second);
} }
write_binary_number(out, commodity->last_lookup);
write_binary_amount(out, commodity->conversion); write_binary_amount(out, commodity->conversion);
} }

View file

@ -8,47 +8,54 @@ namespace ledger {
void quotes_by_script::operator()(commodity_t * commodity, void quotes_by_script::operator()(commodity_t * commodity,
const std::time_t moment, const std::time_t moment,
const std::time_t date, const std::time_t date,
const std::time_t last,
amount_t& price) amount_t& price)
{ {
if (commodity->flags & COMMODITY_STYLE_CONSULTED || DEBUG_CLASS("ledger.quotes.download");
std::difftime(moment, now) < pricing_leeway ||
DEBUG_PRINT_("commodity: " << commodity->symbol);
DEBUG_PRINT_TIME_(now);
DEBUG_PRINT_TIME_(moment);
DEBUG_PRINT_TIME_(date);
DEBUG_PRINT_TIME_(last);
DEBUG_PRINT_TIME_(commodity->last_lookup);
DEBUG_PRINT_("pricing_leeway is " << pricing_leeway);
if (std::difftime(now, commodity->last_lookup) < pricing_leeway ||
std::difftime(now, last) < pricing_leeway ||
(price && std::difftime(moment, date) <= pricing_leeway)) (price && std::difftime(moment, date) <= pricing_leeway))
return; return;
using namespace std; using namespace std;
DEBUG_PRINT("ledger.quotes.download", DEBUG_PRINT_("downloading quote for symbol " << commodity->symbol);
"downloading quote for symbol " << commodity->symbol);
DEBUG_PRINT("ledger.quotes.download",
"pricing_leeway is " << pricing_leeway);
DEBUG_PRINT_TIME("ledger.quotes.download", now);
DEBUG_PRINT_TIME("ledger.quotes.download", moment);
DEBUG_PRINT_TIME("ledger.quotes.download", date);
// Only consult the Internet once for any commodity // Only consult the Internet once for any commodity
commodity->flags |= COMMODITY_STYLE_CONSULTED; commodity->last_lookup = now;
cache_dirty = true; cache_dirty = true;
char buf[256]; char buf[256];
buf[0] = '\0'; buf[0] = '\0';
bool success = true;
if (FILE * fp = popen((string("getquote ") + if (FILE * fp = popen((string("getquote ") +
commodity->symbol).c_str(), "r")) { commodity->symbol).c_str(), "r")) {
if (feof(fp) || ! fgets(buf, 255, fp)) { if (feof(fp) || ! fgets(buf, 255, fp))
fclose(fp); success = false;
return;
}
fclose(fp); fclose(fp);
} }
if (buf[0]) { if (success && buf[0]) {
char * p = strchr(buf, '\n'); char * p = strchr(buf, '\n');
if (p) *p = '\0'; if (p) *p = '\0';
DEBUG_PRINT_("downloaded quote: " << buf);
price.parse(buf); price.parse(buf);
commodity->add_price(now, price); commodity->add_price(now, price);
if (! price_db.empty()) { if (price && ! price_db.empty()) {
char buf[128]; char buf[128];
strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now)); strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now));
ofstream database(price_db.c_str(), ios_base::out | ios_base::app); ofstream database(price_db.c_str(), ios_base::out | ios_base::app);

View file

@ -21,6 +21,7 @@ class quotes_by_script : public commodity_t::updater_t
virtual void operator()(commodity_t * commodity, virtual void operator()(commodity_t * commodity,
const std::time_t moment, const std::time_t moment,
const std::time_t date, const std::time_t date,
const std::time_t last,
amount_t& price); amount_t& price);
}; };

View file

@ -462,8 +462,7 @@ unsigned int textual_parser_t::parse(std::istream& in,
parse_commodity(in, symbol); parse_commodity(in, symbol);
commodity_t * commodity = commodity_t::find_commodity(symbol, true); commodity_t * commodity = commodity_t::find_commodity(symbol, true);
commodity->flags |= (COMMODITY_STYLE_CONSULTED | commodity->flags |= COMMODITY_STYLE_NOMARKET;
COMMODITY_STYLE_NOMARKET);
break; break;
} }
@ -569,8 +568,7 @@ unsigned int textual_parser_t::parse(std::istream& in,
done: done:
if (time_commodity) { if (time_commodity) {
time_commodity->precision = 2; time_commodity->precision = 2;
time_commodity->flags |= (COMMODITY_STYLE_CONSULTED | time_commodity->flags |= COMMODITY_STYLE_NOMARKET;
COMMODITY_STYLE_NOMARKET);
} }
if (errors > 0) { if (errors > 0) {