changeset 58:7a4112eec15b

scid: add /projects page
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2022 19:45:32 +0200
parents bc617784ec97
children 835d52f72786
files Makefile scid/http.c scid/page-projects.c scid/page-projects.h themes/bulma/header.mustache themes/bulma/project.mustache themes/bulma/projects.mustache themes/bulma/theme.js
diffstat 8 files changed, 137 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Wed Aug 17 18:26:27 2022 +0200
+++ b/Makefile	Wed Aug 17 19:45:32 2022 +0200
@@ -87,6 +87,7 @@
                         scid/page-api-workers.c \
                         scid/page-index.c \
                         scid/page-jobresults.c \
+                        scid/page-projects.c \
                         scid/page-static.c \
                         scid/page-workers.c \
                         scid/pageutil.c \
--- a/scid/http.c	Wed Aug 17 18:26:27 2022 +0200
+++ b/scid/http.c	Wed Aug 17 19:45:32 2022 +0200
@@ -35,6 +35,7 @@
 #include "page-api-workers.h"
 #include "page-index.h"
 #include "page-jobresults.h"
+#include "page-projects.h"
 #include "page-static.h"
 #include "page-workers.h"
 #include "pageutil.h"
@@ -44,6 +45,7 @@
 	PAGE_INDEX,             /* Job results at index. */
 	PAGE_API,               /* JSON API with key authentication. */
 	PAGE_JOBRESULTS,        /* List of jobresult for one job. */
+	PAGE_PROJECTS,          /* User view of projects. */
 	PAGE_STATIC,            /* Static files delivery. */
 	PAGE_WORKERS,           /* User view of workers. */
 	PAGE_LAST               /* Not used. */
@@ -93,6 +95,7 @@
 	[PAGE_INDEX]            = "",
 	[PAGE_API]              = "api",
 	[PAGE_JOBRESULTS]       = "jobresults",
+	[PAGE_PROJECTS]         = "projects",
 	[PAGE_STATIC]           = "static",
 	[PAGE_WORKERS]          = "workers"
 };
@@ -101,6 +104,7 @@
 	[PAGE_INDEX]            = page_index,
 	[PAGE_API]              = dispatch_api,
 	[PAGE_JOBRESULTS]       = page_jobresults,
+	[PAGE_PROJECTS]         = page_projects,
 	[PAGE_STATIC]           = page_static,
 	[PAGE_WORKERS]          = page_workers
 };
--- a/scid/page-projects.c	Wed Aug 17 18:26:27 2022 +0200
+++ b/scid/page-projects.c	Wed Aug 17 19:45:32 2022 +0200
@@ -1,5 +1,5 @@
 /*
- * page-projects.c -- page /projects route
+ * page-projects.c -- page /projects[/<name>] route
  *
  * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr>
  *
@@ -16,110 +16,55 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <errno.h>
+#include <assert.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "db.h"
+#include "page-workers.h"
 #include "pageutil.h"
-#include "scid.h"
 #include "theme.h"
 #include "util.h"
 
 static void
-set_job_status(json_t *project, json_t *job, json_t *jobresults)
+get_one(struct kreq *r)
 {
-	json_t *iter, *status;
-	int exitcode, sigcode;
-	size_t i, ns = 0, nf = 0;
-
-	json_array_foreach(jobresults, i, iter) {
-		json_unpack(iter, "{si si}", "exitcode", &exitcode, "sigcode", &sigcode);
-
-		if (exitcode == 0 && sigcode == 0)
-			ns++;
-		else
-			nf++;
-	}
-
-	if (nf)
-		status = json_string("failed");
-	else
-		status = json_string("success");
-
-	json_object_set_new(job, "status", status);
-	json_object_set_new(project, "n-failed", json_integer(nf));
-	json_object_set_new(project, "n-success", json_integer(ns));
-}
-
-static void
-set_project_jobs(json_t *project, json_t *jobs)
-{
-	json_t *iter, *jobresults;
-	json_int_t job_id;
-	size_t i;
-
-	json_array_foreach(jobs, i, iter) {
-		/* Don't populate too much. */
-		if (i + 1 >= 10)
-			break;
+	json_t *project;
+	char *body;
 
