query.c 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "query.h"
  3. #include "web/api/formatters/rrd2json.h"
  4. #include "rrdr.h"
  5. #include "average/average.h"
  6. #include "countif/countif.h"
  7. #include "incremental_sum/incremental_sum.h"
  8. #include "max/max.h"
  9. #include "median/median.h"
  10. #include "min/min.h"
  11. #include "sum/sum.h"
  12. #include "stddev/stddev.h"
  13. #include "ses/ses.h"
  14. #include "des/des.h"
  15. #include "percentile/percentile.h"
  16. #include "trimmed_mean/trimmed_mean.h"
  17. #define POINTS_TO_EXPAND_QUERY 5
  18. // ----------------------------------------------------------------------------
  19. static struct {
  20. const char *name;
  21. uint32_t hash;
  22. RRDR_TIME_GROUPING value;
  23. RRDR_TIME_GROUPING add_flush;
  24. // One time initialization for the module.
  25. // This is called once, when netdata starts.
  26. void (*init)(void);
  27. // Allocate all required structures for a query.
  28. // This is called once for each netdata query.
  29. void (*create)(struct rrdresult *r, const char *options);
  30. // Cleanup collected values, but don't destroy the structures.
  31. // This is called when the query engine switches dimensions,
  32. // as part of the same query (so same chart, switching metric).
  33. void (*reset)(struct rrdresult *r);
  34. // Free all resources allocated for the query.
  35. void (*free)(struct rrdresult *r);
  36. // Add a single value into the calculation.
  37. // The module may decide to cache it, or use it in the fly.
  38. void (*add)(struct rrdresult *r, NETDATA_DOUBLE value);
  39. // Generate a single result for the values added so far.
  40. // More values and points may be requested later.
  41. // It is up to the module to reset its internal structures
  42. // when flushing it (so for a few modules it may be better to
  43. // continue after a flush as if nothing changed, for others a
  44. // cleanup of the internal structures may be required).
  45. NETDATA_DOUBLE (*flush)(struct rrdresult *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr);
  46. TIER_QUERY_FETCH tier_query_fetch;
  47. } api_v1_data_groups[] = {
  48. {.name = "average",
  49. .hash = 0,
  50. .value = RRDR_GROUPING_AVERAGE,
  51. .add_flush = RRDR_GROUPING_AVERAGE,
  52. .init = NULL,
  53. .create= tg_average_create,
  54. .reset = tg_average_reset,
  55. .free = tg_average_free,
  56. .add = tg_average_add,
  57. .flush = tg_average_flush,
  58. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  59. },
  60. {.name = "avg", // alias on 'average'
  61. .hash = 0,
  62. .value = RRDR_GROUPING_AVERAGE,
  63. .add_flush = RRDR_GROUPING_AVERAGE,
  64. .init = NULL,
  65. .create= tg_average_create,
  66. .reset = tg_average_reset,
  67. .free = tg_average_free,
  68. .add = tg_average_add,
  69. .flush = tg_average_flush,
  70. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  71. },
  72. {.name = "mean", // alias on 'average'
  73. .hash = 0,
  74. .value = RRDR_GROUPING_AVERAGE,
  75. .add_flush = RRDR_GROUPING_AVERAGE,
  76. .init = NULL,
  77. .create= tg_average_create,
  78. .reset = tg_average_reset,
  79. .free = tg_average_free,
  80. .add = tg_average_add,
  81. .flush = tg_average_flush,
  82. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  83. },
  84. {.name = "trimmed-mean1",
  85. .hash = 0,
  86. .value = RRDR_GROUPING_TRIMMED_MEAN1,
  87. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  88. .init = NULL,
  89. .create= tg_trimmed_mean_create_1,
  90. .reset = tg_trimmed_mean_reset,
  91. .free = tg_trimmed_mean_free,
  92. .add = tg_trimmed_mean_add,
  93. .flush = tg_trimmed_mean_flush,
  94. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  95. },
  96. {.name = "trimmed-mean2",
  97. .hash = 0,
  98. .value = RRDR_GROUPING_TRIMMED_MEAN2,
  99. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  100. .init = NULL,
  101. .create= tg_trimmed_mean_create_2,
  102. .reset = tg_trimmed_mean_reset,
  103. .free = tg_trimmed_mean_free,
  104. .add = tg_trimmed_mean_add,
  105. .flush = tg_trimmed_mean_flush,
  106. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  107. },
  108. {.name = "trimmed-mean3",
  109. .hash = 0,
  110. .value = RRDR_GROUPING_TRIMMED_MEAN3,
  111. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  112. .init = NULL,
  113. .create= tg_trimmed_mean_create_3,
  114. .reset = tg_trimmed_mean_reset,
  115. .free = tg_trimmed_mean_free,
  116. .add = tg_trimmed_mean_add,
  117. .flush = tg_trimmed_mean_flush,
  118. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  119. },
  120. {.name = "trimmed-mean5",
  121. .hash = 0,
  122. .value = RRDR_GROUPING_TRIMMED_MEAN,
  123. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  124. .init = NULL,
  125. .create= tg_trimmed_mean_create_5,
  126. .reset = tg_trimmed_mean_reset,
  127. .free = tg_trimmed_mean_free,
  128. .add = tg_trimmed_mean_add,
  129. .flush = tg_trimmed_mean_flush,
  130. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  131. },
  132. {.name = "trimmed-mean10",
  133. .hash = 0,
  134. .value = RRDR_GROUPING_TRIMMED_MEAN10,
  135. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  136. .init = NULL,
  137. .create= tg_trimmed_mean_create_10,
  138. .reset = tg_trimmed_mean_reset,
  139. .free = tg_trimmed_mean_free,
  140. .add = tg_trimmed_mean_add,
  141. .flush = tg_trimmed_mean_flush,
  142. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  143. },
  144. {.name = "trimmed-mean15",
  145. .hash = 0,
  146. .value = RRDR_GROUPING_TRIMMED_MEAN15,
  147. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  148. .init = NULL,
  149. .create= tg_trimmed_mean_create_15,
  150. .reset = tg_trimmed_mean_reset,
  151. .free = tg_trimmed_mean_free,
  152. .add = tg_trimmed_mean_add,
  153. .flush = tg_trimmed_mean_flush,
  154. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  155. },
  156. {.name = "trimmed-mean20",
  157. .hash = 0,
  158. .value = RRDR_GROUPING_TRIMMED_MEAN20,
  159. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  160. .init = NULL,
  161. .create= tg_trimmed_mean_create_20,
  162. .reset = tg_trimmed_mean_reset,
  163. .free = tg_trimmed_mean_free,
  164. .add = tg_trimmed_mean_add,
  165. .flush = tg_trimmed_mean_flush,
  166. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  167. },
  168. {.name = "trimmed-mean25",
  169. .hash = 0,
  170. .value = RRDR_GROUPING_TRIMMED_MEAN25,
  171. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  172. .init = NULL,
  173. .create= tg_trimmed_mean_create_25,
  174. .reset = tg_trimmed_mean_reset,
  175. .free = tg_trimmed_mean_free,
  176. .add = tg_trimmed_mean_add,
  177. .flush = tg_trimmed_mean_flush,
  178. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  179. },
  180. {.name = "trimmed-mean",
  181. .hash = 0,
  182. .value = RRDR_GROUPING_TRIMMED_MEAN,
  183. .add_flush = RRDR_GROUPING_TRIMMED_MEAN,
  184. .init = NULL,
  185. .create= tg_trimmed_mean_create_5,
  186. .reset = tg_trimmed_mean_reset,
  187. .free = tg_trimmed_mean_free,
  188. .add = tg_trimmed_mean_add,
  189. .flush = tg_trimmed_mean_flush,
  190. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  191. },
  192. {.name = "incremental_sum",
  193. .hash = 0,
  194. .value = RRDR_GROUPING_INCREMENTAL_SUM,
  195. .add_flush = RRDR_GROUPING_INCREMENTAL_SUM,
  196. .init = NULL,
  197. .create= tg_incremental_sum_create,
  198. .reset = tg_incremental_sum_reset,
  199. .free = tg_incremental_sum_free,
  200. .add = tg_incremental_sum_add,
  201. .flush = tg_incremental_sum_flush,
  202. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  203. },
  204. {.name = "incremental-sum",
  205. .hash = 0,
  206. .value = RRDR_GROUPING_INCREMENTAL_SUM,
  207. .add_flush = RRDR_GROUPING_INCREMENTAL_SUM,
  208. .init = NULL,
  209. .create= tg_incremental_sum_create,
  210. .reset = tg_incremental_sum_reset,
  211. .free = tg_incremental_sum_free,
  212. .add = tg_incremental_sum_add,
  213. .flush = tg_incremental_sum_flush,
  214. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  215. },
  216. {.name = "median",
  217. .hash = 0,
  218. .value = RRDR_GROUPING_MEDIAN,
  219. .add_flush = RRDR_GROUPING_MEDIAN,
  220. .init = NULL,
  221. .create= tg_median_create,
  222. .reset = tg_median_reset,
  223. .free = tg_median_free,
  224. .add = tg_median_add,
  225. .flush = tg_median_flush,
  226. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  227. },
  228. {.name = "trimmed-median1",
  229. .hash = 0,
  230. .value = RRDR_GROUPING_TRIMMED_MEDIAN1,
  231. .add_flush = RRDR_GROUPING_MEDIAN,
  232. .init = NULL,
  233. .create= tg_median_create_trimmed_1,
  234. .reset = tg_median_reset,
  235. .free = tg_median_free,
  236. .add = tg_median_add,
  237. .flush = tg_median_flush,
  238. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  239. },
  240. {.name = "trimmed-median2",
  241. .hash = 0,
  242. .value = RRDR_GROUPING_TRIMMED_MEDIAN2,
  243. .add_flush = RRDR_GROUPING_MEDIAN,
  244. .init = NULL,
  245. .create= tg_median_create_trimmed_2,
  246. .reset = tg_median_reset,
  247. .free = tg_median_free,
  248. .add = tg_median_add,
  249. .flush = tg_median_flush,
  250. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  251. },
  252. {.name = "trimmed-median3",
  253. .hash = 0,
  254. .value = RRDR_GROUPING_TRIMMED_MEDIAN3,
  255. .add_flush = RRDR_GROUPING_MEDIAN,
  256. .init = NULL,
  257. .create= tg_median_create_trimmed_3,
  258. .reset = tg_median_reset,
  259. .free = tg_median_free,
  260. .add = tg_median_add,
  261. .flush = tg_median_flush,
  262. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  263. },
  264. {.name = "trimmed-median5",
  265. .hash = 0,
  266. .value = RRDR_GROUPING_TRIMMED_MEDIAN5,
  267. .add_flush = RRDR_GROUPING_MEDIAN,
  268. .init = NULL,
  269. .create= tg_median_create_trimmed_5,
  270. .reset = tg_median_reset,
  271. .free = tg_median_free,
  272. .add = tg_median_add,
  273. .flush = tg_median_flush,
  274. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  275. },
  276. {.name = "trimmed-median10",
  277. .hash = 0,
  278. .value = RRDR_GROUPING_TRIMMED_MEDIAN10,
  279. .add_flush = RRDR_GROUPING_MEDIAN,
  280. .init = NULL,
  281. .create= tg_median_create_trimmed_10,
  282. .reset = tg_median_reset,
  283. .free = tg_median_free,
  284. .add = tg_median_add,
  285. .flush = tg_median_flush,
  286. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  287. },
  288. {.name = "trimmed-median15",
  289. .hash = 0,
  290. .value = RRDR_GROUPING_TRIMMED_MEDIAN15,
  291. .add_flush = RRDR_GROUPING_MEDIAN,
  292. .init = NULL,
  293. .create= tg_median_create_trimmed_15,
  294. .reset = tg_median_reset,
  295. .free = tg_median_free,
  296. .add = tg_median_add,
  297. .flush = tg_median_flush,
  298. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  299. },
  300. {.name = "trimmed-median20",
  301. .hash = 0,
  302. .value = RRDR_GROUPING_TRIMMED_MEDIAN20,
  303. .add_flush = RRDR_GROUPING_MEDIAN,
  304. .init = NULL,
  305. .create= tg_median_create_trimmed_20,
  306. .reset = tg_median_reset,
  307. .free = tg_median_free,
  308. .add = tg_median_add,
  309. .flush = tg_median_flush,
  310. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  311. },
  312. {.name = "trimmed-median25",
  313. .hash = 0,
  314. .value = RRDR_GROUPING_TRIMMED_MEDIAN25,
  315. .add_flush = RRDR_GROUPING_MEDIAN,
  316. .init = NULL,
  317. .create= tg_median_create_trimmed_25,
  318. .reset = tg_median_reset,
  319. .free = tg_median_free,
  320. .add = tg_median_add,
  321. .flush = tg_median_flush,
  322. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  323. },
  324. {.name = "trimmed-median",
  325. .hash = 0,
  326. .value = RRDR_GROUPING_TRIMMED_MEDIAN5,
  327. .add_flush = RRDR_GROUPING_MEDIAN,
  328. .init = NULL,
  329. .create= tg_median_create_trimmed_5,
  330. .reset = tg_median_reset,
  331. .free = tg_median_free,
  332. .add = tg_median_add,
  333. .flush = tg_median_flush,
  334. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  335. },
  336. {.name = "percentile25",
  337. .hash = 0,
  338. .value = RRDR_GROUPING_PERCENTILE25,
  339. .add_flush = RRDR_GROUPING_PERCENTILE,
  340. .init = NULL,
  341. .create= tg_percentile_create_25,
  342. .reset = tg_percentile_reset,
  343. .free = tg_percentile_free,
  344. .add = tg_percentile_add,
  345. .flush = tg_percentile_flush,
  346. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  347. },
  348. {.name = "percentile50",
  349. .hash = 0,
  350. .value = RRDR_GROUPING_PERCENTILE50,
  351. .add_flush = RRDR_GROUPING_PERCENTILE,
  352. .init = NULL,
  353. .create= tg_percentile_create_50,
  354. .reset = tg_percentile_reset,
  355. .free = tg_percentile_free,
  356. .add = tg_percentile_add,
  357. .flush = tg_percentile_flush,
  358. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  359. },
  360. {.name = "percentile75",
  361. .hash = 0,
  362. .value = RRDR_GROUPING_PERCENTILE75,
  363. .add_flush = RRDR_GROUPING_PERCENTILE,
  364. .init = NULL,
  365. .create= tg_percentile_create_75,
  366. .reset = tg_percentile_reset,
  367. .free = tg_percentile_free,
  368. .add = tg_percentile_add,
  369. .flush = tg_percentile_flush,
  370. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  371. },
  372. {.name = "percentile80",
  373. .hash = 0,
  374. .value = RRDR_GROUPING_PERCENTILE80,
  375. .add_flush = RRDR_GROUPING_PERCENTILE,
  376. .init = NULL,
  377. .create= tg_percentile_create_80,
  378. .reset = tg_percentile_reset,
  379. .free = tg_percentile_free,
  380. .add = tg_percentile_add,
  381. .flush = tg_percentile_flush,
  382. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  383. },
  384. {.name = "percentile90",
  385. .hash = 0,
  386. .value = RRDR_GROUPING_PERCENTILE90,
  387. .add_flush = RRDR_GROUPING_PERCENTILE,
  388. .init = NULL,
  389. .create= tg_percentile_create_90,
  390. .reset = tg_percentile_reset,
  391. .free = tg_percentile_free,
  392. .add = tg_percentile_add,
  393. .flush = tg_percentile_flush,
  394. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  395. },
  396. {.name = "percentile95",
  397. .hash = 0,
  398. .value = RRDR_GROUPING_PERCENTILE,
  399. .add_flush = RRDR_GROUPING_PERCENTILE,
  400. .init = NULL,
  401. .create= tg_percentile_create_95,
  402. .reset = tg_percentile_reset,
  403. .free = tg_percentile_free,
  404. .add = tg_percentile_add,
  405. .flush = tg_percentile_flush,
  406. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  407. },
  408. {.name = "percentile97",
  409. .hash = 0,
  410. .value = RRDR_GROUPING_PERCENTILE97,
  411. .add_flush = RRDR_GROUPING_PERCENTILE,
  412. .init = NULL,
  413. .create= tg_percentile_create_97,
  414. .reset = tg_percentile_reset,
  415. .free = tg_percentile_free,
  416. .add = tg_percentile_add,
  417. .flush = tg_percentile_flush,
  418. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  419. },
  420. {.name = "percentile98",
  421. .hash = 0,
  422. .value = RRDR_GROUPING_PERCENTILE98,
  423. .add_flush = RRDR_GROUPING_PERCENTILE,
  424. .init = NULL,
  425. .create= tg_percentile_create_98,
  426. .reset = tg_percentile_reset,
  427. .free = tg_percentile_free,
  428. .add = tg_percentile_add,
  429. .flush = tg_percentile_flush,
  430. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  431. },
  432. {.name = "percentile99",
  433. .hash = 0,
  434. .value = RRDR_GROUPING_PERCENTILE99,
  435. .add_flush = RRDR_GROUPING_PERCENTILE,
  436. .init = NULL,
  437. .create= tg_percentile_create_99,
  438. .reset = tg_percentile_reset,
  439. .free = tg_percentile_free,
  440. .add = tg_percentile_add,
  441. .flush = tg_percentile_flush,
  442. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  443. },
  444. {.name = "percentile",
  445. .hash = 0,
  446. .value = RRDR_GROUPING_PERCENTILE,
  447. .add_flush = RRDR_GROUPING_PERCENTILE,
  448. .init = NULL,
  449. .create= tg_percentile_create_95,
  450. .reset = tg_percentile_reset,
  451. .free = tg_percentile_free,
  452. .add = tg_percentile_add,
  453. .flush = tg_percentile_flush,
  454. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  455. },
  456. {.name = "min",
  457. .hash = 0,
  458. .value = RRDR_GROUPING_MIN,
  459. .add_flush = RRDR_GROUPING_MIN,
  460. .init = NULL,
  461. .create= tg_min_create,
  462. .reset = tg_min_reset,
  463. .free = tg_min_free,
  464. .add = tg_min_add,
  465. .flush = tg_min_flush,
  466. .tier_query_fetch = TIER_QUERY_FETCH_MIN
  467. },
  468. {.name = "max",
  469. .hash = 0,
  470. .value = RRDR_GROUPING_MAX,
  471. .add_flush = RRDR_GROUPING_MAX,
  472. .init = NULL,
  473. .create= tg_max_create,
  474. .reset = tg_max_reset,
  475. .free = tg_max_free,
  476. .add = tg_max_add,
  477. .flush = tg_max_flush,
  478. .tier_query_fetch = TIER_QUERY_FETCH_MAX
  479. },
  480. {.name = "sum",
  481. .hash = 0,
  482. .value = RRDR_GROUPING_SUM,
  483. .add_flush = RRDR_GROUPING_SUM,
  484. .init = NULL,
  485. .create= tg_sum_create,
  486. .reset = tg_sum_reset,
  487. .free = tg_sum_free,
  488. .add = tg_sum_add,
  489. .flush = tg_sum_flush,
  490. .tier_query_fetch = TIER_QUERY_FETCH_SUM
  491. },
  492. // standard deviation
  493. {.name = "stddev",
  494. .hash = 0,
  495. .value = RRDR_GROUPING_STDDEV,
  496. .add_flush = RRDR_GROUPING_STDDEV,
  497. .init = NULL,
  498. .create= tg_stddev_create,
  499. .reset = tg_stddev_reset,
  500. .free = tg_stddev_free,
  501. .add = tg_stddev_add,
  502. .flush = tg_stddev_flush,
  503. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  504. },
  505. {.name = "cv", // coefficient variation is calculated by stddev
  506. .hash = 0,
  507. .value = RRDR_GROUPING_CV,
  508. .add_flush = RRDR_GROUPING_CV,
  509. .init = NULL,
  510. .create= tg_stddev_create, // not an error, stddev calculates this too
  511. .reset = tg_stddev_reset, // not an error, stddev calculates this too
  512. .free = tg_stddev_free, // not an error, stddev calculates this too
  513. .add = tg_stddev_add, // not an error, stddev calculates this too
  514. .flush = tg_stddev_coefficient_of_variation_flush,
  515. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  516. },
  517. {.name = "rsd", // alias of 'cv'
  518. .hash = 0,
  519. .value = RRDR_GROUPING_CV,
  520. .add_flush = RRDR_GROUPING_CV,
  521. .init = NULL,
  522. .create= tg_stddev_create, // not an error, stddev calculates this too
  523. .reset = tg_stddev_reset, // not an error, stddev calculates this too
  524. .free = tg_stddev_free, // not an error, stddev calculates this too
  525. .add = tg_stddev_add, // not an error, stddev calculates this too
  526. .flush = tg_stddev_coefficient_of_variation_flush,
  527. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  528. },
  529. // single exponential smoothing
  530. {.name = "ses",
  531. .hash = 0,
  532. .value = RRDR_GROUPING_SES,
  533. .add_flush = RRDR_GROUPING_SES,
  534. .init = tg_ses_init,
  535. .create= tg_ses_create,
  536. .reset = tg_ses_reset,
  537. .free = tg_ses_free,
  538. .add = tg_ses_add,
  539. .flush = tg_ses_flush,
  540. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  541. },
  542. {.name = "ema", // alias for 'ses'
  543. .hash = 0,
  544. .value = RRDR_GROUPING_SES,
  545. .add_flush = RRDR_GROUPING_SES,
  546. .init = NULL,
  547. .create= tg_ses_create,
  548. .reset = tg_ses_reset,
  549. .free = tg_ses_free,
  550. .add = tg_ses_add,
  551. .flush = tg_ses_flush,
  552. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  553. },
  554. {.name = "ewma", // alias for ses
  555. .hash = 0,
  556. .value = RRDR_GROUPING_SES,
  557. .add_flush = RRDR_GROUPING_SES,
  558. .init = NULL,
  559. .create= tg_ses_create,
  560. .reset = tg_ses_reset,
  561. .free = tg_ses_free,
  562. .add = tg_ses_add,
  563. .flush = tg_ses_flush,
  564. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  565. },
  566. // double exponential smoothing
  567. {.name = "des",
  568. .hash = 0,
  569. .value = RRDR_GROUPING_DES,
  570. .add_flush = RRDR_GROUPING_DES,
  571. .init = tg_des_init,
  572. .create= tg_des_create,
  573. .reset = tg_des_reset,
  574. .free = tg_des_free,
  575. .add = tg_des_add,
  576. .flush = tg_des_flush,
  577. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  578. },
  579. {.name = "countif",
  580. .hash = 0,
  581. .value = RRDR_GROUPING_COUNTIF,
  582. .add_flush = RRDR_GROUPING_COUNTIF,
  583. .init = NULL,
  584. .create= tg_countif_create,
  585. .reset = tg_countif_reset,
  586. .free = tg_countif_free,
  587. .add = tg_countif_add,
  588. .flush = tg_countif_flush,
  589. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  590. },
  591. // terminator
  592. {.name = NULL,
  593. .hash = 0,
  594. .value = RRDR_GROUPING_UNDEFINED,
  595. .add_flush = RRDR_GROUPING_AVERAGE,
  596. .init = NULL,
  597. .create= tg_average_create,
  598. .reset = tg_average_reset,
  599. .free = tg_average_free,
  600. .add = tg_average_add,
  601. .flush = tg_average_flush,
  602. .tier_query_fetch = TIER_QUERY_FETCH_AVERAGE
  603. }
  604. };
  605. void time_grouping_init(void) {
  606. int i;
  607. for(i = 0; api_v1_data_groups[i].name ; i++) {
  608. api_v1_data_groups[i].hash = simple_hash(api_v1_data_groups[i].name);
  609. if(api_v1_data_groups[i].init)
  610. api_v1_data_groups[i].init();
  611. }
  612. }
  613. const char *time_grouping_method2string(RRDR_TIME_GROUPING group) {
  614. int i;
  615. for(i = 0; api_v1_data_groups[i].name ; i++) {
  616. if(api_v1_data_groups[i].value == group) {
  617. return api_v1_data_groups[i].name;
  618. }
  619. }
  620. return "unknown-group-method";
  621. }
  622. RRDR_TIME_GROUPING time_grouping_parse(const char *name, RRDR_TIME_GROUPING def) {
  623. int i;
  624. uint32_t hash = simple_hash(name);
  625. for(i = 0; api_v1_data_groups[i].name ; i++)
  626. if(unlikely(hash == api_v1_data_groups[i].hash && !strcmp(name, api_v1_data_groups[i].name)))
  627. return api_v1_data_groups[i].value;
  628. return def;
  629. }
  630. const char *time_grouping_tostring(RRDR_TIME_GROUPING group) {
  631. int i;
  632. for(i = 0; api_v1_data_groups[i].name ; i++)
  633. if(unlikely(group == api_v1_data_groups[i].value))
  634. return api_v1_data_groups[i].name;
  635. return "unknown";
  636. }
  637. static void rrdr_set_grouping_function(RRDR *r, RRDR_TIME_GROUPING group_method) {
  638. int i, found = 0;
  639. for(i = 0; !found && api_v1_data_groups[i].name ;i++) {
  640. if(api_v1_data_groups[i].value == group_method) {
  641. r->time_grouping.create = api_v1_data_groups[i].create;
  642. r->time_grouping.reset = api_v1_data_groups[i].reset;
  643. r->time_grouping.free = api_v1_data_groups[i].free;
  644. r->time_grouping.add = api_v1_data_groups[i].add;
  645. r->time_grouping.flush = api_v1_data_groups[i].flush;
  646. r->time_grouping.tier_query_fetch = api_v1_data_groups[i].tier_query_fetch;
  647. r->time_grouping.add_flush = api_v1_data_groups[i].add_flush;
  648. found = 1;
  649. }
  650. }
  651. if(!found) {
  652. errno = 0;
  653. internal_error(true, "QUERY: grouping method %u not found. Using 'average'", (unsigned int)group_method);
  654. r->time_grouping.create = tg_average_create;
  655. r->time_grouping.reset = tg_average_reset;
  656. r->time_grouping.free = tg_average_free;
  657. r->time_grouping.add = tg_average_add;
  658. r->time_grouping.flush = tg_average_flush;
  659. r->time_grouping.tier_query_fetch = TIER_QUERY_FETCH_AVERAGE;
  660. r->time_grouping.add_flush = RRDR_GROUPING_AVERAGE;
  661. }
  662. }
  663. static inline void time_grouping_add(RRDR *r, NETDATA_DOUBLE value, const RRDR_TIME_GROUPING add_flush) {
  664. switch(add_flush) {
  665. case RRDR_GROUPING_AVERAGE:
  666. tg_average_add(r, value);
  667. break;
  668. case RRDR_GROUPING_MAX:
  669. tg_max_add(r, value);
  670. break;
  671. case RRDR_GROUPING_MIN:
  672. tg_min_add(r, value);
  673. break;
  674. case RRDR_GROUPING_MEDIAN:
  675. tg_median_add(r, value);
  676. break;
  677. case RRDR_GROUPING_STDDEV:
  678. case RRDR_GROUPING_CV:
  679. tg_stddev_add(r, value);
  680. break;
  681. case RRDR_GROUPING_SUM:
  682. tg_sum_add(r, value);
  683. break;
  684. case RRDR_GROUPING_COUNTIF:
  685. tg_countif_add(r, value);
  686. break;
  687. case RRDR_GROUPING_TRIMMED_MEAN:
  688. tg_trimmed_mean_add(r, value);
  689. break;
  690. case RRDR_GROUPING_PERCENTILE:
  691. tg_percentile_add(r, value);
  692. break;
  693. case RRDR_GROUPING_SES:
  694. tg_ses_add(r, value);
  695. break;
  696. case RRDR_GROUPING_DES:
  697. tg_des_add(r, value);
  698. break;
  699. case RRDR_GROUPING_INCREMENTAL_SUM:
  700. tg_incremental_sum_add(r, value);
  701. break;
  702. default:
  703. r->time_grouping.add(r, value);
  704. break;
  705. }
  706. }
  707. static inline NETDATA_DOUBLE time_grouping_flush(RRDR *r, RRDR_VALUE_FLAGS *rrdr_value_options_ptr, const RRDR_TIME_GROUPING add_flush) {
  708. switch(add_flush) {
  709. case RRDR_GROUPING_AVERAGE:
  710. return tg_average_flush(r, rrdr_value_options_ptr);
  711. case RRDR_GROUPING_MAX:
  712. return tg_max_flush(r, rrdr_value_options_ptr);
  713. case RRDR_GROUPING_MIN:
  714. return tg_min_flush(r, rrdr_value_options_ptr);
  715. case RRDR_GROUPING_MEDIAN:
  716. return tg_median_flush(r, rrdr_value_options_ptr);
  717. case RRDR_GROUPING_STDDEV:
  718. return tg_stddev_flush(r, rrdr_value_options_ptr);
  719. case RRDR_GROUPING_CV:
  720. return tg_stddev_coefficient_of_variation_flush(r, rrdr_value_options_ptr);
  721. case RRDR_GROUPING_SUM:
  722. return tg_sum_flush(r, rrdr_value_options_ptr);
  723. case RRDR_GROUPING_COUNTIF:
  724. return tg_countif_flush(r, rrdr_value_options_ptr);
  725. case RRDR_GROUPING_TRIMMED_MEAN:
  726. return tg_trimmed_mean_flush(r, rrdr_value_options_ptr);
  727. case RRDR_GROUPING_PERCENTILE:
  728. return tg_percentile_flush(r, rrdr_value_options_ptr);
  729. case RRDR_GROUPING_SES:
  730. return tg_ses_flush(r, rrdr_value_options_ptr);
  731. case RRDR_GROUPING_DES:
  732. return tg_des_flush(r, rrdr_value_options_ptr);
  733. case RRDR_GROUPING_INCREMENTAL_SUM:
  734. return tg_incremental_sum_flush(r, rrdr_value_options_ptr);
  735. default:
  736. return r->time_grouping.flush(r, rrdr_value_options_ptr);
  737. }
  738. }
  739. RRDR_GROUP_BY group_by_parse(char *s) {
  740. RRDR_GROUP_BY group_by = RRDR_GROUP_BY_NONE;
  741. while(s) {
  742. char *key = strsep_skip_consecutive_separators(&s, ",| ");
  743. if (!key || !*key) continue;
  744. if (strcmp(key, "selected") == 0)
  745. group_by |= RRDR_GROUP_BY_SELECTED;
  746. if (strcmp(key, "dimension") == 0)
  747. group_by |= RRDR_GROUP_BY_DIMENSION;
  748. if (strcmp(key, "instance") == 0)
  749. group_by |= RRDR_GROUP_BY_INSTANCE;
  750. if (strcmp(key, "percentage-of-instance") == 0)
  751. group_by |= RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE;
  752. if (strcmp(key, "label") == 0)
  753. group_by |= RRDR_GROUP_BY_LABEL;
  754. if (strcmp(key, "node") == 0)
  755. group_by |= RRDR_GROUP_BY_NODE;
  756. if (strcmp(key, "context") == 0)
  757. group_by |= RRDR_GROUP_BY_CONTEXT;
  758. if (strcmp(key, "units") == 0)
  759. group_by |= RRDR_GROUP_BY_UNITS;
  760. }
  761. if((group_by & RRDR_GROUP_BY_SELECTED) && (group_by & ~RRDR_GROUP_BY_SELECTED)) {
  762. internal_error(true, "group-by given by query has 'selected' together with more groupings");
  763. group_by = RRDR_GROUP_BY_SELECTED; // remove all other groupings
  764. }
  765. if(group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  766. group_by = RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE; // remove all other groupings
  767. return group_by;
  768. }
  769. void buffer_json_group_by_to_array(BUFFER *wb, RRDR_GROUP_BY group_by) {
  770. if(group_by == RRDR_GROUP_BY_NONE)
  771. buffer_json_add_array_item_string(wb, "none");
  772. else {
  773. if (group_by & RRDR_GROUP_BY_DIMENSION)
  774. buffer_json_add_array_item_string(wb, "dimension");
  775. if (group_by & RRDR_GROUP_BY_INSTANCE)
  776. buffer_json_add_array_item_string(wb, "instance");
  777. if (group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  778. buffer_json_add_array_item_string(wb, "percentage-of-instance");
  779. if (group_by & RRDR_GROUP_BY_LABEL)
  780. buffer_json_add_array_item_string(wb, "label");
  781. if (group_by & RRDR_GROUP_BY_NODE)
  782. buffer_json_add_array_item_string(wb, "node");
  783. if (group_by & RRDR_GROUP_BY_CONTEXT)
  784. buffer_json_add_array_item_string(wb, "context");
  785. if (group_by & RRDR_GROUP_BY_UNITS)
  786. buffer_json_add_array_item_string(wb, "units");
  787. if (group_by & RRDR_GROUP_BY_SELECTED)
  788. buffer_json_add_array_item_string(wb, "selected");
  789. }
  790. }
  791. RRDR_GROUP_BY_FUNCTION group_by_aggregate_function_parse(const char *s) {
  792. if(strcmp(s, "average") == 0)
  793. return RRDR_GROUP_BY_FUNCTION_AVERAGE;
  794. if(strcmp(s, "avg") == 0)
  795. return RRDR_GROUP_BY_FUNCTION_AVERAGE;
  796. if(strcmp(s, "min") == 0)
  797. return RRDR_GROUP_BY_FUNCTION_MIN;
  798. if(strcmp(s, "max") == 0)
  799. return RRDR_GROUP_BY_FUNCTION_MAX;
  800. if(strcmp(s, "sum") == 0)
  801. return RRDR_GROUP_BY_FUNCTION_SUM;
  802. if(strcmp(s, "percentage") == 0)
  803. return RRDR_GROUP_BY_FUNCTION_PERCENTAGE;
  804. return RRDR_GROUP_BY_FUNCTION_AVERAGE;
  805. }
  806. const char *group_by_aggregate_function_to_string(RRDR_GROUP_BY_FUNCTION group_by_function) {
  807. switch(group_by_function) {
  808. default:
  809. case RRDR_GROUP_BY_FUNCTION_AVERAGE:
  810. return "average";
  811. case RRDR_GROUP_BY_FUNCTION_MIN:
  812. return "min";
  813. case RRDR_GROUP_BY_FUNCTION_MAX:
  814. return "max";
  815. case RRDR_GROUP_BY_FUNCTION_SUM:
  816. return "sum";
  817. case RRDR_GROUP_BY_FUNCTION_PERCENTAGE:
  818. return "percentage";
  819. }
  820. }
  821. // ----------------------------------------------------------------------------
  822. // helpers to find our way in RRDR
  823. static inline RRDR_VALUE_FLAGS *UNUSED_FUNCTION(rrdr_line_options)(RRDR *r, long rrdr_line) {
  824. return &r->o[ rrdr_line * r->d ];
  825. }
  826. static inline NETDATA_DOUBLE *UNUSED_FUNCTION(rrdr_line_values)(RRDR *r, long rrdr_line) {
  827. return &r->v[ rrdr_line * r->d ];
  828. }
  829. static inline long rrdr_line_init(RRDR *r __maybe_unused, time_t t __maybe_unused, long rrdr_line) {
  830. rrdr_line++;
  831. internal_fatal(rrdr_line >= (long)r->n,
  832. "QUERY: requested to step above RRDR size for query '%s'",
  833. r->internal.qt->id);
  834. internal_fatal(r->t[rrdr_line] != t,
  835. "QUERY: wrong timestamp at RRDR line %ld, expected %ld, got %ld, of query '%s'",
  836. rrdr_line, r->t[rrdr_line], t, r->internal.qt->id);
  837. return rrdr_line;
  838. }
  839. // ----------------------------------------------------------------------------
  840. // tier management
  841. static bool query_metric_is_valid_tier(QUERY_METRIC *qm, size_t tier) {
  842. if(!qm->tiers[tier].db_metric_handle || !qm->tiers[tier].db_first_time_s || !qm->tiers[tier].db_last_time_s || !qm->tiers[tier].db_update_every_s)
  843. return false;
  844. return true;
  845. }
  846. static size_t query_metric_first_working_tier(QUERY_METRIC *qm) {
  847. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  848. // find the db time-range for this tier for all metrics
  849. STORAGE_METRIC_HANDLE *db_metric_handle = qm->tiers[tier].db_metric_handle;
  850. time_t first_time_s = qm->tiers[tier].db_first_time_s;
  851. time_t last_time_s = qm->tiers[tier].db_last_time_s;
  852. time_t update_every_s = qm->tiers[tier].db_update_every_s;
  853. if(!db_metric_handle || !first_time_s || !last_time_s || !update_every_s)
  854. continue;
  855. return tier;
  856. }
  857. return 0;
  858. }
  859. static long query_plan_points_coverage_weight(time_t db_first_time_s, time_t db_last_time_s, time_t db_update_every_s, time_t after_wanted, time_t before_wanted, size_t points_wanted, size_t tier __maybe_unused) {
  860. if(db_first_time_s == 0 ||
  861. db_last_time_s == 0 ||
  862. db_update_every_s == 0 ||
  863. db_first_time_s > before_wanted ||
  864. db_last_time_s < after_wanted)
  865. return -LONG_MAX;
  866. long long common_first_t = MAX(db_first_time_s, after_wanted);
  867. long long common_last_t = MIN(db_last_time_s, before_wanted);
  868. long long time_coverage = (common_last_t - common_first_t) * 1000000LL / (before_wanted - after_wanted);
  869. long long points_wanted_in_coverage = (long long)points_wanted * time_coverage / 1000000LL;
  870. long long points_available = (common_last_t - common_first_t) / db_update_every_s;
  871. long long points_delta = (long)(points_available - points_wanted_in_coverage);
  872. long long points_coverage = (points_delta < 0) ? (long)(points_available * time_coverage / points_wanted_in_coverage) : time_coverage;
  873. // a way to benefit higher tiers
  874. // points_coverage += (long)tier * 10000;
  875. if(points_available <= 0)
  876. return -LONG_MAX;
  877. return (long)(points_coverage + (25000LL * tier)); // 2.5% benefit for each higher tier
  878. }
  879. static size_t query_metric_best_tier_for_timeframe(QUERY_METRIC *qm, time_t after_wanted, time_t before_wanted, size_t points_wanted) {
  880. if(unlikely(storage_tiers < 2))
  881. return 0;
  882. if(unlikely(after_wanted == before_wanted || points_wanted <= 0))
  883. return query_metric_first_working_tier(qm);
  884. time_t min_first_time_s = 0;
  885. time_t max_last_time_s = 0;
  886. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  887. time_t first_time_s = qm->tiers[tier].db_first_time_s;
  888. time_t last_time_s = qm->tiers[tier].db_last_time_s;
  889. if(!min_first_time_s || (first_time_s && first_time_s < min_first_time_s))
  890. min_first_time_s = first_time_s;
  891. if(!max_last_time_s || (last_time_s && last_time_s > max_last_time_s))
  892. max_last_time_s = last_time_s;
  893. }
  894. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  895. // find the db time-range for this tier for all metrics
  896. STORAGE_METRIC_HANDLE *db_metric_handle = qm->tiers[tier].db_metric_handle;
  897. time_t first_time_s = qm->tiers[tier].db_first_time_s;
  898. time_t last_time_s = qm->tiers[tier].db_last_time_s;
  899. time_t update_every_s = qm->tiers[tier].db_update_every_s;
  900. if( !db_metric_handle ||
  901. !first_time_s ||
  902. !last_time_s ||
  903. !update_every_s ||
  904. first_time_s > before_wanted ||
  905. last_time_s < after_wanted
  906. ) {
  907. qm->tiers[tier].weight = -LONG_MAX;
  908. continue;
  909. }
  910. internal_fatal(first_time_s > before_wanted || last_time_s < after_wanted, "QUERY: invalid db durations");
  911. qm->tiers[tier].weight = query_plan_points_coverage_weight(
  912. min_first_time_s, max_last_time_s, update_every_s,
  913. after_wanted, before_wanted, points_wanted, tier);
  914. }
  915. size_t best_tier = 0;
  916. for(size_t tier = 1; tier < storage_tiers ; tier++) {
  917. if(qm->tiers[tier].weight >= qm->tiers[best_tier].weight)
  918. best_tier = tier;
  919. }
  920. return best_tier;
  921. }
  922. static size_t rrddim_find_best_tier_for_timeframe(QUERY_TARGET *qt, time_t after_wanted, time_t before_wanted, size_t points_wanted) {
  923. if(unlikely(storage_tiers < 2))
  924. return 0;
  925. if(unlikely(after_wanted == before_wanted || points_wanted <= 0)) {
  926. internal_error(true, "QUERY: '%s' has invalid params to tier calculation", qt->id);
  927. return 0;
  928. }
  929. long weight[storage_tiers];
  930. for(size_t tier = 0; tier < storage_tiers ; tier++) {
  931. time_t common_first_time_s = 0;
  932. time_t common_last_time_s = 0;
  933. time_t common_update_every_s = 0;
  934. // find the db time-range for this tier for all metrics
  935. for(size_t i = 0, used = qt->query.used; i < used ; i++) {
  936. QUERY_METRIC *qm = query_metric(qt, i);
  937. time_t first_time_s = qm->tiers[tier].db_first_time_s;
  938. time_t last_time_s = qm->tiers[tier].db_last_time_s;
  939. time_t update_every_s = qm->tiers[tier].db_update_every_s;
  940. if(!first_time_s || !last_time_s || !update_every_s)
  941. continue;
  942. if(!common_first_time_s)
  943. common_first_time_s = first_time_s;
  944. else
  945. common_first_time_s = MIN(first_time_s, common_first_time_s);
  946. if(!common_last_time_s)
  947. common_last_time_s = last_time_s;
  948. else
  949. common_last_time_s = MAX(last_time_s, common_last_time_s);
  950. if(!common_update_every_s)
  951. common_update_every_s = update_every_s;
  952. else
  953. common_update_every_s = MIN(update_every_s, common_update_every_s);
  954. }
  955. weight[tier] = query_plan_points_coverage_weight(common_first_time_s, common_last_time_s, common_update_every_s, after_wanted, before_wanted, points_wanted, tier);
  956. }
  957. size_t best_tier = 0;
  958. for(size_t tier = 1; tier < storage_tiers ; tier++) {
  959. if(weight[tier] >= weight[best_tier])
  960. best_tier = tier;
  961. }
  962. if(weight[best_tier] == -LONG_MAX)
  963. best_tier = 0;
  964. return best_tier;
  965. }
  966. static time_t rrdset_find_natural_update_every_for_timeframe(QUERY_TARGET *qt, time_t after_wanted, time_t before_wanted, size_t points_wanted, RRDR_OPTIONS options, size_t tier) {
  967. size_t best_tier;
  968. if((options & RRDR_OPTION_SELECTED_TIER) && tier < storage_tiers)
  969. best_tier = tier;
  970. else
  971. best_tier = rrddim_find_best_tier_for_timeframe(qt, after_wanted, before_wanted, points_wanted);
  972. // find the db minimum update every for this tier for all metrics
  973. time_t common_update_every_s = default_rrd_update_every;
  974. for(size_t i = 0, used = qt->query.used; i < used ; i++) {
  975. QUERY_METRIC *qm = query_metric(qt, i);
  976. time_t update_every_s = qm->tiers[best_tier].db_update_every_s;
  977. if(!i)
  978. common_update_every_s = update_every_s;
  979. else
  980. common_update_every_s = MIN(update_every_s, common_update_every_s);
  981. }
  982. return common_update_every_s;
  983. }
  984. // ----------------------------------------------------------------------------
  985. // query ops
  986. typedef struct query_point {
  987. STORAGE_POINT sp;
  988. NETDATA_DOUBLE value;
  989. bool added;
  990. #ifdef NETDATA_INTERNAL_CHECKS
  991. size_t id;
  992. #endif
  993. } QUERY_POINT;
  994. QUERY_POINT QUERY_POINT_EMPTY = {
  995. .sp = STORAGE_POINT_UNSET,
  996. .value = NAN,
  997. .added = false,
  998. #ifdef NETDATA_INTERNAL_CHECKS
  999. .id = 0,
  1000. #endif
  1001. };
  1002. #ifdef NETDATA_INTERNAL_CHECKS
  1003. #define query_point_set_id(point, point_id) (point).id = point_id
  1004. #else
  1005. #define query_point_set_id(point, point_id) debug_dummy()
  1006. #endif
  1007. typedef struct query_engine_ops {
  1008. // configuration
  1009. RRDR *r;
  1010. QUERY_METRIC *qm;
  1011. time_t view_update_every;
  1012. time_t query_granularity;
  1013. TIER_QUERY_FETCH tier_query_fetch;
  1014. // query planer
  1015. size_t current_plan;
  1016. time_t current_plan_expire_time;
  1017. time_t plan_expanded_after;
  1018. time_t plan_expanded_before;
  1019. // storage queries
  1020. size_t tier;
  1021. struct query_metric_tier *tier_ptr;
  1022. struct storage_engine_query_handle *handle;
  1023. // aggregating points over time
  1024. size_t group_points_non_zero;
  1025. size_t group_points_added;
  1026. STORAGE_POINT group_point; // aggregates min, max, sum, count, anomaly count for each group point
  1027. STORAGE_POINT query_point; // aggregates min, max, sum, count, anomaly count across the whole query
  1028. RRDR_VALUE_FLAGS group_value_flags;
  1029. // statistics
  1030. size_t db_total_points_read;
  1031. size_t db_points_read_per_tier[RRD_STORAGE_TIERS];
  1032. struct {
  1033. time_t expanded_after;
  1034. time_t expanded_before;
  1035. struct storage_engine_query_handle handle;
  1036. bool initialized;
  1037. bool finalized;
  1038. } plans[QUERY_PLANS_MAX];
  1039. struct query_engine_ops *next;
  1040. } QUERY_ENGINE_OPS;
  1041. // ----------------------------------------------------------------------------
  1042. // query planer
  1043. #define query_plan_should_switch_plan(ops, now) ((now) >= (ops)->current_plan_expire_time)
  1044. static size_t query_planer_expand_duration_in_points(time_t this_update_every, time_t next_update_every) {
  1045. time_t delta = this_update_every - next_update_every;
  1046. if(delta < 0) delta = -delta;
  1047. size_t points;
  1048. if(delta < this_update_every * POINTS_TO_EXPAND_QUERY)
  1049. points = POINTS_TO_EXPAND_QUERY;
  1050. else
  1051. points = (delta + this_update_every - 1) / this_update_every;
  1052. return points;
  1053. }
  1054. static void query_planer_initialize_plans(QUERY_ENGINE_OPS *ops) {
  1055. QUERY_METRIC *qm = ops->qm;
  1056. for(size_t p = 0; p < qm->plan.used ; p++) {
  1057. size_t tier = qm->plan.array[p].tier;
  1058. time_t update_every = qm->tiers[tier].db_update_every_s;
  1059. size_t points_to_add_to_after;
  1060. if(p > 0) {
  1061. // there is another plan before to this
  1062. size_t tier0 = qm->plan.array[p - 1].tier;
  1063. time_t update_every0 = qm->tiers[tier0].db_update_every_s;
  1064. points_to_add_to_after = query_planer_expand_duration_in_points(update_every, update_every0);
  1065. }
  1066. else
  1067. points_to_add_to_after = (tier == 0) ? 0 : POINTS_TO_EXPAND_QUERY;
  1068. size_t points_to_add_to_before;
  1069. if(p + 1 < qm->plan.used) {
  1070. // there is another plan after to this
  1071. size_t tier1 = qm->plan.array[p+1].tier;
  1072. time_t update_every1 = qm->tiers[tier1].db_update_every_s;
  1073. points_to_add_to_before = query_planer_expand_duration_in_points(update_every, update_every1);
  1074. }
  1075. else
  1076. points_to_add_to_before = POINTS_TO_EXPAND_QUERY;
  1077. time_t after = qm->plan.array[p].after - (time_t)(update_every * points_to_add_to_after);
  1078. time_t before = qm->plan.array[p].before + (time_t)(update_every * points_to_add_to_before);
  1079. ops->plans[p].expanded_after = after;
  1080. ops->plans[p].expanded_before = before;
  1081. ops->r->internal.qt->db.tiers[tier].queries++;
  1082. struct query_metric_tier *tier_ptr = &qm->tiers[tier];
  1083. STORAGE_ENGINE *eng = query_metric_storage_engine(ops->r->internal.qt, qm, tier);
  1084. storage_engine_query_init(eng->backend, tier_ptr->db_metric_handle, &ops->plans[p].handle,
  1085. after, before, ops->r->internal.qt->request.priority);
  1086. ops->plans[p].initialized = true;
  1087. ops->plans[p].finalized = false;
  1088. }
  1089. }
  1090. static void query_planer_finalize_plan(QUERY_ENGINE_OPS *ops, size_t plan_id) {
  1091. // QUERY_METRIC *qm = ops->qm;
  1092. if(ops->plans[plan_id].initialized && !ops->plans[plan_id].finalized) {
  1093. storage_engine_query_finalize(&ops->plans[plan_id].handle);
  1094. ops->plans[plan_id].initialized = false;
  1095. ops->plans[plan_id].finalized = true;
  1096. }
  1097. }
  1098. static void query_planer_finalize_remaining_plans(QUERY_ENGINE_OPS *ops) {
  1099. QUERY_METRIC *qm = ops->qm;
  1100. for(size_t p = 0; p < qm->plan.used ; p++)
  1101. query_planer_finalize_plan(ops, p);
  1102. }
  1103. static void query_planer_activate_plan(QUERY_ENGINE_OPS *ops, size_t plan_id, time_t overwrite_after __maybe_unused) {
  1104. QUERY_METRIC *qm = ops->qm;
  1105. internal_fatal(plan_id >= qm->plan.used, "QUERY: invalid plan_id given");
  1106. internal_fatal(!ops->plans[plan_id].initialized, "QUERY: plan has not been initialized");
  1107. internal_fatal(ops->plans[plan_id].finalized, "QUERY: plan has been finalized");
  1108. internal_fatal(qm->plan.array[plan_id].after > qm->plan.array[plan_id].before, "QUERY: flipped after/before");
  1109. ops->tier = qm->plan.array[plan_id].tier;
  1110. ops->tier_ptr = &qm->tiers[ops->tier];
  1111. ops->handle = &ops->plans[plan_id].handle;
  1112. ops->current_plan = plan_id;
  1113. if(plan_id + 1 < qm->plan.used && qm->plan.array[plan_id + 1].after < qm->plan.array[plan_id].before)
  1114. ops->current_plan_expire_time = qm->plan.array[plan_id + 1].after;
  1115. else
  1116. ops->current_plan_expire_time = qm->plan.array[plan_id].before;
  1117. ops->plan_expanded_after = ops->plans[plan_id].expanded_after;
  1118. ops->plan_expanded_before = ops->plans[plan_id].expanded_before;
  1119. }
  1120. static bool query_planer_next_plan(QUERY_ENGINE_OPS *ops, time_t now, time_t last_point_end_time) {
  1121. QUERY_METRIC *qm = ops->qm;
  1122. size_t old_plan = ops->current_plan;
  1123. time_t next_plan_before_time;
  1124. do {
  1125. ops->current_plan++;
  1126. if (ops->current_plan >= qm->plan.used) {
  1127. ops->current_plan = old_plan;
  1128. ops->current_plan_expire_time = ops->r->internal.qt->window.before;
  1129. // let the query run with current plan
  1130. // we will not switch it
  1131. return false;
  1132. }
  1133. next_plan_before_time = qm->plan.array[ops->current_plan].before;
  1134. } while(now >= next_plan_before_time || last_point_end_time >= next_plan_before_time);
  1135. if(!query_metric_is_valid_tier(qm, qm->plan.array[ops->current_plan].tier)) {
  1136. ops->current_plan = old_plan;
  1137. ops->current_plan_expire_time = ops->r->internal.qt->window.before;
  1138. return false;
  1139. }
  1140. query_planer_finalize_plan(ops, old_plan);
  1141. query_planer_activate_plan(ops, ops->current_plan, MIN(now, last_point_end_time));
  1142. return true;
  1143. }
  1144. static int compare_query_plan_entries_on_start_time(const void *a, const void *b) {
  1145. QUERY_PLAN_ENTRY *p1 = (QUERY_PLAN_ENTRY *)a;
  1146. QUERY_PLAN_ENTRY *p2 = (QUERY_PLAN_ENTRY *)b;
  1147. return (p1->after < p2->after)?-1:1;
  1148. }
  1149. static bool query_plan(QUERY_ENGINE_OPS *ops, time_t after_wanted, time_t before_wanted, size_t points_wanted) {
  1150. QUERY_METRIC *qm = ops->qm;
  1151. // put our selected tier as the first plan
  1152. size_t selected_tier;
  1153. bool switch_tiers = true;
  1154. if((ops->r->internal.qt->window.options & RRDR_OPTION_SELECTED_TIER)
  1155. && ops->r->internal.qt->window.tier < storage_tiers
  1156. && query_metric_is_valid_tier(qm, ops->r->internal.qt->window.tier)) {
  1157. selected_tier = ops->r->internal.qt->window.tier;
  1158. switch_tiers = false;
  1159. }
  1160. else {
  1161. selected_tier = query_metric_best_tier_for_timeframe(qm, after_wanted, before_wanted, points_wanted);
  1162. if(!query_metric_is_valid_tier(qm, selected_tier))
  1163. return false;
  1164. if(qm->tiers[selected_tier].db_first_time_s > before_wanted ||
  1165. qm->tiers[selected_tier].db_last_time_s < after_wanted)
  1166. return false;
  1167. }
  1168. qm->plan.used = 1;
  1169. qm->plan.array[0].tier = selected_tier;
  1170. qm->plan.array[0].after = (qm->tiers[selected_tier].db_first_time_s < after_wanted) ? after_wanted : qm->tiers[selected_tier].db_first_time_s;
  1171. qm->plan.array[0].before = (qm->tiers[selected_tier].db_last_time_s > before_wanted) ? before_wanted : qm->tiers[selected_tier].db_last_time_s;
  1172. if(switch_tiers) {
  1173. // the selected tier
  1174. time_t selected_tier_first_time_s = qm->plan.array[0].after;
  1175. time_t selected_tier_last_time_s = qm->plan.array[0].before;
  1176. // check if our selected tier can start the query
  1177. if (selected_tier_first_time_s > after_wanted) {
  1178. // we need some help from other tiers
  1179. for (size_t tr = (int)selected_tier + 1; tr < storage_tiers && qm->plan.used < QUERY_PLANS_MAX ; tr++) {
  1180. if(!query_metric_is_valid_tier(qm, tr))
  1181. continue;
  1182. // find the first time of this tier
  1183. time_t tier_first_time_s = qm->tiers[tr].db_first_time_s;
  1184. // can it help?
  1185. if (tier_first_time_s < selected_tier_first_time_s) {
  1186. // it can help us add detail at the beginning of the query
  1187. QUERY_PLAN_ENTRY t = {
  1188. .tier = tr,
  1189. .after = (tier_first_time_s < after_wanted) ? after_wanted : tier_first_time_s,
  1190. .before = selected_tier_first_time_s,
  1191. };
  1192. ops->plans[qm->plan.used].initialized = false;
  1193. ops->plans[qm->plan.used].finalized = false;
  1194. qm->plan.array[qm->plan.used++] = t;
  1195. internal_fatal(!t.after || !t.before, "QUERY: invalid plan selected");
  1196. // prepare for the tier
  1197. selected_tier_first_time_s = t.after;
  1198. if (t.after <= after_wanted)
  1199. break;
  1200. }
  1201. }
  1202. }
  1203. // check if our selected tier can finish the query
  1204. if (selected_tier_last_time_s < before_wanted) {
  1205. // we need some help from other tiers
  1206. for (int tr = (int)selected_tier - 1; tr >= 0 && qm->plan.used < QUERY_PLANS_MAX ; tr--) {
  1207. if(!query_metric_is_valid_tier(qm, tr))
  1208. continue;
  1209. // find the last time of this tier
  1210. time_t tier_last_time_s = qm->tiers[tr].db_last_time_s;
  1211. //buffer_sprintf(wb, ": EVAL BEFORE tier %d, %ld", tier, last_time_s);
  1212. // can it help?
  1213. if (tier_last_time_s > selected_tier_last_time_s) {
  1214. // it can help us add detail at the end of the query
  1215. QUERY_PLAN_ENTRY t = {
  1216. .tier = tr,
  1217. .after = selected_tier_last_time_s,
  1218. .before = (tier_last_time_s > before_wanted) ? before_wanted : tier_last_time_s,
  1219. };
  1220. ops->plans[qm->plan.used].initialized = false;
  1221. ops->plans[qm->plan.used].finalized = false;
  1222. qm->plan.array[qm->plan.used++] = t;
  1223. // prepare for the tier
  1224. selected_tier_last_time_s = t.before;
  1225. internal_fatal(!t.after || !t.before, "QUERY: invalid plan selected");
  1226. if (t.before >= before_wanted)
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. }
  1232. // sort the query plan
  1233. if(qm->plan.used > 1)
  1234. qsort(&qm->plan.array, qm->plan.used, sizeof(QUERY_PLAN_ENTRY), compare_query_plan_entries_on_start_time);
  1235. if(!query_metric_is_valid_tier(qm, qm->plan.array[0].tier))
  1236. return false;
  1237. #ifdef NETDATA_INTERNAL_CHECKS
  1238. for(size_t p = 0; p < qm->plan.used ;p++) {
  1239. internal_fatal(qm->plan.array[p].after > qm->plan.array[p].before, "QUERY: flipped after/before");
  1240. internal_fatal(qm->plan.array[p].after < after_wanted, "QUERY: too small plan first time");
  1241. internal_fatal(qm->plan.array[p].before > before_wanted, "QUERY: too big plan last time");
  1242. }
  1243. #endif
  1244. query_planer_initialize_plans(ops);
  1245. query_planer_activate_plan(ops, 0, 0);
  1246. return true;
  1247. }
  1248. // ----------------------------------------------------------------------------
  1249. // dimension level query engine
  1250. #define query_interpolate_point(this_point, last_point, now) do { \
  1251. if(likely( \
  1252. /* the point to interpolate is more than 1s wide */ \
  1253. (this_point).sp.end_time_s - (this_point).sp.start_time_s > 1 \
  1254. \
  1255. /* the two points are exactly next to each other */ \
  1256. && (last_point).sp.end_time_s == (this_point).sp.start_time_s \
  1257. \
  1258. /* both points are valid numbers */ \
  1259. && netdata_double_isnumber((this_point).value) \
  1260. && netdata_double_isnumber((last_point).value) \
  1261. \
  1262. )) { \
  1263. (this_point).value = (last_point).value + ((this_point).value - (last_point).value) * (1.0 - (NETDATA_DOUBLE)((this_point).sp.end_time_s - (now)) / (NETDATA_DOUBLE)((this_point).sp.end_time_s - (this_point).sp.start_time_s)); \
  1264. (this_point).sp.end_time_s = now; \
  1265. } \
  1266. } while(0)
  1267. #define query_add_point_to_group(r, point, ops, add_flush) do { \
  1268. if(likely(netdata_double_isnumber((point).value))) { \
  1269. if(likely(fpclassify((point).value) != FP_ZERO)) \
  1270. (ops)->group_points_non_zero++; \
  1271. \
  1272. if(unlikely((point).sp.flags & SN_FLAG_RESET)) \
  1273. (ops)->group_value_flags |= RRDR_VALUE_RESET; \
  1274. \
  1275. time_grouping_add(r, (point).value, add_flush); \
  1276. \
  1277. storage_point_merge_to((ops)->group_point, (point).sp); \
  1278. if(!(point).added) \
  1279. storage_point_merge_to((ops)->query_point, (point).sp); \
  1280. } \
  1281. \
  1282. (ops)->group_points_added++; \
  1283. } while(0)
  1284. static __thread QUERY_ENGINE_OPS *released_ops = NULL;
  1285. static void rrd2rrdr_query_ops_freeall(RRDR *r __maybe_unused) {
  1286. while(released_ops) {
  1287. QUERY_ENGINE_OPS *ops = released_ops;
  1288. released_ops = ops->next;
  1289. onewayalloc_freez(r->internal.owa, ops);
  1290. }
  1291. }
  1292. static void rrd2rrdr_query_ops_release(QUERY_ENGINE_OPS *ops) {
  1293. if(!ops) return;
  1294. ops->next = released_ops;
  1295. released_ops = ops;
  1296. }
  1297. static QUERY_ENGINE_OPS *rrd2rrdr_query_ops_get(RRDR *r) {
  1298. QUERY_ENGINE_OPS *ops;
  1299. if(released_ops) {
  1300. ops = released_ops;
  1301. released_ops = ops->next;
  1302. }
  1303. else {
  1304. ops = onewayalloc_mallocz(r->internal.owa, sizeof(QUERY_ENGINE_OPS));
  1305. }
  1306. memset(ops, 0, sizeof(*ops));
  1307. return ops;
  1308. }
  1309. static QUERY_ENGINE_OPS *rrd2rrdr_query_ops_prep(RRDR *r, size_t query_metric_id) {
  1310. QUERY_TARGET *qt = r->internal.qt;
  1311. QUERY_ENGINE_OPS *ops = rrd2rrdr_query_ops_get(r);
  1312. *ops = (QUERY_ENGINE_OPS) {
  1313. .r = r,
  1314. .qm = query_metric(qt, query_metric_id),
  1315. .tier_query_fetch = r->time_grouping.tier_query_fetch,
  1316. .view_update_every = r->view.update_every,
  1317. .query_granularity = (time_t)(r->view.update_every / r->view.group),
  1318. .group_value_flags = RRDR_VALUE_NOTHING,
  1319. };
  1320. if(!query_plan(ops, qt->window.after, qt->window.before, qt->window.points)) {
  1321. rrd2rrdr_query_ops_release(ops);
  1322. return NULL;
  1323. }
  1324. return ops;
  1325. }
  1326. static void rrd2rrdr_query_execute(RRDR *r, size_t dim_id_in_rrdr, QUERY_ENGINE_OPS *ops) {
  1327. QUERY_TARGET *qt = r->internal.qt;
  1328. QUERY_METRIC *qm = ops->qm;
  1329. const RRDR_TIME_GROUPING add_flush = r->time_grouping.add_flush;
  1330. ops->group_point = STORAGE_POINT_UNSET;
  1331. ops->query_point = STORAGE_POINT_UNSET;
  1332. RRDR_OPTIONS options = qt->window.options;
  1333. size_t points_wanted = qt->window.points;
  1334. time_t after_wanted = qt->window.after;
  1335. time_t before_wanted = qt->window.before; (void)before_wanted;
  1336. // bool debug_this = false;
  1337. // if(strcmp("user", string2str(rd->id)) == 0 && strcmp("system.cpu", string2str(rd->rrdset->id)) == 0)
  1338. // debug_this = true;
  1339. size_t points_added = 0;
  1340. long rrdr_line = -1;
  1341. bool use_anomaly_bit_as_value = (r->internal.qt->window.options & RRDR_OPTION_ANOMALY_BIT) ? true : false;
  1342. NETDATA_DOUBLE min = r->view.min, max = r->view.max;
  1343. QUERY_POINT last2_point = QUERY_POINT_EMPTY;
  1344. QUERY_POINT last1_point = QUERY_POINT_EMPTY;
  1345. QUERY_POINT new_point = QUERY_POINT_EMPTY;
  1346. // ONE POINT READ-AHEAD
  1347. // when we switch plans, we read-ahead a point from the next plan
  1348. // to join them smoothly at the exact time the next plan begins
  1349. STORAGE_POINT next1_point = STORAGE_POINT_UNSET;
  1350. time_t now_start_time = after_wanted - ops->query_granularity;
  1351. time_t now_end_time = after_wanted + ops->view_update_every - ops->query_granularity;
  1352. size_t db_points_read_since_plan_switch = 0; (void)db_points_read_since_plan_switch;
  1353. size_t query_is_finished_counter = 0;
  1354. // The main loop, based on the query granularity we need
  1355. for( ; points_added < points_wanted && query_is_finished_counter <= 10 ;
  1356. now_start_time = now_end_time, now_end_time += ops->view_update_every) {
  1357. if(unlikely(query_plan_should_switch_plan(ops, now_end_time))) {
  1358. query_planer_next_plan(ops, now_end_time, new_point.sp.end_time_s);
  1359. db_points_read_since_plan_switch = 0;
  1360. }
  1361. // read all the points of the db, prior to the time we need (now_end_time)
  1362. size_t count_same_end_time = 0;
  1363. while(count_same_end_time < 100) {
  1364. if(likely(count_same_end_time == 0)) {
  1365. last2_point = last1_point;
  1366. last1_point = new_point;
  1367. }
  1368. if(unlikely(storage_engine_query_is_finished(ops->handle))) {
  1369. query_is_finished_counter++;
  1370. if(count_same_end_time != 0) {
  1371. last2_point = last1_point;
  1372. last1_point = new_point;
  1373. }
  1374. new_point = QUERY_POINT_EMPTY;
  1375. new_point.sp.start_time_s = last1_point.sp.end_time_s;
  1376. new_point.sp.end_time_s = now_end_time;
  1377. //
  1378. // if(debug_this) netdata_log_info("QUERY: is finished() returned true");
  1379. //
  1380. break;
  1381. }
  1382. else
  1383. query_is_finished_counter = 0;
  1384. // fetch the new point
  1385. {
  1386. STORAGE_POINT sp;
  1387. if(likely(storage_point_is_unset(next1_point))) {
  1388. db_points_read_since_plan_switch++;
  1389. sp = storage_engine_query_next_metric(ops->handle);
  1390. ops->db_points_read_per_tier[ops->tier]++;
  1391. ops->db_total_points_read++;
  1392. if(unlikely(options & RRDR_OPTION_ABSOLUTE))
  1393. storage_point_make_positive(sp);
  1394. }
  1395. else {
  1396. // ONE POINT READ-AHEAD
  1397. sp = next1_point;
  1398. storage_point_unset(next1_point);
  1399. db_points_read_since_plan_switch = 1;
  1400. }
  1401. // ONE POINT READ-AHEAD
  1402. if(unlikely(query_plan_should_switch_plan(ops, sp.end_time_s) &&
  1403. query_planer_next_plan(ops, now_end_time, new_point.sp.end_time_s))) {
  1404. // The end time of the current point, crosses our plans (tiers)
  1405. // so, we switched plan (tier)
  1406. //
  1407. // There are 2 cases now:
  1408. //
  1409. // A. the entire point of the previous plan is to the future of point from the next plan
  1410. // B. part of the point of the previous plan overlaps with the point from the next plan
  1411. STORAGE_POINT sp2 = storage_engine_query_next_metric(ops->handle);
  1412. ops->db_points_read_per_tier[ops->tier]++;
  1413. ops->db_total_points_read++;
  1414. if(unlikely(options & RRDR_OPTION_ABSOLUTE))
  1415. storage_point_make_positive(sp);
  1416. if(sp.start_time_s > sp2.start_time_s)
  1417. // the point from the previous plan is useless
  1418. sp = sp2;
  1419. else
  1420. // let the query run from the previous plan
  1421. // but setting this will also cut off the interpolation
  1422. // of the point from the previous plan
  1423. next1_point = sp2;
  1424. }
  1425. new_point.sp = sp;
  1426. new_point.added = false;
  1427. query_point_set_id(new_point, ops->db_total_points_read);
  1428. // if(debug_this)
  1429. // netdata_log_info("QUERY: got point %zu, from time %ld to %ld // now from %ld to %ld // query from %ld to %ld",
  1430. // new_point.id, new_point.start_time, new_point.end_time, now_start_time, now_end_time, after_wanted, before_wanted);
  1431. //
  1432. // get the right value from the point we got
  1433. if(likely(!storage_point_is_unset(sp) && !storage_point_is_gap(sp))) {
  1434. if(unlikely(use_anomaly_bit_as_value))
  1435. new_point.value = storage_point_anomaly_rate(new_point.sp);
  1436. else {
  1437. switch (ops->tier_query_fetch) {
  1438. default:
  1439. case TIER_QUERY_FETCH_AVERAGE:
  1440. new_point.value = sp.sum / (NETDATA_DOUBLE)sp.count;
  1441. break;
  1442. case TIER_QUERY_FETCH_MIN:
  1443. new_point.value = sp.min;
  1444. break;
  1445. case TIER_QUERY_FETCH_MAX:
  1446. new_point.value = sp.max;
  1447. break;
  1448. case TIER_QUERY_FETCH_SUM:
  1449. new_point.value = sp.sum;
  1450. break;
  1451. };
  1452. }
  1453. }
  1454. else
  1455. new_point.value = NAN;
  1456. }
  1457. // check if the db is giving us zero duration points
  1458. if(unlikely(db_points_read_since_plan_switch > 1 &&
  1459. new_point.sp.start_time_s == new_point.sp.end_time_s)) {
  1460. internal_error(true, "QUERY: '%s', dimension '%s' next_metric() returned "
  1461. "point %zu from %ld to %ld, that are both equal",
  1462. qt->id, query_metric_id(qt, qm),
  1463. new_point.id, new_point.sp.start_time_s, new_point.sp.end_time_s);
  1464. new_point.sp.start_time_s = new_point.sp.end_time_s - ops->tier_ptr->db_update_every_s;
  1465. }
  1466. // check if the db is advancing the query
  1467. if(unlikely(db_points_read_since_plan_switch > 1 &&
  1468. new_point.sp.end_time_s <= last1_point.sp.end_time_s)) {
  1469. internal_error(true,
  1470. "QUERY: '%s', dimension '%s' next_metric() returned "
  1471. "point %zu from %ld to %ld, before the "
  1472. "last point %zu from %ld to %ld, "
  1473. "now is %ld to %ld",
  1474. qt->id, query_metric_id(qt, qm),
  1475. new_point.id, new_point.sp.start_time_s, new_point.sp.end_time_s,
  1476. last1_point.id, last1_point.sp.start_time_s, last1_point.sp.end_time_s,
  1477. now_start_time, now_end_time);
  1478. count_same_end_time++;
  1479. continue;
  1480. }
  1481. count_same_end_time = 0;
  1482. // decide how to use this point
  1483. if(likely(new_point.sp.end_time_s < now_end_time)) { // likely to favor tier0
  1484. // this db point ends before our now_end_time
  1485. if(likely(new_point.sp.end_time_s >= now_start_time)) { // likely to favor tier0
  1486. // this db point ends after our now_start time
  1487. query_add_point_to_group(r, new_point, ops, add_flush);
  1488. new_point.added = true;
  1489. }
  1490. else {
  1491. // we don't need this db point
  1492. // it is totally outside our current time-frame
  1493. // this is desirable for the first point of the query
  1494. // because it allows us to interpolate the next point
  1495. // at exactly the time we will want
  1496. // we only log if this is not point 1
  1497. internal_error(new_point.sp.end_time_s < ops->plan_expanded_after &&
  1498. db_points_read_since_plan_switch > 1,
  1499. "QUERY: '%s', dimension '%s' next_metric() "
  1500. "returned point %zu from %ld time %ld, "
  1501. "which is entirely before our current timeframe %ld to %ld "
  1502. "(and before the entire query, after %ld, before %ld)",
  1503. qt->id, query_metric_id(qt, qm),
  1504. new_point.id, new_point.sp.start_time_s, new_point.sp.end_time_s,
  1505. now_start_time, now_end_time,
  1506. ops->plan_expanded_after, ops->plan_expanded_before);
  1507. }
  1508. }
  1509. else {
  1510. // the point ends in the future
  1511. // so, we will interpolate it below, at the inner loop
  1512. break;
  1513. }
  1514. }
  1515. if(unlikely(count_same_end_time)) {
  1516. internal_error(true,
  1517. "QUERY: '%s', dimension '%s', the database does not advance the query,"
  1518. " it returned an end time less or equal to the end time of the last "
  1519. "point we got %ld, %zu times",
  1520. qt->id, query_metric_id(qt, qm),
  1521. last1_point.sp.end_time_s, count_same_end_time);
  1522. if(unlikely(new_point.sp.end_time_s <= last1_point.sp.end_time_s))
  1523. new_point.sp.end_time_s = now_end_time;
  1524. }
  1525. time_t stop_time = new_point.sp.end_time_s;
  1526. if(unlikely(!storage_point_is_unset(next1_point) && next1_point.start_time_s >= now_end_time)) {
  1527. // ONE POINT READ-AHEAD
  1528. // the point crosses the start time of the
  1529. // read ahead storage point we have read
  1530. stop_time = next1_point.start_time_s;
  1531. }
  1532. // the inner loop
  1533. // we have 3 points in memory: last2, last1, new
  1534. // we select the one to use based on their timestamps
  1535. internal_fatal(now_end_time > stop_time || points_added >= points_wanted,
  1536. "QUERY: first part of query provides invalid point to interpolate (now_end_time %ld, stop_time %ld",
  1537. now_end_time, stop_time);
  1538. do {
  1539. // now_start_time is wrong in this loop
  1540. // but, we don't need it
  1541. QUERY_POINT current_point;
  1542. if(likely(now_end_time > new_point.sp.start_time_s)) {
  1543. // it is time for our NEW point to be used
  1544. current_point = new_point;
  1545. new_point.added = true; // first copy, then set it, so that new_point will not be added again
  1546. query_interpolate_point(current_point, last1_point, now_end_time);
  1547. // internal_error(current_point.id > 0
  1548. // && last1_point.id == 0
  1549. // && current_point.end_time > after_wanted
  1550. // && current_point.end_time > now_end_time,
  1551. // "QUERY: '%s', dimension '%s', after %ld, before %ld, view update every %ld,"
  1552. // " query granularity %ld, interpolating point %zu (from %ld to %ld) at %ld,"
  1553. // " but we could really favor by having last_point1 in this query.",
  1554. // qt->id, string2str(qm->dimension.id),
  1555. // after_wanted, before_wanted,
  1556. // ops.view_update_every, ops.query_granularity,
  1557. // current_point.id, current_point.start_time, current_point.end_time,
  1558. // now_end_time);
  1559. }
  1560. else if(likely(now_end_time <= last1_point.sp.end_time_s)) {
  1561. // our LAST point is still valid
  1562. current_point = last1_point;
  1563. last1_point.added = true; // first copy, then set it, so that last1_point will not be added again
  1564. query_interpolate_point(current_point, last2_point, now_end_time);
  1565. // internal_error(current_point.id > 0
  1566. // && last2_point.id == 0
  1567. // && current_point.end_time > after_wanted
  1568. // && current_point.end_time > now_end_time,
  1569. // "QUERY: '%s', dimension '%s', after %ld, before %ld, view update every %ld,"
  1570. // " query granularity %ld, interpolating point %zu (from %ld to %ld) at %ld,"
  1571. // " but we could really favor by having last_point2 in this query.",
  1572. // qt->id, string2str(qm->dimension.id),
  1573. // after_wanted, before_wanted, ops.view_update_every, ops.query_granularity,
  1574. // current_point.id, current_point.start_time, current_point.end_time,
  1575. // now_end_time);
  1576. }
  1577. else {
  1578. // a GAP, we don't have a value this time
  1579. current_point = QUERY_POINT_EMPTY;
  1580. }
  1581. query_add_point_to_group(r, current_point, ops, add_flush);
  1582. rrdr_line = rrdr_line_init(r, now_end_time, rrdr_line);
  1583. size_t rrdr_o_v_index = rrdr_line * r->d + dim_id_in_rrdr;
  1584. // find the place to store our values
  1585. RRDR_VALUE_FLAGS *rrdr_value_options_ptr = &r->o[rrdr_o_v_index];
  1586. // update the dimension options
  1587. if(likely(ops->group_points_non_zero))
  1588. r->od[dim_id_in_rrdr] |= RRDR_DIMENSION_NONZERO;
  1589. // store the specific point options
  1590. *rrdr_value_options_ptr = ops->group_value_flags;
  1591. // store the group value
  1592. NETDATA_DOUBLE group_value = time_grouping_flush(r, rrdr_value_options_ptr, add_flush);
  1593. r->v[rrdr_o_v_index] = group_value;
  1594. r->ar[rrdr_o_v_index] = storage_point_anomaly_rate(ops->group_point);
  1595. if(likely(points_added || r->internal.queries_count)) {
  1596. // find the min/max across all dimensions
  1597. if(unlikely(group_value < min)) min = group_value;
  1598. if(unlikely(group_value > max)) max = group_value;
  1599. }
  1600. else {
  1601. // runs only when r->internal.queries_count == 0 && points_added == 0
  1602. // so, on the first point added for the query.
  1603. min = max = group_value;
  1604. }
  1605. points_added++;
  1606. ops->group_points_added = 0;
  1607. ops->group_value_flags = RRDR_VALUE_NOTHING;
  1608. ops->group_points_non_zero = 0;
  1609. ops->group_point = STORAGE_POINT_UNSET;
  1610. now_end_time += ops->view_update_every;
  1611. } while(now_end_time <= stop_time && points_added < points_wanted);
  1612. // the loop above increased "now" by ops->view_update_every,
  1613. // but the main loop will increase it too,
  1614. // so, let's undo the last iteration of this loop
  1615. now_end_time -= ops->view_update_every;
  1616. }
  1617. query_planer_finalize_remaining_plans(ops);
  1618. qm->query_points = ops->query_point;
  1619. // fill the rest of the points with empty values
  1620. while (points_added < points_wanted) {
  1621. rrdr_line++;
  1622. size_t rrdr_o_v_index = rrdr_line * r->d + dim_id_in_rrdr;
  1623. r->o[rrdr_o_v_index] = RRDR_VALUE_EMPTY;
  1624. r->v[rrdr_o_v_index] = 0.0;
  1625. r->ar[rrdr_o_v_index] = 0.0;
  1626. points_added++;
  1627. }
  1628. r->internal.queries_count++;
  1629. r->view.min = min;
  1630. r->view.max = max;
  1631. r->stats.result_points_generated += points_added;
  1632. r->stats.db_points_read += ops->db_total_points_read;
  1633. for(size_t tr = 0; tr < storage_tiers ; tr++)
  1634. qt->db.tiers[tr].points += ops->db_points_read_per_tier[tr];
  1635. }
  1636. // ----------------------------------------------------------------------------
  1637. // fill the gap of a tier
  1638. void store_metric_at_tier(RRDDIM *rd, size_t tier, struct rrddim_tier *t, STORAGE_POINT sp, usec_t now_ut);
  1639. void rrdr_fill_tier_gap_from_smaller_tiers(RRDDIM *rd, size_t tier, time_t now_s) {
  1640. if(unlikely(tier >= storage_tiers)) return;
  1641. if(storage_tiers_backfill[tier] == RRD_BACKFILL_NONE) return;
  1642. struct rrddim_tier *t = &rd->tiers[tier];
  1643. if(unlikely(!t)) return;
  1644. time_t latest_time_s = storage_engine_latest_time_s(t->backend, t->db_metric_handle);
  1645. time_t granularity = (time_t)t->tier_grouping * (time_t)rd->rrdset->update_every;
  1646. time_t time_diff = now_s - latest_time_s;
  1647. // if the user wants only NEW backfilling, and we don't have any data
  1648. if(storage_tiers_backfill[tier] == RRD_BACKFILL_NEW && latest_time_s <= 0) return;
  1649. // there is really nothing we can do
  1650. if(now_s <= latest_time_s || time_diff < granularity) return;
  1651. struct storage_engine_query_handle handle;
  1652. // for each lower tier
  1653. for(int read_tier = (int)tier - 1; read_tier >= 0 ; read_tier--){
  1654. time_t smaller_tier_first_time = storage_engine_oldest_time_s(rd->tiers[read_tier].backend, rd->tiers[read_tier].db_metric_handle);
  1655. time_t smaller_tier_last_time = storage_engine_latest_time_s(rd->tiers[read_tier].backend, rd->tiers[read_tier].db_metric_handle);
  1656. if(smaller_tier_last_time <= latest_time_s) continue; // it is as bad as we are
  1657. long after_wanted = (latest_time_s < smaller_tier_first_time) ? smaller_tier_first_time : latest_time_s;
  1658. long before_wanted = smaller_tier_last_time;
  1659. struct rrddim_tier *tmp = &rd->tiers[read_tier];
  1660. storage_engine_query_init(tmp->backend, tmp->db_metric_handle, &handle, after_wanted, before_wanted, STORAGE_PRIORITY_HIGH);
  1661. size_t points_read = 0;
  1662. while(!storage_engine_query_is_finished(&handle)) {
  1663. STORAGE_POINT sp = storage_engine_query_next_metric(&handle);
  1664. points_read++;
  1665. if(sp.end_time_s > latest_time_s) {
  1666. latest_time_s = sp.end_time_s;
  1667. store_metric_at_tier(rd, tier, t, sp, sp.end_time_s * USEC_PER_SEC);
  1668. }
  1669. }
  1670. storage_engine_query_finalize(&handle);
  1671. store_metric_collection_completed();
  1672. global_statistics_backfill_query_completed(points_read);
  1673. //internal_error(true, "DBENGINE: backfilled chart '%s', dimension '%s', tier %d, from %ld to %ld, with %zu points from tier %d",
  1674. // rd->rrdset->name, rd->name, tier, after_wanted, before_wanted, points, tr);
  1675. }
  1676. }
  1677. // ----------------------------------------------------------------------------
  1678. // fill RRDR for the whole chart
  1679. #ifdef NETDATA_INTERNAL_CHECKS
  1680. static void rrd2rrdr_log_request_response_metadata(RRDR *r
  1681. , RRDR_OPTIONS options __maybe_unused
  1682. , RRDR_TIME_GROUPING group_method
  1683. , bool aligned
  1684. , size_t group
  1685. , time_t resampling_time
  1686. , size_t resampling_group
  1687. , time_t after_wanted
  1688. , time_t after_requested
  1689. , time_t before_wanted
  1690. , time_t before_requested
  1691. , size_t points_requested
  1692. , size_t points_wanted
  1693. //, size_t after_slot
  1694. //, size_t before_slot
  1695. , const char *msg
  1696. ) {
  1697. QUERY_TARGET *qt = r->internal.qt;
  1698. time_t first_entry_s = qt->db.first_time_s;
  1699. time_t last_entry_s = qt->db.last_time_s;
  1700. internal_error(
  1701. true,
  1702. "rrd2rrdr() on %s update every %ld with %s grouping %s (group: %zu, resampling_time: %ld, resampling_group: %zu), "
  1703. "after (got: %ld, want: %ld, req: %ld, db: %ld), "
  1704. "before (got: %ld, want: %ld, req: %ld, db: %ld), "
  1705. "duration (got: %ld, want: %ld, req: %ld, db: %ld), "
  1706. "points (got: %zu, want: %zu, req: %zu), "
  1707. "%s"
  1708. , qt->id
  1709. , qt->window.query_granularity
  1710. // grouping
  1711. , (aligned) ? "aligned" : "unaligned"
  1712. , time_grouping_method2string(group_method)
  1713. , group
  1714. , resampling_time
  1715. , resampling_group
  1716. // after
  1717. , r->view.after
  1718. , after_wanted
  1719. , after_requested
  1720. , first_entry_s
  1721. // before
  1722. , r->view.before
  1723. , before_wanted
  1724. , before_requested
  1725. , last_entry_s
  1726. // duration
  1727. , (long)(r->view.before - r->view.after + qt->window.query_granularity)
  1728. , (long)(before_wanted - after_wanted + qt->window.query_granularity)
  1729. , (long)before_requested - after_requested
  1730. , (long)((last_entry_s - first_entry_s) + qt->window.query_granularity)
  1731. // points
  1732. , r->rows
  1733. , points_wanted
  1734. , points_requested
  1735. // message
  1736. , msg
  1737. );
  1738. }
  1739. #endif // NETDATA_INTERNAL_CHECKS
  1740. // Returns 1 if an absolute period was requested or 0 if it was a relative period
  1741. bool rrdr_relative_window_to_absolute(time_t *after, time_t *before, time_t *now_ptr) {
  1742. time_t now = now_realtime_sec() - 1;
  1743. if(now_ptr)
  1744. *now_ptr = now;
  1745. int absolute_period_requested = -1;
  1746. long long after_requested, before_requested;
  1747. before_requested = *before;
  1748. after_requested = *after;
  1749. // allow relative for before (smaller than API_RELATIVE_TIME_MAX)
  1750. if(ABS(before_requested) <= API_RELATIVE_TIME_MAX) {
  1751. // if the user asked for a positive relative time,
  1752. // flip it to a negative
  1753. if(before_requested > 0)
  1754. before_requested = -before_requested;
  1755. before_requested = now + before_requested;
  1756. absolute_period_requested = 0;
  1757. }
  1758. // allow relative for after (smaller than API_RELATIVE_TIME_MAX)
  1759. if(ABS(after_requested) <= API_RELATIVE_TIME_MAX) {
  1760. if(after_requested > 0)
  1761. after_requested = -after_requested;
  1762. // if the user didn't give an after, use the number of points
  1763. // to give a sane default
  1764. if(after_requested == 0)
  1765. after_requested = -600;
  1766. // since the query engine now returns inclusive timestamps
  1767. // it is awkward to return 6 points when after=-5 is given
  1768. // so for relative queries we add 1 second, to give
  1769. // more predictable results to users.
  1770. after_requested = before_requested + after_requested + 1;
  1771. absolute_period_requested = 0;
  1772. }
  1773. if(absolute_period_requested == -1)
  1774. absolute_period_requested = 1;
  1775. // check if the parameters are flipped
  1776. if(after_requested > before_requested) {
  1777. long long t = before_requested;
  1778. before_requested = after_requested;
  1779. after_requested = t;
  1780. }
  1781. // if the query requests future data
  1782. // shift the query back to be in the present time
  1783. // (this may also happen because of the rules above)
  1784. if(before_requested > now) {
  1785. long long delta = before_requested - now;
  1786. before_requested -= delta;
  1787. after_requested -= delta;
  1788. }
  1789. time_t absolute_minimum_time = now - (10 * 365 * 86400);
  1790. time_t absolute_maximum_time = now + (1 * 365 * 86400);
  1791. if (after_requested < absolute_minimum_time && !unittest_running)
  1792. after_requested = absolute_minimum_time;
  1793. if (after_requested > absolute_maximum_time && !unittest_running)
  1794. after_requested = absolute_maximum_time;
  1795. if (before_requested < absolute_minimum_time && !unittest_running)
  1796. before_requested = absolute_minimum_time;
  1797. if (before_requested > absolute_maximum_time && !unittest_running)
  1798. before_requested = absolute_maximum_time;
  1799. *before = before_requested;
  1800. *after = after_requested;
  1801. return (absolute_period_requested != 1);
  1802. }
  1803. // #define DEBUG_QUERY_LOGIC 1
  1804. #ifdef DEBUG_QUERY_LOGIC
  1805. #define query_debug_log_init() BUFFER *debug_log = buffer_create(1000)
  1806. #define query_debug_log(args...) buffer_sprintf(debug_log, ##args)
  1807. #define query_debug_log_fin() { \
  1808. netdata_log_info("QUERY: '%s', after:%ld, before:%ld, duration:%ld, points:%zu, res:%ld - wanted => after:%ld, before:%ld, points:%zu, group:%zu, granularity:%ld, resgroup:%ld, resdiv:" NETDATA_DOUBLE_FORMAT_AUTO " %s", qt->id, after_requested, before_requested, before_requested - after_requested, points_requested, resampling_time_requested, after_wanted, before_wanted, points_wanted, group, query_granularity, resampling_group, resampling_divisor, buffer_tostring(debug_log)); \
  1809. buffer_free(debug_log); \
  1810. debug_log = NULL; \
  1811. }
  1812. #define query_debug_log_free() do { buffer_free(debug_log); } while(0)
  1813. #else
  1814. #define query_debug_log_init() debug_dummy()
  1815. #define query_debug_log(args...) debug_dummy()
  1816. #define query_debug_log_fin() debug_dummy()
  1817. #define query_debug_log_free() debug_dummy()
  1818. #endif
  1819. bool query_target_calculate_window(QUERY_TARGET *qt) {
  1820. if (unlikely(!qt)) return false;
  1821. size_t points_requested = (long)qt->request.points;
  1822. time_t after_requested = qt->request.after;
  1823. time_t before_requested = qt->request.before;
  1824. RRDR_TIME_GROUPING group_method = qt->request.time_group_method;
  1825. time_t resampling_time_requested = qt->request.resampling_time;
  1826. RRDR_OPTIONS options = qt->window.options;
  1827. size_t tier = qt->request.tier;
  1828. time_t update_every = qt->db.minimum_latest_update_every_s ? qt->db.minimum_latest_update_every_s : 1;
  1829. // RULES
  1830. // points_requested = 0
  1831. // the user wants all the natural points the database has
  1832. //
  1833. // after_requested = 0
  1834. // the user wants to start the query from the oldest point in our database
  1835. //
  1836. // before_requested = 0
  1837. // the user wants the query to end to the latest point in our database
  1838. //
  1839. // when natural points are wanted, the query has to be aligned to the update_every
  1840. // of the database
  1841. size_t points_wanted = points_requested;
  1842. time_t after_wanted = after_requested;
  1843. time_t before_wanted = before_requested;
  1844. bool aligned = !(options & RRDR_OPTION_NOT_ALIGNED);
  1845. bool automatic_natural_points = (points_wanted == 0);
  1846. bool relative_period_requested = false;
  1847. bool natural_points = (options & RRDR_OPTION_NATURAL_POINTS) || automatic_natural_points;
  1848. bool before_is_aligned_to_db_end = false;
  1849. query_debug_log_init();
  1850. if (ABS(before_requested) <= API_RELATIVE_TIME_MAX || ABS(after_requested) <= API_RELATIVE_TIME_MAX) {
  1851. relative_period_requested = true;
  1852. natural_points = true;
  1853. options |= RRDR_OPTION_NATURAL_POINTS;
  1854. query_debug_log(":relative+natural");
  1855. }
  1856. // if the user wants virtual points, make sure we do it
  1857. if (options & RRDR_OPTION_VIRTUAL_POINTS)
  1858. natural_points = false;
  1859. // set the right flag about natural and virtual points
  1860. if (natural_points) {
  1861. options |= RRDR_OPTION_NATURAL_POINTS;
  1862. if (options & RRDR_OPTION_VIRTUAL_POINTS)
  1863. options &= ~RRDR_OPTION_VIRTUAL_POINTS;
  1864. }
  1865. else {
  1866. options |= RRDR_OPTION_VIRTUAL_POINTS;
  1867. if (options & RRDR_OPTION_NATURAL_POINTS)
  1868. options &= ~RRDR_OPTION_NATURAL_POINTS;
  1869. }
  1870. if (after_wanted == 0 || before_wanted == 0) {
  1871. relative_period_requested = true;
  1872. time_t first_entry_s = qt->db.first_time_s;
  1873. time_t last_entry_s = qt->db.last_time_s;
  1874. if (first_entry_s == 0 || last_entry_s == 0) {
  1875. internal_error(true, "QUERY: no data detected on query '%s' (db first_entry_t = %ld, last_entry_t = %ld)", qt->id, first_entry_s, last_entry_s);
  1876. after_wanted = qt->window.after;
  1877. before_wanted = qt->window.before;
  1878. if(after_wanted == before_wanted)
  1879. after_wanted = before_wanted - update_every;
  1880. if (points_wanted == 0) {
  1881. points_wanted = (before_wanted - after_wanted) / update_every;
  1882. query_debug_log(":zero points_wanted %zu", points_wanted);
  1883. }
  1884. }
  1885. else {
  1886. query_debug_log(":first_entry_t %ld, last_entry_t %ld", first_entry_s, last_entry_s);
  1887. if (after_wanted == 0) {
  1888. after_wanted = first_entry_s;
  1889. query_debug_log(":zero after_wanted %ld", after_wanted);
  1890. }
  1891. if (before_wanted == 0) {
  1892. before_wanted = last_entry_s;
  1893. before_is_aligned_to_db_end = true;
  1894. query_debug_log(":zero before_wanted %ld", before_wanted);
  1895. }
  1896. if (points_wanted == 0) {
  1897. points_wanted = (last_entry_s - first_entry_s) / update_every;
  1898. query_debug_log(":zero points_wanted %zu", points_wanted);
  1899. }
  1900. }
  1901. }
  1902. if (points_wanted == 0) {
  1903. points_wanted = 600;
  1904. query_debug_log(":zero600 points_wanted %zu", points_wanted);
  1905. }
  1906. // convert our before_wanted and after_wanted to absolute
  1907. rrdr_relative_window_to_absolute(&after_wanted, &before_wanted, NULL);
  1908. query_debug_log(":relative2absolute after %ld, before %ld", after_wanted, before_wanted);
  1909. if (natural_points && (options & RRDR_OPTION_SELECTED_TIER) && tier > 0 && storage_tiers > 1) {
  1910. update_every = rrdset_find_natural_update_every_for_timeframe(
  1911. qt, after_wanted, before_wanted, points_wanted, options, tier);
  1912. if (update_every <= 0) update_every = qt->db.minimum_latest_update_every_s;
  1913. query_debug_log(":natural update every %ld", update_every);
  1914. }
  1915. // this is the update_every of the query
  1916. // it may be different to the update_every of the database
  1917. time_t query_granularity = (natural_points) ? update_every : 1;
  1918. if (query_granularity <= 0) query_granularity = 1;
  1919. query_debug_log(":query_granularity %ld", query_granularity);
  1920. // align before_wanted and after_wanted to query_granularity
  1921. if (before_wanted % query_granularity) {
  1922. before_wanted -= before_wanted % query_granularity;
  1923. query_debug_log(":granularity align before_wanted %ld", before_wanted);
  1924. }
  1925. if (after_wanted % query_granularity) {
  1926. after_wanted -= after_wanted % query_granularity;
  1927. query_debug_log(":granularity align after_wanted %ld", after_wanted);
  1928. }
  1929. // automatic_natural_points is set when the user wants all the points available in the database
  1930. if (automatic_natural_points) {
  1931. points_wanted = (before_wanted - after_wanted + 1) / query_granularity;
  1932. if (unlikely(points_wanted <= 0)) points_wanted = 1;
  1933. query_debug_log(":auto natural points_wanted %zu", points_wanted);
  1934. }
  1935. time_t duration = before_wanted - after_wanted;
  1936. // if the resampling time is too big, extend the duration to the past
  1937. if (unlikely(resampling_time_requested > duration)) {
  1938. after_wanted = before_wanted - resampling_time_requested;
  1939. duration = before_wanted - after_wanted;
  1940. query_debug_log(":resampling after_wanted %ld", after_wanted);
  1941. }
  1942. // if the duration is not aligned to resampling time
  1943. // extend the duration to the past, to avoid a gap at the chart
  1944. // only when the missing duration is above 1/10th of a point
  1945. if (resampling_time_requested > query_granularity && duration % resampling_time_requested) {
  1946. time_t delta = duration % resampling_time_requested;
  1947. if (delta > resampling_time_requested / 10) {
  1948. after_wanted -= resampling_time_requested - delta;
  1949. duration = before_wanted - after_wanted;
  1950. query_debug_log(":resampling2 after_wanted %ld", after_wanted);
  1951. }
  1952. }
  1953. // the available points of the query
  1954. size_t points_available = (duration + 1) / query_granularity;
  1955. if (unlikely(points_available <= 0)) points_available = 1;
  1956. query_debug_log(":points_available %zu", points_available);
  1957. if (points_wanted > points_available) {
  1958. points_wanted = points_available;
  1959. query_debug_log(":max points_wanted %zu", points_wanted);
  1960. }
  1961. if(points_wanted > 86400 && !unittest_running) {
  1962. points_wanted = 86400;
  1963. query_debug_log(":absolute max points_wanted %zu", points_wanted);
  1964. }
  1965. // calculate the desired grouping of source data points
  1966. size_t group = points_available / points_wanted;
  1967. if (group == 0) group = 1;
  1968. // round "group" to the closest integer
  1969. if (points_available % points_wanted > points_wanted / 2)
  1970. group++;
  1971. query_debug_log(":group %zu", group);
  1972. if (points_wanted * group * query_granularity < (size_t)duration) {
  1973. // the grouping we are going to do, is not enough
  1974. // to cover the entire duration requested, so
  1975. // we have to change the number of points, to make sure we will
  1976. // respect the timeframe as closely as possibly
  1977. // let's see how many points are the optimal
  1978. points_wanted = points_available / group;
  1979. if (points_wanted * group < points_available)
  1980. points_wanted++;
  1981. if (unlikely(points_wanted == 0))
  1982. points_wanted = 1;
  1983. query_debug_log(":optimal points %zu", points_wanted);
  1984. }
  1985. // resampling_time_requested enforces a certain grouping multiple
  1986. NETDATA_DOUBLE resampling_divisor = 1.0;
  1987. size_t resampling_group = 1;
  1988. if (unlikely(resampling_time_requested > query_granularity)) {
  1989. // the points we should group to satisfy gtime
  1990. resampling_group = resampling_time_requested / query_granularity;
  1991. if (unlikely(resampling_time_requested % query_granularity))
  1992. resampling_group++;
  1993. query_debug_log(":resampling group %zu", resampling_group);
  1994. // adapt group according to resampling_group
  1995. if (unlikely(group < resampling_group)) {
  1996. group = resampling_group; // do not allow grouping below the desired one
  1997. query_debug_log(":group less res %zu", group);
  1998. }
  1999. if (unlikely(group % resampling_group)) {
  2000. group += resampling_group - (group % resampling_group); // make sure group is multiple of resampling_group
  2001. query_debug_log(":group mod res %zu", group);
  2002. }
  2003. // resampling_divisor = group / resampling_group;
  2004. resampling_divisor = (NETDATA_DOUBLE) (group * query_granularity) / (NETDATA_DOUBLE) resampling_time_requested;
  2005. query_debug_log(":resampling divisor " NETDATA_DOUBLE_FORMAT, resampling_divisor);
  2006. }
  2007. // now that we have group, align the requested timeframe to fit it.
  2008. if (aligned && before_wanted % (group * query_granularity)) {
  2009. if (before_is_aligned_to_db_end)
  2010. before_wanted -= before_wanted % (time_t)(group * query_granularity);
  2011. else
  2012. before_wanted += (time_t)(group * query_granularity) - before_wanted % (time_t)(group * query_granularity);
  2013. query_debug_log(":align before_wanted %ld", before_wanted);
  2014. }
  2015. after_wanted = before_wanted - (time_t)(points_wanted * group * query_granularity) + query_granularity;
  2016. query_debug_log(":final after_wanted %ld", after_wanted);
  2017. duration = before_wanted - after_wanted;
  2018. query_debug_log(":final duration %ld", duration + 1);
  2019. query_debug_log_fin();
  2020. internal_error(points_wanted != duration / (query_granularity * group) + 1,
  2021. "QUERY: points_wanted %zu is not points %zu",
  2022. points_wanted, (size_t)(duration / (query_granularity * group) + 1));
  2023. internal_error(group < resampling_group,
  2024. "QUERY: group %zu is less than the desired group points %zu",
  2025. group, resampling_group);
  2026. internal_error(group > resampling_group && group % resampling_group,
  2027. "QUERY: group %zu is not a multiple of the desired group points %zu",
  2028. group, resampling_group);
  2029. // -------------------------------------------------------------------------
  2030. // update QUERY_TARGET with our calculations
  2031. qt->window.after = after_wanted;
  2032. qt->window.before = before_wanted;
  2033. qt->window.relative = relative_period_requested;
  2034. qt->window.points = points_wanted;
  2035. qt->window.group = group;
  2036. qt->window.time_group_method = group_method;
  2037. qt->window.time_group_options = qt->request.time_group_options;
  2038. qt->window.query_granularity = query_granularity;
  2039. qt->window.resampling_group = resampling_group;
  2040. qt->window.resampling_divisor = resampling_divisor;
  2041. qt->window.options = options;
  2042. qt->window.tier = tier;
  2043. qt->window.aligned = aligned;
  2044. return true;
  2045. }
  2046. // ----------------------------------------------------------------------------
  2047. // group by
  2048. struct group_by_label_key {
  2049. DICTIONARY *values;
  2050. };
  2051. static void group_by_label_key_insert_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data) {
  2052. // add the key to our r->label_keys global keys dictionary
  2053. DICTIONARY *label_keys = data;
  2054. dictionary_set(label_keys, dictionary_acquired_item_name(item), NULL, 0);
  2055. // create a dictionary for the values of this key
  2056. struct group_by_label_key *k = value;
  2057. k->values = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE, NULL, 0);
  2058. }
  2059. static void group_by_label_key_delete_cb(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  2060. struct group_by_label_key *k = value;
  2061. dictionary_destroy(k->values);
  2062. }
  2063. static int rrdlabels_traversal_cb_to_group_by_label_key(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
  2064. DICTIONARY *dl = data;
  2065. struct group_by_label_key *k = dictionary_set(dl, name, NULL, sizeof(struct group_by_label_key));
  2066. dictionary_set(k->values, value, NULL, 0);
  2067. return 1;
  2068. }
  2069. void rrdr_json_group_by_labels(BUFFER *wb, const char *key, RRDR *r, RRDR_OPTIONS options) {
  2070. if(!r->label_keys || !r->dl)
  2071. return;
  2072. buffer_json_member_add_object(wb, key);
  2073. void *t;
  2074. dfe_start_read(r->label_keys, t) {
  2075. buffer_json_member_add_array(wb, t_dfe.name);
  2076. for(size_t d = 0; d < r->d ;d++) {
  2077. if(!rrdr_dimension_should_be_exposed(r->od[d], options))
  2078. continue;
  2079. struct group_by_label_key *k = dictionary_get(r->dl[d], t_dfe.name);
  2080. if(k) {
  2081. buffer_json_add_array_item_array(wb);
  2082. void *tt;
  2083. dfe_start_read(k->values, tt) {
  2084. buffer_json_add_array_item_string(wb, tt_dfe.name);
  2085. }
  2086. dfe_done(tt);
  2087. buffer_json_array_close(wb);
  2088. }
  2089. else
  2090. buffer_json_add_array_item_string(wb, NULL);
  2091. }
  2092. buffer_json_array_close(wb);
  2093. }
  2094. dfe_done(t);
  2095. buffer_json_object_close(wb); // key
  2096. }
  2097. static void rrd2rrdr_set_timestamps(RRDR *r) {
  2098. QUERY_TARGET *qt = r->internal.qt;
  2099. internal_fatal(qt->window.points != r->n, "QUERY: mismatch to the number of points in qt and r");
  2100. r->view.group = qt->window.group;
  2101. r->view.update_every = (int) query_view_update_every(qt);
  2102. r->view.before = qt->window.before;
  2103. r->view.after = qt->window.after;
  2104. r->time_grouping.points_wanted = qt->window.points;
  2105. r->time_grouping.resampling_group = qt->window.resampling_group;
  2106. r->time_grouping.resampling_divisor = qt->window.resampling_divisor;
  2107. r->rows = qt->window.points;
  2108. size_t points_wanted = qt->window.points;
  2109. time_t after_wanted = qt->window.after;
  2110. time_t before_wanted = qt->window.before; (void)before_wanted;
  2111. time_t view_update_every = r->view.update_every;
  2112. time_t query_granularity = (time_t)(r->view.update_every / r->view.group);
  2113. size_t rrdr_line = 0;
  2114. time_t first_point_end_time = after_wanted + view_update_every - query_granularity;
  2115. time_t now_end_time = first_point_end_time;
  2116. while (rrdr_line < points_wanted) {
  2117. r->t[rrdr_line++] = now_end_time;
  2118. now_end_time += view_update_every;
  2119. }
  2120. internal_fatal(r->t[0] != first_point_end_time, "QUERY: wrong first timestamp in the query");
  2121. internal_error(r->t[points_wanted - 1] != before_wanted,
  2122. "QUERY: wrong last timestamp in the query, expected %ld, found %ld",
  2123. before_wanted, r->t[points_wanted - 1]);
  2124. }
  2125. static void query_group_by_make_dimension_key(BUFFER *key, RRDR_GROUP_BY group_by, size_t group_by_id, QUERY_TARGET *qt, QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi, QUERY_DIMENSION *qd __maybe_unused, QUERY_METRIC *qm, bool query_has_percentage_of_group) {
  2126. buffer_flush(key);
  2127. if(unlikely(!query_has_percentage_of_group && qm->status & RRDR_DIMENSION_HIDDEN)) {
  2128. buffer_strcat(key, "__hidden_dimensions__");
  2129. }
  2130. else if(unlikely(group_by & RRDR_GROUP_BY_SELECTED)) {
  2131. buffer_strcat(key, "selected");
  2132. }
  2133. else {
  2134. if (group_by & RRDR_GROUP_BY_DIMENSION) {
  2135. buffer_fast_strcat(key, "|", 1);
  2136. buffer_strcat(key, query_metric_name(qt, qm));
  2137. }
  2138. if (group_by & (RRDR_GROUP_BY_INSTANCE|RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)) {
  2139. buffer_fast_strcat(key, "|", 1);
  2140. buffer_strcat(key, string2str(query_instance_id_fqdn(qi, qt->request.version)));
  2141. }
  2142. if (group_by & RRDR_GROUP_BY_LABEL) {
  2143. DICTIONARY *labels = rrdinstance_acquired_labels(qi->ria);
  2144. for (size_t l = 0; l < qt->group_by[group_by_id].used; l++) {
  2145. buffer_fast_strcat(key, "|", 1);
  2146. rrdlabels_get_value_to_buffer_or_unset(labels, key, qt->group_by[group_by_id].label_keys[l], "[unset]");
  2147. }
  2148. }
  2149. if (group_by & RRDR_GROUP_BY_NODE) {
  2150. buffer_fast_strcat(key, "|", 1);
  2151. buffer_strcat(key, qn->rrdhost->machine_guid);
  2152. }
  2153. if (group_by & RRDR_GROUP_BY_CONTEXT) {
  2154. buffer_fast_strcat(key, "|", 1);
  2155. buffer_strcat(key, rrdcontext_acquired_id(qc->rca));
  2156. }
  2157. if (group_by & RRDR_GROUP_BY_UNITS) {
  2158. buffer_fast_strcat(key, "|", 1);
  2159. buffer_strcat(key, query_target_has_percentage_units(qt) ? "%" : rrdinstance_acquired_units(qi->ria));
  2160. }
  2161. }
  2162. }
  2163. static void query_group_by_make_dimension_id(BUFFER *key, RRDR_GROUP_BY group_by, size_t group_by_id, QUERY_TARGET *qt, QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi, QUERY_DIMENSION *qd __maybe_unused, QUERY_METRIC *qm, bool query_has_percentage_of_group) {
  2164. buffer_flush(key);
  2165. if(unlikely(!query_has_percentage_of_group && qm->status & RRDR_DIMENSION_HIDDEN)) {
  2166. buffer_strcat(key, "__hidden_dimensions__");
  2167. }
  2168. else if(unlikely(group_by & RRDR_GROUP_BY_SELECTED)) {
  2169. buffer_strcat(key, "selected");
  2170. }
  2171. else {
  2172. if (group_by & RRDR_GROUP_BY_DIMENSION) {
  2173. buffer_strcat(key, query_metric_name(qt, qm));
  2174. }
  2175. if (group_by & (RRDR_GROUP_BY_INSTANCE|RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)) {
  2176. if (buffer_strlen(key) != 0)
  2177. buffer_fast_strcat(key, ",", 1);
  2178. if (group_by & RRDR_GROUP_BY_NODE)
  2179. buffer_strcat(key, rrdinstance_acquired_id(qi->ria));
  2180. else
  2181. buffer_strcat(key, string2str(query_instance_id_fqdn(qi, qt->request.version)));
  2182. }
  2183. if (group_by & RRDR_GROUP_BY_LABEL) {
  2184. DICTIONARY *labels = rrdinstance_acquired_labels(qi->ria);
  2185. for (size_t l = 0; l < qt->group_by[group_by_id].used; l++) {
  2186. if (buffer_strlen(key) != 0)
  2187. buffer_fast_strcat(key, ",", 1);
  2188. rrdlabels_get_value_to_buffer_or_unset(labels, key, qt->group_by[group_by_id].label_keys[l], "[unset]");
  2189. }
  2190. }
  2191. if (group_by & RRDR_GROUP_BY_NODE) {
  2192. if (buffer_strlen(key) != 0)
  2193. buffer_fast_strcat(key, ",", 1);
  2194. buffer_strcat(key, qn->rrdhost->machine_guid);
  2195. }
  2196. if (group_by & RRDR_GROUP_BY_CONTEXT) {
  2197. if (buffer_strlen(key) != 0)
  2198. buffer_fast_strcat(key, ",", 1);
  2199. buffer_strcat(key, rrdcontext_acquired_id(qc->rca));
  2200. }
  2201. if (group_by & RRDR_GROUP_BY_UNITS) {
  2202. if (buffer_strlen(key) != 0)
  2203. buffer_fast_strcat(key, ",", 1);
  2204. buffer_strcat(key, query_target_has_percentage_units(qt) ? "%" : rrdinstance_acquired_units(qi->ria));
  2205. }
  2206. }
  2207. }
  2208. static void query_group_by_make_dimension_name(BUFFER *key, RRDR_GROUP_BY group_by, size_t group_by_id, QUERY_TARGET *qt, QUERY_NODE *qn, QUERY_CONTEXT *qc, QUERY_INSTANCE *qi, QUERY_DIMENSION *qd __maybe_unused, QUERY_METRIC *qm, bool query_has_percentage_of_group) {
  2209. buffer_flush(key);
  2210. if(unlikely(!query_has_percentage_of_group && qm->status & RRDR_DIMENSION_HIDDEN)) {
  2211. buffer_strcat(key, "__hidden_dimensions__");
  2212. }
  2213. else if(unlikely(group_by & RRDR_GROUP_BY_SELECTED)) {
  2214. buffer_strcat(key, "selected");
  2215. }
  2216. else {
  2217. if (group_by & RRDR_GROUP_BY_DIMENSION) {
  2218. buffer_strcat(key, query_metric_name(qt, qm));
  2219. }
  2220. if (group_by & (RRDR_GROUP_BY_INSTANCE|RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)) {
  2221. if (buffer_strlen(key) != 0)
  2222. buffer_fast_strcat(key, ",", 1);
  2223. if (group_by & RRDR_GROUP_BY_NODE)
  2224. buffer_strcat(key, rrdinstance_acquired_name(qi->ria));
  2225. else
  2226. buffer_strcat(key, string2str(query_instance_name_fqdn(qi, qt->request.version)));
  2227. }
  2228. if (group_by & RRDR_GROUP_BY_LABEL) {
  2229. DICTIONARY *labels = rrdinstance_acquired_labels(qi->ria);
  2230. for (size_t l = 0; l < qt->group_by[group_by_id].used; l++) {
  2231. if (buffer_strlen(key) != 0)
  2232. buffer_fast_strcat(key, ",", 1);
  2233. rrdlabels_get_value_to_buffer_or_unset(labels, key, qt->group_by[group_by_id].label_keys[l], "[unset]");
  2234. }
  2235. }
  2236. if (group_by & RRDR_GROUP_BY_NODE) {
  2237. if (buffer_strlen(key) != 0)
  2238. buffer_fast_strcat(key, ",", 1);
  2239. buffer_strcat(key, rrdhost_hostname(qn->rrdhost));
  2240. }
  2241. if (group_by & RRDR_GROUP_BY_CONTEXT) {
  2242. if (buffer_strlen(key) != 0)
  2243. buffer_fast_strcat(key, ",", 1);
  2244. buffer_strcat(key, rrdcontext_acquired_id(qc->rca));
  2245. }
  2246. if (group_by & RRDR_GROUP_BY_UNITS) {
  2247. if (buffer_strlen(key) != 0)
  2248. buffer_fast_strcat(key, ",", 1);
  2249. buffer_strcat(key, query_target_has_percentage_units(qt) ? "%" : rrdinstance_acquired_units(qi->ria));
  2250. }
  2251. }
  2252. }
  2253. struct rrdr_group_by_entry {
  2254. size_t priority;
  2255. size_t count;
  2256. STRING *id;
  2257. STRING *name;
  2258. STRING *units;
  2259. RRDR_DIMENSION_FLAGS od;
  2260. DICTIONARY *dl;
  2261. };
  2262. static RRDR *rrd2rrdr_group_by_initialize(ONEWAYALLOC *owa, QUERY_TARGET *qt) {
  2263. RRDR *r_tmp = NULL;
  2264. RRDR_OPTIONS options = qt->window.options;
  2265. if(qt->request.version < 2) {
  2266. // v1 query
  2267. RRDR *r = rrdr_create(owa, qt, qt->query.used, qt->window.points);
  2268. if(unlikely(!r)) {
  2269. internal_error(true, "QUERY: cannot create RRDR for %s, after=%ld, before=%ld, dimensions=%u, points=%zu",
  2270. qt->id, qt->window.after, qt->window.before, qt->query.used, qt->window.points);
  2271. return NULL;
  2272. }
  2273. r->group_by.r = NULL;
  2274. for(size_t d = 0; d < qt->query.used ; d++) {
  2275. QUERY_METRIC *qm = query_metric(qt, d);
  2276. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  2277. r->di[d] = rrdmetric_acquired_id_dup(qd->rma);
  2278. r->dn[d] = rrdmetric_acquired_name_dup(qd->rma);
  2279. }
  2280. rrd2rrdr_set_timestamps(r);
  2281. return r;
  2282. }
  2283. // v2 query
  2284. // parse all the group-by label keys
  2285. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  2286. if (qt->request.group_by[g].group_by & RRDR_GROUP_BY_LABEL &&
  2287. qt->request.group_by[g].group_by_label && *qt->request.group_by[g].group_by_label)
  2288. qt->group_by[g].used = quoted_strings_splitter_query_group_by_label(
  2289. qt->request.group_by[g].group_by_label, qt->group_by[g].label_keys,
  2290. GROUP_BY_MAX_LABEL_KEYS);
  2291. if (!qt->group_by[g].used)
  2292. qt->request.group_by[g].group_by &= ~RRDR_GROUP_BY_LABEL;
  2293. }
  2294. // make sure there are valid group-by methods
  2295. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  2296. if(!(qt->request.group_by[g].group_by & SUPPORTED_GROUP_BY_METHODS))
  2297. qt->request.group_by[g].group_by = (g == 0) ? RRDR_GROUP_BY_DIMENSION : RRDR_GROUP_BY_NONE;
  2298. }
  2299. bool query_has_percentage_of_group = query_target_has_percentage_of_group(qt);
  2300. // merge all group-by options to upper levels,
  2301. // so that the top level has all the groupings of the inner levels,
  2302. // and each subsequent level has all the groupings of its inner levels.
  2303. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES - 1 ;g++) {
  2304. if(qt->request.group_by[g].group_by == RRDR_GROUP_BY_NONE)
  2305. continue;
  2306. if(qt->request.group_by[g].group_by == RRDR_GROUP_BY_SELECTED) {
  2307. for (size_t r = g + 1; r < MAX_QUERY_GROUP_BY_PASSES; r++)
  2308. qt->request.group_by[r].group_by = RRDR_GROUP_BY_NONE;
  2309. }
  2310. else {
  2311. for (size_t r = g + 1; r < MAX_QUERY_GROUP_BY_PASSES; r++) {
  2312. if (qt->request.group_by[r].group_by == RRDR_GROUP_BY_NONE)
  2313. continue;
  2314. if (qt->request.group_by[r].group_by != RRDR_GROUP_BY_SELECTED) {
  2315. if(qt->request.group_by[r].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  2316. qt->request.group_by[g].group_by |= RRDR_GROUP_BY_INSTANCE;
  2317. else
  2318. qt->request.group_by[g].group_by |= qt->request.group_by[r].group_by;
  2319. if(qt->request.group_by[r].group_by & RRDR_GROUP_BY_LABEL) {
  2320. for (size_t lr = 0; lr < qt->group_by[r].used; lr++) {
  2321. bool found = false;
  2322. for (size_t lg = 0; lg < qt->group_by[g].used; lg++) {
  2323. if (strcmp(qt->group_by[g].label_keys[lg], qt->group_by[r].label_keys[lr]) == 0) {
  2324. found = true;
  2325. break;
  2326. }
  2327. }
  2328. if (!found && qt->group_by[g].used < GROUP_BY_MAX_LABEL_KEYS * MAX_QUERY_GROUP_BY_PASSES)
  2329. qt->group_by[g].label_keys[qt->group_by[g].used++] = qt->group_by[r].label_keys[lr];
  2330. }
  2331. }
  2332. }
  2333. }
  2334. }
  2335. }
  2336. int added = 0;
  2337. RRDR *first_r = NULL, *last_r = NULL;
  2338. BUFFER *key = buffer_create(0, NULL);
  2339. struct rrdr_group_by_entry *entries = onewayalloc_mallocz(owa, qt->query.used * sizeof(struct rrdr_group_by_entry));
  2340. DICTIONARY *groups = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
  2341. DICTIONARY *label_keys = NULL;
  2342. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  2343. RRDR_GROUP_BY group_by = qt->request.group_by[g].group_by;
  2344. RRDR_GROUP_BY_FUNCTION aggregation_method = qt->request.group_by[g].aggregation;
  2345. if(group_by == RRDR_GROUP_BY_NONE)
  2346. break;
  2347. memset(entries, 0, qt->query.used * sizeof(struct rrdr_group_by_entry));
  2348. dictionary_flush(groups);
  2349. added = 0;
  2350. size_t hidden_dimensions = 0;
  2351. bool final_grouping = (g == MAX_QUERY_GROUP_BY_PASSES - 1 || qt->request.group_by[g + 1].group_by == RRDR_GROUP_BY_NONE) ? true : false;
  2352. if (final_grouping && (options & RRDR_OPTION_GROUP_BY_LABELS))
  2353. label_keys = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE, NULL, 0);
  2354. QUERY_INSTANCE *last_qi = NULL;
  2355. size_t priority = 0;
  2356. time_t update_every_max = 0;
  2357. for (size_t d = 0; d < qt->query.used; d++) {
  2358. QUERY_METRIC *qm = query_metric(qt, d);
  2359. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  2360. QUERY_INSTANCE *qi = query_instance(qt, qm->link.query_instance_id);
  2361. QUERY_CONTEXT *qc = query_context(qt, qm->link.query_context_id);
  2362. QUERY_NODE *qn = query_node(qt, qm->link.query_node_id);
  2363. if (qi != last_qi) {
  2364. last_qi = qi;
  2365. time_t update_every = rrdinstance_acquired_update_every(qi->ria);
  2366. if (update_every > update_every_max)
  2367. update_every_max = update_every;
  2368. }
  2369. priority = qd->priority;
  2370. if(qm->status & RRDR_DIMENSION_HIDDEN)
  2371. hidden_dimensions++;
  2372. // --------------------------------------------------------------------
  2373. // generate the group by key
  2374. query_group_by_make_dimension_key(key, group_by, g, qt, qn, qc, qi, qd, qm, query_has_percentage_of_group);
  2375. // lookup the key in the dictionary
  2376. int pos = -1;
  2377. int *set = dictionary_set(groups, buffer_tostring(key), &pos, sizeof(pos));
  2378. if (*set == -1) {
  2379. // the key just added to the dictionary
  2380. *set = pos = added++;
  2381. // ----------------------------------------------------------------
  2382. // generate the dimension id
  2383. query_group_by_make_dimension_id(key, group_by, g, qt, qn, qc, qi, qd, qm, query_has_percentage_of_group);
  2384. entries[pos].id = string_strdupz(buffer_tostring(key));
  2385. // ----------------------------------------------------------------
  2386. // generate the dimension name
  2387. query_group_by_make_dimension_name(key, group_by, g, qt, qn, qc, qi, qd, qm, query_has_percentage_of_group);
  2388. entries[pos].name = string_strdupz(buffer_tostring(key));
  2389. // add the rest of the info
  2390. entries[pos].units = rrdinstance_acquired_units_dup(qi->ria);
  2391. entries[pos].priority = priority;
  2392. if (label_keys) {
  2393. entries[pos].dl = dictionary_create_advanced(
  2394. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_FIXED_SIZE | DICT_OPTION_DONT_OVERWRITE_VALUE,
  2395. NULL, sizeof(struct group_by_label_key));
  2396. dictionary_register_insert_callback(entries[pos].dl, group_by_label_key_insert_cb, label_keys);
  2397. dictionary_register_delete_callback(entries[pos].dl, group_by_label_key_delete_cb, label_keys);
  2398. }
  2399. } else {
  2400. // the key found in the dictionary
  2401. pos = *set;
  2402. }
  2403. entries[pos].count++;
  2404. if (unlikely(priority < entries[pos].priority))
  2405. entries[pos].priority = priority;
  2406. if(g > 0)
  2407. last_r->dgbs[qm->grouped_as.slot] = pos;
  2408. else
  2409. qm->grouped_as.first_slot = pos;
  2410. qm->grouped_as.slot = pos;
  2411. qm->grouped_as.id = entries[pos].id;
  2412. qm->grouped_as.name = entries[pos].name;
  2413. qm->grouped_as.units = entries[pos].units;
  2414. // copy the dimension flags decided by the query target
  2415. // we need this, because if a dimension is explicitly selected
  2416. // the query target adds to it the non-zero flag
  2417. qm->status |= RRDR_DIMENSION_GROUPED;
  2418. if(query_has_percentage_of_group)
  2419. // when the query has percentage of group
  2420. // there will be no hidden dimensions in the final query,
  2421. // so we have to remove the hidden flag from all dimensions
  2422. entries[pos].od |= qm->status & ~RRDR_DIMENSION_HIDDEN;
  2423. else
  2424. entries[pos].od |= qm->status;
  2425. if (entries[pos].dl)
  2426. rrdlabels_walkthrough_read(rrdinstance_acquired_labels(qi->ria),
  2427. rrdlabels_traversal_cb_to_group_by_label_key, entries[pos].dl);
  2428. }
  2429. RRDR *r = rrdr_create(owa, qt, added, qt->window.points);
  2430. if (!r) {
  2431. internal_error(true,
  2432. "QUERY: cannot create group by RRDR for %s, after=%ld, before=%ld, dimensions=%d, points=%zu",
  2433. qt->id, qt->window.after, qt->window.before, added, qt->window.points);
  2434. goto cleanup;
  2435. }
  2436. // prevent double free at cleanup in case of error
  2437. added = 0;
  2438. // link this RRDR
  2439. if(!last_r)
  2440. first_r = last_r = r;
  2441. else
  2442. last_r->group_by.r = r;
  2443. last_r = r;
  2444. rrd2rrdr_set_timestamps(r);
  2445. r->dp = onewayalloc_callocz(owa, r->d, sizeof(*r->dp));
  2446. r->dview = onewayalloc_callocz(owa, r->d, sizeof(*r->dview));
  2447. r->dgbc = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbc));
  2448. r->gbc = onewayalloc_callocz(owa, r->n * r->d, sizeof(*r->gbc));
  2449. r->dqp = onewayalloc_callocz(owa, r->d, sizeof(STORAGE_POINT));
  2450. if(hidden_dimensions && ((group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE) || (aggregation_method == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)))
  2451. // this is where we are going to group the hidden dimensions
  2452. r->vh = onewayalloc_mallocz(owa, r->n * r->d * sizeof(*r->vh));
  2453. if(!final_grouping)
  2454. // this is where we are going to store the slot in the next RRDR
  2455. // that we are going to group by the dimension of this RRDR
  2456. r->dgbs = onewayalloc_callocz(owa, r->d, sizeof(*r->dgbs));
  2457. if (label_keys) {
  2458. r->dl = onewayalloc_callocz(owa, r->d, sizeof(DICTIONARY *));
  2459. r->label_keys = label_keys;
  2460. label_keys = NULL;
  2461. }
  2462. // zero r (dimension options, names, and ids)
  2463. // this is required, because group-by may lead to empty dimensions
  2464. for (size_t d = 0; d < r->d; d++) {
  2465. r->di[d] = entries[d].id;
  2466. r->dn[d] = entries[d].name;
  2467. r->od[d] = entries[d].od;
  2468. r->du[d] = entries[d].units;
  2469. r->dp[d] = entries[d].priority;
  2470. r->dgbc[d] = entries[d].count;
  2471. if (r->dl)
  2472. r->dl[d] = entries[d].dl;
  2473. }
  2474. // initialize partial trimming
  2475. r->partial_data_trimming.max_update_every = update_every_max;
  2476. r->partial_data_trimming.expected_after =
  2477. (!query_target_aggregatable(qt) &&
  2478. qt->window.before >= qt->window.now - update_every_max) ?
  2479. qt->window.before - update_every_max :
  2480. qt->window.before;
  2481. r->partial_data_trimming.trimmed_after = qt->window.before;
  2482. // make all values empty
  2483. for (size_t i = 0; i != r->n; i++) {
  2484. NETDATA_DOUBLE *cn = &r->v[i * r->d];
  2485. RRDR_VALUE_FLAGS *co = &r->o[i * r->d];
  2486. NETDATA_DOUBLE *ar = &r->ar[i * r->d];
  2487. NETDATA_DOUBLE *vh = r->vh ? &r->vh[i * r->d] : NULL;
  2488. for (size_t d = 0; d < r->d; d++) {
  2489. cn[d] = NAN;
  2490. ar[d] = 0.0;
  2491. co[d] = RRDR_VALUE_EMPTY;
  2492. if(vh)
  2493. vh[d] = NAN;
  2494. }
  2495. }
  2496. }
  2497. if(!first_r || !last_r)
  2498. goto cleanup;
  2499. r_tmp = rrdr_create(owa, qt, 1, qt->window.points);
  2500. if (!r_tmp) {
  2501. internal_error(true,
  2502. "QUERY: cannot create group by temporary RRDR for %s, after=%ld, before=%ld, dimensions=%d, points=%zu",
  2503. qt->id, qt->window.after, qt->window.before, 1, qt->window.points);
  2504. goto cleanup;
  2505. }
  2506. rrd2rrdr_set_timestamps(r_tmp);
  2507. r_tmp->group_by.r = first_r;
  2508. cleanup:
  2509. if(!first_r || !last_r || !r_tmp) {
  2510. if(r_tmp) {
  2511. r_tmp->group_by.r = NULL;
  2512. rrdr_free(owa, r_tmp);
  2513. }
  2514. if(first_r) {
  2515. RRDR *r = first_r;
  2516. while (r) {
  2517. r_tmp = r->group_by.r;
  2518. r->group_by.r = NULL;
  2519. rrdr_free(owa, r);
  2520. r = r_tmp;
  2521. }
  2522. }
  2523. if(entries && added) {
  2524. for (int d = 0; d < added; d++) {
  2525. string_freez(entries[d].id);
  2526. string_freez(entries[d].name);
  2527. string_freez(entries[d].units);
  2528. dictionary_destroy(entries[d].dl);
  2529. }
  2530. }
  2531. dictionary_destroy(label_keys);
  2532. first_r = last_r = r_tmp = NULL;
  2533. }
  2534. buffer_free(key);
  2535. onewayalloc_freez(owa, entries);
  2536. dictionary_destroy(groups);
  2537. return r_tmp;
  2538. }
  2539. static void rrd2rrdr_group_by_add_metric(RRDR *r_dst, size_t d_dst, RRDR *r_tmp, size_t d_tmp,
  2540. RRDR_GROUP_BY_FUNCTION group_by_aggregate_function,
  2541. STORAGE_POINT *query_points, size_t pass __maybe_unused) {
  2542. if(!r_tmp || r_dst == r_tmp || !(r_tmp->od[d_tmp] & RRDR_DIMENSION_QUERIED))
  2543. return;
  2544. internal_fatal(r_dst->n != r_tmp->n, "QUERY: group-by source and destination do not have the same number of rows");
  2545. internal_fatal(d_dst >= r_dst->d, "QUERY: group-by destination dimension number exceeds destination RRDR size");
  2546. internal_fatal(d_tmp >= r_tmp->d, "QUERY: group-by source dimension number exceeds source RRDR size");
  2547. internal_fatal(!r_dst->dqp, "QUERY: group-by destination is not properly prepared (missing dqp array)");
  2548. internal_fatal(!r_dst->gbc, "QUERY: group-by destination is not properly prepared (missing gbc array)");
  2549. bool hidden_dimension_on_percentage_of_group = (r_tmp->od[d_tmp] & RRDR_DIMENSION_HIDDEN) && r_dst->vh;
  2550. if(!hidden_dimension_on_percentage_of_group) {
  2551. r_dst->od[d_dst] |= r_tmp->od[d_tmp];
  2552. storage_point_merge_to(r_dst->dqp[d_dst], *query_points);
  2553. }
  2554. // do the group_by
  2555. for(size_t i = 0; i != rrdr_rows(r_tmp) ; i++) {
  2556. size_t idx_tmp = i * r_tmp->d + d_tmp;
  2557. NETDATA_DOUBLE n_tmp = r_tmp->v[ idx_tmp ];
  2558. RRDR_VALUE_FLAGS o_tmp = r_tmp->o[ idx_tmp ];
  2559. NETDATA_DOUBLE ar_tmp = r_tmp->ar[ idx_tmp ];
  2560. if(o_tmp & RRDR_VALUE_EMPTY)
  2561. continue;
  2562. size_t idx_dst = i * r_dst->d + d_dst;
  2563. NETDATA_DOUBLE *cn = (hidden_dimension_on_percentage_of_group) ? &r_dst->vh[ idx_dst ] : &r_dst->v[ idx_dst ];
  2564. RRDR_VALUE_FLAGS *co = &r_dst->o[ idx_dst ];
  2565. NETDATA_DOUBLE *ar = &r_dst->ar[ idx_dst ];
  2566. uint32_t *gbc = &r_dst->gbc[ idx_dst ];
  2567. switch(group_by_aggregate_function) {
  2568. default:
  2569. case RRDR_GROUP_BY_FUNCTION_AVERAGE:
  2570. case RRDR_GROUP_BY_FUNCTION_SUM:
  2571. case RRDR_GROUP_BY_FUNCTION_PERCENTAGE:
  2572. if(isnan(*cn))
  2573. *cn = n_tmp;
  2574. else
  2575. *cn += n_tmp;
  2576. break;
  2577. case RRDR_GROUP_BY_FUNCTION_MIN:
  2578. if(isnan(*cn) || n_tmp < *cn)
  2579. *cn = n_tmp;
  2580. break;
  2581. case RRDR_GROUP_BY_FUNCTION_MAX:
  2582. if(isnan(*cn) || n_tmp > *cn)
  2583. *cn = n_tmp;
  2584. break;
  2585. }
  2586. if(!hidden_dimension_on_percentage_of_group) {
  2587. *co &= ~RRDR_VALUE_EMPTY;
  2588. *co |= (o_tmp & (RRDR_VALUE_RESET | RRDR_VALUE_PARTIAL));
  2589. *ar += ar_tmp;
  2590. (*gbc)++;
  2591. }
  2592. }
  2593. }
  2594. static void rrdr2rrdr_group_by_partial_trimming(RRDR *r) {
  2595. time_t trimmable_after = r->partial_data_trimming.expected_after;
  2596. // find the point just before the trimmable ones
  2597. ssize_t i = (ssize_t)r->n - 1;
  2598. for( ; i >= 0 ;i--) {
  2599. if (r->t[i] < trimmable_after)
  2600. break;
  2601. }
  2602. if(unlikely(i < 0))
  2603. return;
  2604. size_t last_row_gbc = 0;
  2605. for (; i < (ssize_t)r->n; i++) {
  2606. size_t row_gbc = 0;
  2607. for (size_t d = 0; d < r->d; d++) {
  2608. if (unlikely(!(r->od[d] & RRDR_DIMENSION_QUERIED)))
  2609. continue;
  2610. row_gbc += r->gbc[ i * r->d + d ];
  2611. }
  2612. if (unlikely(r->t[i] >= trimmable_after && row_gbc < last_row_gbc)) {
  2613. // discard the rest of the points
  2614. r->partial_data_trimming.trimmed_after = r->t[i];
  2615. r->rows = i;
  2616. break;
  2617. }
  2618. else
  2619. last_row_gbc = row_gbc;
  2620. }
  2621. }
  2622. static void rrdr2rrdr_group_by_calculate_percentage_of_group(RRDR *r) {
  2623. if(!r->vh)
  2624. return;
  2625. if(query_target_aggregatable(r->internal.qt) && query_has_group_by_aggregation_percentage(r->internal.qt))
  2626. return;
  2627. for(size_t i = 0; i < r->n ;i++) {
  2628. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  2629. NETDATA_DOUBLE *ch = &r->vh[ i * r->d ];
  2630. for(size_t d = 0; d < r->d ;d++) {
  2631. NETDATA_DOUBLE n = cn[d];
  2632. NETDATA_DOUBLE h = ch[d];
  2633. if(isnan(n))
  2634. cn[d] = 0.0;
  2635. else if(isnan(h))
  2636. cn[d] = 100.0;
  2637. else
  2638. cn[d] = n * 100.0 / (n + h);
  2639. }
  2640. }
  2641. }
  2642. static void rrd2rrdr_convert_values_to_percentage_of_total(RRDR *r) {
  2643. if(!(r->internal.qt->window.options & RRDR_OPTION_PERCENTAGE) || query_target_aggregatable(r->internal.qt))
  2644. return;
  2645. size_t global_min_max_values = 0;
  2646. NETDATA_DOUBLE global_min = NAN, global_max = NAN;
  2647. for(size_t i = 0; i != r->n ;i++) {
  2648. NETDATA_DOUBLE *cn = &r->v[ i * r->d ];
  2649. RRDR_VALUE_FLAGS *co = &r->o[ i * r->d ];
  2650. NETDATA_DOUBLE total = 0;
  2651. for (size_t d = 0; d < r->d; d++) {
  2652. if (unlikely(!(r->od[d] & RRDR_DIMENSION_QUERIED)))
  2653. continue;
  2654. if(co[d] & RRDR_VALUE_EMPTY)
  2655. continue;
  2656. total += cn[d];
  2657. }
  2658. if(total == 0.0)
  2659. total = 1.0;
  2660. for (size_t d = 0; d < r->d; d++) {
  2661. if (unlikely(!(r->od[d] & RRDR_DIMENSION_QUERIED)))
  2662. continue;
  2663. if(co[d] & RRDR_VALUE_EMPTY)
  2664. continue;
  2665. NETDATA_DOUBLE n = cn[d];
  2666. n = cn[d] = n * 100.0 / total;
  2667. if(unlikely(!global_min_max_values++))
  2668. global_min = global_max = n;
  2669. else {
  2670. if(n < global_min)
  2671. global_min = n;
  2672. if(n > global_max)
  2673. global_max = n;
  2674. }
  2675. }
  2676. }
  2677. r->view.min = global_min;
  2678. r->view.max = global_max;
  2679. if(!r->dview)
  2680. // v1 query
  2681. return;
  2682. // v2 query
  2683. for (size_t d = 0; d < r->d; d++) {
  2684. if (unlikely(!(r->od[d] & RRDR_DIMENSION_QUERIED)))
  2685. continue;
  2686. size_t count = 0;
  2687. NETDATA_DOUBLE min = 0.0, max = 0.0, sum = 0.0, ars = 0.0;
  2688. for(size_t i = 0; i != r->rows ;i++) { // we use r->rows to respect trimming
  2689. size_t idx = i * r->d + d;
  2690. RRDR_VALUE_FLAGS o = r->o[ idx ];
  2691. if (o & RRDR_VALUE_EMPTY)
  2692. continue;
  2693. NETDATA_DOUBLE ar = r->ar[ idx ];
  2694. ars += ar;
  2695. NETDATA_DOUBLE n = r->v[ idx ];
  2696. sum += n;
  2697. if(!count++)
  2698. min = max = n;
  2699. else {
  2700. if(n < min)
  2701. min = n;
  2702. if(n > max)
  2703. max = n;
  2704. }
  2705. }
  2706. r->dview[d] = (STORAGE_POINT) {
  2707. .sum = sum,
  2708. .count = count,
  2709. .min = min,
  2710. .max = max,
  2711. .anomaly_count = (size_t)(ars * (NETDATA_DOUBLE)count),
  2712. };
  2713. }
  2714. }
  2715. static RRDR *rrd2rrdr_group_by_finalize(RRDR *r_tmp) {
  2716. QUERY_TARGET *qt = r_tmp->internal.qt;
  2717. if(!r_tmp->group_by.r) {
  2718. // v1 query
  2719. rrd2rrdr_convert_values_to_percentage_of_total(r_tmp);
  2720. return r_tmp;
  2721. }
  2722. // v2 query
  2723. // do the additional passes on RRDRs
  2724. RRDR *last_r = r_tmp->group_by.r;
  2725. rrdr2rrdr_group_by_calculate_percentage_of_group(last_r);
  2726. RRDR *r = last_r->group_by.r;
  2727. size_t pass = 0;
  2728. while(r) {
  2729. pass++;
  2730. for(size_t d = 0; d < last_r->d ;d++) {
  2731. rrd2rrdr_group_by_add_metric(r, last_r->dgbs[d], last_r, d,
  2732. qt->request.group_by[pass].aggregation,
  2733. &last_r->dqp[d], pass);
  2734. }
  2735. rrdr2rrdr_group_by_calculate_percentage_of_group(r);
  2736. last_r = r;
  2737. r = last_r->group_by.r;
  2738. }
  2739. // free all RRDRs except the last one
  2740. r = r_tmp;
  2741. while(r != last_r) {
  2742. r_tmp = r->group_by.r;
  2743. r->group_by.r = NULL;
  2744. rrdr_free(r->internal.owa, r);
  2745. r = r_tmp;
  2746. }
  2747. r = last_r;
  2748. // find the final aggregation
  2749. RRDR_GROUP_BY_FUNCTION aggregation = qt->request.group_by[0].aggregation;
  2750. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++)
  2751. if(qt->request.group_by[g].group_by != RRDR_GROUP_BY_NONE)
  2752. aggregation = qt->request.group_by[g].aggregation;
  2753. if(!query_target_aggregatable(qt) && r->partial_data_trimming.expected_after < qt->window.before)
  2754. rrdr2rrdr_group_by_partial_trimming(r);
  2755. // apply averaging, remove RRDR_VALUE_EMPTY, find the non-zero dimensions, min and max
  2756. size_t global_min_max_values = 0;
  2757. size_t dimensions_nonzero = 0;
  2758. NETDATA_DOUBLE global_min = NAN, global_max = NAN;
  2759. for (size_t d = 0; d < r->d; d++) {
  2760. if (unlikely(!(r->od[d] & RRDR_DIMENSION_QUERIED)))
  2761. continue;
  2762. size_t points_nonzero = 0;
  2763. NETDATA_DOUBLE min = 0, max = 0, sum = 0, ars = 0;
  2764. size_t count = 0;
  2765. for(size_t i = 0; i != r->n ;i++) {
  2766. size_t idx = i * r->d + d;
  2767. NETDATA_DOUBLE *cn = &r->v[ idx ];
  2768. RRDR_VALUE_FLAGS *co = &r->o[ idx ];
  2769. NETDATA_DOUBLE *ar = &r->ar[ idx ];
  2770. uint32_t gbc = r->gbc[ idx ];
  2771. if(likely(gbc)) {
  2772. *co &= ~RRDR_VALUE_EMPTY;
  2773. if(gbc != r->dgbc[d])
  2774. *co |= RRDR_VALUE_PARTIAL;
  2775. NETDATA_DOUBLE n;
  2776. sum += *cn;
  2777. ars += *ar;
  2778. if(aggregation == RRDR_GROUP_BY_FUNCTION_AVERAGE && !query_target_aggregatable(qt))
  2779. n = (*cn /= gbc);
  2780. else
  2781. n = *cn;
  2782. if(!query_target_aggregatable(qt))
  2783. *ar /= gbc;
  2784. if(islessgreater(n, 0.0))
  2785. points_nonzero++;
  2786. if(unlikely(!count))
  2787. min = max = n;
  2788. else {
  2789. if(n < min)
  2790. min = n;
  2791. if(n > max)
  2792. max = n;
  2793. }
  2794. if(unlikely(!global_min_max_values++))
  2795. global_min = global_max = n;
  2796. else {
  2797. if(n < global_min)
  2798. global_min = n;
  2799. if(n > global_max)
  2800. global_max = n;
  2801. }
  2802. count += gbc;
  2803. }
  2804. }
  2805. if(points_nonzero) {
  2806. r->od[d] |= RRDR_DIMENSION_NONZERO;
  2807. dimensions_nonzero++;
  2808. }
  2809. r->dview[d] = (STORAGE_POINT) {
  2810. .sum = sum,
  2811. .count = count,
  2812. .min = min,
  2813. .max = max,
  2814. .anomaly_count = (size_t)(ars * RRDR_DVIEW_ANOMALY_COUNT_MULTIPLIER / 100.0),
  2815. };
  2816. }
  2817. r->view.min = global_min;
  2818. r->view.max = global_max;
  2819. if(!dimensions_nonzero && (qt->window.options & RRDR_OPTION_NONZERO)) {
  2820. // all dimensions are zero
  2821. // remove the nonzero option
  2822. qt->window.options &= ~RRDR_OPTION_NONZERO;
  2823. }
  2824. rrd2rrdr_convert_values_to_percentage_of_total(r);
  2825. // update query instance counts in query host and query context
  2826. {
  2827. size_t h = 0, c = 0, i = 0;
  2828. for(; h < qt->nodes.used ; h++) {
  2829. QUERY_NODE *qn = &qt->nodes.array[h];
  2830. for(; c < qt->contexts.used ;c++) {
  2831. QUERY_CONTEXT *qc = &qt->contexts.array[c];
  2832. if(!rrdcontext_acquired_belongs_to_host(qc->rca, qn->rrdhost))
  2833. break;
  2834. for(; i < qt->instances.used ;i++) {
  2835. QUERY_INSTANCE *qi = &qt->instances.array[i];
  2836. if(!rrdinstance_acquired_belongs_to_context(qi->ria, qc->rca))
  2837. break;
  2838. if(qi->metrics.queried) {
  2839. qc->instances.queried++;
  2840. qn->instances.queried++;
  2841. }
  2842. else if(qi->metrics.failed) {
  2843. qc->instances.failed++;
  2844. qn->instances.failed++;
  2845. }
  2846. }
  2847. }
  2848. }
  2849. }
  2850. return r;
  2851. }
  2852. // ----------------------------------------------------------------------------
  2853. // query entry point
  2854. RRDR *rrd2rrdr_legacy(
  2855. ONEWAYALLOC *owa,
  2856. RRDSET *st, size_t points, time_t after, time_t before,
  2857. RRDR_TIME_GROUPING group_method, time_t resampling_time, RRDR_OPTIONS options, const char *dimensions,
  2858. const char *group_options, time_t timeout_ms, size_t tier, QUERY_SOURCE query_source,
  2859. STORAGE_PRIORITY priority) {
  2860. QUERY_TARGET_REQUEST qtr = {
  2861. .version = 1,
  2862. .st = st,
  2863. .points = points,
  2864. .after = after,
  2865. .before = before,
  2866. .time_group_method = group_method,
  2867. .resampling_time = resampling_time,
  2868. .options = options,
  2869. .dimensions = dimensions,
  2870. .time_group_options = group_options,
  2871. .timeout_ms = timeout_ms,
  2872. .tier = tier,
  2873. .query_source = query_source,
  2874. .priority = priority,
  2875. };
  2876. QUERY_TARGET *qt = query_target_create(&qtr);
  2877. RRDR *r = rrd2rrdr(owa, qt);
  2878. if(!r) {
  2879. query_target_release(qt);
  2880. return NULL;
  2881. }
  2882. r->internal.release_with_rrdr_qt = qt;
  2883. return r;
  2884. }
  2885. RRDR *rrd2rrdr(ONEWAYALLOC *owa, QUERY_TARGET *qt) {
  2886. if(!qt || !owa)
  2887. return NULL;
  2888. // qt.window members are the WANTED ones.
  2889. // qt.request members are the REQUESTED ones.
  2890. RRDR *r_tmp = rrd2rrdr_group_by_initialize(owa, qt);
  2891. if(!r_tmp)
  2892. return NULL;
  2893. // the RRDR we group-by at
  2894. RRDR *r = (r_tmp->group_by.r) ? r_tmp->group_by.r : r_tmp;
  2895. // the final RRDR to return to callers
  2896. RRDR *last_r = r_tmp;
  2897. while(last_r->group_by.r)
  2898. last_r = last_r->group_by.r;
  2899. if(qt->window.relative)
  2900. last_r->view.flags |= RRDR_RESULT_FLAG_RELATIVE;
  2901. else
  2902. last_r->view.flags |= RRDR_RESULT_FLAG_ABSOLUTE;
  2903. // -------------------------------------------------------------------------
  2904. // assign the processor functions
  2905. rrdr_set_grouping_function(r_tmp, qt->window.time_group_method);
  2906. // allocate any memory required by the grouping method
  2907. r_tmp->time_grouping.create(r_tmp, qt->window.time_group_options);
  2908. // -------------------------------------------------------------------------
  2909. // do the work for each dimension
  2910. time_t max_after = 0, min_before = 0;
  2911. size_t max_rows = 0;
  2912. long dimensions_used = 0, dimensions_nonzero = 0;
  2913. size_t last_db_points_read = 0;
  2914. size_t last_result_points_generated = 0;
  2915. internal_fatal(released_ops, "QUERY: released_ops should be NULL when the query starts");
  2916. QUERY_ENGINE_OPS **ops = NULL;
  2917. if(qt->query.used)
  2918. ops = onewayalloc_callocz(owa, qt->query.used, sizeof(QUERY_ENGINE_OPS *));
  2919. size_t capacity = libuv_worker_threads * 10;
  2920. size_t max_queries_to_prepare = (qt->query.used > (capacity - 1)) ? (capacity - 1) : qt->query.used;
  2921. size_t queries_prepared = 0;
  2922. while(queries_prepared < max_queries_to_prepare) {
  2923. // preload another query
  2924. ops[queries_prepared] = rrd2rrdr_query_ops_prep(r_tmp, queries_prepared);
  2925. queries_prepared++;
  2926. }
  2927. QUERY_NODE *last_qn = NULL;
  2928. usec_t last_ut = now_monotonic_usec();
  2929. usec_t last_qn_ut = last_ut;
  2930. for(size_t d = 0; d < qt->query.used ; d++) {
  2931. QUERY_METRIC *qm = query_metric(qt, d);
  2932. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  2933. QUERY_INSTANCE *qi = query_instance(qt, qm->link.query_instance_id);
  2934. QUERY_CONTEXT *qc = query_context(qt, qm->link.query_context_id);
  2935. QUERY_NODE *qn = query_node(qt, qm->link.query_node_id);
  2936. usec_t now_ut = last_ut;
  2937. if(qn != last_qn) {
  2938. if(last_qn)
  2939. last_qn->duration_ut = now_ut - last_qn_ut;
  2940. last_qn = qn;
  2941. last_qn_ut = now_ut;
  2942. }
  2943. if(queries_prepared < qt->query.used) {
  2944. // preload another query
  2945. ops[queries_prepared] = rrd2rrdr_query_ops_prep(r_tmp, queries_prepared);
  2946. queries_prepared++;
  2947. }
  2948. size_t dim_in_rrdr_tmp = (r_tmp != r) ? 0 : d;
  2949. // set the query target dimension options to rrdr
  2950. r_tmp->od[dim_in_rrdr_tmp] = qm->status;
  2951. // reset the grouping for the new dimension
  2952. r_tmp->time_grouping.reset(r_tmp);
  2953. if(ops[d]) {
  2954. rrd2rrdr_query_execute(r_tmp, dim_in_rrdr_tmp, ops[d]);
  2955. r_tmp->od[dim_in_rrdr_tmp] |= RRDR_DIMENSION_QUERIED;
  2956. now_ut = now_monotonic_usec();
  2957. qm->duration_ut = now_ut - last_ut;
  2958. last_ut = now_ut;
  2959. if(r_tmp != r) {
  2960. // copy back whatever got updated from the temporary r
  2961. // the query updates RRDR_DIMENSION_NONZERO
  2962. qm->status = r_tmp->od[dim_in_rrdr_tmp];
  2963. // the query updates these
  2964. r->view.min = r_tmp->view.min;
  2965. r->view.max = r_tmp->view.max;
  2966. r->view.after = r_tmp->view.after;
  2967. r->view.before = r_tmp->view.before;
  2968. r->rows = r_tmp->rows;
  2969. rrd2rrdr_group_by_add_metric(r, qm->grouped_as.first_slot, r_tmp, dim_in_rrdr_tmp,
  2970. qt->request.group_by[0].aggregation, &qm->query_points, 0);
  2971. }
  2972. rrd2rrdr_query_ops_release(ops[d]); // reuse this ops allocation
  2973. ops[d] = NULL;
  2974. qi->metrics.queried++;
  2975. qc->metrics.queried++;
  2976. qn->metrics.queried++;
  2977. qd->status |= QUERY_STATUS_QUERIED;
  2978. qm->status |= RRDR_DIMENSION_QUERIED;
  2979. if(qt->request.version >= 2) {
  2980. // we need to make the query points positive now
  2981. // since we will aggregate it across multiple dimensions
  2982. storage_point_make_positive(qm->query_points);
  2983. storage_point_merge_to(qi->query_points, qm->query_points);
  2984. storage_point_merge_to(qc->query_points, qm->query_points);
  2985. storage_point_merge_to(qn->query_points, qm->query_points);
  2986. storage_point_merge_to(qt->query_points, qm->query_points);
  2987. }
  2988. }
  2989. else {
  2990. qi->metrics.failed++;
  2991. qc->metrics.failed++;
  2992. qn->metrics.failed++;
  2993. qd->status |= QUERY_STATUS_FAILED;
  2994. qm->status |= RRDR_DIMENSION_FAILED;
  2995. continue;
  2996. }
  2997. global_statistics_rrdr_query_completed(
  2998. 1,
  2999. r_tmp->stats.db_points_read - last_db_points_read,
  3000. r_tmp->stats.result_points_generated - last_result_points_generated,
  3001. qt->request.query_source);
  3002. last_db_points_read = r_tmp->stats.db_points_read;
  3003. last_result_points_generated = r_tmp->stats.result_points_generated;
  3004. if(qm->status & RRDR_DIMENSION_NONZERO)
  3005. dimensions_nonzero++;
  3006. // verify all dimensions are aligned
  3007. if(unlikely(!dimensions_used)) {
  3008. min_before = r->view.before;
  3009. max_after = r->view.after;
  3010. max_rows = r->rows;
  3011. }
  3012. else {
  3013. if(r->view.after != max_after) {
  3014. internal_error(true, "QUERY: 'after' mismatch between dimensions for chart '%s': max is %zu, dimension '%s' has %zu",
  3015. rrdinstance_acquired_id(qi->ria), (size_t)max_after, rrdmetric_acquired_id(qd->rma), (size_t)r->view.after);
  3016. r->view.after = (r->view.after > max_after) ? r->view.after : max_after;
  3017. }
  3018. if(r->view.before != min_before) {
  3019. internal_error(true, "QUERY: 'before' mismatch between dimensions for chart '%s': max is %zu, dimension '%s' has %zu",
  3020. rrdinstance_acquired_id(qi->ria), (size_t)min_before, rrdmetric_acquired_id(qd->rma), (size_t)r->view.before);
  3021. r->view.before = (r->view.before < min_before) ? r->view.before : min_before;
  3022. }
  3023. if(r->rows != max_rows) {
  3024. internal_error(true, "QUERY: 'rows' mismatch between dimensions for chart '%s': max is %zu, dimension '%s' has %zu",
  3025. rrdinstance_acquired_id(qi->ria), (size_t)max_rows, rrdmetric_acquired_id(qd->rma), (size_t)r->rows);
  3026. r->rows = (r->rows > max_rows) ? r->rows : max_rows;
  3027. }
  3028. }
  3029. dimensions_used++;
  3030. bool cancel = false;
  3031. if (qt->request.interrupt_callback && qt->request.interrupt_callback(qt->request.interrupt_callback_data)) {
  3032. cancel = true;
  3033. netdata_log_access("QUERY INTERRUPTED");
  3034. }
  3035. if (qt->request.timeout_ms && ((NETDATA_DOUBLE)(now_ut - qt->timings.received_ut) / 1000.0) > (NETDATA_DOUBLE)qt->request.timeout_ms) {
  3036. cancel = true;
  3037. netdata_log_access("QUERY CANCELED RUNTIME EXCEEDED %0.2f ms (LIMIT %lld ms)",
  3038. (NETDATA_DOUBLE)(now_ut - qt->timings.received_ut) / 1000.0, (long long)qt->request.timeout_ms);
  3039. }
  3040. if(cancel) {
  3041. r->view.flags |= RRDR_RESULT_FLAG_CANCEL;
  3042. for(size_t i = d + 1; i < queries_prepared ; i++) {
  3043. if(ops[i]) {
  3044. query_planer_finalize_remaining_plans(ops[i]);
  3045. rrd2rrdr_query_ops_release(ops[i]);
  3046. ops[i] = NULL;
  3047. }
  3048. }
  3049. break;
  3050. }
  3051. }
  3052. // free all resources used by the grouping method
  3053. r_tmp->time_grouping.free(r_tmp);
  3054. // get the final RRDR to send to the caller
  3055. r = rrd2rrdr_group_by_finalize(r_tmp);
  3056. #ifdef NETDATA_INTERNAL_CHECKS
  3057. if (dimensions_used && !(r->view.flags & RRDR_RESULT_FLAG_CANCEL)) {
  3058. if(r->internal.log)
  3059. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3060. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3061. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3062. r->internal.log);
  3063. if(r->rows != qt->window.points)
  3064. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3065. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3066. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3067. "got 'points' is not wanted 'points'");
  3068. if(qt->window.aligned && (r->view.before % query_view_update_every(qt)) != 0)
  3069. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3070. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3071. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3072. "'before' is not aligned but alignment is required");
  3073. // 'after' should not be aligned, since we start inside the first group
  3074. //if(qt->window.aligned && (r->after % group) != 0)
  3075. // rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group, qt->window.after, after_requested, before_wanted, before_requested, points_requested, points_wanted, after_slot, before_slot, "'after' is not aligned but alignment is required");
  3076. if(r->view.before != qt->window.before)
  3077. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3078. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3079. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3080. "chart is not aligned to requested 'before'");
  3081. if(r->view.before != qt->window.before)
  3082. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3083. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3084. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3085. "got 'before' is not wanted 'before'");
  3086. // reported 'after' varies, depending on group
  3087. if(r->view.after != qt->window.after)
  3088. rrd2rrdr_log_request_response_metadata(r, qt->window.options, qt->window.time_group_method, qt->window.aligned, qt->window.group, qt->request.resampling_time, qt->window.resampling_group,
  3089. qt->window.after, qt->request.after, qt->window.before, qt->request.before,
  3090. qt->request.points, qt->window.points, /*after_slot, before_slot,*/
  3091. "got 'after' is not wanted 'after'");
  3092. }
  3093. #endif
  3094. // free the query pipelining ops
  3095. for(size_t d = 0; d < qt->query.used ; d++) {
  3096. rrd2rrdr_query_ops_release(ops[d]);
  3097. ops[d] = NULL;
  3098. }
  3099. rrd2rrdr_query_ops_freeall(r);
  3100. internal_fatal(released_ops, "QUERY: released_ops should be NULL when the query ends");
  3101. onewayalloc_freez(owa, ops);
  3102. if(likely(dimensions_used && (qt->window.options & RRDR_OPTION_NONZERO) && !dimensions_nonzero))
  3103. // when all the dimensions are zero, we should return all of them
  3104. qt->window.options &= ~RRDR_OPTION_NONZERO;
  3105. qt->timings.executed_ut = now_monotonic_usec();
  3106. return r;
  3107. }