Job history colation code moved from notif_match to jobstat
authorZdeněk Šustr <sustr4@cesnet.cz>
Thu, 23 Aug 2012 11:30:14 +0000 (11:30 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Thu, 23 Aug 2012 11:30:14 +0000 (11:30 +0000)
History flag is now observed in job status queries
Job history is displayed in HTML Job Detail if present
Unused warn() function removed

org.glite.lb.server/src/jobstat.c
org.glite.lb.server/src/jobstat.h
org.glite.lb.server/src/lb_html.c
org.glite.lb.server/src/notif_match.c

index 06b19d3..89b82e6 100644 (file)
@@ -28,6 +28,7 @@ limitations under the License.
 
 #include "glite/lbu/trio.h"
 #include "glite/lb/events.h"
+#include "glite/lb/events_json.h"
 #include "glite/lb/context-int.h"
 #include "glite/lb/intjobstat.h"
 #include "glite/lb/process_event.h"
@@ -59,7 +60,6 @@ limitations under the License.
 
 #define mov(a,b) { free(a); a = b; b = NULL; }
 
-static void warn (const char* format, ...) UNUSED_VAR ;
 static char *job_owner(edg_wll_Context,char *);
 static edg_wll_ErrorCode get_job_parent(edg_wll_Context ctx, glite_jobid_const_t job, glite_jobid_t *parent);
 
@@ -90,6 +90,108 @@ static char* matched_substr(char *in, regmatch_t match)
        return s;
 }
 
+
+edg_wll_Event* fetch_history(edg_wll_Context ctx, edg_wll_JobStat *stat) {
+        edg_wll_QueryRec         jc0[2], *jc[2];
+        edg_wll_Event *events = NULL;
+
+        jc[0] = jc0;
+        jc[1] = NULL;
+        jc[0][0].attr = EDG_WLL_QUERY_ATTR_JOBID;
+        jc[0][0].op = EDG_WLL_QUERY_OP_EQUAL;
+        jc[0][0].value.j = stat->jobId;
+        jc[0][1].attr = EDG_WLL_QUERY_ATTR_UNDEF;
+
+        edg_wll_QueryEventsServer(ctx, 1, (const edg_wll_QueryRec **)jc, NULL, &events);
+
+        return events;
+}
+
+
+int collate_history(edg_wll_Context ctx, edg_wll_JobStat *stat, edg_wll_Event* events, int authz_flags) {
+        char                    *event_str = NULL, *history = NULL;
+        void *tmpptr;
+        size_t i;
+        size_t  size, len, maxsize = 1024, newsize;
+        char *olduser;
+        char *oldacluser;
+
+        if (!events || !events[0].type) {
+                history = strdup(HISTORY_EMPTY);
+        } else {
+                history = malloc(maxsize);
+                strcpy(history, HISTORY_HEADER);
+                size = HISTORY_HEADER_SIZE;
+
+                oldacluser = NULL;
+                olduser = NULL;
+                for (i = 0; events && events[i].type; i++) {
+
+                        if ((authz_flags & READ_ANONYMIZED) || (authz_flags & STATUS_FOR_MONITORING)) {
+                                //XXX Intorduce a better method of keeping track if more members/types are affected
+                                olduser=events[i].any.user;
+                                events[i].any.user=(authz_flags & STATUS_FOR_MONITORING) ? NULL : anonymize_string(ctx, events[i].any.user);
+                                if (events[i].any.type==EDG_WLL_EVENT_CHANGEACL) {
+                                        oldacluser=events[i].changeACL.user_id;
+                                        events[i].changeACL.user_id=(authz_flags & STATUS_FOR_MONITORING) ? NULL : anonymize_string(ctx, events[i].changeACL.user_id);
+                                }
+                        }
+
+                        if (edg_wll_UnparseEventJSON(ctx, events + i, &event_str) != 0) goto err;
+
+                        if (olduser) {
+                                free(events[i].any.user);
+                                events[i].any.user=olduser;
+                                olduser = NULL;
+                        }
+                        if (oldacluser) {
+                                free(events[i].changeACL.user_id);
+                                events[i].changeACL.user_id=oldacluser;
+                                oldacluser = NULL;
+                        }
+                        len = strlen(event_str);
+                        newsize = size + len + HISTORY_SEPARATOR_SIZE + HISTORY_FOOTER_SIZE + 1;
+                        if (newsize > maxsize) {
+                                maxsize <<= 1;
+                                if (newsize > maxsize) maxsize = newsize;
+                                if ((tmpptr = realloc(history, maxsize)) == NULL) {
+                                        edg_wll_SetError(ctx, ENOMEM, NULL);
+                                        goto err;
+                                }
+                                history = tmpptr;
+                        }
+                        strncpy(history + size, event_str, len + 1);
+                        size += len;
+                        if (events[i+1].type) {
+                                strcpy(history + size, HISTORY_SEPARATOR);
+                                size += HISTORY_SEPARATOR_SIZE;
+                        }
+                        free(event_str);
+                        event_str = NULL;
+                }
+                strcpy(history + size, HISTORY_FOOTER);
+                size += HISTORY_FOOTER_SIZE;
+                stat->history = history;
+                history = NULL;
+        }
+
+        err:
+        free(history);
+        return edg_wll_Error(ctx, NULL, NULL);
+}
+
+
+int clear_history(edg_wll_Context ctx, edg_wll_Event* events) {
+        int i;
+
+        for (i = 0; events && events[i].type; i++)
+                edg_wll_FreeEvent(&events[i]);
+        free(events);
+
+        return edg_wll_Error(ctx, NULL, NULL);
+}
+
+
 int edg_wll_JobStatusServer(
        edg_wll_Context ctx,
        glite_jobid_const_t             job,
@@ -381,6 +483,15 @@ int edg_wll_JobStatusServer(
                        }
                }
 #endif
