Load host labels for archived hosts (#13464)
* Add function to load labels from the database for a host and return a DICTIONARY * Link labels to the newly created archived host
Stelios Fragkakis committed
Aug 2, 2022 at 17:16 UTC
e22dff4b1f3eda456410bcadd99f0a48a6adae18
3 files changed
+42
database/sqlite/sqlite_aclk.c
+3
@@ -313,6 +313,9 @@ static int create_host_callback(void *data, int argc, char **argv, char **column
313
, system_info
314
, 1
315
);
316
+ if (likely(host))
317
+ host->host_labels = sql_load_host_labels((uuid_t *)argv[IDX_HOST_ID]);
318
+
319
#ifdef NETDATA_INTERNAL_CHECKS
320
char node_str[UUID_STR_LEN] = "<none>";
321
if (likely(host->node_id))
database/sqlite/sqlite_functions.c
+38
@@ -2657,6 +2657,44 @@ void sql_store_host_labels(RRDHOST *host)
2657
rrdlabels_walkthrough_read(host->host_labels, save_host_label_callback, host);
2658
}
2659
2660
+#define SELECT_HOST_LABELS "SELECT label_key, label_value, source_type FROM host_label WHERE host_id = @host_id " \
2661
+ "AND label_key IS NOT NULL AND label_value IS NOT NULL;"
2662
+
2663
+DICTIONARY *sql_load_host_labels(uuid_t *host_id)
2664
+{
2665
+ int rc;
2666
+
2667
+ DICTIONARY *labels = NULL;
2668
+ sqlite3_stmt *res = NULL;
2669
+
2670
+ rc = sqlite3_prepare_v2(db_meta, SELECT_HOST_LABELS, -1, &res, 0);
2671
+ if (unlikely(rc != SQLITE_OK)) {
2672
+ error_report("Failed to prepare statement to read host information");
2673
+ return NULL;
2674
+ }
2675
+
2676
+ rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
2677
+ if (unlikely(rc != SQLITE_OK)) {
2678
+ error_report("Failed to bind host parameter host information");
2679
+ goto skip_loading;
2680
+ }
2681
+
2682
+ labels = rrdlabels_create();
2683
+
2684
+ while (sqlite3_step(res) == SQLITE_ROW) {
2685
+ rrdlabels_add(
2686
+ labels,
2687
+ (const char *)sqlite3_column_text(res, 0),
2688
+ (const char *)sqlite3_column_text(res, 1),
2689
+ sqlite3_column_int(res, 2));
2690
+ }
2691
+
2692
+skip_loading:
2693
+ if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
2694
+ error_report("Failed to finalize the prepared statement when reading host information");
2695
+ return labels;
2696
+}
2697
+
2698
// Utils
2699
int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null)
2700
{
database/sqlite/sqlite_functions.h
+1
@@ -112,4 +112,5 @@ void migrate_localhost(uuid_t *host_uuid);
112
extern void sql_store_host_system_info(uuid_t *host_id, const struct rrdhost_system_info *system_info);
113
extern void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *system_info);
114
void sql_store_host_labels(RRDHOST *host);
115
+DICTIONARY *sql_load_host_labels(uuid_t *host_id);
116
#endif //NETDATA_SQLITE_FUNCTIONS_H