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