Browse Source

ckdb/web - add pool/user elapsed and use them

master
kanoi 11 years ago
parent
commit
f0e68485fe
  1. 28
      pool/page.php
  2. 20
      sql/ckdb.sql
  3. 40
      sql/v0.1-v0.2.sql
  4. 63
      src/ckdb.c

28
pool/page.php

@ -133,18 +133,22 @@ function pgtop($dotop, $user, $douser)
$now = time();
if (isset($info['p_hashrate5m']))
{
$phr = $info['p_hashrate5m'];
if ($phr == '?')
$phr = '?THs';
if (isset($info['p_elapsed'])
and isset($info['p_hashrate1hr'])
and $info['p_elapsed'] > 3600)
$phr = $info['p_hashrate1hr'];
if ($phr == '?')
$phr = '?THs';
else
{
$phr /= 10000000;
if ($phr < 100000)
$phr = (round($phr)/100).'GHs';
else
{
$phr /= 10000000;
if ($phr < 100000)
$phr = (round($phr)/100).'GHs';
else
$phr = (round($phr/1000)/100).'THs';
}
$phr = (round($phr/1000)/100).'THs';
}
if (isset($info['lastblock']))
@ -203,7 +207,9 @@ function pgtop($dotop, $user, $douser)
}
}
if (isset($info['u_hashrate1hr']))
if (isset($info['u_hashrate1hr'])
and isset($info['u_elapsed'])
and $info['u_elapsed'] > 3600)
{
$u1hr = $info['u_hashrate1hr'];
if ($u1hr == '?')

20
sql/ckdb.sql

@ -331,6 +331,7 @@ CREATE TABLE auths (
CREATE TABLE poolstats (
poolinstance character varying(256) NOT NULL,
elapsed bigint NOT NULL,
users integer NOT NULL,
workers integer NOT NULL,
hashrate float NOT NULL,
@ -345,10 +346,27 @@ CREATE TABLE poolstats (
);
-- memory only
CREATE TABLE userstats (
poolinstance character varying(256) NOT NULL,
userid bigint NOT NULL,
elapsed bigint NOT NULL,
hashrate float NOT NULL,
hashrate5m float NOT NULL,
hashrate1hr float NOT NULL,
hashrate24hr float NOT NULL,
createdate timestamp with time zone NOT NULL,
createby character varying(64) DEFAULT ''::character varying NOT NULL,
createcode character varying(128) DEFAULT ''::character varying NOT NULL,
createinet character varying(128) DEFAULT ''::character varying NOT NULL,
PRIMARY KEY (poolinstance, userid, createdate)
);
CREATE TABLE version (
vlock integer NOT NULL,
version character varying(256) NOT NULL,
PRIMARY KEY (vlock)
);
insert into version (vlock,version) values (1,'0.1');
insert into version (vlock,version) values (1,'0.2');

40
sql/v0.1-v0.2.sql

@ -0,0 +1,40 @@
SET SESSION AUTHORIZATION 'postgres';
BEGIN transaction;
DO $$
DECLARE ver TEXT;
BEGIN
UPDATE version set version='0.2' where vlock=1 and version='0.1';
IF found THEN
RETURN;
END IF;
SELECT version into ver from version
WHERE vlock=1;
RAISE EXCEPTION 'Wrong DB version - expect "0.1" - found "%"', ver;
END $$;
ALTER TABLE ONLY poolstats
ADD COLUMN elapsed bigint DEFAULT 0 NOT NULL;
CREATE TABLE userstats (
poolinstance character varying(256) NOT NULL,
userid bigint NOT NULL,
elapsed bigint DEFAULT 0 NOT NULL,
hashrate float NOT NULL,
hashrate5m float NOT NULL,
hashrate1hr float NOT NULL,
hashrate24hr float NOT NULL,
createdate timestamp with time zone NOT NULL,
createby character varying(64) DEFAULT ''::character varying NOT NULL,
createcode character varying(128) DEFAULT ''::character varying NOT NULL,
createinet character varying(128) DEFAULT ''::character varying NOT NULL,
PRIMARY KEY (poolinstance, userid, createdate)
);
END transaction;

63
src/ckdb.c

@ -849,6 +849,7 @@ static K_STORE *auths_store;
typedef struct poolstats {
char poolinstance[TXT_BIG+1];
int64_t elapsed;
int32_t users;
int32_t workers;
double hashrate;
@ -871,6 +872,7 @@ static K_STORE *poolstats_store;
// TODO: When to discard?
typedef struct userstats {
char poolinstance[TXT_BIG+1];
int64_t elapsed;
int64_t userid;
double hashrate;
double hashrate5m;
@ -2918,8 +2920,9 @@ static double cmp_poolstats(K_ITEM *a, K_ITEM *b)
return c;
}
static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, char *users,
char *workers, char *hashrate, char *hashrate5m,
static bool poolstats_add(PGconn *conn, bool store, char *poolinstance,
char *elapsed, char *users, char *workers,
char *hashrate, char *hashrate5m,
char *hashrate1hr, char *hashrate24hr,
tv_t *now, char *by, char *code, char *inet)
{
@ -2929,7 +2932,7 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, char *us
int n;
POOLSTATS *row;
char *ins;
char *params[7 + SIMPLEDATECOUNT];
char *params[8 + SIMPLEDATECOUNT];
int par;
bool ok = false;
@ -2942,6 +2945,7 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, char *us
row = DATA_POOLSTATS(p_item);
STRNCPY(row->poolinstance, poolinstance);
TXT_TO_BIGINT("elapsed", elapsed, row->elapsed);
TXT_TO_INT("users", users, row->users);
TXT_TO_INT("workers", workers, row->workers);
TXT_TO_DOUBLE("hashrate", hashrate, row->hashrate);
@ -2955,6 +2959,7 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, char *us
par = 0;
if (store) {
params[par++] = str_to_buf(row->poolinstance, NULL, 0);
params[par++] = bigint_to_buf(row->elapsed, NULL, 0);
params[par++] = int_to_buf(row->users, NULL, 0);
params[par++] = int_to_buf(row->workers, NULL, 0);
params[par++] = bigint_to_buf(row->hashrate, NULL, 0);
@ -2965,7 +2970,8 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, char *us
PARCHK(par, params);
ins = "insert into poolstats "
"(poolinstance,users,workers,hashrate,hashrate5m,hashrate1hr,hashrate24hr"
"(poolinstance,elapsed,users,workers,hashrate,"
"hashrate5m,hashrate1hr,hashrate24hr"
SIMPLEDATECONTROL ") values (" PQPARAM11 ")";
res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0);
@ -3138,9 +3144,10 @@ static double cmp_userstats(K_ITEM *a, K_ITEM *b)
return c;
}
static bool userstats_add(char *poolinstance, char *username, char *hashrate,
char *hashrate5m, char *hashrate1hr, char *hashrate24hr,
tv_t *now, char *by, char *code, char *inet)
static bool userstats_add(char *poolinstance, char *elapsed, char *username,
char *hashrate, char *hashrate5m, char *hashrate1hr,
char *hashrate24hr, tv_t *now, char *by, char *code,
char *inet)
{
K_ITEM *s_item, *u_item;
USERSTATS *row;
@ -3154,6 +3161,7 @@ static bool userstats_add(char *poolinstance, char *username, char *hashrate,
row = DATA_USERSTATS(s_item);
STRNCPY(row->poolinstance, poolinstance);
TXT_TO_BIGINT("elapsed", elapsed, row->elapsed);
u_item = find_users(username);
if (!u_item)
return false;
@ -3436,8 +3444,9 @@ static char *cmd_poolstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
// log to logfile
K_ITEM *i_poolinstance, *i_users, *i_workers, *i_hashrate, *i_hashrate5m;
K_ITEM *i_hashrate1hr, *i_hashrate24hr, *i_createdate, look, *ps;
K_ITEM *i_poolinstance, *i_elapsed, *i_users, *i_workers;
K_ITEM *i_hashrate, *i_hashrate5m, *i_hashrate1hr, *i_hashrate24hr;
K_ITEM *i_createdate, look, *ps;
tv_t createdate;
POOLSTATS row;
bool ok = false;
@ -3448,6 +3457,10 @@ static char *cmd_poolstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
if (!i_poolinstance)
return strdup(reply);
i_elapsed = require_name("elapsed", 1, NULL, reply, siz);
if (!i_elapsed)
return strdup(reply);
i_users = require_name("users", 1, NULL, reply, siz);
if (!i_users)
return strdup(reply);
@ -3492,6 +3505,7 @@ static char *cmd_poolstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
conn = dbconnect();
ok = poolstats_add(conn, store, DATA_TRANSFER(i_poolinstance)->data,
DATA_TRANSFER(i_elapsed)->data,
DATA_TRANSFER(i_users)->data,
DATA_TRANSFER(i_workers)->data,
DATA_TRANSFER(i_hashrate)->data,
@ -3517,8 +3531,8 @@ static char *cmd_userstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
// log to logfile
K_ITEM *i_poolinstance, *i_username, *i_hashrate, *i_hashrate5m;
K_ITEM *i_hashrate1hr, *i_hashrate24hr;
K_ITEM *i_poolinstance, *i_elapsed, *i_username, *i_hashrate;
K_ITEM *i_hashrate5m, *i_hashrate1hr, *i_hashrate24hr;
bool ok = false;
LOGDEBUG("%s(): cmd '%s'", __func__, cmd);
@ -3527,6 +3541,10 @@ static char *cmd_userstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
if (!i_poolinstance)
return strdup(reply);
i_elapsed = require_name("elapsed", 1, NULL, reply, siz);
if (!i_elapsed)
return strdup(reply);
i_username = require_name("username", 1, NULL, reply, siz);
if (!i_username)
return strdup(reply);
@ -3548,6 +3566,7 @@ static char *cmd_userstats(char *cmd, __maybe_unused char *id, tv_t *now, char *
return strdup(reply);
ok = userstats_add(DATA_TRANSFER(i_poolinstance)->data,
DATA_TRANSFER(i_elapsed)->data,
DATA_TRANSFER(i_username)->data,
DATA_TRANSFER(i_hashrate)->data,
DATA_TRANSFER(i_hashrate5m)->data,
@ -4009,9 +4028,18 @@ static char *cmd_homepage(char *cmd, char *id, __maybe_unused tv_t *now, __maybe
double_to_buf(DATA_POOLSTATS(p_item)->hashrate5m, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "p_hashrate5m=%s%c", reply, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
double_to_buf(DATA_POOLSTATS(p_item)->hashrate1hr, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "p_hashrate1hr=%s%c", reply, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
bigint_to_buf(DATA_POOLSTATS(p_item)->elapsed, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "p_elapsed=%s%c", reply, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
} else {
snprintf(tmp, sizeof(tmp), "users=?%cworkers=?%cp_hashrate5m=?%c",
FLDSEP, FLDSEP, FLDSEP);
snprintf(tmp, sizeof(tmp), "users=?%cworkers=?%cp_hashrate5m=?%c"
"p_hashrate1hr=?%cp_elapsed=?%c",
FLDSEP, FLDSEP, FLDSEP, FLDSEP, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
}
@ -4036,10 +4064,15 @@ static char *cmd_homepage(char *cmd, char *id, __maybe_unused tv_t *now, __maybe
APPEND_REALLOC(buf, off, len, tmp);
double_to_buf(DATA_USERSTATS(us_item)->hashrate1hr, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "u_hashrate1hr=%s", reply);
snprintf(tmp, sizeof(tmp), "u_hashrate1hr=%s%c", reply, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
bigint_to_buf(DATA_USERSTATS(us_item)->elapsed, reply, sizeof(reply));
snprintf(tmp, sizeof(tmp), "u_elapsed=%s", reply);
APPEND_REALLOC(buf, off, len, tmp);
} else {
snprintf(tmp, sizeof(tmp), "u_hashrate5m=?%cu_hashrate1hr=?", FLDSEP);
snprintf(tmp, sizeof(tmp), "u_hashrate5m=?%cu_hashrate1hr=?%cu_elapsed=?",
FLDSEP, FLDSEP);
APPEND_REALLOC(buf, off, len, tmp);
}

Loading…
Cancel
Save