query.c 143 KB

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