+               if (flags & EDG_WLL_NOTIF_HISTORY) {
+                       edg_wll_Event* events = NULL;
+                       if (!(events = fetch_history(ctx, stat))) {
+                               edg_wll_SetError(ctx, ctx->errCode, "Error extracting job history");
+                       }
+                       else {
+                               collate_history(ctx, stat, events, authz_flags);
+                       }
+               }
 
                whole_cycle = 1;
 rollback:
@@ -654,24 +765,6 @@ err:
 }
 
 
-/*
- * Helper for warning printouts
- */
-
-static void warn(const char* format, ...)
-{
-       va_list l;
-       va_start(l, format);
-
-       /*
-       fprintf(stderr, "Warning: ");
-       vfprintf(stderr, format, l);
-       fputc('\n', stderr);
-       */
-
-       va_end(l);
-}
-
 static char *job_owner(edg_wll_Context ctx,char *md5_jobid)
 {
        char    *stmt = NULL,*out = NULL;
index 8bbc5a7..b1322a2 100644 (file)
@@ -25,6 +25,14 @@ limitations under the License.
 #include "glite/lb/intjobstat_supp.h"
 #include "glite/lbu/db.h"
 
+#define HISTORY_EMPTY "[]"
+#define HISTORY_HEADER "[\n"
+#define HISTORY_HEADER_SIZE 2
+#define HISTORY_FOOTER "\n]"
+#define HISTORY_FOOTER_SIZE 2
+#define HISTORY_SEPARATOR ",\n"
+#define HISTORY_SEPARATOR_SIZE 2
+
 int edg_wll_JobStatusServer(edg_wll_Context, glite_jobid_const_t, int, edg_wll_JobStat *);
 
 
@@ -61,4 +69,9 @@ int add_stringlist(char ***, const char *);
 edg_wll_ErrorCode edg_wll_GetSubjobHistogram(edg_wll_Context, glite_jobid_const_t parent_jobid, int *hist);
 edg_wll_ErrorCode edg_wll_StoreSubjobHistogram(edg_wll_Context, glite_jobid_const_t parent_jobid, intJobStat *ijs);
 
+edg_wll_Event* fetch_history(edg_wll_Context ctx, edg_wll_JobStat *stat);
+int collate_history(edg_wll_Context ctx, edg_wll_JobStat *stat, edg_wll_Event* events, int authz_flags);
+int clear_history();
+
+
 #endif /* GLITE_LB_LBS_JOBSTAT_H*/
index 5e6777a..8861d9b 100644 (file)
@@ -467,7 +467,7 @@ int edg_wll_NotificationToHTML(edg_wll_Context ctx UNUSED_VAR, notifInfo *ni, ch
 /* construct Message-Body of Response-Line for edg_wll_JobStatus */
 int edg_wll_GeneralJobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobStat stat, char **message, int text)
 {
-        char *out_tmp = NULL, *header = NULL, *out, *jdl = NULL, *rsl = NULL, *children = NULL, *chtemp;
+        char *out_tmp = NULL, *header = NULL, *out, *history = NULL, *jdl = NULL, *rsl = NULL, *children = NULL, *chtemp;
        time_t time;
 
         char *pomA = NULL;
@@ -486,6 +486,18 @@ int edg_wll_GeneralJobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobSt
 
         free(header);
 
+       if (stat.history) {
+               if (!text) asprintf(&history,"<h3>Job History</h3>\r\n<pre>%s</pre>\r\n", stat.history);
+               else {
+                       out_tmp = glite_lbu_EscapeULM(stat.history);
+                       asprintf(&history,"history=%s\n", out_tmp);
+                       free(out_tmp); }
+       }
+       else {
+               if (!text) asprintf(&history,"<H3><A HREF=\"%s?flags=history\">Display full history of this job</A></H3>\n", chtemp);
+               else asprintf(&history,"history=\n");
+       }
+
        add_row(&out, "Job", "Job ID", chtemp, chtemp, text);
        free(chtemp);
        chtemp = stat.parent_job ? edg_wlc_JobIdUnparse(stat.parent_job) : NULL;
@@ -510,6 +522,9 @@ int edg_wll_GeneralJobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobSt
                        break;
 //             case EDG_WLL_STAT_FILE_TRANSFER:
 //             case EDG_WLL_STAT_FILE_TRANSFER_COLLECTION:
+               default:
+                       // No handling for other job types
+                       break;
        }
 
        add_row(&out, "owner", "Owner", stat.owner, NULL, text);
@@ -595,7 +610,7 @@ int edg_wll_GeneralJobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobSt
        else if (text) asprintf(&jdl,"jdl=\n");
 
        if (stat.rsl) {
-               if (text) asprintf(&rsl,"<h3>RSL</h3>\r\n<pre>%s</pre>\r\n",stat.rsl);
+               if (!text) asprintf(&rsl,"<h3>RSL</h3>\r\n<pre>%s</pre>\r\n",stat.rsl);
                else asprintf(&rsl,"rsl=%s\n", stat.rsl);
        }
        else if (text) asprintf(&rsl,"rsl=\n");
@@ -609,8 +624,9 @@ int edg_wll_GeneralJobStatusToHTML(edg_wll_Context ctx UNUSED_VAR, edg_wll_JobSt
                }
        }
 
