Skip to content

Commit c1bbfa5

Browse files
committed
update kline
1 parent c6f6af2 commit c1bbfa5

18 files changed

Lines changed: 208 additions & 30 deletions

accessws/aw_config.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,56 @@
77

88
struct settings settings;
99

10+
static int read_depth_merge_cfg(json_t *root, const char *key)
11+
{
12+
json_t *obj = json_object_get(root, key);
13+
if (obj == NULL || !json_is_object(obj))
14+
return -__LINE__;
15+
16+
settings.depth_market_count = json_object_size(obj);
17+
settings.depth_market_lists = malloc(sizeof(depth_merge_cfg) * settings.depth_market_count);
18+
19+
char *market;
20+
json_t *value;
21+
int index = 0;
22+
json_object_foreach(obj, market, value) {
23+
depth_merge_cfg *cfg = &settings.depth_market_lists[index++];
24+
cfg->market = strdup(market);
25+
if (!json_is_array(value))
26+
return -__LINE__;
27+
cfg->count = json_array_size(value);
28+
cfg->limit = malloc(sizeof(void *) * cfg->count);
29+
for (int i = 0; i < cfg->count; ++i) {
30+
const char *str = json_string_value(json_array_get(value, i));
31+
if (str == NULL)
32+
return -__LINE__;
33+
cfg->limit[i] = decimal(str, 0);
34+
if (cfg->limit[i] == NULL)
35+
return -__LINE__;
36+
}
37+
}
38+
39+
return 0;
40+
}
41+
42+
static int read_depth_limit_cfg(json_t *root, const char *key)
43+
{
44+
json_t *obj = json_object_get(root, key);
45+
if (obj == NULL || !json_is_array(obj))
46+
return -__LINE__;
47+
48+
settings.depth_limit.count = json_array_size(obj);
49+
settings.depth_limit.limit = malloc(sizeof(int) * settings.depth_limit.count);
50+
51+
for (int i = 0; i < settings.depth_limit.count; ++i) {
52+
settings.depth_limit.limit[i] = json_integer_value(json_array_get(obj, i));
53+
if (settings.depth_limit.limit[i] == 0)
54+
return -__LINE__;
55+
}
56+
57+
return 0;
58+
}
59+
1060
static int read_config_from_json(json_t *root)
1161
{
1262
int ret;
@@ -72,7 +122,8 @@ static int read_config_from_json(json_t *root)
72122
ERR_RET(read_cfg_real(root, "kline_interval", &settings.kline_interval, false, 0.5));
73123
ERR_RET(read_cfg_real(root, "depth_interval", &settings.depth_interval, false, 0.5));
74124

75-
ERR_RET(read_cfg_int(root, "depth_subscribe_max", &settings.depth_subscribe_max, false, 100));
125+
ERR_RET(read_depth_merge_cfg(root, "depth_merge"));
126+
ERR_RET(read_depth_limit_cfg(root, "depth_limit"));
76127

77128
return 0;
78129
}

accessws/aw_config.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444

4545
# define AW_LISTENER_BIND "seqpacket@/tmp/accessws_listener.sock"
4646

47+
typedef struct depth_merge_cfg {
48+
char *market;
49+
int count;
50+
mpd_t **limit;
51+
} depth_merge_cfg;
52+
53+
typedef struct depth_limit_cfg {
54+
int count;
55+
int *limit;
56+
} depth_limit_cfg;
57+
4758
struct settings {
4859
process_cfg process;
4960
log_cfg log;
@@ -67,7 +78,9 @@ struct settings {
6778
double kline_interval;
6879
double depth_interval;
6980

70-
int depth_subscribe_max;
81+
int depth_market_count;
82+
depth_merge_cfg *depth_market_lists;
83+
depth_limit_cfg depth_limit;
7184
};
7285

7386
extern struct settings settings;

accessws/aw_depth.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,46 @@ int init_depth(void)
387387
return 0;
388388
}
389389

