Scorum
base_api_impl.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 namespace scorum {
6 namespace common_statistics {
7 
8 template <typename Bucket, typename Statistic> class common_statistics_api_impl
9 {
10  typedef typename chainbase::get_index_type<Bucket>::type bucket_index;
11 
12 public:
14 
16  const std::string _plugin_name;
17 
18 public:
20  : _app(app)
21  , _plugin_name(name)
22  {
23  }
24 
25  Statistic get_stats_for_time(const fc::time_point_sec& open, uint32_t interval) const
26  {
27  Statistic result;
28  const auto& bucket_idx = _app.chain_database()->template get_index<bucket_index, by_bucket>();
29  auto itr = bucket_idx.lower_bound(boost::make_tuple(interval, open));
30 
31  if (itr != bucket_idx.end())
32  result += *itr;
33 
34  return result;
35  }
36 
37  template <typename Plugin>
38  Statistic get_stats_for_interval(const fc::time_point_sec& start, const fc::time_point_sec& end) const
39  {
40  Statistic result;
41  const auto& bucket_itr = _app.chain_database()->template get_index<bucket_index, by_bucket>();
42  const auto& sizes = _app.get_plugin<Plugin>(_plugin_name)->get_tracked_buckets();
43  auto size_itr = sizes.rbegin();
44  auto time = start;
45 
46  // This is a greedy algorithm, same as the ubiquitous "making change" problem.
47  // So long as the bucket sizes share a common denominator, the greedy solution
48  // has the same efficiency as the dynamic solution.
49  while (size_itr != sizes.rend() && time < end)
50  {
51  auto itr = bucket_itr.find(boost::make_tuple(*size_itr, time));
52 
53  while (itr != bucket_itr.end() && itr->seconds == *size_itr && time + itr->seconds <= end)
54  {
55  time += *size_itr;
56  result += *itr;
57  itr++;
58  }
59 
60  size_itr++;
61  }
62 
63  return result;
64  }
65 
66  Statistic get_lifetime_stats() const
67  {
68  Statistic result;
69 
70  const auto& bucket_idx = _app.chain_database()->template get_index<bucket_index, by_bucket>();
71  auto itr = bucket_idx.find(boost::make_tuple(LIFE_TIME_PERIOD, fc::time_point_sec()));
72 
73  if (itr != bucket_idx.end())
74  {
75  result += *itr;
76  }
77 
78  return result;
79  }
80 };
81 
82 } // namespace common_statistics
83 } // namespace scorum
#define LIFE_TIME_PERIOD
std::shared_ptr< chain::database > chain_database() const
std::shared_ptr< abstract_plugin > get_plugin(const std::string &name) const
common_statistics_api_impl(scorum::app::application &app, const std::string &name)
Statistic get_stats_for_time(const fc::time_point_sec &open, uint32_t interval) const
Statistic get_stats_for_interval(const fc::time_point_sec &start, const fc::time_point_sec &end) const
Definition: asset.cpp:15