sqlite_context.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_functions.h"
  3. #include "sqlite_context.h"
  4. #include "sqlite_db_migration.h"
  5. #define DB_CONTEXT_METADATA_VERSION 1
  6. const char *database_context_config[] = {
  7. "CREATE TABLE IF NOT EXISTS context (host_id BLOB, id TEXT NOT NULL, version INT NOT NULL, title TEXT NOT NULL, " \
  8. "chart_type TEXT NOT NULL, unit TEXT NOT NULL, priority INT NOT NULL, first_time_t INT NOT NULL, "
  9. "last_time_t INT NOT NULL, deleted INT NOT NULL, "
  10. "family TEXT, PRIMARY KEY (host_id, id));",
  11. NULL
  12. };
  13. const char *database_context_cleanup[] = {
  14. "VACUUM;",
  15. NULL
  16. };
  17. sqlite3 *db_context_meta = NULL;
  18. /*
  19. * Initialize the SQLite database
  20. * Return 0 on success
  21. */
  22. int sql_init_context_database(int memory)
  23. {
  24. char sqlite_database[FILENAME_MAX + 1];
  25. int rc;
  26. if (likely(!memory))
  27. snprintfz(sqlite_database, FILENAME_MAX, "%s/context-meta.db", netdata_configured_cache_dir);
  28. else
  29. strcpy(sqlite_database, ":memory:");
  30. rc = sqlite3_open(sqlite_database, &db_context_meta);
  31. if (rc != SQLITE_OK) {
  32. error_report("Failed to initialize database at %s, due to \"%s\"", sqlite_database, sqlite3_errstr(rc));
  33. sqlite3_close(db_context_meta);
  34. db_context_meta = NULL;
  35. return 1;
  36. }
  37. netdata_log_info("SQLite database %s initialization", sqlite_database);
  38. char buf[1024 + 1] = "";
  39. const char *list[2] = { buf, NULL };
  40. int target_version = DB_CONTEXT_METADATA_VERSION;
  41. if (likely(!memory))
  42. target_version = perform_context_database_migration(db_context_meta, DB_CONTEXT_METADATA_VERSION);
  43. // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
  44. // PRAGMA schema.auto_vacuum = 0 | NONE | 1 | FULL | 2 | INCREMENTAL;
  45. snprintfz(buf, 1024, "PRAGMA auto_vacuum=%s;", config_get(CONFIG_SECTION_SQLITE, "auto vacuum", "INCREMENTAL"));
  46. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  47. // https://www.sqlite.org/pragma.html#pragma_synchronous
  48. // PRAGMA schema.synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL | 3 | EXTRA;
  49. snprintfz(buf, 1024, "PRAGMA synchronous=%s;", config_get(CONFIG_SECTION_SQLITE, "synchronous", "NORMAL"));
  50. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  51. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  52. // PRAGMA schema.journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
  53. snprintfz(buf, 1024, "PRAGMA journal_mode=%s;", config_get(CONFIG_SECTION_SQLITE, "journal mode", "WAL"));
  54. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  55. // https://www.sqlite.org/pragma.html#pragma_temp_store
  56. // PRAGMA temp_store = 0 | DEFAULT | 1 | FILE | 2 | MEMORY;
  57. snprintfz(buf, 1024, "PRAGMA temp_store=%s;", config_get(CONFIG_SECTION_SQLITE, "temp store", "MEMORY"));
  58. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  59. // https://www.sqlite.org/pragma.html#pragma_journal_size_limit
  60. // PRAGMA schema.journal_size_limit = N ;
  61. snprintfz(buf, 1024, "PRAGMA journal_size_limit=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "journal size limit", 16777216));
  62. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  63. // https://www.sqlite.org/pragma.html#pragma_cache_size
  64. // PRAGMA schema.cache_size = pages;
  65. // PRAGMA schema.cache_size = -kibibytes;
  66. snprintfz(buf, 1024, "PRAGMA cache_size=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "cache size", -2000));
  67. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  68. snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version);
  69. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  70. if (likely(!memory))
  71. snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir);
  72. else
  73. snprintfz(buf, 1024, "ATTACH DATABASE ':memory:' as meta;");
  74. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  75. if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_config[0]))
  76. return 1;
  77. if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_cleanup[0]))
  78. return 1;
  79. return 0;
  80. }
  81. /*
  82. * Close the sqlite database
  83. */
  84. void sql_close_context_database(void)
  85. {
  86. int rc;
  87. if (unlikely(!db_context_meta))
  88. return;
  89. netdata_log_info("Closing context SQLite database");
  90. rc = sqlite3_close_v2(db_context_meta);
  91. if (unlikely(rc != SQLITE_OK))
  92. error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
  93. }
  94. //
  95. // Fetching data
  96. //
  97. #define CTX_GET_CHART_LIST "SELECT c.chart_id, c.type||'.'||c.id, c.name, c.context, c.title, c.unit, c.priority, " \
  98. "c.update_every, c.chart_type, c.family FROM meta.chart c WHERE c.host_id = @host_id and c.chart_id is not null; "
  99. void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, void *), void *data)
  100. {
  101. int rc;
  102. static __thread sqlite3_stmt *res = NULL;
  103. if (unlikely(!host_uuid)) {
  104. internal_error(true, "Requesting context chart list without host_id");
  105. return;
  106. }
  107. if (unlikely(!res)) {
  108. rc = prepare_statement(db_context_meta, CTX_GET_CHART_LIST, &res);
  109. if (rc != SQLITE_OK) {
  110. error_report("Failed to prepare statement to fetch chart list");
  111. return;
  112. }
  113. }
  114. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  115. if (unlikely(rc != SQLITE_OK)) {
  116. error_report("Failed to bind host_id to fetch the chart list");
  117. goto skip_load;
  118. }
  119. SQL_CHART_DATA chart_data = { 0 };
  120. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  121. uuid_copy(chart_data.chart_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  122. chart_data.id = (char *) sqlite3_column_text(res, 1);
  123. chart_data.name = (char *) sqlite3_column_text(res, 2);
  124. chart_data.context = (char *) sqlite3_column_text(res, 3);
  125. chart_data.title = (char *) sqlite3_column_text(res, 4);
  126. chart_data.units = (char *) sqlite3_column_text(res, 5);
  127. chart_data.priority = sqlite3_column_int(res, 6);
  128. chart_data.update_every = sqlite3_column_int(res, 7);
  129. chart_data.chart_type = sqlite3_column_int(res, 8);
  130. chart_data.family = (char *) sqlite3_column_text(res, 9);
  131. dict_cb(&chart_data, data);
  132. }
  133. skip_load:
  134. rc = sqlite3_reset(res);
  135. if (rc != SQLITE_OK)
  136. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  137. }
  138. // Dimension list
  139. #define CTX_GET_DIMENSION_LIST "SELECT d.dim_id, d.id, d.name, CASE WHEN INSTR(d.options,\"hidden\") > 0 THEN 1 ELSE 0 END " \
  140. "FROM meta.dimension d WHERE d.chart_id = @id and d.dim_id is not null ORDER BY d.rowid ASC;"
  141. void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DATA *, void *), void *data)
  142. {
  143. int rc;
  144. static __thread sqlite3_stmt *res = NULL;
  145. if (unlikely(!res)) {
  146. rc = prepare_statement(db_context_meta, CTX_GET_DIMENSION_LIST, &res);
  147. if (rc != SQLITE_OK) {
  148. error_report("Failed to prepare statement to fetch chart dimension data");
  149. return;
  150. }
  151. }
  152. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  153. if (unlikely(rc != SQLITE_OK)) {
  154. error_report("Failed to bind chart_id to fetch dimension list");
  155. goto failed;
  156. }
  157. SQL_DIMENSION_DATA dimension_data;
  158. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  159. uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  160. dimension_data.id = (char *) sqlite3_column_text(res, 1);
  161. dimension_data.name = (char *) sqlite3_column_text(res, 2);
  162. dimension_data.hidden = sqlite3_column_int(res, 3);
  163. dict_cb(&dimension_data, data);
  164. }
  165. failed:
  166. rc = sqlite3_reset(res);
  167. if (rc != SQLITE_OK)
  168. error_report("Failed to reset statement that fetches the chart dimension list, rc = %d", rc);
  169. }
  170. // LABEL LIST
  171. #define CTX_GET_LABEL_LIST "SELECT l.label_key, l.label_value, l.source_type FROM meta.chart_label l WHERE l.chart_id = @id;"
  172. void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, void *), void *data)
  173. {
  174. int rc;
  175. static __thread sqlite3_stmt *res = NULL;
  176. if (unlikely(!res)) {
  177. rc = prepare_statement(db_context_meta, CTX_GET_LABEL_LIST, &res);
  178. if (rc != SQLITE_OK) {
  179. error_report("Failed to prepare statement to fetch chart labels");
  180. return;
  181. }
  182. }
  183. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  184. if (unlikely(rc != SQLITE_OK)) {
  185. error_report("Failed to bind chart_id to fetch chart labels");
  186. goto failed;
  187. }
  188. SQL_CLABEL_DATA label_data;
  189. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  190. label_data.label_key = (char *) sqlite3_column_text(res, 0);
  191. label_data.label_value = (char *) sqlite3_column_text(res, 1);
  192. label_data.label_source = sqlite3_column_int(res, 2);
  193. dict_cb(&label_data, data);
  194. }
  195. failed:
  196. rc = sqlite3_reset(res);
  197. if (rc != SQLITE_OK)
  198. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  199. }
  200. // CONTEXT LIST
  201. #define CTX_GET_CONTEXT_LIST "SELECT id, version, title, chart_type, unit, priority, first_time_t, " \
  202. "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id;"
  203. void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_DATA *, void *), void *data)
  204. {
  205. if (unlikely(!host_uuid))
  206. return;
  207. int rc;
  208. static __thread sqlite3_stmt *res = NULL;
  209. if (unlikely(!res)) {
  210. rc = prepare_statement(db_context_meta, CTX_GET_CONTEXT_LIST, &res);
  211. if (rc != SQLITE_OK) {
  212. error_report("Failed to prepare statement to fetch stored context list");
  213. return;
  214. }
  215. }
  216. VERSIONED_CONTEXT_DATA context_data = {0};
  217. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  218. if (unlikely(rc != SQLITE_OK)) {
  219. error_report("Failed to bind host_id to fetch versioned context data");
  220. goto failed;
  221. }
  222. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  223. context_data.id = (char *) sqlite3_column_text(res, 0);
  224. context_data.version = sqlite3_column_int64(res, 1);
  225. context_data.title = (char *) sqlite3_column_text(res, 2);
  226. context_data.chart_type = (char *) sqlite3_column_text(res, 3);
  227. context_data.units = (char *) sqlite3_column_text(res, 4);
  228. context_data.priority = sqlite3_column_int64(res, 5);
  229. context_data.first_time_s = sqlite3_column_int64(res, 6);
  230. context_data.last_time_s = sqlite3_column_int64(res, 7);
  231. context_data.deleted = sqlite3_column_int(res, 8);
  232. context_data.family = (char *) sqlite3_column_text(res, 9);
  233. dict_cb(&context_data, data);
  234. }
  235. failed:
  236. rc = sqlite3_reset(res);
  237. if (rc != SQLITE_OK)
  238. error_report("Failed to reset statement that fetches stored context versioned data, rc = %d", rc);
  239. }
  240. //
  241. // Storing Data
  242. //
  243. #define CTX_STORE_CONTEXT "INSERT OR REPLACE INTO context " \
  244. "(host_id, id, version, title, chart_type, unit, priority, first_time_t, last_time_t, deleted, family) " \
  245. "VALUES (@host_id, @context, @version, @title, @chart_type, @unit, @priority, @first_time_t, @last_time_t, @deleted, @family);"
  246. int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  247. {
  248. int rc, rc_stored = 1;
  249. sqlite3_stmt *res = NULL;
  250. if (unlikely(!host_uuid || !context_data || !context_data->id))
  251. return 0;
  252. rc = sqlite3_prepare_v2(db_context_meta, CTX_STORE_CONTEXT, -1, &res, 0);
  253. if (unlikely(rc != SQLITE_OK)) {
  254. error_report("Failed to prepare statement to store context");
  255. return 1;
  256. }
  257. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  258. if (unlikely(rc != SQLITE_OK)) {
  259. error_report("Failed to bind host_uuid to store context details");
  260. goto skip_store;
  261. }
  262. rc = bind_text_null(res, 2, context_data->id, 0);
  263. if (unlikely(rc != SQLITE_OK)) {
  264. error_report("Failed to bind context to store details");
  265. goto skip_store;
  266. }
  267. rc = sqlite3_bind_int64(res, 3, (time_t) context_data->version);
  268. if (unlikely(rc != SQLITE_OK)) {
  269. error_report("Failed to bind first_time_t to store context details");
  270. goto skip_store;
  271. }
  272. rc = bind_text_null(res, 4, context_data->title, 0);
  273. if (unlikely(rc != SQLITE_OK)) {
  274. error_report("Failed to bind context to store details");
  275. goto skip_store;
  276. }
  277. rc = bind_text_null(res, 5, context_data->chart_type, 0);
  278. if (unlikely(rc != SQLITE_OK)) {
  279. error_report("Failed to bind context to store details");
  280. goto skip_store;
  281. }
  282. rc = bind_text_null(res, 6, context_data->units, 0);
  283. if (unlikely(rc != SQLITE_OK)) {
  284. error_report("Failed to bind context to store details");
  285. goto skip_store;
  286. }
  287. rc = sqlite3_bind_int64(res, 7, (time_t) context_data->priority);
  288. if (unlikely(rc != SQLITE_OK)) {
  289. error_report("Failed to bind first_time_t to store context details");
  290. goto skip_store;
  291. }
  292. rc = sqlite3_bind_int64(res, 8, (time_t) context_data->first_time_s);
  293. if (unlikely(rc != SQLITE_OK)) {
  294. error_report("Failed to bind first_time_t to store context details");
  295. goto skip_store;
  296. }
  297. rc = sqlite3_bind_int64(res, 9, (time_t) context_data->last_time_s);
  298. if (unlikely(rc != SQLITE_OK)) {
  299. error_report("Failed to bind last_time_t to store context details");
  300. goto skip_store;
  301. }
  302. rc = sqlite3_bind_int(res, 10, context_data->deleted);
  303. if (unlikely(rc != SQLITE_OK)) {
  304. error_report("Failed to bind deleted flag to store context details");
  305. goto skip_store;
  306. }
  307. rc = bind_text_null(res, 11, context_data->family, 1);
  308. if (unlikely(rc != SQLITE_OK)) {
  309. error_report("Failed to bind context to store details");
  310. goto skip_store;
  311. }
  312. rc_stored = execute_insert(res);
  313. if (rc_stored != SQLITE_DONE)
  314. error_report("Failed store context details for context %s, rc = %d", context_data->id, rc_stored);
  315. skip_store:
  316. rc = sqlite3_finalize(res);
  317. if (rc != SQLITE_OK)
  318. error_report("Failed to finalize statement that stores context details, rc = %d", rc);
  319. return (rc_stored != SQLITE_DONE);
  320. }
  321. // Delete a context
  322. #define CTX_DELETE_CONTEXT "DELETE FROM context WHERE host_id = @host_id AND id = @context;"
  323. int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  324. {
  325. int rc, rc_stored = 1;
  326. sqlite3_stmt *res = NULL;
  327. if (unlikely(!context_data || !context_data->id))
  328. return 0;
  329. rc = sqlite3_prepare_v2(db_context_meta, CTX_DELETE_CONTEXT, -1, &res, 0);
  330. if (unlikely(rc != SQLITE_OK)) {
  331. error_report("Failed to prepare statement to delete context");
  332. return 1;
  333. }
  334. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  335. if (unlikely(rc != SQLITE_OK)) {
  336. error_report("Failed to bind host_id to delete context data");
  337. goto skip_delete;
  338. }
  339. rc = sqlite3_bind_text(res, 2, context_data->id, -1, SQLITE_STATIC);
  340. if (unlikely(rc != SQLITE_OK)) {
  341. error_report("Failed to bind context id for data deletion");
  342. goto skip_delete;
  343. }
  344. rc_stored = execute_insert(res);
  345. if (rc_stored != SQLITE_DONE)
  346. error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
  347. #ifdef NETDATA_INTERNAL_CHECKS
  348. else {
  349. char host_uuid_str[UUID_STR_LEN];
  350. uuid_unparse_lower(*host_uuid, host_uuid_str);
  351. netdata_log_info("%s: Deleted context %s under host %s", __FUNCTION__, context_data->id, host_uuid_str);
  352. }
  353. #endif
  354. skip_delete:
  355. rc = sqlite3_finalize(res);
  356. if (rc != SQLITE_OK)
  357. error_report("Failed to finalize statement where deleting a context, rc = %d", rc);
  358. return (rc_stored != SQLITE_DONE);
  359. }
  360. int sql_context_cache_stats(int op)
  361. {
  362. int count, dummy;
  363. if (unlikely(!db_context_meta))
  364. return 0;
  365. netdata_thread_disable_cancelability();
  366. sqlite3_db_status(db_context_meta, op, &count, &dummy, 0);
  367. netdata_thread_enable_cancelability();
  368. return count;
  369. }
  370. //
  371. // TESTING FUNCTIONS
  372. //
  373. static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, void *data)
  374. {
  375. (void)data;
  376. netdata_log_info(" Context id = %s "
  377. "version = %"PRIu64" "
  378. "title = %s "
  379. "chart_type = %s "
  380. "units = %s "
  381. "priority = %"PRIu64" "
  382. "first time = %"PRIu64" "
  383. "last time = %"PRIu64" "
  384. "deleted = %d "
  385. "family = %s",
  386. context_data->id,
  387. context_data->version,
  388. context_data->title,
  389. context_data->chart_type,
  390. context_data->units,
  391. context_data->priority,
  392. context_data->first_time_s,
  393. context_data->last_time_s,
  394. context_data->deleted,
  395. context_data->family);
  396. }
  397. int ctx_unittest(void)
  398. {
  399. uuid_t host_uuid;
  400. uuid_generate(host_uuid);
  401. initialize_thread_key_pool();
  402. int rc = sql_init_context_database(1);
  403. if (rc != SQLITE_OK)
  404. return 1;
  405. // Store a context
  406. VERSIONED_CONTEXT_DATA context_data;
  407. context_data.id = strdupz("cpu.cpu");
  408. context_data.title = strdupz("TestContextTitle");
  409. context_data.units= strdupz("TestContextUnits");
  410. context_data.chart_type = strdupz("TestContextChartType");
  411. context_data.family = strdupz("TestContextFamily");
  412. context_data.priority = 50000;
  413. context_data.deleted = 0;
  414. context_data.first_time_s = 1657781000;
  415. context_data.last_time_s = 1657781100;
  416. context_data.version = now_realtime_usec();
  417. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  418. netdata_log_info("Entry %s inserted", context_data.id);
  419. else
  420. netdata_log_info("Entry %s not inserted", context_data.id);
  421. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  422. netdata_log_info("Entry %s inserted", context_data.id);
  423. else
  424. netdata_log_info("Entry %s not inserted", context_data.id);
  425. // This will change end time
  426. context_data.first_time_s = 1657781000;
  427. context_data.last_time_s = 1657782001;
  428. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  429. netdata_log_info("Entry %s updated", context_data.id);
  430. else
  431. netdata_log_info("Entry %s not updated", context_data.id);
  432. netdata_log_info("List context start after insert");
  433. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  434. netdata_log_info("List context end after insert");
  435. // This will change start time
  436. context_data.first_time_s = 1657782000;
  437. context_data.last_time_s = 1657782001;
  438. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  439. netdata_log_info("Entry %s updated", context_data.id);
  440. else
  441. netdata_log_info("Entry %s not updated", context_data.id);
  442. // This will list one entry
  443. netdata_log_info("List context start after insert");
  444. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  445. netdata_log_info("List context end after insert");
  446. netdata_log_info("List context start after insert");
  447. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  448. netdata_log_info("List context end after insert");
  449. // This will delete the entry
  450. if (likely(!ctx_delete_context(&host_uuid, &context_data)))
  451. netdata_log_info("Entry %s deleted", context_data.id);
  452. else
  453. netdata_log_info("Entry %s not deleted", context_data.id);
  454. freez((void *)context_data.id);
  455. freez((void *)context_data.title);
  456. freez((void *)context_data.chart_type);
  457. freez((void *)context_data.family);
  458. freez((void *)context_data.units);
  459. // The list should be empty
  460. netdata_log_info("List context start after delete");
  461. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  462. netdata_log_info("List context end after delete");
  463. sql_close_context_database();
  464. return 0;
  465. }