Skip to content

Commit 3cc99d9

Browse files
authored
Merge pull request #2722 from fengzeroz/main
tag: option unit
2 parents b8a9ce4 + ae4623e commit 3cc99d9

File tree

13 files changed

+152
-34
lines changed

13 files changed

+152
-34
lines changed

include/neuron/define.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define NEU_TAG_META_LENGTH 20
6060
#define NEU_TAG_META_SIZE 32
6161
#define NEU_TAG_FORMAT_LENGTH 16
62+
#define NEU_TAG_UNIT_LENGTH 16
6263

6364
#define NEU_LOG_LEVEL_DEBUG "debug"
6465
#define NEU_LOG_LEVEL_INFO "info"

include/neuron/msg.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,11 @@ neu_tag_value_to_json_paginate(neu_resp_tag_value_meta_paginate_t *tag_value,
14701470
tag_json->datatag.bias = tag_value->datatag.bias;
14711471
tag_json->datatag.description = strdup(tag_value->datatag.description);
14721472
tag_json->datatag.option = tag_value->datatag.option;
1473+
if (tag_value->datatag.unit) {
1474+
tag_json->datatag.unit = strdup(tag_value->datatag.unit);
1475+
} else {
1476+
tag_json->datatag.unit = strdup("");
1477+
}
14731478
memcpy(tag_json->datatag.meta, tag_value->datatag.meta,
14741479
NEU_TAG_META_LENGTH);
14751480

include/neuron/tag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ typedef struct {
9393
double decimal;
9494
double bias;
9595
char * description;
96+
char * unit;
9697
neu_datatag_addr_option_u option;
9798
uint8_t meta[NEU_TAG_META_LENGTH];
9899
uint8_t format[NEU_TAG_FORMAT_LENGTH];

persistence/0120_2.15.0_tag.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BEGIN TRANSACTION;
2+
3+
alter TABLE tags add column unit TEXT NULL check(length(unit) <= 128);
4+
5+
COMMIT;

plugins/restful/datatag_handle.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ void handle_add_tags(nng_aio *aio)
9595
} else {
9696
cmd.tags[i].description = strdup("");
9797
}
98+
if (req->tags[i].unit == NULL) {
99+
cmd.tags[i].unit = strdup("");
100+
} else {
101+
cmd.tags[i].unit = strdup(req->tags[i].unit);
102+
}
98103
}
99104

100105
int ret = neu_plugin_op(plugin, header, &cmd);
@@ -216,6 +221,12 @@ void handle_add_gtags(nng_aio *aio)
216221
} else {
217222
cmd.groups[i].tags[j].description = strdup("");
218223
}
224+
if (req->groups[i].tags[j].unit == NULL) {
225+
cmd.groups[i].tags[j].unit = strdup("");
226+
} else {
227+
cmd.groups[i].tags[j].unit =
228+
strdup(req->groups[i].tags[j].unit);
229+
}
219230
}
220231
}
221232

@@ -382,6 +393,11 @@ void handle_update_tags(nng_aio *aio)
382393
} else {
383394
cmd.tags[i].description = strdup("");
384395
}
396+
if (req->tags[i].unit == NULL) {
397+
cmd.tags[i].unit = strdup("");
398+
} else {
399+
cmd.tags[i].unit = strdup(req->tags[i].unit);
400+
}
385401
}
386402

387403
ret = neu_plugin_op(plugin, header, &cmd);
@@ -529,6 +545,7 @@ void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags)
529545
tags_res.tags[index].decimal = tag->decimal;
530546
tags_res.tags[index].bias = tag->bias;
531547
tags_res.tags[index].t = NEU_JSON_UNDEFINE;
548+
tags_res.tags[index].unit = tag->unit;
532549
}
533550

534551
neu_json_encode_by_fn(&tags_res, neu_json_encode_get_tags_resp, &result);

plugins/restful/rw_handle.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,12 @@ void handle_read_paginate_resp(nng_aio * aio,
894894
strdup(tag_value->datatag.address);
895895
new_tag_value.datatag.description =
896896
strdup(tag_value->datatag.description);
897+
if (tag_value->datatag.unit) {
898+
new_tag_value.datatag.unit =
899+
strdup(tag_value->datatag.unit);
900+
} else {
901+
new_tag_value.datatag.unit = strdup("");
902+
}
897903

898904
utarray_push_back(filtered_tags, &new_tag_value);
899905
}
@@ -908,6 +914,7 @@ void handle_read_paginate_resp(nng_aio * aio,
908914
free(orig_tag_value->datatag.name);
909915
free(orig_tag_value->datatag.address);
910916
free(orig_tag_value->datatag.description);
917+
free(orig_tag_value->datatag.unit);
911918
}
912919
}
913920

@@ -997,6 +1004,7 @@ void handle_read_paginate_resp(nng_aio * aio,
9971004
free(final_tag_value->datatag.name);
9981005
free(final_tag_value->datatag.address);
9991006
free(final_tag_value->datatag.description);
1007+
free(final_tag_value->datatag.unit);
10001008
}
10011009
}
10021010

src/base/tag.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ static void tag_array_copy(void *_dst, const void *_src)
4040
dst->address = strdup(src->address);
4141
dst->name = strdup(src->name);
4242
dst->description = strdup(src->description);
43+
if (src->unit == NULL) {
44+
dst->unit = strdup("");
45+
} else {
46+
dst->unit = strdup(src->unit);
47+
}
4348

