From 630d15b7c9a43d71cf031cc35bc2e72cc5758750 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Fri, 3 Mar 2017 23:17:34 +0000 Subject: [PATCH] New webdesign --- executor.cpp | 143 ++++++++++++++++++++++++++++++++++++++-- executor.h | 8 ++- httpd.cpp | 26 ++++++-- webdesign.cpp | 166 +++++++++++++++++++++++++++++++++++++++++++++++ webdesign.h | 19 ++++++ xmr-stak-amd.cbp | 2 + 6 files changed, 349 insertions(+), 15 deletions(-) create mode 100644 webdesign.cpp create mode 100644 webdesign.h diff --git a/executor.cpp b/executor.cpp index fc7ead5..16383ea 100644 --- a/executor.cpp +++ b/executor.cpp @@ -33,6 +33,7 @@ #include "jconf.h" #include "console.h" #include "donate-level.h" +#include "webdesign.h" #ifdef __GNUC__ #include @@ -483,13 +484,11 @@ void executor::ex_main() inline const char* hps_format(double h, char* buf, size_t l) { - if(std::isnormal(h)) + if(std::isnormal(h) || h == 0.0) { snprintf(buf, l, " %03.1f", h); return buf; } - else if(h == 0.0) //Zero is not normal but we want it - return " 0.0"; else return " (na)"; } @@ -699,6 +698,138 @@ void executor::print_report(ex_event_name ev) printer::inst()->print_str(out.c_str()); } +void executor::http_hashrate_report(std::string& out) +{ + char num_a[32], num_b[32], num_c[32], num_d[32]; + char buffer[4096]; + size_t nthd = pvThreads->size(); + + out.reserve(4096); + + snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Hashrate Report", "Hashrate Report"); + out.append(buffer); + + snprintf(buffer, sizeof(buffer), sHtmlHashrateBodyHigh, (unsigned int)nthd + 3); + out.append(buffer); + + double fTotal[3] = { 0.0, 0.0, 0.0}; + for(size_t i=0; i < nthd; i++) + { + double fHps[3]; + + fHps[0] = telem->calc_telemetry_data(2500, i); + fHps[1] = telem->calc_telemetry_data(60000, i); + fHps[2] = telem->calc_telemetry_data(900000, i); + + num_a[0] = num_b[0] = num_c[0] ='\0'; + hps_format(fHps[0], num_a, sizeof(num_a)); + hps_format(fHps[1], num_b, sizeof(num_b)); + hps_format(fHps[2], num_c, sizeof(num_c)); + + fTotal[0] += fHps[0]; + fTotal[1] += fHps[1]; + fTotal[2] += fHps[2]; + + snprintf(buffer, sizeof(buffer), sHtmlHashrateTableRow, (unsigned int)i, num_a, num_b, num_c); + out.append(buffer); + } + + num_a[0] = num_b[0] = num_c[0] = num_d[0] ='\0'; + hps_format(fTotal[0], num_a, sizeof(num_a)); + hps_format(fTotal[1], num_b, sizeof(num_b)); + hps_format(fTotal[2], num_c, sizeof(num_c)); + hps_format(fHighestHps, num_d, sizeof(num_d)); + + snprintf(buffer, sizeof(buffer), sHtmlHashrateBodyLow, num_a, num_b, num_c, num_d); + out.append(buffer); +} + +void executor::http_result_report(std::string& out) +{ + char date[128]; + char buffer[4096]; + + out.reserve(4096); + + snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Result Report", "Result Report"); + out.append(buffer); + + size_t iGoodRes = vMineResults[0].count, iTotalRes = iGoodRes; + size_t ln = vMineResults.size(); + + for(size_t i=1; i < ln; i++) + iTotalRes += vMineResults[i].count; + + double fGoodResPrc = 0.0; + if(iTotalRes > 0) + fGoodResPrc = 100.0 * iGoodRes / iTotalRes; + + double fAvgResTime = 0.0; + if(iPoolCallTimes.size() > 0) + { + using namespace std::chrono; + fAvgResTime = ((double)duration_cast(system_clock::now() - tPoolConnTime).count()) + / iPoolCallTimes.size(); + } + + snprintf(buffer, sizeof(buffer), sHtmlResultBodyHigh, + iPoolDiff, iGoodRes, iTotalRes, fGoodResPrc, fAvgResTime, iPoolHashes, + int_port(iTopDiff[0]), int_port(iTopDiff[1]), int_port(iTopDiff[2]), int_port(iTopDiff[3]), + int_port(iTopDiff[4]), int_port(iTopDiff[5]), int_port(iTopDiff[6]), int_port(iTopDiff[7]), + int_port(iTopDiff[8]), int_port(iTopDiff[9])); + + out.append(buffer); + + for(size_t i=1; i < vMineResults.size(); i++) + { + snprintf(buffer, sizeof(buffer), sHtmlResultTableRow, vMineResults[i].msg.c_str(), + int_port(vMineResults[i].count), time_format(date, sizeof(date), vMineResults[i].time)); + out.append(buffer); + } + + out.append(sHtmlResultBodyLow); +} + +void executor::http_connection_report(std::string& out) +{ + char date[128]; + char buffer[4096]; + + out.reserve(4096); + + snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Connection Report", "Connection Report"); + out.append(buffer); + + jpsock* pool = pick_pool_by_id(dev_pool_id + 1); + const char* cdate = "not connected"; + if (pool->is_running() && pool->is_logged_in()) + cdate = time_format(date, sizeof(date), tPoolConnTime); + + size_t n_calls = iPoolCallTimes.size(); + unsigned int ping_time = 0; + if (n_calls > 1) + { + //Not-really-but-good-enough median + std::nth_element(iPoolCallTimes.begin(), iPoolCallTimes.begin() + n_calls/2, iPoolCallTimes.end()); + ping_time = iPoolCallTimes[n_calls/2]; + } + + snprintf(buffer, sizeof(buffer), sHtmlConnectionBodyHigh, + jconf::inst()->GetPoolAddress(), + cdate, ping_time); + out.append(buffer); + + + for(size_t i=0; i < vSocketLog.size(); i++) + { + snprintf(buffer, sizeof(buffer), sHtmlConnectionTableRow, + time_format(date, sizeof(date), vSocketLog[i].time), vSocketLog[i].msg.c_str()); + out.append(buffer); + } + + out.append(sHtmlConnectionBodyLow); +} + void executor::http_report(ex_event_name ev) { assert(pHttpString != nullptr); @@ -706,15 +837,15 @@ void executor::http_report(ex_event_name ev) switch(ev) { case EV_HTML_HASHRATE: - hashrate_report(*pHttpString); + http_hashrate_report(*pHttpString); break; case EV_HTML_RESULTS: - result_report(*pHttpString); + http_result_report(*pHttpString); break; case EV_HTML_CONNSTAT: - connection_report(*pHttpString); + http_connection_report(*pHttpString); break; default: assert(false); diff --git a/executor.h b/executor.h index dc5274b..a6c3444 100644 --- a/executor.h +++ b/executor.h @@ -78,6 +78,10 @@ private: void result_report(std::string& out); void connection_report(std::string& out); + void http_hashrate_report(std::string& out); + void http_result_report(std::string& out); + void http_connection_report(std::string& out); + void http_report(ex_event_name ev); void print_report(ex_event_name ev); @@ -140,8 +144,8 @@ private: std::array iTopDiff { { } }; //Initialize to zero std::chrono::system_clock::time_point tPoolConnTime; - size_t iPoolHashes; - uint64_t iPoolDiff; + size_t iPoolHashes = 0; + uint64_t iPoolDiff = 0; // Set it to 16 bit so that we can just let it grow // Maximum realistic growth rate - 5MB / month diff --git a/httpd.cpp b/httpd.cpp index a21e4cd..84612dd 100644 --- a/httpd.cpp +++ b/httpd.cpp @@ -32,6 +32,8 @@ #include "executor.h" #include "jconf.h" +#include "webdesign.h" + #ifdef _WIN32 #include "libmicrohttpd/microhttpd.h" #define strcasecmp _stricmp @@ -63,27 +65,37 @@ int httpd::req_handler(void * cls, *ptr = nullptr; std::string str; - if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0) + if(strcasecmp(url, "/style.css") == 0) + { + const char* req_etag = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "If-None-Match"); + + if(req_etag != NULL && strcmp(req_etag, sHtmlCssEtag) == 0) + { //Cache hit + rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT); + + int ret = MHD_queue_response(connection, MHD_HTTP_NOT_MODIFIED, rsp); + MHD_destroy_response(rsp); + return ret; + } + + rsp = MHD_create_response_from_buffer(sHtmlCssSize, (void*)sHtmlCssFile, MHD_RESPMEM_PERSISTENT); + MHD_add_response_header(rsp, "ETag", sHtmlCssEtag); + } + else if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0) { - str.append("Hashrate Report
");
 		executor::inst()->get_http_report(EV_HTML_HASHRATE, str);
