pages added

This commit is contained in:
moneroexamples 2016-04-15 02:59:41 +00:00
parent 6a9235715d
commit 9b6aa38a90
3 changed files with 69 additions and 22 deletions

View File

@ -82,10 +82,17 @@ int main(int ac, const char* av[]) {
return xmrblocks.index();
});
CROW_ROUTE(app, "/page/<uint>")
([&](size_t page_no) {
return xmrblocks.index(page_no);
});
CROW_ROUTE(app, "/autorefresh")
([&]() {
uint64_t page_no {0};
bool refresh_page {true};
return xmrblocks.index(refresh_page);
return xmrblocks.index(page_no, refresh_page);
});
CROW_ROUTE(app, "/css/style.css")

View File

@ -62,11 +62,14 @@ namespace xmreg {
}
string
index(bool refresh_page = false)
index(uint64_t page_no = 0, bool refresh_page = false)
{
//get current server timestamp
server_timestamp = std::time(nullptr);
// number of last blocks to show
uint64_t no_of_last_blocks {100 + 1};
// get the current blockchain height. Just to check if it reads ok.
// uint64_t height = core_storage->get_current_blockchain_height() - 1;
@ -74,20 +77,34 @@ namespace xmreg {
// initalise page tempate map with basic info about blockchain
mstch::map context {
{"refresh", refresh_page},
{"height", fmt::format("{:d}", height)},
{"refresh" , refresh_page},
{"height" , fmt::format("{:d}", height)},
{"server_timestamp", xmreg::timestamp_to_str(server_timestamp)},
{"blocks", mstch::array()}
{"blocks" , mstch::array()},
{"page_no" , fmt::format("{:d}", page_no)},
{"total_page_no" , fmt::format("{:d}", height / (no_of_last_blocks))},
{"is_page_zero" , bool(page_no)},
{"next_page" , fmt::format("{:d}", page_no + 1)},
{"prev_page" , fmt::format("{:d}", (page_no > 0 ? page_no - 1 : 0))},
};
// number of last blocks to show
size_t no_of_last_blocks {100 + 1};
// get reference to blocks template map to be field below
mstch::array& blocks = boost::get<mstch::array>(context["blocks"]);
uint64_t start_height = height - no_of_last_blocks * (page_no + 1);
uint64_t end_height = height - no_of_last_blocks * (page_no);
start_height = start_height > 0 ? start_height : 0;
end_height = end_height < height ? end_height : height;
start_height = start_height > end_height ? 0 : start_height;
cout << start_height << ", " << end_height << endl;
// iterate over last no_of_last_blocks of blocks
for (size_t i = height - no_of_last_blocks; i <= height; ++i)
for (uint64_t i = start_height; i <= end_height; ++i)
{
// get block at the given height i
block blk;
@ -109,6 +126,21 @@ namespace xmreg {
delta_time[2], delta_time[3],
delta_time[4]);
// if have days or yeras, change age format
if (delta_time[0] > 0)
{
age_str = fmt::format("{:02d}:{:02d}:{:02d}:{:02d}:{:02d}",
delta_time[0], delta_time[1], delta_time[2],
delta_time[3], delta_time[4]);
}
else if (delta_time[1] > 0)
{
age_str = fmt::format("{:02d}:{:02d}:{:02d}:{:02d}",
delta_time[1], delta_time[2],
delta_time[3], delta_time[4]);
}
// get xmr in the block reward
array<uint64_t, 2> coinbase_tx = sum_money_in_tx(blk.miner_tx);
@ -172,7 +204,7 @@ namespace xmreg {
// block. This is done so that time delats
// are easier to calcualte in the above for loop
std::reverse(blocks.begin(), blocks.end());
blocks.pop_back();
//blocks.pop_back();
// get memory pool rendered template
string mempool_html = mempool();

View File

@ -48,4 +48,12 @@
{{/blocks}}
</table>
<div class="center" style="text-align: center;">
{{#is_page_zero}}
<a href="/page/{{prev_page}}">previous page</a> |
{{/is_page_zero}}
current page: {{page_no}}/<a href="/page/{{total_page_no}}">{{total_page_no}}</a>
| <a href="/page/{{next_page}}">next page</a>
</div>
</div>