summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2023-12-11 11:12:57 +0800
committerchenzizhan <[email protected]>2023-12-11 11:12:57 +0800
commita3bd4934eb46e67d76489e2fec597e9cb30a84de (patch)
tree3fd4789202bd552bf75671d543f1a91891a123df
parent8cb0a7ede8d30b0e1a49fa1582e26795d5258b27 (diff)
feat: query cube modev4.4.3
-rw-r--r--include/fieldstat/fieldstat.h8
-rw-r--r--src/fieldstat.c23
-rw-r--r--test/test_register_and_reset.cpp26
3 files changed, 57 insertions, 0 deletions
diff --git a/include/fieldstat/fieldstat.h b/include/fieldstat/fieldstat.h
index 4832a06..e5c14e1 100644
--- a/include/fieldstat/fieldstat.h
+++ b/include/fieldstat/fieldstat.h
@@ -67,6 +67,7 @@ int fieldstat_calibrate(const struct fieldstat *master, struct fieldstat *replic
* @return cube id, if success; otherwise, return FS_ERR_NULL_HANDLER, or FS_ERR_INVALID_PARAM when (max_n_cell == 0 && mode == TOPK). return FS_ERR_INVALID_KEY when the shared_tags is not unique.
*/
int fieldstat_create_cube(struct fieldstat *instance, const struct fieldstat_tag *shared_tags, size_t n_tag, enum sampling_mode mode, size_t max_n_cell);
+
/*
@brief Change the topk cube primary metric id. When fieldstat_counter_add or fieldstat_counter_set are called on the primary metric, the topk record of such cell will be updated.
the default primary metric id is 0.
@@ -224,6 +225,13 @@ struct fieldstat_tag_list *fieldstat_get_shared_tags(const struct fieldstat *ins
*/
int fieldstat_find_cube(const struct fieldstat *instance, const struct fieldstat_tag *shared_tags, size_t n_shared_tags);
+/*
+ return FS_ERR_NULL_HANDLER, FS_ERR_INVALID_CUBE_ID if fail.
+ When return OK, mode == COMPREHENSIVE, primary_metric_id == -1.
+ When return OK, mode == TOPK, primary_metric_id >= 0.
+*/
+int fieldstat_get_cube_mode(const struct fieldstat *instance, int cube_id, enum sampling_mode *mode, int *primary_metric_id);
+
/*
get the cell numbers in a cube. Return FS_ERR_INVALID_CUBE_ID if cube_id is invalid.
*/
diff --git a/src/fieldstat.c b/src/fieldstat.c
index 33f60de..b62ea39 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -1433,3 +1433,26 @@ int fieldstat_find_cube(const struct fieldstat *instance, const struct fieldstat
return cube_id;
}
+
+int fieldstat_get_cube_mode(const struct fieldstat *instance, int cube_id, enum sampling_mode *mode, int *primary_metric_id)
+{
+ if (instance == NULL) {
+ return FS_ERR_NULL_HANDLER;
+ }
+ if (cube_id < 0 || cube_id >= instance->valid_cube_arr_length) {
+ return FS_ERR_INVALID_CUBE_ID;
+ }
+ const struct fs_cube *cube = instance->cube[cube_id];
+ if (cube == NULL) {
+ return FS_ERR_INVALID_CUBE_ID;
+ }
+
+ *mode = cube->sampling_mode;
+ if (cube->sampling_mode == SAMPLING_MODE_TOPK) {
+ *primary_metric_id = cube->primary_metric_id;
+ } else {
+ *primary_metric_id = -1;
+ }
+
+ return FS_OK;
+} \ No newline at end of file
diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp
index c1f176f..20f31cf 100644
--- a/test/test_register_and_reset.cpp
+++ b/test/test_register_and_reset.cpp
@@ -499,6 +499,32 @@ TEST(calibrate, master_many_cube)
fieldstat_free(target);
}
+TEST(test_register, get_cube_mode)
+{
+ // int fieldstat_get_cube_mode(const struct fieldstat *instance, int cube_id, enum sampling_mode *mode, int *primary_metric_id)
+
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10);
+ fieldstat_register_counter(instance, "counter");
+ fieldstat_register_counter(instance, "counter2");
+ fieldstat_cube_set_primary_metric(instance, cube_id, 1);
+ int cube_id2 = fieldstat_create_cube(instance, &TEST_TAG_INT, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
+
+ enum sampling_mode mode;
+ int primary_metric_id;
+ EXPECT_EQ(fieldstat_get_cube_mode(instance, cube_id, &mode, &primary_metric_id), 0);
+ EXPECT_EQ(mode, SAMPLING_MODE_TOPK);
+ EXPECT_EQ(primary_metric_id, 1);
+
+ EXPECT_EQ(fieldstat_get_cube_mode(instance, cube_id2, &mode, &primary_metric_id), 0);
+ EXPECT_EQ(mode, SAMPLING_MODE_COMPREHENSIVE);
+ EXPECT_EQ(primary_metric_id, -1);
+
+ EXPECT_EQ(fieldstat_get_cube_mode(instance, 100, &mode, &primary_metric_id), FS_ERR_INVALID_CUBE_ID);
+
+ fieldstat_free(instance);
+}
+
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);