390+
static bool is_good_limit(int limit)
391+
{
392+
for (int i = 0; i < settings.depth_limit.count; ++i) {
393+
if (settings.depth_limit.limit[i] == limit) {
394+
return true;
395+
}
396+
}
397+
398+
return false;
399+
}
400+
401+
static bool is_good_interval(const char *market, const char *interval)
402+
{
403+
mpd_t *merge = decimal(interval, 0);
404+
if (merge == NULL)
405+
return false;
406+
407+
for (int i = 0; i < settings.depth_market_count; ++i) {
408+
depth_merge_cfg *cfg = &settings.depth_market_lists[i];
409+
if (strcmp(cfg->market, market) != 0)
410+
continue;
411+
for (int j = 0; j < cfg->count; ++j) {
412+
if (mpd_cmp(cfg->limit[j], merge, &mpd_ctx) == 0) {
413+
mpd_del(merge);
414+
return true;
415+
}
416+
}
417+
}
418+
419+
mpd_del(merge);
420+
return false;
421+
}
422+
390423
int depth_subscribe(nw_ses *ses, const char *market, uint32_t limit, const char *interval)
391424
{
425+
if (!is_good_limit(limit))
426+
return -1;
427+
if (!is_good_interval(market, interval))
428+
return -1;
429+
392430
struct depth_key key;
393431
memset(&key, 0, sizeof(key));
394432
strncpy(key.market, market, MARKET_NAME_MAX_LEN - 1);
@@ -397,9 +435,6 @@ int depth_subscribe(nw_ses *ses, const char *market, uint32_t limit, const char
397435

398436
dict_entry *entry = dict_find(dict_depth, &key);
399437
if (entry == NULL) {
400-
if (dict_depth->used >= settings.depth_subscribe_max)
401-
return -__LINE__;
402-
403438
struct depth_val val;
404439
memset(&val, 0, sizeof(val));
405440

accessws/aw_server.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ static int process_cache(nw_ses *ses, uint64_t id, sds key)
150150
return 0;
151151
}
152152

153+
json_incref(cache->result);
153154
send_result(ses, id, cache->result);
155+
154156
return 1;
155157
}
156158

@@ -267,8 +269,12 @@ static int on_method_depth_subscribe(nw_ses *ses, uint64_t id, struct clt_info *
267269
return send_error_invalid_argument(ses, id);
268270

269271
depth_unsubscribe(ses);
270-
if (depth_subscribe(ses, market, limit, interval) < 0)
272+
int ret = depth_subscribe(ses, market, limit, interval);
273+
if (ret == -1) {
274+
return send_error_invalid_argument(ses, id);
275+
} else if (ret < 0) {
271276
return send_error_internal_error(ses, id);
277+
}
272278

273279
send_success(ses, id);
274280
depth_send_clean(ses, market, limit, interval);

accessws/config.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,11 @@
6363
},
6464
"backend_timeout": 1.0,
6565
"cache_timeout": 10.0,
66-
"auth_url": "http://192.168.1.6:8000/internal/exchange/user/auth"
66+
"auth_url": "http://192.168.1.6:8000/internal/exchange/user/auth",
67+
"depth_merge": {
68+
"BTCCNY": ["0", "1", "5", "10"],
69+
"ETHCNY": ["0", "0.1", "1", "5"],
70+
"ETHBTC": ["0", "0.00001", "0.0001", "0.0005"]
71+
},
72+
"depth_limit": [1, 5, 10, 20, 30, 50, 100]
6773
}

marketprice/mp_config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ static int read_config_from_json(json_t *root)
4646
return -__LINE__;
4747
}
4848

49-
read_cfg_int(root, "timezone", &settings.timezone, false, -28800);
5049
read_cfg_int(root, "sec_max", &settings.sec_max, false, 86400 * 7);
5150
read_cfg_int(root, "min_max", &settings.min_max, false, 60 * 24 * 365);
5251
read_cfg_int(root, "hour_max", &settings.hour_max, false, 24 * 365 * 10);
5352

53+
settings.timezone = get_timezone_offset();
54+
5455
return 0;
5556
}
5657

marketprice/mp_message.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,17 @@ static void add_update(struct market_info *info, int type, time_t timestamp)
355355
dict_add(info->update, &key, NULL);
356356
}
357357

