block size and median size of blocks added

This commit is contained in:
moneroexamples 2017-01-18 00:37:15 +00:00
parent 52a3de72ab
commit 93d7c63198
4 changed files with 42 additions and 11 deletions

View File

@ -522,6 +522,8 @@ public:
// previous blk timestamp, initalised to lowest possible value
double prev_blk_timestamp {std::numeric_limits<double>::lowest()};
vector<double> blk_sizes;
// iterate over last no_of_last_blocks of blocks
for (uint64_t i = start_height; i <= end_height; ++i)
{
@ -537,11 +539,17 @@ public:
// get block's hash
crypto::hash blk_hash = core_storage->get_block_id_by_height(i);
// get block size in kB
double blk_size = static_cast<double>(core_storage->get_db().get_block_size(i))/1024.0;
string blk_size_str = fmt::format("{:0.2f}", blk_size);
blk_sizes.push_back(blk_size);
// remove "<" and ">" from the hash string
string blk_hash_str = REMOVE_HASH_BRAKETS(fmt::format("{:s}", blk_hash));
// get block age
pair<string, string> age = get_age(server_timestamp, blk.timestamp);
context["age_format"] = age.second;
@ -585,6 +593,7 @@ public:
txd_map.insert({"age" , age.first});
txd_map.insert({"is_ringct" , (tx.version > 1)});
txd_map.insert({"rct_type" , tx.rct_signatures.type});
txd_map.insert({"blk_size" , blk_size_str});
// do not show block info for other than
@ -595,6 +604,7 @@ public:
txd_map["height"] = string("");
txd_map["age"] = string("");
txd_map["time_delta"] = string("");
txd_map["blk_size"] = string("");
}
txs.push_back(txd_map);
@ -607,18 +617,16 @@ public:
} // for (uint64_t i = start_height; i <= end_height; ++i)
// calculate median size of the blocks shown
double blk_size_median = xmreg::calc_median(blk_sizes.begin(), blk_sizes.end());
context["blk_size_median"] = fmt::format("{:0.2f}", blk_size_median);
// reverse txs and remove last (i.e., oldest)
// tx. This is done so that time delats
// are easier to calcualte in the above for loop
std::reverse(txs.begin(), txs.end());
// if we look at the genesis time, we should not remove
// the last block, i.e. genesis one.
if (!(start_height < 2))
{
//txs.pop_back();
}
// get memory pool rendered template
string mempool_html = mempool();

View File

@ -23,30 +23,36 @@
{{{mempool_info}}}
{{#is_page_zero}}
<h2>Transactions in the last 25 blocks</h2>
<h2 style="margin-bottom: 0px">Transactions in the last 25 blocks</h2>
{{/is_page_zero}}
{{^is_page_zero}}
<h2>Transactions in older blocks<!--(height: {{height}})--></h2>
<h2 style="margin-bottom: 0px">Transactions in older blocks<!--(height: {{height}})--></h2>
{{/is_page_zero}}
<h4 style="font-size: 14px; margin-top: 0px">(Median size of these blocks: {{blk_size_median}} kB)</h4>
<div class="center">
<table class="center">
<tr>
<td>height</td>
<td>age {{age_format}}<!--(Δm)--></td>
<td>size [kB]<!--(Δm)--></td>
<td>tx hash</td>
<td>fees</td>
<td>outputs</td>
<td>in/out</td>
<td>rct/type</td>
<td>mixin</td>
<td>size [kB]</td>
<td>tx size [kB]</td>
</tr>
{{#txs}}
<tr>
<td><a href="/block/{{height}}">{{height}}</a></td>
<td>{{age}}<!--{{time_delta}}--></td>
<td>{{blk_size}}</td>
<td><a href="/tx/{{hash}}">{{hash}}</a></td>
<td>{{tx_fee_short}}</td>
<td>{{sum_outputs_short}}</td>

View File

@ -1332,5 +1332,6 @@ get_human_readable_timestamp(uint64_t ts)
}
}

View File

@ -30,6 +30,8 @@
#include <string>
#include <vector>
#include <array>
#include <iterator>
#include <algorithm>
/**
* Some helper functions used in the example.
@ -302,6 +304,20 @@ make_printable(const string& in_s);
string
get_human_readable_timestamp(uint64_t ts);
// Get the median of an unordered set of numbers of arbitrary
// type without modifying the underlying dataset.
// taken from http://stackoverflow.com/a/19695285
template <typename It>
typename std::iterator_traits<It>::value_type
calc_median(It it_begin, It it_end)
{
using T = typename std::iterator_traits<It>::value_type;
std::vector<T> data(it_begin, it_end);
std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end());
return data[data.size() / 2];
}
}
#endif //XMREG01_TOOLS_H