From b754e6050a216864bb297693cd0eab22062fb3ee Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 3 Mar 2017 00:02:11 +0000 Subject: [PATCH 1/2] started construction of template_file map --- src/page.h | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/page.h b/src/page.h index 28bb3d0..bf5c15d 100644 --- a/src/page.h +++ b/src/page.h @@ -269,6 +269,13 @@ class page { uint64_t no_of_mempool_tx_of_frontpage; + // instead of constatnly reading template files + // from hard drive for each request, we can read + // them only once, when the explorer starts into this map + // this will improve performance of the explorer and reduce + // read operation in OS + map template_file; + public: @@ -321,6 +328,16 @@ public: << endl; } + + // read template files for all the pages + // into template_file map + + template_file["header"] = xmreg::read(TMPL_HEADER); + template_file["footer"] = get_footer(); + template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2)); + template_file["mempool"] = xmreg::read(TMPL_MEMPOOL); + template_file["mempool_full"] = get_full_page(template_file["mempool"]); + } /** @@ -490,16 +507,10 @@ public: // append mempool_html to the index context map context["mempool_info"] = mempool_html; - // read index.html - string index2_html = xmreg::read(TMPL_INDEX2); - - // add header and footer - string full_page = get_full_page(index2_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["index2"], context); } @@ -633,8 +644,6 @@ public: return t1 > t2; }); - // read mempool.html - string mempool_html = xmreg::read(TMPL_MEMPOOL); if (add_header_and_footer) { @@ -643,11 +652,8 @@ public: context["partial_mempool_shown"] = false; - // add header and footer - string full_page = get_full_page(mempool_html); - // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["mempool_full"], context); } // this is for partial disply on front page. @@ -665,7 +671,7 @@ public: context["partial_mempool_shown"] = true; // render the page - return mstch::render(mempool_html, context); + return mstch::render(template_file["mempool"], context); } @@ -4688,11 +4694,11 @@ private: string - get_full_page(string& middle) + get_full_page(const string& middle) { - return xmreg::read(TMPL_HEADER) + return template_file["header"] + middle - + get_footer(); + + template_file["footer"]; } string From 4b10de5ca44e9fa2d0d68b5f40160e33a72074f4 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 3 Mar 2017 01:03:45 +0000 Subject: [PATCH 2/2] template_file map repleased reading templates from hdd for each request --- src/page.h | 119 ++++++++++++++--------------------------------------- 1 file changed, 32 insertions(+), 87 deletions(-) diff --git a/src/page.h b/src/page.h index bf5c15d..01cc998 100644 --- a/src/page.h +++ b/src/page.h @@ -253,8 +253,6 @@ class page { string lmdb2_path; - string css_styles; - bool testnet; bool enable_pusher; @@ -297,7 +295,7 @@ public: enable_output_key_checker {_enable_output_key_checker}, enable_autorefresh_option {_enable_autorefresh_option} { - css_styles = xmreg::read(TMPL_CSS_STYLES); + no_of_mempool_tx_of_frontpage = 25; // just moneky patching now, to check @@ -332,11 +330,24 @@ public: // read template files for all the pages // into template_file map - template_file["header"] = xmreg::read(TMPL_HEADER); - template_file["footer"] = get_footer(); - template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2)); - template_file["mempool"] = xmreg::read(TMPL_MEMPOOL); - template_file["mempool_full"] = get_full_page(template_file["mempool"]); + template_file["css_styles"] = xmreg::read(TMPL_CSS_STYLES); + template_file["header"] = xmreg::read(TMPL_HEADER); + template_file["footer"] = get_footer(); + template_file["index2"] = get_full_page(xmreg::read(TMPL_INDEX2)); + template_file["mempool"] = xmreg::read(TMPL_MEMPOOL); + template_file["mempool_full"] = get_full_page(template_file["mempool"]); + template_file["block"] = get_full_page(xmreg::read(TMPL_BLOCK)); + template_file["tx"] = get_full_page(xmreg::read(TMPL_TX)); + template_file["my_outputs"] = get_full_page(xmreg::read(TMPL_MY_OUTPUTS)); + template_file["rawtx"] = get_full_page(xmreg::read(TMPL_MY_RAWTX)); + template_file["checkrawtx"] = get_full_page(xmreg::read(TMPL_MY_CHECKRAWTX)); + template_file["pushrawtx"] = get_full_page(xmreg::read(TMPL_MY_PUSHRAWTX)); + template_file["rawkeyimgs"] = get_full_page(xmreg::read(TMPL_MY_RAWKEYIMGS)); + template_file["rawoutputkeys"] = get_full_page(xmreg::read(TMPL_MY_RAWOUTPUTKEYS)); + template_file["checkrawkeyimgs"] = get_full_page(xmreg::read(TMPL_MY_CHECKRAWKEYIMGS)); + template_file["checkoutputkeys"] = get_full_page(xmreg::read(TMPL_MY_CHECKRAWOUTPUTKEYS)); + template_file["address"] = get_full_page(xmreg::read(TMPL_ADDRESS)); + template_file["search_results"] = get_full_page(xmreg::read(TMPL_SEARCH_RESULTS)); } @@ -841,16 +852,10 @@ public: context["blk_reward"] = xmreg::xmr_amount_to_str(txd_coinbase.xmr_outputs - sum_fees, "{:0.6f}"); - // read block.html - string block_html = xmreg::read(TMPL_BLOCK); - add_css_style(context); - // add header and footer - string full_page = get_full_page(block_html); - // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["block"], context); } @@ -1642,20 +1647,10 @@ public: context["possible_spending"] = xmreg::xmr_amount_to_str( possible_spending, "{:0.12f}", false); - - //cout << "no_of_matched_mixins: " << no_of_matched_mixins << endl; - - - // read my_outputs.html - string my_outputs_html = xmreg::read(TMPL_MY_OUTPUTS); - - // add header and footer - string full_page = get_full_page(my_outputs_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["my_outputs"], context); } string @@ -1679,16 +1674,10 @@ public: {"have_custom_lmdb" , have_custom_lmdb} }; - // read rawtx.html - string rawtx_html = xmreg::read(TMPL_MY_RAWTX); - - // add header and footer - string full_page = get_full_page(rawtx_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["rawtx"], context); } string @@ -2312,17 +2301,10 @@ public: {"tx_details", xmreg::read(string(TMPL_PARIALS_DIR) + "/tx_details.html")}, }; - - // read checkrawtx.html - string checkrawtx_html = xmreg::read(TMPL_MY_CHECKRAWTX); - - // add header and footer - string full_page = get_full_page(checkrawtx_html); - add_css_style(context); // render the page - return mstch::render(full_page, context, partials); + return mstch::render(template_file["checkrawtx"], context, partials); } string @@ -2347,11 +2329,8 @@ public: }; context.emplace("txs", mstch::array{}); - // read pushrawtx.html - string pushrawtx_html = xmreg::read(TMPL_MY_PUSHRAWTX); - // add header and footer - string full_page = get_full_page(pushrawtx_html); + string full_page = template_file["pushrawtx"]; add_css_style(context); @@ -2525,16 +2504,10 @@ public: {"have_custom_lmdb" , have_custom_lmdb} }; - // read rawkeyimgs.html - string rawkeyimgs_html = xmreg::read(TMPL_MY_RAWKEYIMGS); - - // add header and footer - string full_page = get_full_page(rawkeyimgs_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["rawkeyimgs"], context); } string @@ -2546,16 +2519,10 @@ public: {"have_custom_lmdb" , have_custom_lmdb} }; - // read rawoutputkeys.html - string rawoutputkeys_html = xmreg::read(TMPL_MY_RAWOUTPUTKEYS); - - // add header and footer - string full_page = get_full_page(rawoutputkeys_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["rawoutputkeys"], context); } string @@ -2578,11 +2545,8 @@ public: {"error_msg" , string{}}, }; - // read page template - string checkrawkeyimgs_html = xmreg::read(TMPL_MY_CHECKRAWKEYIMGS); - // add header and footer - string full_page = get_full_page(checkrawkeyimgs_html); + string full_page = template_file["checkrawkeyimgs"]; add_css_style(context); @@ -2941,12 +2905,8 @@ public: {"error_msg" , string{}} }; - - // read page template - string checkoutputkeys_html = xmreg::read(TMPL_MY_CHECKRAWOUTPUTKEYS); - // add header and footer - string full_page = get_full_page(checkoutputkeys_html); + string full_page = template_file["checkoutputkeys"]; add_css_style(context); @@ -3705,16 +3665,10 @@ public: {"have_custom_lmdb" , have_custom_lmdb} }; - // read address.html - string address_html = xmreg::read(TMPL_ADDRESS); - - // add header and footer - string full_page = get_full_page(address_html); - add_css_style(context); // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["address"], context); } // ; @@ -3739,16 +3693,10 @@ public: {"have_custom_lmdb" , have_custom_lmdb} }; - // read address.html - string address_html = xmreg::read(TMPL_ADDRESS); - add_css_style(context); - // add header and footer - string full_page = get_full_page(address_html); - // render the page - return mstch::render(full_page, context); + return mstch::render(template_file["address"], context); } map> @@ -3950,11 +3898,8 @@ public: } } - // read search_results.html - string search_results_html = xmreg::read(TMPL_SEARCH_RESULTS); - // add header and footer - string full_page = get_full_page(search_results_html); + string full_page = template_file["search_results"]; // read partial for showing details of tx(s) found map partials { @@ -4721,7 +4666,7 @@ private: add_css_style(mstch::map& context) { context["css_styles"] = mstch::lambda{[&](const std::string& text) -> mstch::node { - return this->css_styles; + return template_file["css_styles"]; }}; }