Browse Source

Store dimension hidden option in the metadata db (#12196)

* Add a function to update dimension options in the metadata database

* Update the option for dimension to be hidden/unhinden when rrdim_hide/rrdim_unhide is called

* Store the hidden option for dimensions to the database
Stelios Fragkakis 3 years ago
parent
commit
a763d4111c

+ 6 - 1
collectors/plugins.d/pluginsd_parser.c

@@ -132,13 +132,18 @@ PARSER_RC pluginsd_dimension_action(void *user, RRDSET *st, char *id, char *name
             rrddim_is_obsolete(st, rd);
         else
             rrddim_isnot_obsolete(st, rd);
-        if (strstr(options, "hidden") != NULL)
+        if (strstr(options, "hidden") != NULL) {
             rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
+            (void) sql_set_dimension_option(&rd->state->metric_uuid, "hidden");
+        }
+        else
+            (void) sql_set_dimension_option(&rd->state->metric_uuid, NULL);
         if (strstr(options, "noreset") != NULL)
             rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
         if (strstr(options, "nooverflow") != NULL)
             rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
     } else {
+        (void) sql_set_dimension_option(&rd->state->metric_uuid, NULL);
         rrddim_isnot_obsolete(st, rd);
     }
     return PARSER_RC_OK;

+ 2 - 0
database/rrddim.c

@@ -541,6 +541,7 @@ int rrddim_hide(RRDSET *st, const char *id) {
         error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
         return 1;
     }
+    (void) sql_set_dimension_option(&rd->state->metric_uuid, "hidden");
 
     rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
 #ifdef ENABLE_ACLK
@@ -558,6 +559,7 @@ int rrddim_unhide(RRDSET *st, const char *id) {
         error("Cannot find dimension with id '%s' on stats '%s' (%s) on host '%s'.", id, st->name, st->id, host->hostname);
         return 1;
     }
+    (void) sql_set_dimension_option(&rd->state->metric_uuid, NULL);
 
     rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
 #ifdef ENABLE_ACLK

+ 43 - 0
database/sqlite/sqlite_functions.c

@@ -905,6 +905,49 @@ bind_fail:
     return 1;
 }
 
+/*
+ * Store set option for a dimension
+ */
+int sql_set_dimension_option(uuid_t *dim_uuid, char *option)
+{
+    sqlite3_stmt *res = NULL;
+    int rc;
+
+    if (unlikely(!db_meta)) {
+        if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
+            return 0;
+        error_report("Database has not been initialized");
+        return 1;
+    }
+
+    rc = sqlite3_prepare_v2(db_meta, "UPDATE dimension SET options = @options WHERE dim_id = @dim_id", -1, &res, 0);
+    if (unlikely(rc != SQLITE_OK)) {
+        error_report("Failed to prepare statement to update dimension options");
+        return 0;
+    };
+
+    rc = sqlite3_bind_blob(res, 2, dim_uuid, sizeof(*dim_uuid), SQLITE_STATIC);
+    if (unlikely(rc != SQLITE_OK))
+        goto bind_fail;
+
+    if (!option || !strcmp(option,"unhide"))
+        rc = sqlite3_bind_null(res, 1);
+    else
+        rc = sqlite3_bind_text(res, 1, option, -1, SQLITE_STATIC);
+    if (unlikely(rc != SQLITE_OK))
+        goto bind_fail;
+
+    rc = execute_insert(res);
+    if (unlikely(rc != SQLITE_DONE))
+        error_report("Failed to update dimension option, rc = %d", rc);
+
+bind_fail:
+    rc = sqlite3_finalize(res);
+    if (unlikely(rc != SQLITE_OK))
+        error_report("Failed to finalize statement in update dimension options, rc = %d", rc);
+    return 0;
+}
+
 
 //
 // Support for archived charts

+ 1 - 0
database/sqlite/sqlite_functions.h

@@ -98,4 +98,5 @@ extern void invalidate_node_instances(uuid_t *host_id, uuid_t *claim_id);
 extern struct node_instance_list *get_node_list(void);
 extern void sql_load_node_id(RRDHOST *host);
 extern void compute_chart_hash(RRDSET *st);
+extern int sql_set_dimension_option(uuid_t *dim_uuid, char *option);
 #endif //NETDATA_SQLITE_FUNCTIONS_H