-		str.append("
"); rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY); } else if(strcasecmp(url, "/c") == 0 || strcasecmp(url, "/connection") == 0) { - str.append("Connection Report
");
 		executor::inst()->get_http_report(EV_HTML_CONNSTAT, str);
-		str.append("
"); rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY); } else if(strcasecmp(url, "/r") == 0 || strcasecmp(url, "/results") == 0) { - str.append("Results Report
");
 		executor::inst()->get_http_report(EV_HTML_RESULTS, str);
-		str.append("
"); rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY); } diff --git a/webdesign.cpp b/webdesign.cpp new file mode 100644 index 0000000..6d96824 --- /dev/null +++ b/webdesign.cpp @@ -0,0 +1,166 @@ +#include + +extern const char sHtmlCssEtag [] = "00000005"; +extern const char sHtmlCssFile [] = + "body {" + "font-family: Tahoma, Arial, sans-serif;" + "font-size: 80%;" + "background-color: rgb(230, 230, 230);" + "}" + + "a {" + "color: rgb(43, 51, 62);" + "}" + + "a:link {" + "text-decoration: none;" + "}" + + "a:visited {" + "color: rgb(43, 51, 62);" + "}" + + "a:hover {" + "color: rgb(255, 153, 0);" + "}" + + "a:active {" + "color: rgb(204, 122, 0);" + "}" + + ".header {" + "background-color: rgb(30, 30, 30);" + "color: white;" + "padding: 10px;" + "font-weight: bold;" + "margin: 10px 0px;" + "}" + + ".links {" + "padding: 5px;" + "text-align: center;" + "background-color: rgb(213, 213, 213);" + "box-shadow: 0px 1px 3px 0px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14), 0px 2px 1px -1px rgba(0, 0, 0, 0.12);" + "}" + + ".data th, td {" + "padding: 5px 12px;" + "text-align: right;" + "border-bottom: 1px solid #ccc;" + "}" + + ".data tr:nth-child(even) {" + "background-color: #ddd;" + "}" + + ".data th {" + "background-color: #ccc;" + "}" + + ".data table {" + "width: 100%;" + "max-width: 600px;" + "}" + + ".letter {" + "font-weight: bold;" + "}" + + "h4 {" + "background-color: rgb(0, 129, 124);" + "color: white;" + "padding: 10px;" + "margin: 10px 0px;" + "}" + + ".flex-container {" + "display: -webkit-flex;" + "display: flex;" + "}" + + ".flex-item {" + "width: 33%;" + "margin: 6px;" + "}"; + +size_t sHtmlCssSize = sizeof(sHtmlCssFile) - 1; + +extern const char sHtmlCommonHeader [] = + "" + "" + "" + "%s" + "" + "
XMR-Stak-CPU
" + + "
" + "" + "" + "" + "
" + "

%s

"; + +extern const char sHtmlHashrateBodyHigh [] = + "
" + "" + ""; + +extern const char sHtmlHashrateTableRow [] = + ""; + +extern const char sHtmlHashrateBodyLow [] = + "" + "" + "
Thread ID2.5s60s15mH/s
%u%s%s%s
Totals:%s%s%s
Highest:%s
" + "
"; + +extern const char sHtmlConnectionBodyHigh [] = + "
" + "" + "" + "" + "" + "
Pool address%s
Connected since%s
Pool ping time%u ms
" + "

Network error log

" + "" + ""; + +extern const char sHtmlConnectionTableRow [] = + ""; + +extern const char sHtmlConnectionBodyLow [] = + "
DateError
%s%s
"; + +extern const char sHtmlResultBodyHigh [] = + "
" + "" + "" + "" + "" + "" + "
Difficulty%u
Good results%u / %u (%.1f %%)
Avg result time%.1f sec
Pool-side hashes%u
" + "

Top 10 best results found

" + "" + "" + "" + "" + "" + "" + "
1%llu2%llu
3%llu4%llu
5%llu6%llu
7%llu8%llu
9%llu10%llu
" + "

Error details

" + "" + "" + ""; + +extern const char sHtmlResultTableRow [] = + ""; + +extern const char sHtmlResultBodyLow [] = + "
Error text
CountLast seen
%s
%llu%s
"; + diff --git a/webdesign.h b/webdesign.h new file mode 100644 index 0000000..a516afb --- /dev/null +++ b/webdesign.h @@ -0,0 +1,19 @@ +#pragma once + +extern const char sHtmlCssEtag[]; +extern const char sHtmlCssFile[]; +extern size_t sHtmlCssSize; + +extern const char sHtmlCommonHeader[]; + +extern const char sHtmlHashrateBodyHigh[]; +extern const char sHtmlHashrateTableRow[]; +extern const char sHtmlHashrateBodyLow[]; + +extern const char sHtmlConnectionBodyHigh[]; +extern const char sHtmlConnectionTableRow[]; +extern const char sHtmlConnectionBodyLow[]; + +extern const char sHtmlResultBodyHigh[]; +extern const char sHtmlResultTableRow[]; +extern const char sHtmlResultBodyLow[]; diff --git a/xmr-stak-amd.cbp b/xmr-stak-amd.cbp index 7e971a3..1c8d262 100644 --- a/xmr-stak-amd.cbp +++ b/xmr-stak-amd.cbp @@ -160,6 +160,8 @@ + +