Skip to content

Commit 333b540

Browse files
committed
Add getStatistics to JavaScript
1 parent e9a503a commit 333b540

1 file changed

Lines changed: 148 additions & 24 deletions

File tree

src/napi/addon.c

Lines changed: 148 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define JS_GET_OBJECT_TYPE ((CWS_CONFIG *)(js_cws_instance->soap_internal->user))->object_type
4747
#define JS_GET_OBJECT_NAME ((CWS_CONFIG *)(js_cws_instance->soap_internal->user))->object_name
4848
#define JS_GET_OBJECT_TYPE ((CWS_CONFIG *)(js_cws_instance->soap_internal->user))->object_type
49+
#define JS_GET_CWS_CONFIG ((CWS_CONFIG *)(js_cws_instance->soap_internal->user))
4950
#define JS_GET_ERROR ((CWS_CONFIG *)(js_cws_instance->soap_internal->user))->internal_soap_error
5051

5152
#define ERR js_cws_instance->err
@@ -88,6 +89,41 @@ struct cws_js_err_t {
8889
char err[16];
8990
};
9091

92+
#define SET_JS_CWS_UINT32_STAT(s) {#s, offsetof(struct statistics_t, s)}
93+
struct js_cws_uint32_stat_t {
94+
const char *name;
95+
size_t offset;
96+
} JS_CWS_UINT32_STAT[] = {
97+
SET_JS_CWS_UINT32_STAT(costs),
98+
SET_JS_CWS_UINT32_STAT(strings),
99+
SET_JS_CWS_UINT32_STAT(shorts),
100+
SET_JS_CWS_UINT32_STAT(ints),
101+
SET_JS_CWS_UINT32_STAT(long64s),
102+
SET_JS_CWS_UINT32_STAT(enums),
103+
SET_JS_CWS_UINT32_STAT(arrays),
104+
SET_JS_CWS_UINT32_STAT(booleans),
105+
SET_JS_CWS_UINT32_STAT(doubles),
106+
SET_JS_CWS_UINT32_STAT(date_times),
107+
SET_JS_CWS_UINT32_STAT(measures),
108+
SET_JS_CWS_UINT32_STAT(event_types),
109+
SET_JS_CWS_UINT32_STAT(total),
110+
{NULL}
111+
};
112+
113+
#define SET_JS_CWS_UINT64_STAT(s) SET_JS_CWS_UINT32_STAT(s)
114+
115+
struct js_cws_uint64_stat_t {
116+
const char *name;
117+
size_t offset;
118+
} JS_CWS_UINT64_STAT[] = {
119+
SET_JS_CWS_UINT64_STAT(used_memory),
120+
{NULL}
121+
};
122+
123+
#undef SET_JS_CWS_UINT64_STAT
124+
#undef SET_JS_CWS_UINT32_STAT
125+
126+
91127
#define SET_JS_FN_CALL(fn) {#fn, c_##fn}
92128
typedef struct cws_js_fn_call_t {
93129
const char *function_name;
@@ -125,6 +161,40 @@ struct cws_js_int32_t {
125161
{NULL}
126162
};
127163

164+
static int cws_set_uint32_stat_list(napi_env env, napi_value exports, struct statistics_t *stat, struct js_cws_uint32_stat_t *list)
165+
{
166+
napi_value int32;
167+
168+
while (list->name) {
169+
if (napi_create_uint32(env, (uint32_t)*((uint32_t *)(((size_t)stat)+list->offset)), &int32)!=napi_ok)
170+
return 650;
171+
172+
if (napi_set_named_property(env, exports, list->name, int32)!=napi_ok)
173+
return 651;
174+
175+
list++;
176+
}
177+
178+
return 0;
179+
}
180+
181+
static int cws_set_uint64_stat_list(napi_env env, napi_value exports, struct statistics_t *stat, struct js_cws_uint64_stat_t *list)
182+
{
183+
napi_value uint64;
184+
185+
while (list->name) {
186+
if (napi_create_bigint_uint64(env, (uint64_t)*((uint64_t *)(((size_t)stat)+list->offset)), &uint64)!=napi_ok)
187+
return 650;
188+
189+
if (napi_set_named_property(env, exports, list->name, uint64)!=napi_ok)
190+
return 651;
191+
192+
list++;
193+
}
194+
195+
return 0;
196+
}
197+
128198
#define TEXT_BUF_ALLOC_MAX_SZ 2048
129199
static char *textBufAlloc(size_t len)
130200
{
@@ -288,17 +358,15 @@ static int cws_add_function_util(napi_value *fn_out, napi_env env, napi_value ex
288358
return 0;
289359
}
290360

291-
static int cws_add_int32_util(napi_value *int32_out, napi_env env, napi_value exports, struct cws_js_int32_t *value)
361+
static int cws_add_int32_util(napi_env env, napi_value exports, struct cws_js_int32_t *value)
292362
{
293-
napi_value *int32Tmp, int32;
294-
295-
int32Tmp=(int32_out)?int32_out:&int32;
363+
napi_value int32;
296364

297365
while (value->name) {
298-
if (napi_create_int32(env, value->value, int32Tmp)!=napi_ok)
366+
if (napi_create_int32(env, value->value, &int32)!=napi_ok)
299367
return 600;
300368

301-
if (napi_set_named_property(env, exports, value->name, *int32Tmp)!=napi_ok)
369+
if (napi_set_named_property(env, exports, value->name, int32)!=napi_ok)
302370
return 601;
303371

304372
value++;
@@ -309,15 +377,17 @@ static int cws_add_int32_util(napi_value *int32_out, napi_env env, napi_value ex
309377

310378
static int cws_add_int32_object_list_util(napi_value *int32_out, const char *objName, napi_env env, napi_value exports, struct cws_js_int32_t *value)
311379
{
312-
napi_value obj;
380+
napi_value obj, *objTmp;
313381

314-
if (napi_create_object(env, &obj)!=napi_ok)
382+
objTmp=(int32_out)?int32_out:&obj;
383+
384+
if (napi_create_object(env, objTmp)!=napi_ok)
315385
return 602;
316386

317-
if (napi_set_named_property(env, exports, objName, obj)!=napi_ok)
387+
if (napi_set_named_property(env, exports, objName, *objTmp)!=napi_ok)
318388
return 603;
319389

320-
return cws_add_int32_util(int32_out, env, obj, value);
390+
return cws_add_int32_util(env, obj, value);
321391
}
322392

323393
inline int js_cws_new_array_buffer(napi_value *dest, napi_env env, void *src, size_t src_sz)
@@ -837,18 +907,72 @@ napi_value c_getJson(napi_env env, napi_callback_info info)
837907
return res;
838908
}
839909

910+
napi_value c_getStatistics(napi_env env, napi_callback_info info)
911+
{
912+
napi_value argv=NULL, res;
913+
int err;
914+
size_t argc=0;
915+
struct js_cws_config_t *js_cws_instance;
916+
struct cws_js_err_t cws_js_err;
917+
struct statistics_t *stat;
918+
919+
JS_CWS_THROW_COND(
920+
napi_get_cb_info(env, info, &argc, &argv, NULL, (void **)&js_cws_instance)!=napi_ok,
921+
"napi_get_cb_info",
922+
"Can't parse arguments. Wrong argument at c_getStatistics",
923+
90
924+
)
925+
926+
JS_CWS_THROW_COND(argc, "c_getStatistics", "Too many arguments @ c_getStatistics", 91)
927+
928+
JS_CWS_THROW_COND(
929+
(js_cws_instance==NULL),
930+
"c_getStatistics",
931+
"Fatal: js_cws_instance @ c_getStatistics. Was expected NOT NULL",
932+
92
933+
)
934+
935+
CHECK_HAS_ERROR("c_getStatistics", "Could not get statistics. Object not parsed or internal error", 93)
936+
937+
JS_CWS_THROW_COND(
938+
(napi_create_object(env, &res)!=napi_ok),
939+
"napi_create_object",
940+
"napi_create_object @ c_getStatistics. Unable to create object",
941+
94
942+
)
943+
944+
JS_CWS_THROW_COND(
945+
(err=cws_set_uint32_stat_list(env, res, (stat=getStatistics(js_cws_instance->soap_internal)), JS_CWS_UINT32_STAT)),
946+
"cws_set_uint32_stat_list",
947+
"cws_set_uint32_stat_list @ c_getStatistics. Unable to parse statistic to JavaScript object",
948+
err
949+
)
950+
951+
_Static_assert(sizeof(uint64_t)>=sizeof(size_t), "Archtecture error. Refactor it");
952+
953+
JS_CWS_THROW_COND(
954+
(err=cws_set_uint64_stat_list(env, res, stat, JS_CWS_UINT64_STAT)),
955+
"cws_set_uint64_stat_list",
956+
"cws_set_uint64_stat_list @ c_getStatistics. Unable to parse statistic to JavaScript object",
957+
err
958+
)
959+
960+
return res;
961+
}
962+
840963
CWS_JS_FUNCTIONS_OBJ CWS_JS_CREATE_FUNCTIONS[] = {
841-
SET_JS_FN_CALL(getInstanceName),
842-
SET_JS_FN_CALL(getObjectName),
843-
SET_JS_FN_CALL(getObjectType),
844-
SET_JS_FN_CALL(getBsonBytes),
845-
SET_JS_FN_CALL(getJson),
846-
SET_JS_FN_CALL(getError),
847-
SET_JS_FN_CALL(parseFromFile),
848-
SET_JS_FN_CALL(parseFromFileJSON),
849-
SET_JS_FN_CALL(saveToFile),
850-
SET_JS_FN_CALL(saveToFileJSON),
851-
{NULL}
964+
SET_JS_FN_CALL(getInstanceName),
965+
SET_JS_FN_CALL(getObjectName),
966+
SET_JS_FN_CALL(getObjectType),
967+
SET_JS_FN_CALL(getBsonBytes),
968+
SET_JS_FN_CALL(getJson),
969+
SET_JS_FN_CALL(getStatistics),
970+
SET_JS_FN_CALL(getError),
971+
SET_JS_FN_CALL(parseFromFile),
972+
SET_JS_FN_CALL(parseFromFileJSON),
973+
SET_JS_FN_CALL(saveToFile),
974+
SET_JS_FN_CALL(saveToFileJSON),
975+
{NULL}
852976
};
853977

854978
napi_value c_create(napi_env env, napi_callback_info info)
@@ -898,9 +1022,9 @@ napi_value c_create(napi_env env, napi_callback_info info)
8981022
}
8991023

9001024
CWS_JS_FUNCTIONS_OBJ CWS_JS_INIT_FUNCTIONS[] = {
901-
SET_JS_FN_CALL(create),
902-
SET_JS_FN_CALL(getBsonVersion),
903-
{NULL}
1025+
SET_JS_FN_CALL(create),
1026+
SET_JS_FN_CALL(getBsonVersion),
1027+
{NULL}
9041028
};
9051029

9061030
napi_value Init(napi_env env, napi_value exports)

0 commit comments

Comments
 (0)