@@ -34,6 +34,152 @@ void seterror (job_list_error_t *errp, const char *fmt, ...)
3434 }
3535}
3636
37+ static int store_attr (struct job * job ,
38+ const char * attr ,
39+ json_t * o ,
40+ job_list_error_t * errp )
41+ {
42+ json_t * val = NULL ;
43+
44+ if (!strcmp (attr , "userid" )) {
45+ val = json_integer (job -> userid );
46+ }
47+ else if (!strcmp (attr , "urgency" )) {
48+ val = json_integer (job -> urgency );
49+ }
50+ else if (!strcmp (attr , "priority" )) {
51+ if (!(job -> states_mask & FLUX_JOB_STATE_SCHED ))
52+ return 0 ;
53+ val = json_integer (job -> priority );
54+ }
55+ else if (!strcmp (attr , "t_submit" )
56+ || !strcmp (attr , "t_depend" )) {
57+ if (!(job -> states_mask & FLUX_JOB_STATE_DEPEND ))
58+ return 0 ;
59+ val = json_real (job -> t_submit );
60+ }
61+ else if (!strcmp (attr , "t_run" )) {
62+ if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
63+ return 0 ;
64+ val = json_real (job -> t_run );
65+ }
66+ else if (!strcmp (attr , "t_cleanup" )) {
67+ if (!(job -> states_mask & FLUX_JOB_STATE_CLEANUP ))
68+ return 0 ;
69+ val = json_real (job -> t_cleanup );
70+ }
71+ else if (!strcmp (attr , "t_inactive" )) {
72+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
73+ return 0 ;
74+ val = json_real (job -> t_inactive );
75+ }
76+ else if (!strcmp (attr , "state" )) {
77+ val = json_integer (job -> state );
78+ }
79+ else if (!strcmp (attr , "name" )) {
80+ /* potentially NULL if jobspec invalid */
81+ if (job -> name )
82+ val = json_string (job -> name );
83+ else
84+ val = json_string ("" );
85+ }
86+ else if (!strcmp (attr , "ntasks" )) {
87+ val = json_integer (job -> ntasks );
88+ }
89+ else if (!strcmp (attr , "nnodes" )) {
90+ if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
91+ return 0 ;
92+ val = json_integer (job -> nnodes );
93+ }
94+ else if (!strcmp (attr , "ranks" )) {
95+ if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
96+ return 0 ;
97+ /* potentially NULL if R invalid */
98+ if (job -> ranks )
99+ val = json_string (job -> ranks );
100+ else
101+ val = json_string ("" );
102+ }
103+ else if (!strcmp (attr , "nodelist" )) {
104+ if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
105+ return 0 ;
106+ /* potentially NULL if R invalid */
107+ if (job -> nodelist )
108+ val = json_string (job -> nodelist );
109+ else
110+ val = json_string ("" );
111+ }
112+ else if (!strcmp (attr , "expiration" )) {
113+ if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
114+ return 0 ;
115+ val = json_real (job -> expiration );
116+ }
117+ else if (!strcmp (attr , "waitstatus" )) {
118+ if (job -> wait_status < 0 )
119+ return 0 ;
120+ val = json_integer (job -> wait_status );
121+ }
122+ else if (!strcmp (attr , "success" )) {
123+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
124+ return 0 ;
125+ val = json_boolean (job -> success );
126+ }
127+ else if (!strcmp (attr , "exception_occurred" )) {
128+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
129+ return 0 ;
130+ val = json_boolean (job -> exception_occurred );
131+ }
132+ else if (!strcmp (attr , "exception_severity" )) {
133+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
134+ || !job -> exception_occurred )
135+ return 0 ;
136+ val = json_integer (job -> exception_severity );
137+ }
138+ else if (!strcmp (attr , "exception_type" )) {
139+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
140+ || !job -> exception_occurred )
141+ return 0 ;
142+ val = json_string (job -> exception_type );
143+ }
144+ else if (!strcmp (attr , "exception_note" )) {
145+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
146+ || !job -> exception_occurred )
147+ return 0 ;
148+ val = json_string (job -> exception_note );
149+ }
150+ else if (!strcmp (attr , "result" )) {
151+ if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
152+ return 0 ;
153+ val = json_integer (job -> result );
154+ }
155+ else if (!strcmp (attr , "annotations" )) {
156+ if (!job -> annotations )
157+ return 0 ;
158+ val = json_incref (job -> annotations );
159+ }
160+ else if (!strcmp (attr , "dependencies" )) {
161+ if (!job -> dependencies )
162+ return 0 ;
163+ val = json_incref (grudgeset_tojson (job -> dependencies ));
164+ }
165+ else {
166+ seterror (errp , "%s is not a valid attribute" , attr );
167+ errno = EINVAL ;
168+ return -1 ;
169+ }
170+ if (val == NULL ) {
171+ errno = ENOMEM ;
172+ return -1 ;
173+ }
174+ if (json_object_set_new (o , attr , val ) < 0 ) {
175+ json_decref (val );
176+ errno = ENOMEM ;
177+ return -1 ;
178+ }
179+
180+ return 0 ;
181+ }
182+
37183/* For a given job, create a JSON object containing the jobid and any
38184 * additional requested attributes and their values. Returns JSON
39185 * object which the caller must free. On error, return NULL with
@@ -44,10 +190,10 @@ void seterror (job_list_error_t *errp, const char *fmt, ...)
44190 */
45191json_t * job_to_json (struct job * job , json_t * attrs , job_list_error_t * errp )
46192{
193+ json_t * val = NULL ;
47194 size_t index ;
48195 json_t * value ;
49196 json_t * o ;
50- json_t * val = NULL ;
51197
52198 memset (errp , 0 , sizeof (* errp ));
53199
@@ -66,138 +212,8 @@ json_t *job_to_json (struct job *job, json_t *attrs, job_list_error_t *errp)
66212 errno = EINVAL ;
67213 goto error ;
68214 }
69- if (!strcmp (attr , "userid" )) {
70- val = json_integer (job -> userid );
71- }
72- else if (!strcmp (attr , "urgency" )) {
73- val = json_integer (job -> urgency );
74- }
75- else if (!strcmp (attr , "priority" )) {
76- if (!(job -> states_mask & FLUX_JOB_STATE_SCHED ))
77- continue ;
78- val = json_integer (job -> priority );
79- }
80- else if (!strcmp (attr , "t_submit" )
81- || !strcmp (attr , "t_depend" )) {
82- if (!(job -> states_mask & FLUX_JOB_STATE_DEPEND ))
83- continue ;
84- val = json_real (job -> t_submit );
85- }
86- else if (!strcmp (attr , "t_run" )) {
87- if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
88- continue ;
89- val = json_real (job -> t_run );
90- }
91- else if (!strcmp (attr , "t_cleanup" )) {
92- if (!(job -> states_mask & FLUX_JOB_STATE_CLEANUP ))
93- continue ;
94- val = json_real (job -> t_cleanup );
95- }
96- else if (!strcmp (attr , "t_inactive" )) {
97- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
98- continue ;
99- val = json_real (job -> t_inactive );
100- }
101- else if (!strcmp (attr , "state" )) {
102- val = json_integer (job -> state );
103- }
104- else if (!strcmp (attr , "name" )) {
105- /* potentially NULL if jobspec invalid */
106- if (job -> name )
107- val = json_string (job -> name );
108- else
109- val = json_string ("" );
110- }
111- else if (!strcmp (attr , "ntasks" )) {
112- val = json_integer (job -> ntasks );
113- }
114- else if (!strcmp (attr , "nnodes" )) {
115- if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
116- continue ;
117- val = json_integer (job -> nnodes );
118- }
119- else if (!strcmp (attr , "ranks" )) {
120- if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
121- continue ;
122- /* potentially NULL if R invalid */
123- if (job -> ranks )
124- val = json_string (job -> ranks );
125- else
126- val = json_string ("" );
127- }
128- else if (!strcmp (attr , "nodelist" )) {
129- if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
130- continue ;
131- /* potentially NULL if R invalid */
132- if (job -> nodelist )
133- val = json_string (job -> nodelist );
134- else
135- val = json_string ("" );
136- }
137- else if (!strcmp (attr , "expiration" )) {
138- if (!(job -> states_mask & FLUX_JOB_STATE_RUN ))
139- continue ;
140- val = json_real (job -> expiration );
141- }
142- else if (!strcmp (attr , "waitstatus" )) {
143- if (job -> wait_status < 0 )
144- continue ;
145- val = json_integer (job -> wait_status );
146- }
147- else if (!strcmp (attr , "success" )) {
148- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
149- continue ;
150- val = json_boolean (job -> success );
151- }
152- else if (!strcmp (attr , "exception_occurred" )) {
153- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
154- continue ;
155- val = json_boolean (job -> exception_occurred );
156- }
157- else if (!strcmp (attr , "exception_severity" )) {
158- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
159- || !job -> exception_occurred )
160- continue ;
161- val = json_integer (job -> exception_severity );
162- }
163- else if (!strcmp (attr , "exception_type" )) {
164- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
165- || !job -> exception_occurred )
166- continue ;
167- val = json_string (job -> exception_type );
168- }
169- else if (!strcmp (attr , "exception_note" )) {
170- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE )
171- || !job -> exception_occurred )
172- continue ;
173- val = json_string (job -> exception_note );
174- }
175- else if (!strcmp (attr , "result" )) {
176- if (!(job -> states_mask & FLUX_JOB_STATE_INACTIVE ))
177- continue ;
178- val = json_integer (job -> result );
179- }
180- else if (!strcmp (attr , "annotations" )) {
181- if (!job -> annotations )
182- continue ;
183- val = json_incref (job -> annotations );
184- }
185- else if (!strcmp (attr , "dependencies" )) {
186- if (!job -> dependencies )
187- continue ;
188- val = json_incref (grudgeset_tojson (job -> dependencies ));
189- }
190- else {
191- seterror (errp , "%s is not a valid attribute" , attr );
192- errno = EINVAL ;
215+ if (store_attr (job , attr , o , errp ) < 0 )
193216 goto error ;
194- }
195- if (val == NULL )
196- goto error_nomem ;
197- if (json_object_set_new (o , attr , val ) < 0 ) {
198- json_decref (val );
199- goto error_nomem ;
200- }
201217 }
202218 return o ;
203219 error_nomem :
0 commit comments