4449
memcpy(dst->format, src->format, sizeof(src->format));
4550
dst->n_format = src->n_format;
@@ -53,6 +58,9 @@ static void tag_array_free(void *_elt)
5358
free(elt->name);
5459
free(elt->address);
5560
free(elt->description);
61+
if (elt->unit) {
62+
free(elt->unit);
63+
}
5664
}
5765

5866
void neu_tag_format_str(const neu_datatag_t *tag, char *buf, int len)

src/parser/neu_json_rw.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,26 @@ int neu_json_encode_read_paginate_resp(void *json_object, void *param)
137137
tag_elems[7].t = NEU_JSON_DOUBLE;
138138
tag_elems[7].v.val_double = p_tag->datatag.bias;
139139

140+
tag_elems[8].name = "unit";
141+
tag_elems[8].t = NEU_JSON_STR;
142+
tag_elems[8].v.val_str = p_tag->datatag.unit;
143+
140144
if (p_tag->error != 0) {
141-
tag_elems[8].name = "error";
142-
tag_elems[8].t = NEU_JSON_INT;
143-
tag_elems[8].v.val_int = p_tag->error;
145+
tag_elems[9].name = "error";
146+
tag_elems[9].t = NEU_JSON_INT;
147+
tag_elems[9].v.val_int = p_tag->error;
144148
} else {
145-
tag_elems[8].name = "value";
146-
tag_elems[8].t = p_tag->t;
147-
tag_elems[8].v = p_tag->value;
148-
tag_elems[8].precision = p_tag->precision;
149-
tag_elems[8].bias = p_tag->datatag.bias;
149+
tag_elems[9].name = "value";
150+
tag_elems[9].t = p_tag->t;
151+
tag_elems[9].v = p_tag->value;
152+
tag_elems[9].precision = p_tag->precision;
153+
tag_elems[9].bias = p_tag->datatag.bias;
150154

151155
if (p_tag->t == NEU_JSON_FLOAT || p_tag->t == NEU_JSON_DOUBLE) {
152-
if_precision = 1;
153-
tag_elems[9].name = "transferPrecision";
154-
tag_elems[9].t = NEU_JSON_INT;
155-
tag_elems[9].v.val_int =
156+
if_precision = 1;
157+
tag_elems[10].name = "transferPrecision";
158+
tag_elems[10].t = NEU_JSON_INT;
159+
tag_elems[10].v.val_int =
156160
p_tag->precision > 0 ? p_tag->precision : 1;
157161
}
158162
}
@@ -166,16 +170,17 @@ int neu_json_encode_read_paginate_resp(void *json_object, void *param)
166170
neu_json_encode_field(attributes_object, &meta_elem, 1);
167171
}
168172

169-
tag_elems[if_precision + 9].name = "attributes";
170-
tag_elems[if_precision + 9].t = NEU_JSON_OBJECT;
171-
tag_elems[if_precision + 9].v.val_object = attributes_object;
173+
tag_elems[if_precision + 10].name = "attributes";
174+
tag_elems[if_precision + 10].t = NEU_JSON_OBJECT;
175+
tag_elems[if_precision + 10].v.val_object = attributes_object;
172176

173177
tag_array =
174-
neu_json_encode_array(tag_array, tag_elems, 10 + if_precision);
178+
neu_json_encode_array(tag_array, tag_elems, 11 + if_precision);
175179

176180
free(p_tag->datatag.name);
177181
free(p_tag->datatag.address);
178182
free(p_tag->datatag.description);
183+
free(p_tag->datatag.unit);
179184

180185
p_tag++;
181186
}

src/parser/neu_json_tag.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ int neu_json_encode_tag(void *json_obj, void *param)
8484
.t = tag->t,
8585
.v = tag->value,
8686
},
87+
{
88+
.name = "unit",
89+
.t = NEU_JSON_STR,
90+
.v.val_str = tag->unit,
91+
},
8792
};
8893

8994
ret = neu_json_encode_field(json_obj, tag_elems,
@@ -140,6 +145,11 @@ int neu_json_decode_tag_json(void *json_obj, neu_json_tag_t *tag_p)
140145
.t = NEU_JSON_DOUBLE,
141146
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
142147
},
148+
{
149+
.name = "unit",
150+
.t = NEU_JSON_STR,
151+
.attribute = NEU_JSON_ATTRIBUTE_OPTIONAL,
152+
},
143153
};
144154

145155
int ret = neu_json_decode_by_json(json_obj, NEU_JSON_ELEM_SIZE(tag_elems),
@@ -157,6 +167,7 @@ int neu_json_decode_tag_json(void *json_obj, neu_json_tag_t *tag_p)
157167
.t = tag_elems[7].t,
158168
.value = tag_elems[7].v,
159169
.bias = tag_elems[8].v.val_double,
170+
.unit = tag_elems[9].v.val_str,
160171
};
161172

162173
if (0 != ret) {
@@ -180,6 +191,7 @@ void neu_json_decode_tag_fini(neu_json_tag_t *tag)
180191
free(tag->name);
181192
free(tag->address);
182193
free(tag->description);
194+
free(tag->unit);
183195
if (NEU_JSON_STR == tag->t) {
184196
free(tag->value.val_str);
185197
}

src/parser/neu_json_tag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef struct {
4242
double bias;
4343
neu_json_type_e t;
4444
neu_json_value_u value;
45+
char * unit;
4546
} neu_json_tag_t;
4647

4748
int neu_json_encode_tag(void *json_obj, void *param);

0 commit comments

Comments
 (0)