query.c 143 KB

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