Improve request handling, use less loops, optimize array pointer allocations
This commit is contained in:
74
src/main.h
74
src/main.h
@@ -19,6 +19,8 @@
|
|||||||
#define PAGE_ASSET 1
|
#define PAGE_ASSET 1
|
||||||
#define PAGE_ERROR 2
|
#define PAGE_ERROR 2
|
||||||
|
|
||||||
|
mxml_node_t* doc;
|
||||||
|
|
||||||
uint8_t running = 0;
|
uint8_t running = 0;
|
||||||
char* daemon_name = "Stethoscope";
|
char* daemon_name = "Stethoscope";
|
||||||
uint16_t listen_port = 7800;
|
uint16_t listen_port = 7800;
|
||||||
@@ -85,7 +87,7 @@ struct XMLServiceRow {
|
|||||||
|
|
||||||
static enum MHD_Result http_response(void *cls, struct MHD_Connection *conn, const char* uri, const char* method, const char* version, const char* upload_Data, size_t* upload_data_size, void** req_cls) {
|
static enum MHD_Result http_response(void *cls, struct MHD_Connection *conn, const char* uri, const char* method, const char* version, const char* upload_Data, size_t* upload_data_size, void** req_cls) {
|
||||||
const union MHD_ConnectionInfo *info = MHD_get_connection_info(conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
|
const union MHD_ConnectionInfo *info = MHD_get_connection_info(conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
|
||||||
const char *client_ip = {};
|
const char* client_ip = (char*)malloc(64);
|
||||||
uint16_t client_port = 0;
|
uint16_t client_port = 0;
|
||||||
if (info != NULL) {
|
if (info != NULL) {
|
||||||
const struct sockaddr *addr = info->client_addr;
|
const struct sockaddr *addr = info->client_addr;
|
||||||
@@ -100,7 +102,7 @@ static enum MHD_Result http_response(void *cls, struct MHD_Connection *conn, con
|
|||||||
client_ip = inet_ntop(AF_INET6, &addr6->sin6_addr, ip_str, sizeof(ip_str));
|
client_ip = inet_ntop(AF_INET6, &addr6->sin6_addr, ip_str, sizeof(ip_str));
|
||||||
client_port = ntohs(addr6->sin6_port);
|
client_port = ntohs(addr6->sin6_port);
|
||||||
}
|
}
|
||||||
} else client_ip = "?.?.?.?";
|
}
|
||||||
|
|
||||||
uint16_t response_code = 0;
|
uint16_t response_code = 0;
|
||||||
char* page;
|
char* page;
|
||||||
@@ -145,12 +147,15 @@ static enum MHD_Result http_response(void *cls, struct MHD_Connection *conn, con
|
|||||||
int row_index = 0;
|
int row_index = 0;
|
||||||
struct XMLServiceRow table_rows[services.size];
|
struct XMLServiceRow table_rows[services.size];
|
||||||
|
|
||||||
Iterator it = vector_begin(&services);
|
mxml_node_t* table = mxmlNewElement(center, "table");
|
||||||
Iterator end = vector_end(&services);
|
mxmlElementSetAttr(table, "width", "100%");
|
||||||
for (; !iterator_equals(&it, &end); iterator_increment(&it)) {
|
|
||||||
struct Service* service = *(struct Service**)iterator_get(&it);
|
mxml_node_t* colgroup = mxmlNewElement(table, "colgroup");
|
||||||
mxml_node_t* table = mxmlNewElement(center, "table");
|
|
||||||
mxmlElementSetAttr(table, "width", "100%");
|
Iterator service_it = vector_begin(&services);
|
||||||
|
Iterator service_end = vector_end(&services);
|
||||||
|
for (; !iterator_equals(&service_it, &service_end); iterator_increment(&service_it)) {
|
||||||
|
struct Service* service = *(struct Service**)iterator_get(&service_it);
|
||||||
mxml_node_t* header_row = mxmlNewElement(table, "tr");
|
mxml_node_t* header_row = mxmlNewElement(table, "tr");
|
||||||
mxml_node_t* header_cell = mxmlNewElement(table, "th");
|
mxml_node_t* header_cell = mxmlNewElement(table, "th");
|
||||||
mxmlElementSetAttr(header_cell, "align", "left");
|
mxmlElementSetAttr(header_cell, "align", "left");
|
||||||
@@ -162,38 +167,65 @@ static enum MHD_Result http_response(void *cls, struct MHD_Connection *conn, con
|
|||||||
row_index++;
|
row_index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int col_count = 1;
|
||||||
|
long now = microtime();
|
||||||
|
long earliest_time = 0;
|
||||||
|
|
||||||
|
Iterator count_it = vector_begin(&status_timeline);
|
||||||
|
Iterator peroid_end = vector_end(&status_timeline);
|
||||||
|
for (; !iterator_equals(&count_it, &peroid_end); iterator_increment(&count_it)) {
|
||||||
|
struct StatusPeroid* peroid = *(struct StatusPeroid**)iterator_get(&count_it);
|
||||||
|
if (earliest_time == 0) earliest_time = peroid->time;
|
||||||
|
else if (peroid->time <= (now - 86400000000)) {
|
||||||
|
earliest_time = peroid->time;
|
||||||
|
} else col_count++;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator pit = vector_begin(&status_timeline);
|
long last_time = earliest_time;
|
||||||
Iterator pend = vector_end(&status_timeline);
|
long total_time = now - earliest_time;
|
||||||
for (; !iterator_equals(&pit, &pend); iterator_increment(&pit)) {
|
Iterator peroid_it = vector_begin(&status_timeline);
|
||||||
struct StatusPeroid* peroid = *(struct StatusPeroid**)iterator_get(&pit);
|
for (; !iterator_equals(&peroid_it, &peroid_end); iterator_increment(&peroid_it)) {
|
||||||
|
struct StatusPeroid* peroid = *(struct StatusPeroid**)iterator_get(&peroid_it);
|
||||||
|
if (peroid->time < earliest_time) continue;
|
||||||
|
|
||||||
|
long duration = peroid->time - last_time;
|
||||||
|
if (total_time > 0 && duration > 0) {
|
||||||
|
double col_size = (double)duration / total_time * 100.0L;
|
||||||
|
char* size_str = (char*)malloc(50);
|
||||||
|
snprintf(size_str, 50, "%lf%%", col_size);
|
||||||
|
|
||||||
|
mxml_node_t* col = mxmlNewElement(colgroup, "col");
|
||||||
|
mxmlElementSetAttr(col, "width", size_str);
|
||||||
|
last_time = peroid->time;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator sit = vector_begin(&peroid->server_statuses);
|
Iterator sit = vector_begin(&peroid->server_statuses);
|
||||||
Iterator send = vector_end(&peroid->server_statuses);
|
Iterator send = vector_end(&peroid->server_statuses);
|
||||||
for (; !iterator_equals(&sit, &send); iterator_increment(&sit)) {
|
for (; !iterator_equals(&sit, &send); iterator_increment(&sit)) {
|
||||||
struct ServerStatus* srvstat = *(struct ServerStatus**)iterator_get(&sit);
|
struct ServerStatus* srvstat = *(struct ServerStatus**)iterator_get(&sit);
|
||||||
|
|
||||||
struct XMLServiceRow row_struct;
|
struct XMLServiceRow* row_struct;
|
||||||
for (int x = 0; x < services.size; x++)
|
for (int x = 0; x < services.size; x++)
|
||||||
if (table_rows[x].service == srvstat->service) {
|
if (table_rows[x].service == srvstat->service) {
|
||||||
row_struct = table_rows[x];
|
row_struct = &table_rows[x];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
mxml_node_t* cell = mxmlNewElement(row_struct.row, "td");
|
if (row_struct->service != 0) {
|
||||||
mxmlElementSetAttr(cell, "_status", state_text(srvstat->state));
|
mxml_node_t* cell = mxmlNewElement(row_struct->row, "td");
|
||||||
char* ping_str = (char*)malloc(16);
|
mxmlElementSetAttr(cell, "_status", state_text(srvstat->state));
|
||||||
snprintf(ping_str, 16, "%f ms", srvstat->ping);
|
char* ping_str = (char*)malloc(50);
|
||||||
mxmlElementSetAttr(cell, "_ping", ping_str);
|
snprintf(ping_str, 50, "%f ms", srvstat->ping);
|
||||||
|
mxmlElementSetAttr(cell, "_ping", ping_str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response_code = 200;
|
response_code = 200;
|
||||||
} else {
|
} else {
|
||||||
mxml_node_t* error_header = mxmlNewElement(center, "h1");
|
mxml_node_t* error_header = mxmlNewElement(center, "h1");
|
||||||
char* err_str = (char*)malloc(5);
|
char err_str[5];
|
||||||
snprintf(err_str, 5, "%i", response_code);
|
snprintf(err_str, sizeof(err_str)-1, "%i", response_code);
|
||||||
mxmlNewText(error_header, 0, err_str);
|
mxmlNewText(error_header, 0, err_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user