Skip to content

Commit 84317cf

Browse files
committed
New webdesign for the HTTP report
1 parent b5902fd commit 84317cf

File tree

6 files changed

+349
-15
lines changed

6 files changed

+349
-15
lines changed

executor.cpp

+137-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "jconf.h"
3434
#include "console.h"
3535
#include "donate-level.h"
36+
#include "webdesign.h"
3637

3738
#ifdef _WIN32
3839
#define strncasecmp _strnicmp
@@ -478,13 +479,11 @@ void executor::ex_main()
478479

479480
inline const char* hps_format(double h, char* buf, size_t l)
480481
{
481-
if(std::isnormal(h))
482+
if(std::isnormal(h) || h == 0.0)
482483
{
483484
snprintf(buf, l, " %03.1f", h);
484485
return buf;
485486
}
486-
else if(h == 0.0) //Zero is not normal but we want it
487-
return " 0.0";
488487
else
489488
return " (na)";
490489
}
@@ -695,22 +694,154 @@ void executor::print_report(ex_event_name ev)
695694
printer::inst()->print_str(out.c_str());
696695
}
697696

697+
void executor::http_hashrate_report(std::string& out)
698+
{
699+
char num_a[32], num_b[32], num_c[32], num_d[32];
700+
char buffer[4096];
701+
size_t nthd = pvThreads->size();
702+
703+
out.reserve(4096);
704+
705+
snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Hashrate Report", "Hashrate Report");
706+
out.append(buffer);
707+
708+
snprintf(buffer, sizeof(buffer), sHtmlHashrateBodyHigh, (unsigned int)nthd + 3);
709+
out.append(buffer);
710+
711+
double fTotal[3] = { 0.0, 0.0, 0.0};
712+
for(size_t i=0; i < nthd; i++)
713+
{
714+
double fHps[3];
715+
716+
fHps[0] = telem->calc_telemetry_data(2500, i);
717+
fHps[1] = telem->calc_telemetry_data(60000, i);
718+
fHps[2] = telem->calc_telemetry_data(900000, i);
719+
720+
num_a[0] = num_b[0] = num_c[0] ='\0';
721+
hps_format(fHps[0], num_a, sizeof(num_a));
722+
hps_format(fHps[1], num_b, sizeof(num_b));
723+
hps_format(fHps[2], num_c, sizeof(num_c));
724+
725+
fTotal[0] += fHps[0];
726+
fTotal[1] += fHps[1];
727+
fTotal[2] += fHps[2];
728+
729+
snprintf(buffer, sizeof(buffer), sHtmlHashrateTableRow, (unsigned int)i, num_a, num_b, num_c);
730+
out.append(buffer);
731+
}
732+
733+
num_a[0] = num_b[0] = num_c[0] = num_d[0] ='\0';
734+
hps_format(fTotal[0], num_a, sizeof(num_a));
735+
hps_format(fTotal[1], num_b, sizeof(num_b));
736+
hps_format(fTotal[2], num_c, sizeof(num_c));
737+
hps_format(fHighestHps, num_d, sizeof(num_d));
738+
739+
snprintf(buffer, sizeof(buffer), sHtmlHashrateBodyLow, num_a, num_b, num_c, num_d);
740+
out.append(buffer);
741+
}
742+
743+
void executor::http_result_report(std::string& out)
744+
{
745+
char date[128];
746+
char buffer[4096];
747+
748+
out.reserve(4096);
749+
750+
snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Result Report", "Result Report");
751+
out.append(buffer);
752+
753+
size_t iGoodRes = vMineResults[0].count, iTotalRes = iGoodRes;
754+
size_t ln = vMineResults.size();
755+
756+
for(size_t i=1; i < ln; i++)
757+
iTotalRes += vMineResults[i].count;
758+
759+
double fGoodResPrc = 0.0;
760+
if(iTotalRes > 0)
761+
fGoodResPrc = 100.0 * iGoodRes / iTotalRes;
762+
763+
double fAvgResTime = 0.0;
764+
if(iPoolCallTimes.size() > 0)
765+
{
766+
using namespace std::chrono;
767+
fAvgResTime = ((double)duration_cast<seconds>(system_clock::now() - tPoolConnTime).count())
768+
/ iPoolCallTimes.size();
769+
}
770+
771+
snprintf(buffer, sizeof(buffer), sHtmlResultBodyHigh,
772+
iPoolDiff, iGoodRes, iTotalRes, fGoodResPrc, fAvgResTime, iPoolHashes,
773+
int_port(iTopDiff[0]), int_port(iTopDiff[1]), int_port(iTopDiff[2]), int_port(iTopDiff[3]),
774+
int_port(iTopDiff[4]), int_port(iTopDiff[5]), int_port(iTopDiff[6]), int_port(iTopDiff[7]),
775+
int_port(iTopDiff[8]), int_port(iTopDiff[9]));
776+
777+
out.append(buffer);
778+
779+
for(size_t i=1; i < vMineResults.size(); i++)
780+
{
781+
snprintf(buffer, sizeof(buffer), sHtmlResultTableRow, vMineResults[i].msg.c_str(),
782+
int_port(vMineResults[i].count), time_format(date, sizeof(date), vMineResults[i].time));
783+
out.append(buffer);
784+
}
785+
786+
out.append(sHtmlResultBodyLow);
787+
}
788+
789+
void executor::http_connection_report(std::string& out)
790+
{
791+
char date[128];
792+
char buffer[4096];
793+
794+
out.reserve(4096);
795+
796+
snprintf(buffer, sizeof(buffer), sHtmlCommonHeader, "Connection Report", "Connection Report");
797+
out.append(buffer);
798+
799+
jpsock* pool = pick_pool_by_id(dev_pool_id + 1);
800+
const char* cdate = "not connected";
801+
if (pool->is_running() && pool->is_logged_in())
802+
cdate = time_format(date, sizeof(date), tPoolConnTime);
803+
804+
size_t n_calls = iPoolCallTimes.size();
805+
unsigned int ping_time = 0;
806+
if (n_calls > 1)
807+
{
808+
//Not-really-but-good-enough median
809+
std::nth_element(iPoolCallTimes.begin(), iPoolCallTimes.begin() + n_calls/2, iPoolCallTimes.end());
810+
ping_time = iPoolCallTimes[n_calls/2];
811+
}
812+
813+
snprintf(buffer, sizeof(buffer), sHtmlConnectionBodyHigh,
814+
jconf::inst()->GetPoolAddress(),
815+
cdate, ping_time);
816+
out.append(buffer);
817+
818+
819+
for(size_t i=0; i < vSocketLog.size(); i++)
820+
{
821+
snprintf(buffer, sizeof(buffer), sHtmlConnectionTableRow,
822+
time_format(date, sizeof(date), vSocketLog[i].time), vSocketLog[i].msg.c_str());
823+
out.append(buffer);
824+
}
825+
826+
out.append(sHtmlConnectionBodyLow);
827+
}
828+
698829
void executor::http_report(ex_event_name ev)
699830
{
700831
assert(pHttpString != nullptr);
701832

702833
switch(ev)
703834
{
704835
case EV_HTML_HASHRATE:
705-
hashrate_report(*pHttpString);
836+
http_hashrate_report(*pHttpString);
706837
break;
707838

708839
case EV_HTML_RESULTS:
709-
result_report(*pHttpString);
840+
http_result_report(*pHttpString);
710841
break;
711842

712843
case EV_HTML_CONNSTAT:
713-
connection_report(*pHttpString);
844+
http_connection_report(*pHttpString);
714845
break;
715846
default:
716847
assert(false);

executor.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ class executor
7474
void result_report(std::string& out);
7575
void connection_report(std::string& out);
7676

77+
void http_hashrate_report(std::string& out);
78+
void http_result_report(std::string& out);
79+
void http_connection_report(std::string& out);
80+
7781
void http_report(ex_event_name ev);
7882
void print_report(ex_event_name ev);
7983

@@ -136,8 +140,8 @@ class executor
136140
std::array<size_t, 10> iTopDiff { { } }; //Initialize to zero
137141

138142
std::chrono::system_clock::time_point tPoolConnTime;
139-
size_t iPoolHashes;
140-
uint64_t iPoolDiff;
143+
size_t iPoolHashes = 0;
144+
uint64_t iPoolDiff = 0;
141145

142146
// Set it to 16 bit so that we can just let it grow
143147
// Maximum realistic growth rate - 5MB / month

httpd.cpp

+19-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "executor.h"
3333
#include "jconf.h"
3434

35+
#include "webdesign.h"
36+
3537
#ifdef _WIN32
3638
#include "libmicrohttpd/microhttpd.h"
3739
#define strcasecmp _stricmp
@@ -63,27 +65,37 @@ int httpd::req_handler(void * cls,
6365
*ptr = nullptr;
6466

6567
std::string str;
66-
if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0)
68+
if(strcasecmp(url, "/style.css") == 0)
69+
{
70+
const char* req_etag = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "If-None-Match");
71+
72+
if(req_etag != NULL && strcmp(req_etag, sHtmlCssEtag) == 0)
73+
{ //Cache hit
74+
rsp = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_PERSISTENT);
75+
76+
int ret = MHD_queue_response(connection, MHD_HTTP_NOT_MODIFIED, rsp);
77+
MHD_destroy_response(rsp);
78+
return ret;
79+
}
80+
81+
rsp = MHD_create_response_from_buffer(sHtmlCssSize, (void*)sHtmlCssFile, MHD_RESPMEM_PERSISTENT);
82+
MHD_add_response_header(rsp, "ETag", sHtmlCssEtag);
83+
}
84+
else if(strcasecmp(url, "/h") == 0 || strcasecmp(url, "/hashrate") == 0)
6785
{
68-
str.append("<html><head><title>Hashrate Report</title></head><body><pre>");
6986
executor::inst()->get_http_report(EV_HTML_HASHRATE, str);
70-
str.append("</pre></body></html>");
7187

7288
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
7389
}
7490
else if(strcasecmp(url, "/c") == 0 || strcasecmp(url, "/connection") == 0)
7591
{
76-
str.append("<html><head><title>Connection Report</title></head><body><pre>");
7792
executor::inst()->get_http_report(EV_HTML_CONNSTAT, str);
78-
str.append("</pre></body></html>");
7993

8094
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
8195
}
8296
else if(strcasecmp(url, "/r") == 0 || strcasecmp(url, "/results") == 0)
8397
{
84-
str.append("<html><head><title>Results Report</title></head><body><pre>");
8598
executor::inst()->get_http_report(EV_HTML_RESULTS, str);
86-
str.append("</pre></body></html>");
8799

88100
rsp = MHD_create_response_from_buffer(str.size(), (void*)str.c_str(), MHD_RESPMEM_MUST_COPY);
89101
}

0 commit comments

Comments
 (0)