diff scid/page-projects.c @ 58:7a4112eec15b

scid: add /projects page
author David Demelier <markand@malikania.fr>
date Wed, 17 Aug 2022 19:45:32 +0200
parents 576f4b1ec79f
children 71cd8447e3a4
line wrap: on
line diff
--- 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