feat(tag): set decimal

This commit is contained in:
fengzero
2022-08-11 09:52:52 +00:00
parent 961635d7af
commit 56a0ecdfff
11 changed files with 127 additions and 20 deletions
+1
View File
@@ -76,6 +76,7 @@ typedef struct {
neu_attribute_e attribute;
neu_type_e type;
uint8_t precision;
double decimal;
neu_datatag_addr_option_u option;
uint8_t meta[NEU_TAG_META_SIZE];
} neu_datatag_t;
+4 -2
View File
@@ -365,14 +365,16 @@ class Migration:
log("Import tag {}".format(tag))
self.cursor_.execute(
"""INSERT INTO tags (driver_name, group_name, name, address,
attribute, type, description)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
attribute, precision, decimal, type, description)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(
node_name,
group_name,
tag["name"],
tag["address"],
tag["attribute"],
0,
0,
tag["type"],
tag.get("description", ""),
),
+1
View File
@@ -57,6 +57,7 @@ CREATE TABLE IF NOT EXISTS
address NOT NULL check(length(address) <= 64),
attribute INTEGER NOT NULL check(attribute BETWEEN 0 AND 7),
precision INTEGER NOT NULL check(precision BETWEEN 0 AND 17),
decimal REAL NOT NULL,
type INTEGER NOT NULL check(type BETWEEN 0 AND 15),
description NOT NULL check(length(description) <= 128),
UNIQUE (driver_name, group_name, name),
@@ -30,6 +30,7 @@ CREATE TABLE
address NOT NULL check(length(address) <= 64),
attribute INTEGER NOT NULL check(attribute BETWEEN 0 AND 7),
precision INTEGER NOT NULL check(precision BETWEEN 0 AND 17),
decimal REAL NOT NULL,
TYPE INTEGER NOT NULL check(TYPE BETWEEN 0 AND 15),
description NOT NULL check(length(description) <= 128),
UNIQUE (driver_name, group_name, name),
+3
View File
@@ -51,6 +51,7 @@ void handle_add_tags(nng_aio *aio)
cmd.tags[i].attribute = req->tags[i].attribute;
cmd.tags[i].type = req->tags[i].type;
cmd.tags[i].precision = req->tags[i].precision;
cmd.tags[i].decimal = req->tags[i].decimal;
cmd.tags[i].address = strdup(req->tags[i].address);
cmd.tags[i].name = strdup(req->tags[i].name);
if (req->tags[i].description != NULL) {
@@ -134,6 +135,7 @@ void handle_update_tags(nng_aio *aio)
cmd.tags[i].attribute = req->tags[i].attribute;
cmd.tags[i].type = req->tags[i].type;
cmd.tags[i].precision = req->tags[i].precision;
cmd.tags[i].decimal = req->tags[i].decimal;
cmd.tags[i].address = strdup(req->tags[i].address);
cmd.tags[i].name = strdup(req->tags[i].name);
if (req->tags[i].description != NULL) {
@@ -214,6 +216,7 @@ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags)
tags_res.tags[index].type = tag->type;
tags_res.tags[index].attribute = tag->attribute;
tags_res.tags[index].precision = tag->precision;
tags_res.tags[index].decimal = tag->decimal;
}
neu_json_encode_by_fn(&tags_res, neu_json_encode_get_tags_resp, &result);
+72
View File
@@ -710,6 +710,42 @@ static int read_report_group(int64_t timestamp, int64_t timeout,
datas[index].value.type = tag->type;
datas[index].value.value = value.value;
datas[index].value.precision = tag->precision;
if (tag->decimal != 0) {
datas[index].value.type = NEU_TYPE_DOUBLE;
switch (tag->type) {
case NEU_TYPE_INT8:
case NEU_TYPE_UINT8:
datas[index].value.value.d64 =
(double) datas[index].value.value.u8 * tag->decimal;
break;
case NEU_TYPE_INT16:
case NEU_TYPE_UINT16:
datas[index].value.value.d64 =
(double) datas[index].value.value.u16 * tag->decimal;
break;
case NEU_TYPE_INT32:
case NEU_TYPE_UINT32:
datas[index].value.value.d64 =
(double) datas[index].value.value.u32 * tag->decimal;
break;
case NEU_TYPE_INT64:
case NEU_TYPE_UINT64:
datas[index].value.value.d64 =
(double) datas[index].value.value.u64 * tag->decimal;
break;
case NEU_TYPE_FLOAT:
datas[index].value.value.d64 =
(double) datas[index].value.value.f32 * tag->decimal;
break;
case NEU_TYPE_DOUBLE:
datas[index].value.value.d64 =
(double) datas[index].value.value.d64 * tag->decimal;
break;
default:
datas[index].value.type = tag->type;
break;
}
}
}
index += 1;
}
@@ -747,6 +783,42 @@ static void read_group(int64_t timestamp, int64_t timeout,
datas[index].value.type = tag->type;
datas[index].value.value = value.value;
datas[index].value.precision = tag->precision;
if (tag->decimal != 0) {
datas[index].value.type = NEU_TYPE_DOUBLE;
switch (tag->type) {
case NEU_TYPE_INT8:
case NEU_TYPE_UINT8:
datas[index].value.value.d64 =
(double) datas[index].value.value.u8 * tag->decimal;
break;
case NEU_TYPE_INT16:
case NEU_TYPE_UINT16:
datas[index].value.value.d64 =
(double) datas[index].value.value.u16 * tag->decimal;
break;
case NEU_TYPE_INT32:
case NEU_TYPE_UINT32:
datas[index].value.value.d64 =
(double) datas[index].value.value.u32 * tag->decimal;
break;
case NEU_TYPE_INT64:
case NEU_TYPE_UINT64:
datas[index].value.value.d64 =
(double) datas[index].value.value.u64 * tag->decimal;
break;
case NEU_TYPE_FLOAT:
datas[index].value.value.d64 =
(double) datas[index].value.value.f32 * tag->decimal;
break;
case NEU_TYPE_DOUBLE:
datas[index].value.value.d64 =
(double) datas[index].value.value.d64 * tag->decimal;
break;
default:
datas[index].value.type = tag->type;
break;
}
}
}
}
}
+3
View File
@@ -120,6 +120,7 @@ int neu_group_add_tag(neu_group_t *group, neu_datatag_t *tag)
el->tag->type = tag->type;
el->tag->attribute = tag->attribute;
el->tag->precision = tag->precision;
el->tag->decimal = tag->decimal;
el->tag->option = tag->option;
memcpy(el->tag->meta, tag->meta, sizeof(tag->meta));
@@ -153,6 +154,7 @@ int neu_group_update_tag(neu_group_t *group, neu_datatag_t *tag)
el->tag->type = tag->type;
el->tag->attribute = tag->attribute;
el->tag->precision = tag->precision;
el->tag->decimal = tag->decimal;
el->tag->option = tag->option;
memcpy(el->tag->meta, tag->meta, sizeof(tag->meta));
@@ -224,6 +226,7 @@ neu_datatag_t *neu_group_find_tag(neu_group_t *group, const char *tag)
result->type = find->tag->type;
result->attribute = find->tag->attribute;
result->precision = find->tag->precision;
result->decimal = find->tag->decimal;
result->name = strdup(find->tag->name);
result->address = strdup(find->tag->address);
result->description = strdup(find->tag->description);
+1
View File
@@ -32,6 +32,7 @@ static void tag_array_copy(void *_dst, const void *_src)
dst->type = src->type;
dst->attribute = src->attribute;
dst->precision = src->precision;
dst->decimal = src->decimal;
dst->option = src->option;
memcpy(dst->meta, src->meta, sizeof(src->meta));
dst->address = strdup(src->address);
+21 -4
View File
@@ -82,6 +82,11 @@ int neu_json_decode_add_tags_req(char *buf, neu_json_add_tags_req_t **result)
.name = "address",
.t = NEU_JSON_STR,
},
{
.name = "decimal",
.t = NEU_JSON_DOUBLE,
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
},
{
.name = "precision",
.t = NEU_JSON_INT,
@@ -103,8 +108,9 @@ int neu_json_decode_add_tags_req(char *buf, neu_json_add_tags_req_t **result)
p_tag->name = tag_elems[1].v.val_str;
p_tag->attribute = tag_elems[2].v.val_int;
p_tag->address = tag_elems[3].v.val_str;
p_tag->precision = tag_elems[4].v.val_int;
p_tag->description = tag_elems[5].v.val_str;
p_tag->decimal = tag_elems[4].v.val_double;
p_tag->precision = tag_elems[5].v.val_int;
p_tag->description = tag_elems[6].v.val_str;
p_tag++;
}
@@ -281,6 +287,11 @@ int neu_json_encode_get_tags_resp(void *json_object, void *param)
.t = NEU_JSON_INT,
.v.val_int = p_tag->precision,
},
{
.name = "decimal",
.t = NEU_JSON_DOUBLE,
.v.val_double = p_tag->decimal,
},
{
.name = "address",
.t = NEU_JSON_STR,
@@ -363,6 +374,11 @@ int neu_json_decode_update_tags_req(char * buf,
.name = "address",
.t = NEU_JSON_STR,
},
{
.name = "decimal",
.t = NEU_JSON_DOUBLE,
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
},
{
.name = "precision",
.t = NEU_JSON_INT,
@@ -384,8 +400,9 @@ int neu_json_decode_update_tags_req(char * buf,
p_tag->name = tag_elems[1].v.val_str;
p_tag->attribute = tag_elems[2].v.val_int;
p_tag->address = tag_elems[3].v.val_str;
p_tag->precision = tag_elems[4].v.val_int;
p_tag->description = tag_elems[5].v.val_str;
p_tag->decimal = tag_elems[4].v.val_double;
p_tag->precision = tag_elems[5].v.val_int;
p_tag->description = tag_elems[6].v.val_str;
p_tag++;
}
+3
View File
@@ -38,6 +38,7 @@ typedef struct {
int64_t type;
int64_t attribute;
int64_t precision;
double decimal;
} neu_json_add_tags_req_tag_t;
typedef struct {
@@ -76,6 +77,7 @@ typedef struct {
int64_t type;
int64_t attribute;
int64_t precision;
double decimal;
} neu_json_get_tags_resp_tag_t;
typedef struct {
@@ -92,6 +94,7 @@ typedef struct {
int64_t type;
int64_t attribute;
int64_t precision;
double decimal;
} neu_json_update_tags_req_tag_t;
typedef struct {
+17 -14
View File
@@ -830,22 +830,23 @@ int neu_persister_load_plugins(neu_persister_t *persister,
int neu_persister_store_tag(neu_persister_t *persister, const char *driver_name,
const char *group_name, const neu_datatag_t *tag)
{
return execute_sql(
persister->db,
"INSERT INTO tags (driver_name, group_name, name, "
"address, attribute, precision, "
"type, description) VALUES(%Q, %Q, %Q, %Q, %i, %i, %i, %Q)",
driver_name, group_name, tag->name, tag->address, tag->attribute,
tag->precision, tag->type, tag->description);
return execute_sql(persister->db,
"INSERT INTO tags (driver_name, group_name, name, "
"address, attribute, precision, "
"type, decimal, description) VALUES(%Q, %Q, %Q, %Q, %i, "
"%i, %i, %lf, %Q)",
driver_name, group_name, tag->name, tag->address,
tag->attribute, tag->precision, tag->type, tag->decimal,
tag->description);
}
int neu_persister_load_tags(neu_persister_t *persister, const char *driver_name,
const char *group_name, UT_array **tags)
{
sqlite3_stmt *stmt = NULL;
const char * query =
"SELECT name, address, attribute, precision, type, description "
"FROM tags WHERE driver_name=? AND group_name=?";
sqlite3_stmt *stmt = NULL;
const char * query = "SELECT name, address, attribute, precision, type, "
"decimal, description "
"FROM tags WHERE driver_name=? AND group_name=?";
utarray_new(*tags, neu_tag_get_icd());
@@ -876,7 +877,8 @@ int neu_persister_load_tags(neu_persister_t *persister, const char *driver_name,
.attribute = sqlite3_column_int(stmt, 2),
.precision = sqlite3_column_int64(stmt, 3),
.type = sqlite3_column_int(stmt, 4),
.description = (char *) sqlite3_column_text(stmt, 5),
.decimal = sqlite3_column_double(stmt, 5),
.description = (char *) sqlite3_column_text(stmt, 6),
};
utarray_push_back(*tags, &tag);
@@ -903,10 +905,11 @@ int neu_persister_update_tag(neu_persister_t *persister,
{
return execute_sql(persister->db,
"UPDATE tags SET address=%Q, attribute=%i, "
"precision=%i, type=%i, description=%Q "
"precision=%i, type=%i, decimal=%lf, description=%Q "
"WHERE driver_name=%Q AND group_name=%Q AND name=%Q",
tag->address, tag->attribute, tag->precision, tag->type,
tag->description, driver_name, group_name, tag->name);
tag->decimal, tag->description, driver_name, group_name,
tag->name);
}
int neu_persister_delete_tag(neu_persister_t *persister,