query.c 145 KB

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