358+
static time_t get_day_start(time_t timestamp)
359+
{
360+
struct tm *timeinfo = localtime(&timestamp);
361+
struct tm dtm;
362+
memset(&dtm, 0, sizeof(dtm));
363+
dtm.tm_year = timeinfo->tm_year;
364+
dtm.tm_mon = timeinfo->tm_mon;
365+
dtm.tm_mday = timeinfo->tm_mday;
366+
return mktime(&dtm);
367+
}
368+
358369
static int market_update(const char *market, double timestamp, mpd_t *price, mpd_t *amount, int side, uint64_t id)
359370
{
360371
struct market_info *info = market_query(market);
@@ -415,7 +426,7 @@ static int market_update(const char *market, double timestamp, mpd_t *price, mpd
415426
add_update(info, KLINE_HOUR, time_hour);
416427

417428
// update day
418-
time_t time_day = time_sec / 86400 * 86400 + settings.timezone;
429+
time_t time_day = get_day_start(time_sec);
419430
entry = dict_find(info->day, &time_day);
420431
if (entry) {
421432
kinfo = entry->val;
@@ -935,7 +946,7 @@ json_t *get_market_status_today(const char *market)
935946

936947
json_t *result = json_object();
937948
time_t now = time(NULL);
938-
time_t start = now / 86400 * 86400 + settings.timezone;
949+
time_t start = get_day_start(now);
939950
struct kline_info *klast = get_last_kline(info->day, start - 86400, start - 86400 * 30, 86400);
940951
dict_entry *entry = dict_find(info->day, &start);
941952
if (entry) {
@@ -1072,11 +1083,10 @@ json_t *get_market_kline_hour(const char *market, time_t start, time_t end, int
10721083
time_t start_min = now / 3600 * 3600 - settings.hour_max * 3600;
10731084
if (start < start_min)
10741085
start = start_min;
1075-
time_t base = start / 86400 * 86400 - 86400 + settings.timezone;
1076-
while (base <= start) {
1086+
time_t base = get_day_start(start);
1087+
while ((base + interval) <= start)
10771088
base += interval;
1078-
}
1079-
start = base - interval;
1089+
start = base;
10801090

10811091
struct kline_info *klast = get_last_kline(info->hour, start - 3600, start_min, 3600);
10821092
int step = interval / 3600;
@@ -1112,10 +1122,7 @@ json_t *get_market_kline_day(const char *market, time_t start, time_t end, int i
11121122
return NULL;
11131123

11141124
json_t *result = json_array();
1115-
time_t base = start / interval * interval + settings.timezone;
1116-
if (base > start)
1117-
base -= interval;
1118-
start = base;
1125+
start = (start - settings.timezone) / interval * interval + settings.timezone;
11191126

11201127
struct kline_info *klast = get_last_kline(info->day, start - 86400, start - 86400 * 30, 86400);
11211128
int step = interval / 86400;
@@ -1151,10 +1158,10 @@ json_t *get_market_kline_week(const char *market, time_t start, time_t end, int
11511158
return NULL;
11521159

11531160
json_t *result = json_array();
1154-
time_t base = start / interval * interval - 3 * 86400 + settings.timezone;
1155-
while (base <= start)
1161+
time_t base = (start - settings.timezone) / interval * interval - 3 * 86400 + settings.timezone;
1162+
while ((base + interval) <= start)
11561163
base += interval;
1157-
start = base - interval;
1164+
start = base;
11581165

11591166
struct kline_info *klast = get_last_kline(info->day, start - 86400, start - 86400 * 30, 86400);
11601167
int step = interval / 86400;

matchengine/me_cli.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@
99
# include "me_market.h"
1010
# include "me_trade.h"
1111
# include "me_persist.h"
12+
# include "me_operlog.h"
13+
# include "me_history.h"
14+
# include "me_message.h"
1215

1316
static cli_svr *svr;
1417

18+
static sds on_cmd_status(const char *cmd, int argc, sds *argv)
19+
{
20+
sds reply = sdsempty();
21+
reply = market_status(reply);
22+
reply = operlog_status(reply);
23+
reply = history_status(reply);
24+
reply = message_status(reply);
25+
return reply;
26+
}
27+
1528
static sds on_cmd_balance_list(const char *cmd, int argc, sds *argv)
1629
{
1730
sds reply = sdsempty();
@@ -169,6 +182,7 @@ int init_cli(void)
169182
return -__LINE__;
170183
}
171184

185+
cli_svr_add_cmd(svr, "status", on_cmd_status);
172186
cli_svr_add_cmd(svr, "balance", on_cmd_balance);
173187
cli_svr_add_cmd(svr, "market", on_cmd_market);
174188
cli_svr_add_cmd(svr, "makeslice", on_cmd_makeslice);

matchengine/me_history.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,8 @@ bool is_history_block(void)
322322
return false;
323323
}
324324

325+
sds history_status(sds reply)
326+
{
327+
return sdscatprintf(reply, "history pending %d\n", job->request_count);
328+
}
329+

matchengine/me_history.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int append_order_deal_history(double t, uint64_t ask, int ask_role, uint64_t bid
1818
int append_user_balance_history(double t, uint32_t user_id, const char *asset, const char *business, mpd_t *change, const char *detail);
1919

2020
bool is_history_block(void);
21+
sds history_status(sds reply);
2122

2223
# endif
2324

0 commit comments

Comments
 (0)