-		/*
-		 * For this job, find all jobresult to check how many have
-		 * failed or not.
-		 *
-		 * Also, since we have the project name, we can remove it.
-		 */
-		json_object_del(iter, "project_name");
-		json_unpack(iter, "{sI}", "id", &job_id);
-
-		if (!(jobresults = db_jobresult_list_by_job_group(job_id)))
-			continue;
-
-		set_job_status(project, iter, jobresults);
-		json_decref(jobresults);
-	}
-
-	json_object_set_new(project, "jobs", jobs);
-}
-
-/*
- * For every projects, find their jobs and add them as 'jobs' property.
- */
-static void
-update_projects(json_t *projects)
-{
-	json_t *jobs, *iter;
-	const char *name;
-	size_t i;
-
-	json_array_foreach(projects, i, iter) {
-		/* Script is not necessary at this point. */
-		json_object_del(iter, "script");
-		json_unpack(iter, "{ss}", "name", &name);
-
-		/* Find jobs for this project. */
-		if (!(jobs = db_job_list(name)))
-			continue;
-
-		set_project_jobs(iter, jobs);
+	if (!(project = db_project_find(r->path)))
+		pageutil_status(r, KHTTP_404);
+	else {
+		body = theme_render("onPageProject", project);
+		pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, body);
+		free(body);
 	}
 }
 
 static void
-get(struct kreq *req)
+get_all(struct kreq *r)
 {
-	json_t *projects, *root;
-	char *data;
+	json_t *projects;
+	char *body;
 
-	/* First, fetch all projects. */
-	if ((projects = db_project_list())) {
-		root = json_pack("{so}", "projects", projects);
-		data = theme_page_index(root);
-		pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data);
-		free(data);
-		json_decref(root);
-	} else
-		pageutil_status(req, KHTTP_500);
+	if (!(projects = db_project_list()))
+		pageutil_status(r, KHTTP_500);
+	else {
+		body = theme_render("onPageProjects", util_json_pack("{so}",
+			"projects", projects
+		));
+		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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scid/page-projects.h	Wed Aug 17 19:45:32 2022 +0200
@@ -0,0 +1,40 @@
+/*
+ * page-projects.h -- page /projects[/<name>] route
+ *
+ * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr>
+ *
+ * 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_PROJECTS_H
+#define SCI_PAGE_PROJECTS_H
+
+/**
+ * \file page-projects.h
+ * \brief Page /projects[/name] route.
+ *
+ * This module does not log messages.
+ */
+
+struct kreq;
+
+/**
+ * Run the page.
+ *
+ * \pre r != NULL
+ * \param r the request
+ */
+void
+page_projects(struct kreq *r);
+
+#endif /* !SCI_PAGE_PROJECTS_H */
--- a/themes/bulma/header.mustache	Wed Aug 17 18:26:27 2022 +0200
+++ b/themes/bulma/header.mustache	Wed Aug 17 19:45:32 2022 +0200
@@ -15,7 +15,7 @@
 
 				<div class="navbar-menu">
 					<div class="navbar-start">
-						<a class="navbar-item">projects</a>
+						<a class="navbar-item" href="/projects">projects</a>
 						<a class="navbar-item" href="/workers">workers</a>
 					</div>
 				</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/themes/bulma/project.mustache	Wed Aug 17 19:45:32 2022 +0200
@@ -0,0 +1,29 @@
+				<h1 class="title">Project information</h1>
+				<div class="columns">
+					<div class="column is-1">
+						<strong>Name</strong>
+					</div>
+					<div class="column">
+						{{name}}
+					</div>
+				</div>
+				<div class="columns">
+					<div class="column is-1">
+						<strong>Description</strong>
+					</div>
+					<div class="column">
+						{{desc}}
+					</div>
+				</div>
+				<div class="columns">
+					<div class="column is-1">
+						<strong>URL</strong>
+					</div>
+					<div class="column">
+						<a href="{{url}}">{{url}}</a>
+					</div>
+				</div>
+				<h1 class="title">Project script</h1>
+					<pre>
+{{script}}
+					</pre>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/themes/bulma/projects.mustache	Wed Aug 17 19:45:32 2022 +0200
@@ -0,0 +1,18 @@
+<table class="table">
+	<thead>
+		<tr>
+			<th>Name</th>
+			<th>Description</th>
+			<th>URL</th>
+		</tr>
+		<tbody>
+			{{#projects}}
+			<tr>
+				<td><a href="/projects/{{name}}">{{name}}</a></td>
+				<td>{{desc}}</td>
+				<td><a href="{{url}}">{{url}}</a></td>
+			</tr>
+			{{/projects}}
+		</tbody>
+	</thead>
+</table>
--- a/themes/bulma/theme.js	Wed Aug 17 18:26:27 2022 +0200
+++ b/themes/bulma/theme.js	Wed Aug 17 19:45:32 2022 +0200
@@ -90,3 +90,13 @@
 {
 	render(rdr, "workers.mustache", "sci -- workers", data);
 }
+
+function onPageProjects(rdr, data)
+{
+	render(rdr, "projects.mustache", "sci -- projects", data);
+}
+
+function onPageProject(rdr, data)
+{
+	render(rdr, "project.mustache", "sci -- project", data);
+}