chart_config.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "chart_config.h"
  2. #include "proto/chart/v1/config.pb.h"
  3. #include "libnetdata/libnetdata.h"
  4. #include "schema_wrapper_utils.h"
  5. void destroy_update_chart_config(struct update_chart_config *cfg)
  6. {
  7. freez(cfg->claim_id);
  8. freez(cfg->node_id);
  9. freez(cfg->hashes);
  10. }
  11. void destroy_chart_config_updated(struct chart_config_updated *cfg)
  12. {
  13. freez(cfg->type);
  14. freez(cfg->family);
  15. freez(cfg->context);
  16. freez(cfg->title);
  17. freez(cfg->plugin);
  18. freez(cfg->module);
  19. freez(cfg->units);
  20. freez(cfg->config_hash);
  21. }
  22. struct update_chart_config parse_update_chart_config(const char *data, size_t len)
  23. {
  24. chart::v1::UpdateChartConfigs cfgs;
  25. update_chart_config res;
  26. memset(&res, 0, sizeof(res));
  27. if (!cfgs.ParseFromArray(data, len))
  28. return res;
  29. res.claim_id = strdupz(cfgs.claim_id().c_str());
  30. res.node_id = strdupz(cfgs.node_id().c_str());
  31. // to not do bazillion tiny allocations for individual strings
  32. // we calculate how much memory we will need for all of them
  33. // and allocate at once
  34. int hash_count = cfgs.config_hashes_size();
  35. size_t total_strlen = 0;
  36. for (int i = 0; i < hash_count; i++)
  37. total_strlen += cfgs.config_hashes(i).length();
  38. total_strlen += hash_count; //null bytes
  39. res.hashes = (char**)callocz( 1,
  40. (hash_count+1) * sizeof(char*) + //char * array incl. terminating NULL at the end
  41. total_strlen //strings themselves incl. 1 null byte each
  42. );
  43. char* dest = ((char*)res.hashes) + (hash_count + 1 /* NULL ptr */) * sizeof(char *);
  44. // now copy them strings
  45. // null bytes handled by callocz
  46. for (int i = 0; i < hash_count; i++) {
  47. strcpy(dest, cfgs.config_hashes(i).c_str());
  48. res.hashes[i] = dest;
  49. dest += strlen(dest) + 1 /* end string null */;
  50. }
  51. return res;
  52. }
  53. char *generate_chart_configs_updated(size_t *len, const struct chart_config_updated *config_list, int list_size)
  54. {
  55. chart::v1::ChartConfigsUpdated configs;
  56. for (int i = 0; i < list_size; i++) {
  57. chart::v1::ChartConfigUpdated *config = configs.add_configs();
  58. config->set_type(config_list[i].type);
  59. if (config_list[i].family)
  60. config->set_family(config_list[i].family);
  61. config->set_context(config_list[i].context);
  62. config->set_title(config_list[i].title);
  63. config->set_priority(config_list[i].priority);
  64. config->set_plugin(config_list[i].plugin);
  65. if (config_list[i].module)
  66. config->set_module(config_list[i].module);
  67. switch (config_list[i].chart_type) {
  68. case RRDSET_TYPE_LINE:
  69. config->set_chart_type(chart::v1::LINE);
  70. break;
  71. case RRDSET_TYPE_AREA:
  72. config->set_chart_type(chart::v1::AREA);
  73. break;
  74. case RRDSET_TYPE_STACKED:
  75. config->set_chart_type(chart::v1::STACKED);
  76. break;
  77. default:
  78. return NULL;
  79. }
  80. config->set_units(config_list[i].units);
  81. config->set_config_hash(config_list[i].config_hash);
  82. }
  83. *len = PROTO_COMPAT_MSG_SIZE(configs);
  84. char *bin = (char*)mallocz(*len);
  85. configs.SerializeToArray(bin, *len);
  86. return bin;
  87. }