comparison 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
comparison
equal deleted inserted replaced
57:bc617784ec97 58:7a4112eec15b
1 /* 1 /*
2 * page-projects.c -- page /projects route 2 * page-projects.c -- page /projects[/<name>] route
3 * 3 *
4 * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr> 4 * Copyright (c) 2021-2022 David Demelier <markand@malikania.fr>
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <errno.h> 19 #include <assert.h>
20 #include <stdlib.h>
20 #include <string.h> 21 #include <string.h>
21 22
22 #include "db.h" 23 #include "db.h"
24 #include "page-workers.h"
23 #include "pageutil.h" 25 #include "pageutil.h"
24 #include "scid.h"
25 #include "theme.h" 26 #include "theme.h"
26 #include "util.h" 27 #include "util.h"
27 28
28 static void 29 static void
29 set_job_status(json_t *project, json_t *job, json_t *jobresults) 30 get_one(struct kreq *r)
30 { 31 {
31 json_t *iter, *status; 32 json_t *project;
32 int exitcode, sigcode; 33 char *body;
33 size_t i, ns = 0, nf = 0;
34 34
35 json_array_foreach(jobresults, i, iter) { 35 if (!(project = db_project_find(r->path)))
36 json_unpack(iter, "{si si}", "exitcode", &exitcode, "sigcode", &sigcode); 36 pageutil_status(r, KHTTP_404);
37 37 else {
38 if (exitcode == 0 && sigcode == 0) 38 body = theme_render("onPageProject", project);
39 ns++; 39 pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, body);
40 else 40 free(body);
41 nf++;
42 }
43
44 if (nf)
45 status = json_string("failed");
46 else
47 status = json_string("success");
48
49 json_object_set_new(job, "status", status);
50 json_object_set_new(project, "n-failed", json_integer(nf));
51 json_object_set_new(project, "n-success", json_integer(ns));
52 }
53
54 static void
55 set_project_jobs(json_t *project, json_t *jobs)
56 {
57 json_t *iter, *jobresults;
58 json_int_t job_id;
59 size_t i;
60
61 json_array_foreach(jobs, i, iter) {
62 /* Don't populate too much. */
63 if (i + 1 >= 10)
64 break;
65
66 /*
67 * For this job, find all jobresult to check how many have
68 * failed or not.
69 *
70 * Also, since we have the project name, we can remove it.
71 */
72 json_object_del(iter, "project_name");
73 json_unpack(iter, "{sI}", "id", &job_id);
74
75 if (!(jobresults = db_jobresult_list_by_job_group(job_id)))
76 continue;
77
78 set_job_status(project, iter, jobresults);
79 json_decref(jobresults);
80 }
81
82 json_object_set_new(project, "jobs", jobs);
83 }
84
85 /*
86 * For every projects, find their jobs and add them as 'jobs' property.
87 */
88 static void
89 update_projects(json_t *projects)
90 {
91 json_t *jobs, *iter;
92 const char *name;
93 size_t i;
94
95 json_array_foreach(projects, i, iter) {
96 /* Script is not necessary at this point. */
97 json_object_del(iter, "script");
98 json_unpack(iter, "{ss}", "name", &name);
99
100 /* Find jobs for this project. */
101 if (!(jobs = db_job_list(name)))
102 continue;
103
104 set_project_jobs(iter, jobs);
105 } 41 }
106 } 42 }
107 43
108 static void 44 static void
109 get(struct kreq *req) 45 get_all(struct kreq *r)
110 { 46 {
111 json_t *projects, *root; 47 json_t *projects;
112 char *data; 48 char *body;
113 49
114 /* First, fetch all projects. */ 50 if (!(projects = db_project_list()))
115 if ((projects = db_project_list())) { 51 pageutil_status(r, KHTTP_500);
116 root = json_pack("{so}", "projects", projects); 52 else {
117 data = theme_page_index(root); 53 body = theme_render("onPageProjects", util_json_pack("{so}",
118 pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data); 54 "projects", projects
119 free(data); 55 ));
120 json_decref(root); 56 pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, body);
121 } else 57 free(body);
122 pageutil_status(req, KHTTP_500); 58 }
59 }
60
61 static void
62 get(struct kreq *r)
63 {
64 if (strlen(r->path) > 0)
65 get_one(r);
66 else
67 get_all(r);
123 } 68 }
124 69
125 void 70 void
126 page_projects(struct kreq *r) 71 page_projects(struct kreq *r)
127 { 72 {