GnuCash  5.6-150-g038405b370+
gnc-quotes.cpp
1 /********************************************************************\
2  * gnc-quotes.hpp -- proxy for Finance::Quote *
3  * Copyright (C) 2021 Geert Janssens <geert@kobaltwit.be> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA gnu@gnu.org *
21 \ *******************************************************************/
22 
23 #include <boost/process/environment.hpp>
24 #include <config.h>
25 #include <qoflog.h>
26 
27 #include <algorithm>
28 #include <stdexcept>
29 #include <vector>
30 #include <string>
31 #include <iostream>
32 #include <boost/version.hpp>
33 #if BOOST_VERSION < 107600
34 // json_parser uses a deprecated version of bind.hpp
35 #define BOOST_BIND_GLOBAL_PLACEHOLDERS
36 #endif
37 #include <boost/algorithm/string.hpp>
38 #include <boost/filesystem.hpp>
39 #include <boost/version.hpp>
40 #if BOOST_VERSION < 108600
41 #include <boost/process.hpp>
42 #ifdef BOOST_WINDOWS_API
43 #include <boost/process/windows.hpp>
44 #endif
45 #else
46 #include <boost/process/v1/async.hpp>
47 #include <boost/process/v1/child.hpp>
48 #include <boost/process/v1/env.hpp>
49 #include <boost/process/v1/environment.hpp>
50 #include <boost/process/v1/error.hpp>
51 #include <boost/process/v1/group.hpp>
52 #include <boost/process/v1/io.hpp>
53 #include <boost/process/v1/pipe.hpp>
54 #include <boost/process/v1/search_path.hpp>
55 #include <boost/process/v1/start_dir.hpp>
56 #ifdef BOOST_WINDOWS_API
57 #include <boost/process/v1/windows.hpp>
58 #endif
59 #endif
60 #include <boost/regex.hpp>
61 #include <boost/property_tree/ptree.hpp>
62 #include <boost/property_tree/json_parser.hpp>
63 #include <boost/iostreams/device/array.hpp>
64 #include <boost/iostreams/stream_buffer.hpp>
65 #include <boost/locale.hpp>
66 #include <boost/asio.hpp>
67 #include <glib.h>
68 #include "gnc-commodity.hpp"
69 #include <gnc-datetime.hpp>
70 #include <gnc-numeric.hpp>
71 #include "gnc-quotes.hpp"
72 
73 #include <gnc-commodity.h>
74 #include <gnc-path.h>
75 #include "gnc-ui-util.h"
76 #include <gnc-prefs.h>
77 #include <gnc-session.h>
78 #include <regex.h>
79 #include <qofbook.h>
80 
81 static const QofLogModule log_module = "gnc.price-quotes";
82 static const char* av_api_env = "ALPHAVANTAGE_API_KEY";
83 static const char* av_api_key = "alphavantage-api-key";
84 static const char* yh_api_env = "FINANCEAPI_API_KEY";
85 static const char* yh_api_key = "yhfinance-api-key";
86 
87 namespace bl = boost::locale;
88 #if BOOST_VERSION < 108800
89 namespace bp = boost::process;
90 #else
91 namespace bp = boost::process::v1;
92 #endif
93 namespace bfs = boost::filesystem;
94 namespace bpt = boost::property_tree;
95 namespace bio = boost::iostreams;
96 
97 using QuoteResult = std::tuple<int, StrVec, StrVec>;
98 
99 struct GncQuoteSourceError : public std::runtime_error
100 {
101  GncQuoteSourceError(const std::string& err) : std::runtime_error(err) {}
102 };
103 
104 CommVec
105 gnc_quotes_get_quotable_commodities(const gnc_commodity_table * table);
106 
108 {
109 public:
110  virtual ~GncQuoteSource() = default;
111  virtual const StrVec& get_sources() const noexcept = 0;
112  virtual const std::string & get_version() const noexcept = 0;
113  virtual QuoteResult get_quotes(const std::string& json_str) const = 0;
114 };
115 
116 
118 {
119 public:
120  // Constructor - checks for presence of Finance::Quote and import version and quote sources
121  GncQuotesImpl ();
122  explicit GncQuotesImpl (QofBook *book);
123  GncQuotesImpl(QofBook*, std::unique_ptr<GncQuoteSource>);
124 
125  void fetch (QofBook *book);
126  void fetch (CommVec& commodities);
127  void fetch (gnc_commodity *comm);
128  void report (const char* source, const StrVec& commodities, bool verbose);
129 
130  const std::string& version() noexcept { return m_quotesource->get_version(); }
131  const QuoteSources& sources() noexcept { return m_sources; }
132  bool had_failures() noexcept { return !m_failures.empty(); }
133  const QFVec& failures() noexcept;
134  std::string report_failures() noexcept;
135 
136 private:
137  std::string query_fq (const char* source, const StrVec& commoditites);
138  std::string query_fq (const CommVec&);
139  bpt::ptree parse_quotes (const std::string& quote_str);
140  void create_quotes(const bpt::ptree& pt, const CommVec& comm_vec);
141  std::string comm_vec_to_json_string(const CommVec&) const;
142  GNCPrice* parse_one_quote(const bpt::ptree&, gnc_commodity*);
143 
144  std::unique_ptr<GncQuoteSource> m_quotesource;
145  QuoteSources m_sources;
146  QFVec m_failures;
147  QofBook *m_book;
148  gnc_commodity *m_dflt_curr;
149 };
150 
151 class GncFQQuoteSource final : public GncQuoteSource
152 {
153  const bfs::path c_cmd;
154  std::string c_fq_wrapper;
155  std::string m_version;
156  StrVec m_sources;
157  bp::environment m_env;
158 public:
160  ~GncFQQuoteSource() = default;
161  const std::string& get_version() const noexcept override { return m_version; }
162  const StrVec& get_sources() const noexcept override { return m_sources; }
163  QuoteResult get_quotes(const std::string&) const override;
164 private:
165  QuoteResult run_cmd (const StrVec& args, const std::string& json_string) const;
166  void set_api_key(const char* api_pref, const char* api_env);
167 };
168 
169 static void show_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose);
170 static void show_currency_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose);
171 static std::string parse_quotesource_error(const std::string& line);
172 
173 static const std::string empty_string{};
174 
175 GncFQQuoteSource::GncFQQuoteSource() :
176 c_cmd{bp::search_path("perl")},
177 m_version{}, m_sources{}, m_env{boost::this_process::environment()}
178 {
179  char *bindir = gnc_path_get_bindir();
180  c_fq_wrapper = std::string(bindir) + "/finance-quote-wrapper";
181  g_free(bindir);
182  StrVec args{"-w", c_fq_wrapper, "-v"};
183  auto [rv, sources, errors] = run_cmd(args, empty_string);
184  if (rv)
185  {
186  std::string err{bl::translate("Failed to initialize Finance::Quote: ")};
187  for (const auto& err_line : errors)
188  err += err_line.empty() ? "" : err_line + "\n";
189  throw(GncQuoteSourceError(err));
190  }
191  if (!errors.empty())
192  {
193  std::string err{bl::translate("Finance::Quote check returned error ")};
194  for(const auto& err_line : errors)
195  err += err.empty() ? "" : err_line + "\n";
196  throw(GncQuoteSourceError(err));
197  }
198  auto version{sources.front()};
199  if (version.empty())
200  {
201  std::string err{bl::translate("No Finance::Quote Version")};
202  throw(GncQuoteSourceError(err));
203  }
204  m_version = std::move(version);
205  sources.erase(sources.begin());
206  m_sources = std::move(sources);
207  std::sort (m_sources.begin(), m_sources.end());
208 
209  set_api_key(av_api_key, av_api_env);
210  set_api_key(yh_api_key, yh_api_env);
211 }
212 
213 QuoteResult
214 GncFQQuoteSource::get_quotes(const std::string& json_str) const
215 {
216  StrVec args{"-w", c_fq_wrapper, "-f" };
217  return run_cmd(args, json_str);
218 }
219 
220 QuoteResult
221 GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) const
222 {
223  StrVec out_vec, err_vec;
224  int cmd_result;
225 
226  try
227  {
228  std::future<std::vector<char> > out_buf, err_buf;
229  boost::asio::io_context svc;
230 
231  auto input_buf = bp::buffer (json_string);
232  bp::child process;
233  process = bp::child(c_cmd, args,
234  bp::std_out > out_buf,
235  bp::std_err > err_buf,
236  bp::std_in < input_buf,
237 #ifdef BOOST_WINDOWS_API
238  bp::windows::create_no_window,
239 #endif
240  m_env,
241  svc);
242 
243  svc.run();
244  process.wait();
245 
246  {
247  auto raw = out_buf.get();
248  std::vector<std::string> data;
249  std::string line;
250  bio::stream_buffer<bio::array_source> sb(raw.data(), raw.size());
251  std::istream is(&sb);
252 
253  while (std::getline(is, line) && !line.empty())
254  {
255 #ifdef __WIN32
256  if (line.back() == '\r')
257  line.pop_back();
258 #endif
259  out_vec.push_back (std::move(line));
260  }
261  raw = err_buf.get();
262  bio::stream_buffer<bio::array_source> eb(raw.data(), raw.size());
263  std::istream es(&eb);
264 
265  while (std::getline(es, line) && !line.empty())
266  err_vec.push_back (std::move(line));
267  }
268  cmd_result = process.exit_code();
269  }
270  catch (std::exception &e)
271  {
272  cmd_result = -1;
273  err_vec.push_back(e.what());
274  };
275 
276  return QuoteResult (cmd_result, std::move(out_vec), std::move(err_vec));
277 }
278 
279 void
280 GncFQQuoteSource::set_api_key(const char* api_key, const char* api_env)
281 {
282  auto key = gnc_prefs_get_string("general.finance-quote", api_key);
283  if (key && *key)
284  {
285  m_env[api_env] = key;
286  g_free(key);
287  }
288  else
289  {
290  if (api_key == av_api_key && m_env.find(api_env) == m_env.end())
291  PWARN("No Alpha Vantage API key set, currency quotes and other "
292  "AlphaVantage based quotes won't work.");
293  g_free(key);
294  }
295 }
296 
297 /* GncQuotes implementation */
298 GncQuotesImpl::GncQuotesImpl() : m_quotesource{new GncFQQuoteSource},
299  m_sources{}, m_failures{},
300  m_book{qof_session_get_book(gnc_get_current_session())},
301  m_dflt_curr{gnc_default_currency()}
302 {
303  m_sources = m_quotesource->get_sources();
304 }
305 
306 GncQuotesImpl::GncQuotesImpl(QofBook* book) : m_quotesource{new GncFQQuoteSource},
307 m_sources{}, m_book{book},
308 m_dflt_curr{gnc_default_currency()}
309 {
310  m_sources = m_quotesource->get_sources();
311 }
312 
313 GncQuotesImpl::GncQuotesImpl(QofBook* book, std::unique_ptr<GncQuoteSource> quote_source) :
314 m_quotesource{std::move(quote_source)},
315 m_sources{}, m_book{book}, m_dflt_curr{gnc_default_currency()}
316 {
317  m_sources = m_quotesource->get_sources();
318 }
319 
320 void
321 GncQuotesImpl::fetch (QofBook *book)
322 {
323  if (!book)
324  throw (GncQuoteException(bl::translate("GncQuotes::Fetch called with no book.")));
325  auto commodities = gnc_quotes_get_quotable_commodities (
327  fetch (commodities);
328 }
329 
330 void
331 GncQuotesImpl::fetch (gnc_commodity *comm)
332 {
333  auto commodities = CommVec {comm};
334  fetch (commodities);
335 }
336 
337 void
338 GncQuotesImpl::fetch (CommVec& commodities)
339 {
340  m_failures.clear();
341  if (commodities.empty())
342  throw (GncQuoteException(bl::translate("GncQuotes::Fetch called with no commodities.")));
343  auto quote_str{query_fq (commodities)};
344  auto ptree{parse_quotes (quote_str)};
345  create_quotes(ptree, commodities);
346 }
347 
348 void
349 GncQuotesImpl::report (const char* source, const StrVec& commodities,
350  bool verbose)
351 {
352  if (!source)
353  throw (GncQuoteException(bl::translate("GncQuotes::Report called with no source.")));
354 
355  bool is_currency{strcmp(source, "currency") == 0};
356  m_failures.clear();
357  if (commodities.empty())
358  {
359  std::cerr << _("There were no commodities for which to retrieve quotes.") << std::endl;
360  return;
361  }
362  try
363  {
364  auto quote_str{query_fq (source, commodities)};
365  auto ptree{parse_quotes (quote_str)};
366  auto source_pt_ai{ptree.find(source)};
367  if (is_currency)
368  show_currency_quotes(source_pt_ai->second, commodities, verbose);
369  else
370  show_quotes(source_pt_ai->second, commodities, verbose);
371  }
372  catch (const GncQuoteException& err)
373  {
374  std::cerr << _("Finance::Quote retrieval failed with error ") << err.what() << std::endl;
375  }
376 }
377 
378 const QFVec&
379 GncQuotesImpl::failures() noexcept
380 {
381  return m_failures;
382 }
383 
384 static std::string
385 explain(GncQuoteError err, const std::string& errmsg)
386 {
387  std::string retval;
388  switch (err)
389  {
390  case GncQuoteError::NO_RESULT:
391  if (errmsg.empty())
392  retval += _("Finance::Quote returned no data and set no error.");
393  else
394  retval += _("Finance::Quote returned an error: ") + errmsg;
395  break;
396  case GncQuoteError::QUOTE_FAILED:
397  if (errmsg.empty())
398  retval += _("Finance::Quote reported failure set no error.");
399  else
400  retval += _("Finance::Quote reported failure with error: ") + errmsg;
401  break;
402  case GncQuoteError::NO_CURRENCY:
403  retval += _("Finance::Quote returned a quote with no currency.");
404  break;
405  case GncQuoteError::UNKNOWN_CURRENCY:
406  retval += _("Finance::Quote returned a quote with a currency GnuCash doesn't know about.");
407  break;
408  case GncQuoteError::NO_PRICE:
409  retval += _("Finance::Quote returned a quote with no price element.");
410  break;
411  case GncQuoteError::PRICE_PARSE_FAILURE:
412  retval += _("Finance::Quote returned a quote with a price that GnuCash was unable to covert to a number.");
413  break;
414  case GncQuoteError::SUCCESS:
415  default:
416  retval += _("The quote has no error set.");
417  break;
418  }
419  return retval;
420 }
421 
422 std::string
423 GncQuotesImpl::report_failures() noexcept
424 {
425  std::string retval{_("Quotes for the following commodities were unavailable or unusable:\n")};
426  std::for_each(m_failures.begin(), m_failures.end(),
427  [&retval](auto failure)
428  {
429  auto [ns, sym, reason, err] = failure;
430  retval += "* " + ns + ":" + sym + " " +
431  explain(reason, err) + "\n";
432  });
433  return retval;
434 }
435 
436 /* **** Private function implementations ****/
437 
438 using Path = bpt::ptree::path_type;
439 static inline Path make_quote_path(const std::string &name_space,
440  const std::string &symbol)
441 {
442  using Path = bpt::ptree::path_type;
443  Path key{name_space, '|'};
444  key /= Path{symbol, '|'};
445  return key;
446 };
447 
448 std::string
449 GncQuotesImpl::comm_vec_to_json_string(const CommVec &comm_vec) const
450 {
451  bpt::ptree pt, pt_child;
452  pt.put("defaultcurrency", gnc_commodity_get_mnemonic(m_dflt_curr));
453 
454  std::for_each (comm_vec.cbegin(), comm_vec.cend(),
455  [this, &pt] (auto comm)
456  {
457  auto comm_mnemonic = gnc_commodity_get_mnemonic (comm);
458  auto comm_ns = std::string("currency");
459  if (gnc_commodity_is_currency (comm))
460  {
461  if (gnc_commodity_equiv(comm, m_dflt_curr) ||
462  (!comm_mnemonic || (strcmp(comm_mnemonic, "XXX") == 0)))
463  return;
464  }
465  else
467 
468  pt.put (make_quote_path(comm_ns, comm_mnemonic), "");
469  }
470  );
471 
472  std::ostringstream result;
473  bpt::write_json(result, pt);
474  return result.str();
475 }
476 
477 static inline std::string
478 get_quotes(const std::string& json_str, const std::unique_ptr<GncQuoteSource>& qs)
479 {
480  auto [rv, quotes, errors] = qs->get_quotes(json_str);
481  std::string answer;
482 
483  if (rv == 0)
484  {
485  for (const auto& line : quotes)
486  answer.append(line + "\n");
487  }
488  else
489  {
490  std::string err_str;
491  for (const auto& line: errors)
492  {
493  if (line == "invalid_json\n")
494  PERR("Finanace Quote Wrapper was unable to parse %s",
495  json_str.c_str());
496  err_str += parse_quotesource_error(line);
497  }
498  throw(GncQuoteException(err_str));
499  }
500 
501  return answer;
502 }
503 
504 std::string
505 GncQuotesImpl::query_fq (const char* source, const StrVec& commodities)
506 {
507  bpt::ptree pt;
508  auto is_currency{strcmp(source, "currency") == 0};
509 
510  if (is_currency && commodities.size() < 2)
511  throw(GncQuoteException(_("Currency quotes requires at least two currencies")));
512 
513  if (is_currency)
514  pt.put("defaultcurrency", commodities[0].c_str());
515  else
516  pt.put("defaultcurrency", gnc_commodity_get_mnemonic(m_dflt_curr));
517 
518  std::for_each(is_currency ? ++commodities.cbegin() : commodities.cbegin(),
519  commodities.cend(),
520  [source, &pt](auto sym)
521  {
522  pt.put(make_quote_path(source, sym), "");
523  });
524  std::ostringstream result;
525  bpt::write_json(result, pt);
526  auto result_str{result.str()};
527  PINFO("Query JSON: %s\n", result_str.c_str());
528  return get_quotes(result.str(), m_quotesource);
529 }
530 
531 std::string
532 GncQuotesImpl::query_fq (const CommVec& comm_vec)
533 {
534  auto json_str{comm_vec_to_json_string(comm_vec)};
535  PINFO("Query JSON: %s\n", json_str.c_str());
536  return get_quotes(json_str, m_quotesource);
537 }
538 
540 {
541  const char* ns;
542  const char* mnemonic;
543  bool success;
544  std::string type;
545  boost::optional<std::string> price;
546  bool inverted;
547  boost::optional<std::string> date;
548  boost::optional<std::string> time;
549  boost::optional<std::string> currency;
550  boost::optional<std::string> errormsg;
551 };
552 
553 static void
554 get_price_and_type(PriceParams& p, const bpt::ptree& comm_pt)
555 {
556  p.type = "last";
557  p.price = comm_pt.get_optional<std::string> (p.type);
558 
559  if (!p.price)
560  {
561  p.type = "nav";
562  p.price = comm_pt.get_optional<std::string> (p.type);
563  }
564 
565  if (!p.price)
566  {
567  p.type = "price";
568  p.price = comm_pt.get_optional<std::string> (p.type);
569  /* guile wrapper used "unknown" as price type when "price" was found,
570  * reproducing here to keep same result for users in the pricedb */
571  p.type = p.price ? "unknown" : "missing";
572  }
573 }
574 
575 static void
576 parse_quote_json(PriceParams& p, const bpt::ptree& comm_pt)
577 {
578  auto success = comm_pt.get_optional<bool> ("success");
579  p.success = success && *success;
580  if (!p.success)
581  p.errormsg = comm_pt.get_optional<std::string> ("errormsg");
582  get_price_and_type(p, comm_pt);
583  auto inverted = comm_pt.get_optional<bool> ("inverted");
584  p.inverted = inverted && *inverted;
585  p.date = comm_pt.get_optional<std::string> ("date");
586  p.time = comm_pt.get_optional<std::string> ("time");
587  p.currency = comm_pt.get_optional<std::string> ("currency");
588 
589 
590  PINFO("Commodity: %s", p.mnemonic);
591  PINFO(" Success: %s", (p.success ? "yes" : "no"));
592  PINFO(" Date: %s", (p.date ? p.date->c_str() : "missing"));
593  PINFO(" Time: %s", (p.time ? p.time->c_str() : "missing"));
594  PINFO(" Currency: %s", (p.currency ? p.currency->c_str() : "missing"));
595  PINFO(" Price: %s", (p.price ? p.price->c_str() : "missing"));
596  PINFO(" Inverted: %s\n", (p.inverted ? "yes" : "no"));
597 }
598 
599 static time64
600 calc_price_time(const PriceParams& p)
601 {
602  /* Note that as of F::Q v. 1.52 the only sources that provide
603  * quote times are ftfunds (aka ukfunds), morningstarch, and
604  * mstaruk_fund, but it's faked with a comment "Set a dummy time
605  * as gnucash insists on having a valid format". It's also wrong,
606  * as it lacks seconds. Best ignored.
607  */
608  if (p.date && !p.date->empty())
609  {
610  try
611  {
612  auto quote_time{GncDateTime(GncDate(*p.date, "m-d-y"))};
613  PINFO("Quote date included, using %s for %s:%s",
614  quote_time.format("%Y-%m-%d %H:%M:%S %z").c_str(), p.ns, p.mnemonic);
615  return static_cast<time64>(quote_time);
616  }
617  catch (const std::exception &err)
618  {
619  auto now{GncDateTime()};
620  PWARN("Warning: failed to parse quote date '%s' for %s:%s because %s - will use %s",
621  p.date->c_str(), p.ns, p.mnemonic, err.what(), now.format("%Y-%m-%d %H:%M:%S %z").c_str());
622  return static_cast<time64>(now);
623  }
624  }
625 
626  auto now{GncDateTime()};
627  PINFO("No date was returned for %s:%s - will use %s",
628  p.ns, p.mnemonic, now.format("%Y-%m-%d %H:%M:%S %z").c_str());
629  return static_cast<time64>(now);
630 }
631 
632 static boost::optional<GncNumeric>
633 get_price(const PriceParams& p)
634 {
635  boost::optional<GncNumeric> price;
636  try
637  {
638  price = GncNumeric { *p.price };
639  }
640  catch (...)
641  {
642  PWARN("Skipped %s:%s - failed to parse returned price '%s'",
643  p.ns, p.mnemonic, p.price->c_str());
644  }
645 
646  if (price && p.inverted)
647  *price = price->inv();
648 
649  return price;
650 }
651 
652 static gnc_commodity*
653 get_currency(const PriceParams& p, QofBook* book, QFVec& failures)
654 {
655  if (!p.currency)
656  {
657  failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::NO_CURRENCY,
658  empty_string);
659  PWARN("Skipped %s:%s - Finance::Quote returned a quote with no currency",
660  p.ns, p.mnemonic);
661  return nullptr;
662  }
663  std::string curr_str = *p.currency;
664  boost::to_upper (curr_str);
665  auto commodity_table = gnc_commodity_table_get_table (book);
666  auto currency = gnc_commodity_table_lookup (commodity_table, "ISO4217", curr_str.c_str());
667 
668  if (!currency)
669  {
670  failures.emplace_back(p.ns, p.mnemonic,
671  GncQuoteError::UNKNOWN_CURRENCY, empty_string);
672  PWARN("Skipped %s:%s - failed to parse returned currency '%s'",
673  p.ns, p.mnemonic, p.currency->c_str());
674  return nullptr;
675  }
676 
677  return currency;
678 }
679 
680 GNCPrice*
681 GncQuotesImpl::parse_one_quote(const bpt::ptree& pt, gnc_commodity* comm)
682 {
683  PriceParams p;
684  bpt::ptree comm_pt;
685 
686  p.ns = gnc_commodity_get_namespace (comm);
687  p.mnemonic = gnc_commodity_get_mnemonic (comm);
688  if (gnc_commodity_equiv(comm, m_dflt_curr) ||
689  (!p.mnemonic || (strcmp (p.mnemonic, "XXX") == 0)))
690  return nullptr;
692  auto source_pt_ai{pt.find(source)};
693  auto ok{source_pt_ai != pt.not_found()};
694  if (ok)
695  {
696  auto comm_pt_ai{source_pt_ai->second.find(p.mnemonic)};
697  ok = (comm_pt_ai != pt.not_found());
698  if (ok)
699  comm_pt = comm_pt_ai->second;
700  }
701  if (!ok)
702  {
703  m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::NO_RESULT,
704  empty_string);
705  PINFO("Skipped %s:%s - Finance::Quote didn't return any data from %s.",
706  p.ns, p.mnemonic, source);
707  return nullptr;
708  }
709 
710  parse_quote_json(p, comm_pt);
711  if (!p.success)
712  {
713  m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::QUOTE_FAILED,
714  p.errormsg ? *p.errormsg : empty_string);
715  PWARN("Skipped %s:%s - Finance::Quote returned fetch failure.\nReason %s",
716  p.ns, p.mnemonic,
717  (p.errormsg ? p.errormsg->c_str() : "unknown"));
718  return nullptr;
719  }
720 
721  if (!p.price)
722  {
723  m_failures.emplace_back(p.ns, p.mnemonic,
724  GncQuoteError::NO_PRICE, empty_string);
725  PWARN("Skipped %s:%s - Finance::Quote didn't return a valid price",
726  p.ns, p.mnemonic);
727  return nullptr;
728  }
729 
730  auto price{get_price(p)};
731  if (!price)
732  {
733  m_failures.emplace_back(p.ns, p.mnemonic,
734  GncQuoteError::PRICE_PARSE_FAILURE,
735  empty_string);
736  return nullptr;
737  }
738 
739  auto currency{get_currency(p, m_book, m_failures)};
740  if (!currency)
741  return nullptr;
742 
743  auto quotedt{calc_price_time(p)};
744  auto gnc_price = gnc_price_create (m_book);
745  gnc_price_begin_edit (gnc_price);
746  gnc_price_set_commodity (gnc_price, comm);
747  gnc_price_set_currency (gnc_price, currency);
748  gnc_price_set_time64 (gnc_price, static_cast<time64> (quotedt));
749  gnc_price_set_source (gnc_price, PRICE_SOURCE_FQ);
750  gnc_price_set_typestr (gnc_price, p.type.c_str());
751  gnc_price_set_value (gnc_price, *price);
752  gnc_price_commit_edit (gnc_price);
753  return gnc_price;
754 }
755 
756 bpt::ptree
757 GncQuotesImpl::parse_quotes (const std::string& quote_str)
758 {
759  bpt::ptree pt;
760  std::istringstream ss {quote_str};
761  std::string what;
762 
763  try
764  {
765  bpt::read_json (ss, pt);
766  }
767  catch (bpt::json_parser_error &e) {
768  what = e.what();
769  }
770  catch (const std::runtime_error& e)
771  {
772  what = e.what();
773  }
774  catch (const std::logic_error& e)
775  {
776  what = e.what();
777  }
778  catch (...) {
779  std::string error_msg{_("Failed to parse result returned by Finance::Quote.")};
780  error_msg += "\n";
781  //Translators: This labels the return value of a query to Finance::Quote written in an error.
782  error_msg += _("Result:");
783  error_msg += "\n";
784  error_msg += quote_str;
785  throw(GncQuoteException(error_msg));
786  }
787  if (!what.empty())
788  {
789  std::string error_msg{_("Failed to parse result returned by Finance::Quote.")};
790  error_msg += "\n";
791  //Translators: This is the error message reported by the Online Quotes processing code.
792  error_msg += _("Error message:");
793  error_msg += "\n";
794  error_msg += what;
795  error_msg += "\n";
796  //Translators: This labels the return value of a query to Finance::Quote written in an error.
797  error_msg += _("Result:");
798  error_msg += "\n";
799  error_msg += quote_str;
800  throw(GncQuoteException(error_msg));
801  }
802  return pt;
803 }
804 
805 void
806 GncQuotesImpl::create_quotes (const bpt::ptree& pt, const CommVec& comm_vec)
807 {
808  auto pricedb{gnc_pricedb_get_db(m_book)};
809  for (auto comm : comm_vec)
810  {
811  auto price{parse_one_quote(pt, comm)};
812  if (!price)
813  continue;
814 // See the comment at gnc_pricedb_add_price
815  gnc_pricedb_add_price(pricedb, price);
816  }
817 }
818 
819 static void
820 show_verbose_quote(const bpt::ptree& comm_pt)
821 {
822  std::for_each(comm_pt.begin(), comm_pt.end(),
823  [](auto elem) {
824  std::cout << std::setw(12) << std::right << elem.first << " => " <<
825  std::left << elem.second.data() << "\n";
826  });
827  std::cout << std::endl;
828 }
829 
830 static void
831 show_gnucash_quote(const bpt::ptree& comm_pt)
832 {
833  constexpr const char* ptr{"<=== "};
834  constexpr const char* dptr{"<=\\ "};
835  constexpr const char* uptr{"<=/ "};
836  //Translators: Means that the preceding element is required
837  const char* reqd{C_("Finance::Quote", "required")};
838  //Translators: Means that the quote will work best if the preceding element is provided
839  const char* rec{C_("Finance::Quote", "recommended")};
840  //Translators: Means that one of the indicated elements is required
841  const char* oot{C_("Finance::Quote", "one of these")};
842  //Translators: Means that a required element wasn't reported. The *s are for emphasis.
843  const char* miss{C_("Finance::Quote", "**missing**")};
844 
845  const std::string miss_str{miss};
846  auto outline{[](const char* label, std::string value, const char* pointer, const char* req) {
847  std::cout << std::setw(12) << std::right << label << std::setw(16) << std::left <<
848  value << pointer << req << "\n";
849  }};
850  std::cout << _("Finance::Quote fields GnuCash uses:") << "\n";
851 //Translators: The stock or Mutual Fund symbol, ISIN, CUSIP, etc.
852  outline(C_("Finance::Quote", "symbol: "), comm_pt.get<char>("symbol", miss), ptr, reqd);
853 //Translators: The date of the quote.
854  outline(C_("Finance::Quote", "date: "), comm_pt.get<char>("date", miss), ptr, rec);
855 //Translators: The quote currency
856  outline(C_("Finance::Quote", "currency: "), comm_pt.get<char>("currency", miss), ptr, reqd);
857  auto last{comm_pt.get<char>("last", "")};
858  auto nav{comm_pt.get<char>("nav", "")};
859  auto price{comm_pt.get<char>("nav", "")};
860  auto no_price{last.empty() && nav.empty() && price.empty()};
861 //Translators: The quote is for the most recent trade on the exchange
862  outline(C_("Finance::Quote", "last: "), no_price ? miss_str : last, dptr, "");
863 //Translators: The quote is for an open-ended mutual fund and represents the net asset value of one unit of the fund at the previous close of trading.
864  outline(C_("Finance::Quote", "nav: "), no_price ? miss_str : nav, ptr, oot);
865 //Translators: The quote is neither a last trade nor an NAV.
866  outline(C_("Finance::Quote", "price: "), no_price ? miss_str : price, uptr, "");
867  std::cout << std::endl;
868 }
869 static const bpt::ptree empty_tree{};
870 
871 static inline const bpt::ptree&
872 get_commodity_data(const bpt::ptree& pt, const std::string& comm)
873 {
874  auto commdata{pt.find(comm)};
875  if (commdata == pt.not_found())
876  {
877  std::cout << comm << " " << _("Finance::Quote returned no data and set no error.") << std::endl;
878  return empty_tree;
879  }
880  auto& comm_pt{commdata->second};
881  auto success = comm_pt.get_optional<bool> ("success");
882  if (!(success && *success))
883  {
884  auto errormsg = comm_pt.get_optional<std::string> ("errormsg");
885  if (errormsg && !errormsg->empty())
886  std::cout << _("Finance::Quote reported a failure for symbol ") <<
887  comm << ": " << *errormsg << std::endl;
888  else
889  std::cout << _("Finance::Quote failed silently to retrieve a quote for symbol ") <<
890  comm << std::endl;
891  return empty_tree;
892  }
893  return comm_pt;
894 }
895 
896 static void
897 show_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose)
898 {
899  for (const auto& comm : commodities)
900  {
901  auto comm_pt{get_commodity_data(pt, comm)};
902 
903  if (comm_pt == empty_tree)
904  continue;
905 
906  if (verbose)
907  {
908  std::cout << comm << ":\n";
909  show_verbose_quote(comm_pt);
910  }
911  else
912  {
913  show_gnucash_quote(comm_pt);
914  }
915  }
916 }
917 
918 static void
919 show_currency_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose)
920 {
921  auto to_cur{commodities.front()};
922  for (const auto& comm : commodities)
923  {
924  if (comm == to_cur)
925  continue;
926 
927  auto comm_pt{get_commodity_data(pt, comm)};
928 
929  if (comm_pt == empty_tree)
930  continue;
931 
932  if (verbose)
933  {
934  std::cout << comm << ":\n";
935  show_verbose_quote(comm_pt);
936  }
937  else
938  {
939  std::cout << "1 " << comm << " = " <<
940  comm_pt.get<char>("last", "Not Found") << " " << to_cur << "\n";
941  }
942  std::cout << std::endl;
943  }
944 }
945 
946 static std::string
947 parse_quotesource_error(const std::string& line)
948 {
949  std::string err_str;
950  if (line == "invalid_json\n")
951  {
952  err_str += _("GnuCash submitted invalid json to Finance::Quote. The details were logged.");
953  }
954  else if (line.substr(0, 15) == "missing_modules")
955  {
956  PERR("Missing Finance::Quote Dependencies: %s",
957  line.substr(17).c_str());
958  err_str += _("Perl is missing the following modules. Please see https://wiki.gnucash.org/wiki/Online_Quotes#Finance::Quote for detailed corrective action. ");
959  err_str += line.substr(17);
960  }
961  else
962  {
963  PERR("Unrecognized Finance::Quote Error %s", line.c_str());
964  err_str +=_("Unrecognized Finance::Quote Error: ");
965  err_str += line;
966  }
967  err_str += "\n";
968  return err_str;
969 }
970 
971 /********************************************************************
972  * gnc_quotes_get_quotable_commodities
973  * list commodities in a given namespace that get price quotes
974  ********************************************************************/
975 /* Helper function to be passed to g_list_for_each applied to the result
976  * of gnc_commodity_namespace_get_commodity_list.
977  */
978 static void
979 get_quotables_helper1 (gpointer value, gpointer data)
980 {
981  auto l = static_cast<CommVec *> (data);
982  auto comm = static_cast<gnc_commodity *> (value);
983  auto quote_flag = gnc_commodity_get_quote_flag (comm);
984  auto quote_source = gnc_commodity_get_quote_source (comm);
985  auto quote_source_supported = gnc_quote_source_get_supported (quote_source);
986 
987  if (!quote_flag ||
988  !quote_source || !quote_source_supported)
989  return;
990  l->push_back (comm);
991 }
992 
993 // Helper function to be passed to gnc_commodity_table_for_each
994 static gboolean
995 get_quotables_helper2 (gnc_commodity *comm, gpointer data)
996 {
997  auto l = static_cast<CommVec *> (data);
998  auto quote_flag = gnc_commodity_get_quote_flag (comm);
999  auto quote_source = gnc_commodity_get_quote_source (comm);
1000  auto quote_source_supported = gnc_quote_source_get_supported (quote_source);
1001 
1002  if (!quote_flag ||
1003  !quote_source || !quote_source_supported)
1004  return TRUE;
1005  l->push_back (comm);
1006  return TRUE;
1007 }
1008 
1009 CommVec
1010 gnc_quotes_get_quotable_commodities (const gnc_commodity_table * table)
1011 {
1012  gnc_commodity_namespace * ns = NULL;
1013  const char *name_space;
1014  GList * nslist, * tmp;
1015  CommVec l;
1016  regex_t pattern;
1017  const char *expression = gnc_prefs_get_namespace_regexp ();
1018 
1019  // ENTER("table=%p, expression=%s", table, expression);
1020  if (!table)
1021  return CommVec ();
1022 
1023  if (expression && *expression)
1024  {
1025  if (regcomp (&pattern, expression, REG_EXTENDED | REG_ICASE) != 0)
1026  {
1027  // LEAVE ("Cannot compile regex");
1028  return CommVec ();
1029  }
1030 
1032  for (tmp = nslist; tmp; tmp = tmp->next)
1033  {
1034  name_space = static_cast<const char *> (tmp->data);
1035  if (regexec (&pattern, name_space, 0, NULL, 0) == 0)
1036  {
1037  // DEBUG ("Running list of %s commodities", name_space);
1038  ns = gnc_commodity_table_find_namespace (table, name_space);
1039  if (ns)
1040  {
1041  auto cm_list = gnc_commodity_namespace_get_commodity_list (ns);
1042  g_list_foreach (cm_list, &get_quotables_helper1, (gpointer) &l);
1043  g_list_free (cm_list);
1044  }
1045  }
1046  }
1047  g_list_free (nslist);
1048  regfree (&pattern);
1049  }
1050  else
1051  {
1052  gnc_commodity_table_foreach_commodity (table, get_quotables_helper2,
1053  (gpointer) &l);
1054  }
1055  //LEAVE ("list head %p", &l);
1056  return l;
1057 }
1058 
1059 /* Public interface functions */
1060 // Constructor - checks for presence of Finance::Quote and import version and quote sources
1062 {
1063  try
1064  {
1065  m_impl = std::make_unique<GncQuotesImpl>();
1066  } catch (const GncQuoteSourceError &err) {
1067  throw(GncQuoteException(err.what()));
1068  }
1069 }
1070 
1071 
1072 void
1073 GncQuotes::fetch (QofBook *book)
1074 {
1075  m_impl->fetch (book);
1076 }
1077 
1078 void GncQuotes::fetch (CommVec& commodities)
1079 {
1080  m_impl->fetch (commodities);
1081 }
1082 
1083 void GncQuotes::fetch (gnc_commodity *comm)
1084 {
1085  m_impl->fetch (comm);
1086 }
1087 
1088 void GncQuotes::report (const char* source, const StrVec& commodities,
1089  bool verbose)
1090 {
1091  m_impl->report(source, commodities, verbose);
1092 }
1093 
1094 const std::string& GncQuotes::version() noexcept
1095 {
1096  return m_impl->version ();
1097 }
1098 
1099 const QuoteSources& GncQuotes::sources() noexcept
1100 {
1101  return m_impl->sources ();
1102 }
1103 
1104 GncQuotes::~GncQuotes() = default;
1105 
1106 bool
1108 {
1109  return m_impl->had_failures();
1110 }
1111 
1112 const QFVec&
1114 {
1115  return m_impl->failures();
1116 }
1117 
1118 const std::string
1119 GncQuotes::report_failures() noexcept
1120 {
1121  return m_impl->report_failures();
1122 }
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
gboolean gnc_commodity_table_foreach_commodity(const gnc_commodity_table *table, gboolean(*f)(gnc_commodity *cm, gpointer user_data), gpointer user_data)
Call a function once for each commodity in the commodity table.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
gboolean gnc_commodity_is_currency(const gnc_commodity *cm)
Checks to see if the specified commodity is an ISO 4217 recognized currency or a legacy currency...
GnuCash DateTime class.
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
Retrieve the mnemonic for the specified commodity.
utility functions for the GnuCash UI
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
gboolean gnc_commodity_get_quote_flag(const gnc_commodity *cm)
Retrieve the automatic price quote flag for the specified commodity.
Commodity handling public routines (C++ api)
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
gboolean gnc_quote_source_get_supported(const gnc_quote_source *source)
Given a gnc_quote_source data structure, return the flag that indicates whether this particular quote...
bool had_failures() noexcept
Report if there were quotes requested but not retrieved.
The primary numeric class for representing amounts and values.
Definition: gnc-numeric.hpp:60
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
GList * gnc_commodity_namespace_get_commodity_list(const gnc_commodity_namespace *name_space)
Return a list of all commodity data structures in the specified namespace.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
#define PWARN(format, args...)
Log a warning.
Definition: qoflog.h:250
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
Definition: qofsession.cpp:574
GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table *table)
Return a list of all namespaces in the commodity table.
const QFVec & failures() noexcept
Report the commodities for which quotes were requested but not successfully retrieved.
GncQuotes()
Create a GncQuotes object.
Encapsulate all the information about a dataset.
Generic api to store and retrieve preferences.
gnc_quote_source * gnc_commodity_get_quote_source(const gnc_commodity *cm)
Retrieve the automatic price quote source for the specified commodity.
gnc_commodity_namespace * gnc_commodity_table_find_namespace(const gnc_commodity_table *table, const char *name_space)
This function finds a commodity namespace in the set of existing commodity namespaces.
void report(const char *source, const StrVec &commodities, bool verbose=false)
Report quote results from Finance::Quote to std::cout.
void fetch(QofBook *book)
Fetch quotes for all commodities in our db that have a quote source set.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
Definition: gnc-date.h:87
const char * gnc_quote_source_get_internal_name(const gnc_quote_source *source)
Given a gnc_quote_source data structure, return the internal name of this quote source.
const std::string & version() noexcept
Get the installed Finance::Quote version.
const QuoteSources & sources() noexcept
Get the available Finance::Quote sources as a std::vector.
Commodity handling public routines.
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
GnuCash Date class.