-       asprintf(&pomA, "%s%s%s%s%s", 
+       asprintf(&pomA, "%s%s%s%s%s%s", 
                        out,
+                       history ? history : "",
                        jdl ? jdl : "",
                        rsl ? rsl : "",
                        children ? children : "",
index c663e83..0190547 100644 (file)
@@ -26,7 +26,6 @@ limitations under the License.
 #include <assert.h>
 
 #include "glite/lb/context-int.h"
-#include "glite/lb/events_json.h"
 #include "glite/lbu/trio.h"
 #include "glite/lbu/log.h"
 
@@ -39,11 +38,9 @@ limitations under the License.
 #include "authz_policy.h"
 #include "get_events.h"
 #include "server_stats.h"
+#include "jobstat.h"
 
 static int notif_match_conditions(edg_wll_Context,const edg_wll_JobStat *,const edg_wll_JobStat *,const char *, int flags);
-static edg_wll_Event* fetch_history(edg_wll_Context ctx, edg_wll_JobStat *stat);
-static int collate_history(edg_wll_Context ctx, edg_wll_JobStat *stat, edg_wll_Event* events, int authz_flags);
-static int clear_history();
 
 int edg_wll_NotifExpired(edg_wll_Context,const char *);
 
@@ -315,113 +312,3 @@ int edg_wll_NotifCheckAuthz(edg_wll_Context ctx,edg_wll_JobStat *stat,
        return ret;
 }
 
-
-#define HISTORY_EMPTY "[]"
-#define HISTORY_HEADER "[\n"
-#define HISTORY_HEADER_SIZE 2
-#define HISTORY_FOOTER "\n]"
-#define HISTORY_FOOTER_SIZE 2
-#define HISTORY_SEPARATOR ",\n"
-#define HISTORY_SEPARATOR_SIZE 2
-
-static edg_wll_Event* fetch_history(edg_wll_Context ctx, edg_wll_JobStat *stat) {
-        edg_wll_QueryRec         jc0[2], *jc[2];
-        edg_wll_Event *events = NULL;
-
-       jc[0] = jc0;
-       jc[1] = NULL;
-       jc[0][0].attr = EDG_WLL_QUERY_ATTR_JOBID;
-       jc[0][0].op = EDG_WLL_QUERY_OP_EQUAL;
-       jc[0][0].value.j = stat->jobId;
-       jc[0][1].attr = EDG_WLL_QUERY_ATTR_UNDEF;
-
-       if (edg_wll_QueryEventsServer(ctx, 1, (const edg_wll_QueryRec **)jc, NULL, &events) == 0) {
-               glite_common_log(LOG_CATEGORY_LB_SERVER, LOG_PRIORITY_DEBUG, "NOTIFY: events fetched");
-
-       }
-
-       return events;
-}
-
-static int collate_history(edg_wll_Context ctx, edg_wll_JobStat *stat, edg_wll_Event* events, int authz_flags) {
-       char                    *event_str = NULL, *history = NULL;
-       void *tmpptr;
-       size_t i;
-        size_t  size, len, maxsize = 1024, newsize;
-       char *olduser;
-       char *oldacluser;
-
-       if (!events || !events[0].type) {
-               history = strdup(HISTORY_EMPTY);
-       } else {
-               history = malloc(maxsize);
-               strcpy(history, HISTORY_HEADER);
-               size = HISTORY_HEADER_SIZE;
-
-               oldacluser = NULL;
-               olduser = NULL;
-               for (i = 0; events && events[i].type; i++) {
-       
-                       if ((authz_flags & READ_ANONYMIZED) || (authz_flags & STATUS_FOR_MONITORING)) {
-                               //XXX Intorduce a better method of keeping track if more members/types are affected
-                               olduser=events[i].any.user;
-                               events[i].any.user=(authz_flags & STATUS_FOR_MONITORING) ? NULL : anonymize_string(ctx, events[i].any.user);
-                               if (events[i].any.type==EDG_WLL_EVENT_CHANGEACL) {
-                                       oldacluser=events[i].changeACL.user_id;
-                                       events[i].changeACL.user_id=(authz_flags & STATUS_FOR_MONITORING) ? NULL : anonymize_string(ctx, events[i].changeACL.user_id);
-                               }
-                       }
-
-                       if (edg_wll_UnparseEventJSON(ctx, events + i, &event_str) != 0) goto err;
-
-                       if (olduser) {
-                               free(events[i].any.user);
-                               events[i].any.user=olduser;
-                               olduser = NULL;
-                       }
-                       if (oldacluser) {
-                               free(events[i].changeACL.user_id);
-                               events[i].changeACL.user_id=oldacluser;
-                               oldacluser = NULL;
-                       }
-
-                       len = strlen(event_str);
-                       newsize = size + len + HISTORY_SEPARATOR_SIZE + HISTORY_FOOTER_SIZE + 1;
-                       if (newsize > maxsize) {
-                               maxsize <<= 1;
-                               if (newsize > maxsize) maxsize = newsize;
-                               if ((tmpptr = realloc(history, maxsize)) == NULL) {
-                                       edg_wll_SetError(ctx, ENOMEM, NULL);
-                                       goto err;
-                               }
-                               history = tmpptr;
-                       }
-                       strncpy(history + size, event_str, len + 1);
-                       size += len;
-                       if (events[i+1].type) {
-                               strcpy(history + size, HISTORY_SEPARATOR);
-                               size += HISTORY_SEPARATOR_SIZE;
-                       }
-                       free(event_str);
-                       event_str = NULL;
-               }
-               strcpy(history + size, HISTORY_FOOTER);
-               size += HISTORY_FOOTER_SIZE;
-               stat->history = history;
-               history = NULL;
-       }
-
-       err:
-       free(history);
-       return edg_wll_Error(ctx, NULL, NULL);
-}
-
-static int clear_history(edg_wll_Context ctx, edg_wll_Event* events) {
-       int i;
-
-       for (i = 0; events && events[i].type; i++)
-               edg_wll_FreeEvent(&events[i]);
-       free(events);
-
-       return edg_wll_Error(ctx, NULL, NULL);
-}