From fe93a914e111888c5eda51b1b176b5209aacb1c1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 22 Mar 2015 21:04:59 +1100 Subject: [PATCH] Add API function to get worker stats --- src/stratifier.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/stratifier.c b/src/stratifier.c index a0099030..5939a982 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2451,13 +2451,51 @@ static void getuser(sdata_t *sdata, const char *buf, int *sockd) "user", user->username, "id", user->id, "workers", user->workers, "bestdiff", user->best_diff, "dsps1", user->dsps1, "dsps5", user->dsps5, "dsps60", user->dsps60, "dsps1440", user->dsps1440, "dsps10080", user->dsps10080, - "lastshare", user->last_update.tv_sec); + "lastshare", user->last_share.tv_sec); out: free(username); send_api_response(val, *sockd); _Close(sockd); } +static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername); + +static void getworker(sdata_t *sdata, const char *buf, int *sockd) +{ + char *tmp, *username, *workername = NULL; + worker_instance_t *worker; + user_instance_t *user; + json_error_t err_val; + json_t *val = NULL; + + val = json_loads(buf, 0, &err_val); + if (unlikely(!val)) { + val = json_encode_errormsg(&err_val); + goto out; + } + if (!json_get_string(&workername, val, "worker")) { + val = json_errormsg("Failed to find worker key"); + goto out; + } + if (!strlen(workername)) { + val = json_errormsg("Zero length worker key"); + goto out; + } + tmp = strdupa(workername); + username = strsep(&tmp, "._"); + user = get_user(sdata, username); + worker = get_worker(sdata, user, workername); + JSON_CPACK(val, "{ss,ss,sI,sf,sf,sf,sf,si,sf,si,sb}", + "user", username, "worker", workername, "id", user->id, + "dsps1", worker->dsps1, "dsps5", worker->dsps5, "dsps60", worker->dsps60, + "dsps1440", worker->dsps1440, "lastshare", worker->last_share.tv_sec, + "bestdiff", worker->best_diff, "mindiff", worker->mindiff, "idle", worker->idle); +out: + free(workername); + send_api_response(val, *sockd); + _Close(sockd); +} + /* Return the user masked priority value of the proxy */ static int proxy_prio(const proxy_t *proxy) { @@ -2639,6 +2677,10 @@ retry: getuser(sdata, buf + 8, &sockd); goto retry; } + if (cmdmatch(buf, "getworker")) { + getworker(sdata, buf + 10, &sockd); + goto retry; + } if (cmdmatch(buf, "getproxy")) { getproxy(sdata, buf + 9, &sockd); goto retry;