query.c 144 KB

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