comparison scid/page-index.c @ 30:43333d18e4b8

scid: document theme
author David Demelier <markand@malikania.fr>
date Thu, 04 Aug 2022 14:54:43 +0200
parents 695637f1d8a7
children 8c2087e7d381
comparison
equal deleted inserted replaced
29:695637f1d8a7 30:43333d18e4b8
25 #include "pageutil.h" 25 #include "pageutil.h"
26 #include "scid.h" 26 #include "scid.h"
27 #include "theme.h" 27 #include "theme.h"
28 #include "util.h" 28 #include "util.h"
29 29
30 #if 0
31
32 /*
33 * Document we create for templatize.
34 *
35 * {
36 * "projects: [
37 * {
38 * "name": "project name",
39 * "desc": "project short description",
40 * "url": "project URL or homepage",
41 * "jobs": [
42 * {
43 * "job": job-id,
44 * "tag": "job tag / revision",
45 * "status": "failed / success" // failed if at least one has failed
46 * }
47 * ]
48 * "n-failed": number of failed jobs
49 * "n-succes": number of successful jobs
50 * }
51 * ]
52 * }
53 */
54
55 #endif
56
57 static void 30 static void
58 fill_jobresults(json_t *project, json_t *job, json_t *jobresults) 31 set_job_status(json_t *project, json_t *job, json_t *jobresults)
59 { 32 {
60 json_t *iter, *status; 33 json_t *iter, *status;
61 int exitcode, sigcode; 34 int exitcode, sigcode;
62 size_t i, ns = 0, nf = 0; 35 size_t i, ns = 0, nf = 0;
63 36
79 json_object_set_new(project, "n-failed", json_integer(nf)); 52 json_object_set_new(project, "n-failed", json_integer(nf));
80 json_object_set_new(project, "n-success", json_integer(ns)); 53 json_object_set_new(project, "n-success", json_integer(ns));
81 } 54 }
82 55
83 static void 56 static void
84 fill_jobs(json_t *project, json_t *jobs) 57 set_project_jobs(json_t *project, json_t *jobs)
85 { 58 {
86 json_t *iter, *jobresults; 59 json_t *iter, *jobresults;
87 json_int_t job_id; 60 json_int_t job_id;
88 size_t i; 61 size_t i;
89 62
90 json_array_foreach(jobs, i, iter) { 63 json_array_foreach(jobs, i, iter) {
64 /* Don't populate too much. */
65 if (i + 1 >= 10)
66 break;
67
91 /* 68 /*
92 * For this job, find all jobresult to check how many have 69 * For this job, find all jobresult to check how many have
93 * failed or not. 70 * failed or not.
94 * 71 *
95 * Also, since we have the project name, we can remove it. 72 * Also, since we have the project name, we can remove it.
98 json_unpack(iter, "{sI}", "id", &job_id); 75 json_unpack(iter, "{sI}", "id", &job_id);
99 76
100 if (!(jobresults = db_jobresult_list_by_job_group(job_id))) 77 if (!(jobresults = db_jobresult_list_by_job_group(job_id)))
101 continue; 78 continue;
102 79
103 fill_jobresults(project, iter, jobresults); 80 set_job_status(project, iter, jobresults);
104 json_decref(jobresults); 81 json_decref(jobresults);
105 } 82 }
106 83
107 json_object_set_new(project, "jobs", jobs); 84 json_object_set_new(project, "jobs", jobs);
108 } 85 }
109 86
87 /*
88 * For every projects, find their jobs and add them as 'jobs' property.
89 */
110 static void 90 static void
111 fill_projects(json_t *projects) 91 update_projects(json_t *projects)
112 { 92 {
113 json_t *jobs, *iter; 93 json_t *jobs, *iter;
114 const char *name; 94 const char *name;
115 size_t i; 95 size_t i;
116 96
121 101
122 /* Find jobs for this project. */ 102 /* Find jobs for this project. */
123 if (!(jobs = db_job_list(name))) 103 if (!(jobs = db_job_list(name)))
124 continue; 104 continue;
125 105
126 fill_jobs(iter, jobs); 106 set_project_jobs(iter, jobs);
127 } 107 }
128 } 108 }
129 109
130 static void 110 static void
131 get(struct kreq *req) 111 get(struct kreq *req)
133 json_t *projects, *root; 113 json_t *projects, *root;
134 char *data; 114 char *data;
135 115
136 /* First, fetch all projects. */ 116 /* First, fetch all projects. */
137 if ((projects = db_project_list())) { 117 if ((projects = db_project_list())) {
138 fill_projects(projects); 118 update_projects(projects);
139
140 root = json_pack("{so}", "projects", projects); 119 root = json_pack("{so}", "projects", projects);
141 printf("===\n%s\n===\n", json_dumps(root, JSON_INDENT(4))); 120 data = theme_page_index(root);
142 data = theme_page_index(scid.theme, root);
143 pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data); 121 pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data);
122 free(data);
123 json_decref(root);
144 json_decref(projects); 124 json_decref(projects);
145 } else 125 } else
146 pageutil_status(req, KHTTP_500); 126 pageutil_status(req, KHTTP_500);
147 } 127 }
148 128