rrdr.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdr.h"
  3. /*
  4. static void rrdr_dump(RRDR *r)
  5. {
  6. long c, i;
  7. RRDDIM *d;
  8. fprintf(stderr, "\nCHART %s (%s)\n", r->st->id, r->st->name);
  9. for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
  10. fprintf(stderr, "DIMENSION %s (%s), %s%s%s%s\n"
  11. , d->id
  12. , d->name
  13. , (r->od[c] & RRDR_EMPTY)?"EMPTY ":""
  14. , (r->od[c] & RRDR_RESET)?"RESET ":""
  15. , (r->od[c] & RRDR_DIMENSION_HIDDEN)?"HIDDEN ":""
  16. , (r->od[c] & RRDR_DIMENSION_NONZERO)?"NONZERO ":""
  17. );
  18. }
  19. if(r->rows <= 0) {
  20. fprintf(stderr, "RRDR does not have any values in it.\n");
  21. return;
  22. }
  23. fprintf(stderr, "RRDR includes %d values in it:\n", r->rows);
  24. // for each line in the array
  25. for(i = 0; i < r->rows ;i++) {
  26. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  27. RRDR_DIMENSION_FLAGS *co = &r->o[ i * r->d ];
  28. // print the id and the timestamp of the line
  29. fprintf(stderr, "%ld %ld ", i + 1, r->t[i]);
  30. // for each dimension
  31. for(c = 0, d = r->st->dimensions; d ;c++, d = d->next) {
  32. if(unlikely(r->od[c] & RRDR_DIMENSION_HIDDEN)) continue;
  33. if(unlikely(!(r->od[c] & RRDR_DIMENSION_NONZERO))) continue;
  34. if(co[c] & RRDR_EMPTY)
  35. fprintf(stderr, "null ");
  36. else
  37. fprintf(stderr, NETDATA_DOUBLE_FORMAT " %s%s%s%s "
  38. , cn[c]
  39. , (co[c] & RRDR_EMPTY)?"E":" "
  40. , (co[c] & RRDR_RESET)?"R":" "
  41. , (co[c] & RRDR_DIMENSION_HIDDEN)?"H":" "
  42. , (co[c] & RRDR_DIMENSION_NONZERO)?"N":" "
  43. );
  44. }
  45. fprintf(stderr, "\n");
  46. }
  47. }
  48. */
  49. inline void rrdr_free(ONEWAYALLOC *owa, RRDR *r) {
  50. if(unlikely(!r)) return;
  51. for(size_t d = 0; d < r->d ;d++) {
  52. string_freez(r->di[d]);
  53. string_freez(r->dn[d]);
  54. string_freez(r->du[d]);
  55. }
  56. query_target_release(r->internal.release_with_rrdr_qt);
  57. onewayalloc_freez(owa, r->t);
  58. onewayalloc_freez(owa, r->v);
  59. onewayalloc_freez(owa, r->vh);
  60. onewayalloc_freez(owa, r->o);
  61. onewayalloc_freez(owa, r->od);
  62. onewayalloc_freez(owa, r->di);
  63. onewayalloc_freez(owa, r->dn);
  64. onewayalloc_freez(owa, r->du);
  65. onewayalloc_freez(owa, r->dp);
  66. onewayalloc_freez(owa, r->dview);
  67. onewayalloc_freez(owa, r->dqp);
  68. onewayalloc_freez(owa, r->ar);
  69. onewayalloc_freez(owa, r->gbc);
  70. onewayalloc_freez(owa, r->dgbc);
  71. onewayalloc_freez(owa, r->dgbs);
  72. if(r->dl) {
  73. for(size_t d = 0; d < r->d ;d++)
  74. dictionary_destroy(r->dl[d]);
  75. onewayalloc_freez(owa, r->dl);
  76. }
  77. dictionary_destroy(r->label_keys);
  78. if(r->group_by.r) {
  79. // prevent accidental infinite recursion
  80. r->group_by.r->group_by.r = NULL;
  81. // do not release qt twice
  82. r->group_by.r->internal.qt = NULL;
  83. rrdr_free(owa, r->group_by.r);
  84. }
  85. onewayalloc_freez(owa, r);
  86. }
  87. RRDR *rrdr_create(ONEWAYALLOC *owa, QUERY_TARGET *qt, size_t dimensions, size_t points) {
  88. if(unlikely(!qt))
  89. return NULL;
  90. // create the rrdr
  91. RRDR *r = onewayalloc_callocz(owa, 1, sizeof(RRDR));
  92. r->internal.owa = owa;
  93. r->internal.qt = qt;
  94. r->view.before = qt->window.before;
  95. r->view.after = qt->window.after;
  96. r->time_grouping.points_wanted = points;
  97. r->d = (int)dimensions;
  98. r->n = (int)points;
  99. if(points && dimensions) {
  100. r->v = onewayalloc_mallocz(owa, points * dimensions * sizeof(NETDATA_DOUBLE));
  101. r->o = onewayalloc_mallocz(owa, points * dimensions * sizeof(RRDR_VALUE_FLAGS));
  102. r->ar = onewayalloc_mallocz(owa, points * dimensions * sizeof(NETDATA_DOUBLE));
  103. }
  104. if(points) {
  105. r->t = onewayalloc_callocz(owa, points, sizeof(time_t));
  106. }
  107. if(dimensions) {
  108. r->od = onewayalloc_mallocz(owa, dimensions * sizeof(RRDR_DIMENSION_FLAGS));
  109. r->di = onewayalloc_callocz(owa, dimensions, sizeof(STRING *));
  110. r->dn = onewayalloc_callocz(owa, dimensions, sizeof(STRING *));
  111. r->du = onewayalloc_callocz(owa, dimensions, sizeof(STRING *));
  112. }
  113. r->view.group = 1;
  114. r->view.update_every = 1;
  115. return r;
  116. }