Mercurial > sci
diff scid/page-index.c @ 26:7e10cace67a3
scid: add basic mustache support
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 02 Aug 2022 13:24:13 +0200 |
parents | 600204c31bf0 |
children | dae2de19ca5d |
line wrap: on
line diff
--- a/scid/page-index.c Mon Jul 25 21:22:13 2022 +0200 +++ b/scid/page-index.c Tue Aug 02 13:24:13 2022 +0200 @@ -16,79 +16,112 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <sys/types.h> -#include <assert.h> -#include <stdarg.h> -#include <stdint.h> +#include <errno.h> +#include <string.h> -#include <kcgi.h> - -#include "database.h" -#include "fragment-paste.h" -#include "page-index.h" +#include "log.h" +#include "config.h" +#include "db.h" #include "page.h" -#include "paste.h" +#include "types.h" #include "util.h" -struct template { - struct kreq *req; - const struct paste *pastes; - size_t pastesz; -}; +/* + * Document we create for templatize. + * + * { + * "projects: [ + * { + * "name": "project name", + * "description": "project short description", + * "url": "project URL or homepage", + * "jobs": [ + * { + * "job": job-id, + * "tag": "job tag / revision", + * "success": true, // on success (absent otherwise) + * "failed: true // on failure (absent otherwise) + * } + * ] + * } + * ] + * } + */ -static const char *keywords[] = { - "pastes" -}; +static json_t * +make_job(const struct job *job) +{ + struct jobresult res[SCI_WORKER_MAX]; + ssize_t resz; + json_t *doc = NULL; -static int -template(size_t keyword, void *arg) -{ - struct template *tp = arg; + doc = json_pack("{sI ss}", + "id", (json_int_t)job->id, + "tag", job->tag + ); + + /* Find every job result associated to see if there are failures. */ + resz = db_jobresult_list_by_job_group(res, UTIL_SIZE(res), job->id); + + for (ssize_t i = 0; i < resz; ++i) + if (res[i].exitcode) + json_object_set_new(doc, "failed", json_true()); + + if (!json_object_get(doc, "failed")) + json_object_set_new(doc, "success", json_true()); - switch (keyword) { - case 0: - for (size_t i = 0; i < tp->pastesz; ++i) - fragment_paste(tp->req, &tp->pastes[i]); - break; - default: - break; + return doc; +} + +static json_t * +make_jobs(const char *project) +{ + struct job jobs[10]; + ssize_t jobsz; + json_t *array = NULL, *obj; + + if ((jobsz = db_job_list(jobs, UTIL_SIZE(jobs), project)) >= 0) { + if (!(array = json_array())) + return NULL; + for (ssize_t i = 0; i < jobsz; ++i) + if ((obj = make_job(&jobs[i]))) + json_array_append(array, obj); } - return 1; + return array; +} + +static json_t * +make_project(const struct project *project) +{ + return json_pack("{ss ss ss so*}", + "name", project->name, + "description", project->desc, + "url", project->url, + "jobs", make_jobs(project->name) + ); } static void get(struct kreq *r) { - struct paste pastes[10] = {0}; - size_t pastesz = NELEM(pastes); + (void)r; + struct project projects[SCI_PROJECT_MAX] = {0}; + ssize_t projectsz = 0; + json_t *array; - if (!database_recents(pastes, &pastesz)) - page(r, NULL, KHTTP_500, "pages/500.html"); - else - page_index_render(r, pastes, pastesz); - - for (size_t i = 0; i < pastesz; ++i) - paste_finish(&pastes[i]); -} + /* 'projects' array. */ + if (!(array = json_array())) + log_die("page-index: %s", strerror(ENOMEM)); -void -page_index_render(struct kreq *r, const struct paste *pastes, size_t pastesz) -{ - struct template data = { - .req = r, - .pastes = pastes, - .pastesz = pastesz - }; + projectsz = db_project_list(projects, UTIL_SIZE(projects)); - struct ktemplate kt = { - .key = keywords, - .keysz = NELEM(keywords), - .arg = &data, - .cb = template - }; + for (ssize_t i = 0; i < projectsz; ++i) + json_array_append(array, make_project(&projects[i])); - page(r, &kt, KHTTP_200, "pages/index.html"); + page(r, KHTTP_200, KMIME_TEXT_HTML, "pages/index.html", json_pack("{so}", + "projects", array + )); } void @@ -99,7 +132,7 @@ get(r); break; default: - page(r, NULL, KHTTP_400, "400.html"); + page(r, KHTTP_400, KMIME_TEXT_HTML, "pages/400.html", NULL); break; } }