diff --git a/common/branch-mgr.c b/common/branch-mgr.c index 4539318e..2549199f 100644 --- a/common/branch-mgr.c +++ b/common/branch-mgr.c @@ -320,11 +320,18 @@ get_commit_id (SeafDBRow *row, void *data) static void publish_repo_update_event (const char *repo_id, const char *commit_id) { - char buf[128]; - snprintf (buf, sizeof(buf), "repo-update\t%s\t%s", - repo_id, commit_id); + json_t *msg = json_object (); + char *msg_str = NULL; - seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, buf); + json_object_set_new (msg, "msg_type", json_string("repo-update")); + json_object_set_new (msg, "repo_id", json_string(repo_id)); + json_object_set_new (msg, "commit_id", json_string(commit_id)); + + msg_str = json_dumps (msg, JSON_PRESERVE_ORDER); + + seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, msg_str); + g_free (msg_str); + json_decref (msg); } static void diff --git a/fileserver/sync_api.go b/fileserver/sync_api.go index b76b10f0..c96c2d83 100644 --- a/fileserver/sync_api.go +++ b/fileserver/sync_api.go @@ -81,7 +81,7 @@ type repoEventData struct { clientName string } -type statusEventData struct { +type statsEventData struct { eType string user string repoID string @@ -904,16 +904,23 @@ func getRepoStoreID(repoID string) (string, error) { } func sendStatisticMsg(repoID, user, operation string, bytes uint64) { - rData := &statusEventData{operation, user, repoID, bytes} + rData := &statsEventData{operation, user, repoID, bytes} - publishStatusEvent(rData) + publishStatsEvent(rData) } -func publishStatusEvent(rData *statusEventData) { - buf := fmt.Sprintf("%s\t%s\t%s\t%d", - rData.eType, rData.user, - rData.repoID, rData.bytes) - if _, err := rpcclient.Call("publish_event", seafileServerChannelStats, buf); err != nil { +func publishStatsEvent(rData *statsEventData) { + data := make(map[string]interface{}) + data["msg_type"] = rData.eType + data["user_name"] = rData.user + data["repo_id"] = rData.repoID + data["bytes"] = rData.bytes + jsonData, err := json.Marshal(data) + if err != nil { + log.Warnf("Failed to publish event: %v", err) + return + } + if _, err := rpcclient.Call("publish_event", seafileServerChannelStats, string(jsonData)); err != nil { log.Warnf("Failed to publish event: %v", err) } } @@ -1374,17 +1381,34 @@ func publishRepoEvent(rData *repoEventData) { if rData.path == "" { rData.path = "/" } - buf := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s", - rData.eType, rData.user, rData.ip, - rData.clientName, rData.repoID, rData.path) - if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, buf); err != nil { + data := make(map[string]interface{}) + data["msg_type"] = rData.eType + data["user_name"] = rData.user + data["ip"] = rData.ip + data["user_agent"] = rData.clientName + data["repo_id"] = rData.repoID + data["file_path"] = rData.path + jsonData, err := json.Marshal(data) + if err != nil { + log.Warnf("Failed to publish event: %v", err) + return + } + if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, string(jsonData)); err != nil { log.Warnf("Failed to publish event: %v", err) } } func publishUpdateEvent(repoID string, commitID string) { - buf := fmt.Sprintf("repo-update\t%s\t%s", repoID, commitID) - if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, buf); err != nil { + data := make(map[string]interface{}) + data["msg_type"] = "repo-update" + data["repo_id"] = repoID + data["commit_id"] = commitID + jsonData, err := json.Marshal(data) + if err != nil { + log.Warnf("Failed to publish event: %v", err) + return + } + if _, err := rpcclient.Call("publish_event", seafileServerChannelEvent, string(jsonData)); err != nil { log.Warnf("Failed to publish event: %v", err) } } diff --git a/server/http-server.c b/server/http-server.c index 2930764d..fe128dad 100644 --- a/server/http-server.c +++ b/server/http-server.c @@ -541,28 +541,49 @@ free_stats_event_data (StatsEventData *data) static void publish_repo_event (RepoEventData *rdata) { - GString *buf = g_string_new (NULL); - g_string_printf (buf, "%s\t%s\t%s\t%s\t%s\t%s", - rdata->etype, rdata->user, rdata->ip, - rdata->client_name ? rdata->client_name : "", - rdata->repo_id, rdata->path ? rdata->path : "/"); + json_t *msg = json_object (); + char *msg_str = NULL; + + json_object_set_new (msg, "msg_type", json_string(rdata->etype)); + json_object_set_new (msg, "user_name", json_string(rdata->user)); + json_object_set_new (msg, "ip", json_string(rdata->ip)); + if (rdata->client_name) { + json_object_set_new (msg, "user_agent", json_string(rdata->client_name)); + } else { + json_object_set_new (msg, "user_agent", json_string("")); + } + json_object_set_new (msg, "repo_id", json_string(rdata->repo_id)); + if (rdata->path) { + json_object_set_new (msg, "file_path", json_string(rdata->path)); + } else { + json_object_set_new (msg, "file_path", json_string("/")); + } - seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, buf->str); + msg_str = json_dumps (msg, JSON_PRESERVE_ORDER); - g_string_free (buf, TRUE); + seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_EVENT, msg_str); + + g_free (msg_str); + json_decref (msg); } static void publish_stats_event (StatsEventData *rdata) { - GString *buf = g_string_new (NULL); - g_string_printf (buf, "%s\t%s\t%s\t%"G_GUINT64_FORMAT, - rdata->etype, rdata->user, - rdata->repo_id, rdata->bytes); + json_t *msg = json_object (); + char *msg_str = NULL; + + json_object_set_new (msg, "msg_type", json_string(rdata->etype)); + json_object_set_new (msg, "user_name", json_string(rdata->user)); + json_object_set_new (msg, "repo_id", json_string(rdata->repo_id)); + json_object_set_new (msg, "bytes", json_integer(rdata->bytes)); + + msg_str = json_dumps (msg, JSON_PRESERVE_ORDER); - seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_STATS, buf->str); + seaf_mq_manager_publish_event (seaf->mq_mgr, SEAFILE_SERVER_CHANNEL_STATS, msg_str); - g_string_free (buf, TRUE); + g_free (msg_str); + json_decref (msg); } static void