Add the ability to store chart labels in the database (#10718)
Stelios Fragkakis committed
Mar 9, 2021 at 12:44 UTC
fddcb83ee9cd94d61be47e774e006be433a85aeb
3 files changed
+66
-2
database/rrdset.c
+12
@@ -1932,6 +1932,18 @@ void rrdset_finalize_labels(RRDSET *st)
1932
} else {
1933
replace_label_list(labels, new_labels);
1934
}
1935
+#ifdef ENABLE_DBENGINE
1936
+ if (st->rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
1937
+ netdata_rwlock_wrlock(&labels->labels_rwlock);
1938
+ struct label *lbl = labels->head;
1939
+ while (lbl) {
1940
+ sql_store_chart_label(st->chart_uuid, (int)lbl->label_source, lbl->key, lbl->value);
1941
+ lbl = lbl->next;
1942
+ }
1943
+ netdata_rwlock_unlock(&labels->labels_rwlock);
1944
+ }
1945
+#endif
1946
+
1947
st->state->new_labels = NULL;
1948
}
1949
database/sqlite/sqlite_functions.c
+53
-1
@@ -17,12 +17,14 @@ const char *database_config[] = {
17
"CREATE TABLE IF NOT EXISTS metadata_migration(filename text, file_size, date_created int);",
18
"CREATE INDEX IF NOT EXISTS ind_d1 on dimension (chart_id, id, name);",
19
"CREATE INDEX IF NOT EXISTS ind_c1 on chart (host_id, id, type, name);",
20
+ "CREATE TABLE IF NOT EXISTS chart_label(chart_id blob, source_type int, label_key text, "
21
+ "label_value text, date_created int, PRIMARY KEY (chart_id, label_key));",
22
23
"delete from chart_active;",
24
"delete from dimension_active;",
23
-
25
"delete from chart where chart_id not in (select chart_id from dimension);",
26
"delete from host where host_id not in (select host_id from chart);",
27
+ "delete from chart_label where chart_id not in (select chart_id from chart);",
28
NULL
29
};
30
@@ -1021,3 +1023,53 @@ void add_migrated_file(char *path, uint64_t file_size)
1023
1024
return;
1025
}
1026
+
1027
+#define SQL_INS_CHART_LABEL "insert or replace into chart_label " \
1028
+ "(chart_id, source_type, label_key, label_value, date_created) " \
1029
+ "values (@chart, @source, @label, @value, strftime('%s'));"
1030
+
1031
+void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value)
1032
+{
1033
+ sqlite3_stmt *res = NULL;
1034
+ int rc;
1035
+
1036
+ rc = sqlite3_prepare_v2(db_meta, SQL_INS_CHART_LABEL, -1, &res, 0);
1037
+ if (unlikely(rc != SQLITE_OK)) {
1038
+ error_report("Failed to prepare statement store chart labels");
1039
+ return;
1040
+ }
1041
+
1042
+ rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
1043
+ if (unlikely(rc != SQLITE_OK)) {
1044
+ error_report("Failed to bind chart_id parameter to store label information");
1045
+ goto failed;
1046
+ }
1047
+
1048
+ rc = sqlite3_bind_int(res, 2, source_type);
1049
+ if (unlikely(rc != SQLITE_OK)) {
1050
+ error_report("Failed to bind type parameter to store label information");
1051
+ goto failed;
1052
+ }
1053
+
1054
+ rc = sqlite3_bind_text(res, 3, label, -1, SQLITE_STATIC);
1055
+ if (unlikely(rc != SQLITE_OK)) {
1056
+ error_report("Failed to bind label parameter to store label information");
1057
+ goto failed;
1058
+ }
1059
+
1060
+ rc = sqlite3_bind_text(res, 4, value, -1, SQLITE_STATIC);
1061
+ if (unlikely(rc != SQLITE_OK)) {
1062
+ error_report("Failed to bind value parameter to store label information");
1063
+ goto failed;
1064
+ }
1065
+
1066
+ rc = execute_insert(res);
1067
+ if (unlikely(rc != SQLITE_DONE))
1068
+ error_report("Failed to store chart label entry, rc = %d", rc);
1069
+
1070
+failed:
1071
+ if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
1072
+ error_report("Failed to finalize the prepared statement when storing chart label information");
1073
+
1074
+ return;
1075
+}
database/sqlite/sqlite_functions.h
+1
-1
@@ -58,5 +58,5 @@ extern void add_migrated_file(char *path, uint64_t file_size);
58
extern void db_unlock(void);
59
extern void db_lock(void);
60
extern void delete_dimension_uuid(uuid_t *dimension_uuid);
61
-
61
+extern void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value);
62
#endif //NETDATA_SQLITE_FUNCTIONS_H