Merge pull request #2260 from cesanta/2259-refactor-event-log-page-for-examplesdevice-dashboard

refactored device-dashboard code
This commit is contained in:
Sergey Lyubka 2023-06-19 18:24:15 +01:00 committed by GitHub
commit 6bfd113a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 42 deletions

View File

@ -4,18 +4,6 @@
#include "mongoose.h"
#include "net.h"
void ui_event_next() {
if (events_no < 0 || events_no >= MAX_EVENTS_NO)
return;
events[events_no].type = rand() % 3;
events[events_no].prio = rand() % 3;
events[events_no].timestamp = events_no;
mg_snprintf(events[events_no].text, MAX_EVENT_TEXT_SIZE,
"event#%d", events_no);
events_no++;
}
static int s_sig_num;
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler);
@ -24,8 +12,6 @@ static void signal_handler(int sig_num) {
int main(void) {
struct mg_mgr mgr;
uint64_t last_ts = mg_millis();
uint64_t crt_ts;
signal(SIGPIPE, SIG_IGN);
signal(SIGINT, signal_handler);
@ -38,11 +24,6 @@ int main(void) {
web_init(&mgr);
while (s_sig_num == 0) {
mg_mgr_poll(&mgr, 50);
crt_ts = mg_millis();
if (crt_ts - last_ts > 1000) {
last_ts = crt_ts;
ui_event_next(); // generate a new event
}
}
mg_mgr_free(&mgr);

View File

@ -13,9 +13,6 @@ struct user {
const char *name, *pass, *access_token;
};
int events_no;
struct ui_event events[MAX_EVENTS_NO];
// Settings
struct settings {
bool log_enabled;
@ -56,6 +53,23 @@ uint64_t mg_now(void) {
return mg_millis() + s_boot_timestamp;
}
int ui_event_next(int no, struct ui_event *e) {
if (no < 0 || no >= MAX_EVENTS_NO)
return 0;
srand(no);
e->type = (uint8_t) rand() % 4;
e->prio = (uint8_t) rand() % 3;
e->timestamp = (unsigned long) (mg_now() - 86400 * 1000 /* one day back */ +
no * 300 * 1000 /* 5 mins between alerts */ +
1000 * (rand() % 300) /* randomize event time */) /
1000;
mg_snprintf(e->text, MAX_EVENT_TEXT_SIZE,
"event#%d", no);
return no + 1;
}
// SNTP connection event handler. When we get a response from an SNTP server,
// adjust s_boot_timestamp. We'll get a valid time from that point on
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
@ -149,30 +163,29 @@ static void handle_stats_get(struct mg_connection *c) {
static size_t print_events(void (*out)(char, void *), void *ptr, va_list *ap) {
size_t len = 0;
int pageno = va_arg(*ap, int);
int start = (pageno - 1) * EVENTS_PER_PAGE;
int end = start + EVENTS_PER_PAGE;
struct ui_event ev;
int pageno = va_arg(*ap, unsigned);
int no = (pageno - 1) * EVENTS_PER_PAGE;
int end = no + EVENTS_PER_PAGE;
for (int i = start; i < end && i < events_no; i++) {
while ((no = ui_event_next(no, &ev)) != 0 && no <= end) {
len += mg_xprintf(out, ptr, "%s{%m:%lu,%m:%d,%m:%d,%m:%m}", //
len == 0 ? "" : ",", //
MG_ESC("time"), events[i].timestamp, //
MG_ESC("type"), events[i].type, //
MG_ESC("prio"), events[i].prio, //
MG_ESC("text"), MG_ESC(events[i].text));
MG_ESC("time"), ev.timestamp, //
MG_ESC("type"), ev.type, //
MG_ESC("prio"), ev.prio, //
MG_ESC("text"), MG_ESC(ev.text));
}
return len;
}
static void handle_events_get(struct mg_connection *c, struct mg_str query) {
int pageno;
// query is represented by 'page=<page_id>'
pageno = atoi(query.ptr + 5);
if (pageno > events_no / EVENTS_PER_PAGE + 1 || pageno < 1) pageno = 1;
int pageno = atoi(query.ptr + 5); // query is represented by 'page=<page_id>'
if (pageno > MAX_EVENTS_NO / EVENTS_PER_PAGE + 1 || pageno < 1) pageno = 1;
mg_http_reply(c, 200, s_json_header, "{%m:[%M], %m:%d}", MG_ESC("arr"),
print_events, pageno, MG_ESC("totalCount"), events_no);
print_events, pageno, MG_ESC("totalCount"), MAX_EVENTS_NO);
}
static void handle_settings_set(struct mg_connection *c, struct mg_str body) {

View File

@ -13,19 +13,15 @@
#endif
#define MAX_DEVICE_NAME 40
#define MAX_EVENTS_NO 1000
#define MAX_EVENTS_NO 400
#define MAX_EVENT_TEXT_SIZE 10
#define EVENTS_PER_PAGE 20
// Event log entry
struct ui_event {
int type, prio;
uint8_t type, prio;
unsigned long timestamp;
char text[10];
};
extern int events_no;
extern struct ui_event events[MAX_EVENTS_NO];
void ui_event_next();
void web_init(struct mg_mgr *mgr);

View File

@ -77,7 +77,7 @@ function Events({}) {
<tr>
<${Td} text=${['power', 'hardware', 'tier3', 'tier4'][e.type]} />
<${Td} text=${html`<${Prio} prio=${e.prio}/>`} />
<${Td} text=${e.time || '1970-01-01'} />
<${Td} text=${e.time ? (new Date(e.time * 1000)).toLocaleString() : '1970-01-01'} />
<${Td} text=${e.text} />
<//>`;