# HG changeset patch # User David Demelier # Date 1660667348 -7200 # Node ID e8f24896b4845d17086c485b6229a5ed4148ab1c # Parent 16f1c72d1635dc05ad9de8818d3ce2436a3f8ab0 scid: add /workers page diff -r 16f1c72d1635 -r e8f24896b484 Makefile --- a/Makefile Mon Aug 15 17:57:34 2022 +0200 +++ b/Makefile Tue Aug 16 18:29:08 2022 +0200 @@ -88,6 +88,7 @@ scid/page-index.c \ scid/page-jobresults.c \ scid/page-static.c \ + scid/page-workers.c \ scid/pageutil.c \ scid/scid.c \ scid/theme.c diff -r 16f1c72d1635 -r e8f24896b484 scid/http.c --- a/scid/http.c Mon Aug 15 17:57:34 2022 +0200 +++ b/scid/http.c Tue Aug 16 18:29:08 2022 +0200 @@ -36,14 +36,16 @@ #include "page-index.h" #include "page-jobresults.h" #include "page-static.h" +#include "page-workers.h" #include "pageutil.h" #include "scid.h" enum page { PAGE_INDEX, /* Job results at index. */ + PAGE_API, /* JSON API with key authentication. */ PAGE_JOBRESULTS, /* List of jobresult for one job. */ - PAGE_API, - PAGE_STATIC, + PAGE_STATIC, /* Static files delivery. */ + PAGE_WORKERS, /* User view of workers. */ PAGE_LAST /* Not used. */ }; @@ -86,18 +88,20 @@ } } -static const char *pages[] = { +static const char * const pages[] = { [PAGE_INDEX] = "", + [PAGE_API] = "api", [PAGE_JOBRESULTS] = "jobresults", - [PAGE_API] = "api", - [PAGE_STATIC] = "static" + [PAGE_STATIC] = "static", + [PAGE_WORKERS] = "workers" }; static void (*handlers[])(struct kreq *req) = { [PAGE_INDEX] = page_index, + [PAGE_API] = dispatch_api, [PAGE_JOBRESULTS] = page_jobresults, - [PAGE_API] = dispatch_api, - [PAGE_STATIC] = page_static + [PAGE_STATIC] = page_static, + [PAGE_WORKERS] = page_workers }; static void diff -r 16f1c72d1635 -r e8f24896b484 scid/page-workers.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scid/page-workers.c Tue Aug 16 18:29:08 2022 +0200 @@ -0,0 +1,88 @@ +/* + * page-workers.c -- page /workers/ route + * + * Copyright (c) 2021-2022 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include + +#include "db.h" +#include "page-workers.h" +#include "pageutil.h" +#include "theme.h" +#include "util.h" + +static void +get_one(struct kreq *r) +{ + json_t *worker, *jobs; + char *body; + + if (!(worker = db_worker_find(r->path))) + pageutil_status(r, KHTTP_404); + else { + /* Silently ignore jobs if it has failed. */ + if ((jobs = db_jobresult_list_by_worker(r->path))) + json_object_set_new(worker, "jobs", jobs); + + //printf("worker=%s\n", json_dumps(worker, JSON_INDENT(4))); + body = theme_render("onPageWorker", worker); + pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, body); + free(body); + } +} + +static void +get_all(struct kreq *r) +{ + json_t *workers; + char *body; + + if (!(workers = db_worker_list())) + pageutil_status(r, KHTTP_500); + else { + body = theme_render("onPageWorkers", util_json_pack("{so}", + "workers", workers + )); + pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, body); + free(body); + } +} + +static void +get(struct kreq *r) +{ + if (strlen(r->path) > 0) + get_one(r); + else + get_all(r); +} + +void +page_workers(struct kreq *r) +{ + (void)r; + + switch (r->method) { + case KMETHOD_GET: + get(r); + break; + default: + pageutil_status(r, KHTTP_400); + break; + } +} diff -r 16f1c72d1635 -r e8f24896b484 scid/page-workers.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scid/page-workers.h Tue Aug 16 18:29:08 2022 +0200 @@ -0,0 +1,40 @@ +/* + * page-workers.h -- page /workers/ route + * + * Copyright (c) 2021-2022 David Demelier + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef SCI_PAGE_WORKERS_H +#define SCI_PAGE_WORKERS_H + +/** + * \file page-workers.h + * \brief Page /workers/id route. + * + * This module does not log messages. + */ + +struct kreq; + +/** + * Run the page. + * + * \pre r != NULL + * \param r the request + */ +void +page_workers(struct kreq *r); + +#endif /* !SCI_PAGE_WORKERS_H */ diff -r 16f1c72d1635 -r e8f24896b484 themes/bulma/header.mustache --- a/themes/bulma/header.mustache Mon Aug 15 17:57:34 2022 +0200 +++ b/themes/bulma/header.mustache Tue Aug 16 18:29:08 2022 +0200 @@ -16,7 +16,7 @@ diff -r 16f1c72d1635 -r e8f24896b484 themes/bulma/theme.js --- a/themes/bulma/theme.js Mon Aug 15 17:57:34 2022 +0200 +++ b/themes/bulma/theme.js Tue Aug 16 18:29:08 2022 +0200 @@ -26,20 +26,29 @@ Scid.render(rdr, "footer.mustache"); } +function addStatusClasses(jobs) +{ + for (var j = 0; j < jobs.length; ++j) { + if (jobs[j].exitcode !== 0 || jobs[j].sigcode !== 0) { + jobs[j].color = "is-danger"; + jobs[j].classname = "has-text-success"; + jobs[j].status = "success"; + } else { + jobs[j].color = "is-success"; + jobs[j].textcolor = "has-text-danger"; + jobs[j].status = "failed"; + } + } +} + function onPageIndex(rdr, data) { /* * Add is-danger/is-success for every job depending on their success * status, this is required to show the appropriate tag. */ - for (var i = 0; i < data.projects.length; ++i) { - for (var j = 0; j < data.projects[i].jobs.length; ++j) { - if (data.projects[i].jobs[j].status === "success") - data.projects[i].jobs[j].classname = "is-success"; - else - data.projects[i].jobs[j].classname = "is-danger"; - } - } + for (var i = 0; i < data.projects.length; ++i) + addStatusClasses(data.projects[i].jobs); render(rdr, "index.mustache", "sci -- index page", data); } @@ -66,3 +75,17 @@ { render(rdr, "status.mustache", "sci -- " + data.status, data); } + +function onPageWorker(rdr, data) +{ + /* Similar to index page, add classes. */ + if (typeof (data.jobs) === "object") + addStatusClasses(data.jobs); + + render(rdr, "worker.mustache", "sci -- worker", data); +} + +function onPageWorkers(rdr, data) +{ + render(rdr, "workers.mustache", "sci -- workers", data); +} diff -r 16f1c72d1635 -r e8f24896b484 themes/bulma/worker.mustache --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/themes/bulma/worker.mustache Tue Aug 16 18:29:08 2022 +0200 @@ -0,0 +1,34 @@ +

Worker information

+
+
+ Name +
+
+ {{name}} +
+
+
+
+ Description +
+
+ {{desc}} +
+
+ +

Recent jobs from this worker

+ + + + + + + + + {{#jobs}} + + + + + {{/jobs}} +
jobstatus
{{id}}{{status}}
diff -r 16f1c72d1635 -r e8f24896b484 themes/bulma/workers.mustache --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/themes/bulma/workers.mustache Tue Aug 16 18:29:08 2022 +0200 @@ -0,0 +1,18 @@ +

Available workers

+ + + + + + + + + + {{#workers}} + + + + + {{/workers}} + +
NameDescription
{{name}}{{desc}}