2
0

allocation.go 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501
  1. package opencost
  2. import (
  3. "fmt"
  4. "math"
  5. "sort"
  6. "strings"
  7. "time"
  8. "github.com/opencost/opencost/core/pkg/filter"
  9. "github.com/opencost/opencost/core/pkg/filter/ast"
  10. "github.com/opencost/opencost/core/pkg/filter/matcher"
  11. "github.com/opencost/opencost/core/pkg/log"
  12. "github.com/opencost/opencost/core/pkg/util"
  13. "github.com/opencost/opencost/core/pkg/util/timeutil"
  14. "golang.org/x/exp/slices"
  15. )
  16. // TODO Clean-up use of IsEmpty; nil checks should be separated for safety.
  17. // TODO Consider making Allocation an interface, which is fulfilled by structs
  18. // like KubernetesAllocation, IdleAllocation, and ExternalAllocation.
  19. // ExternalSuffix indicates an external allocation
  20. const ExternalSuffix = "__external__"
  21. // IdleSuffix indicates an idle allocation property
  22. const IdleSuffix = "__idle__"
  23. // SharedSuffix indicates an shared allocation property
  24. const SharedSuffix = "__shared__"
  25. // UnallocatedSuffix indicates an unallocated allocation property
  26. const UnallocatedSuffix = "__unallocated__"
  27. // UnmountedSuffix indicated allocation to an unmounted resource (PV or LB)
  28. const UnmountedSuffix = "__unmounted__"
  29. // ShareWeighted indicates that a shared resource should be shared as a
  30. // proportion of the cost of the remaining allocations.
  31. const ShareWeighted = "__weighted__"
  32. // ShareEven indicates that a shared resource should be shared evenly across
  33. // all remaining allocations.
  34. const ShareEven = "__even__"
  35. // ShareNone indicates that a shareable resource should not be shared
  36. const ShareNone = "__none__"
  37. // Allocation is a unit of resource allocation and cost for a given window
  38. // of time and for a given kubernetes construct with its associated set of
  39. // properties.
  40. // TODO:CLEANUP consider dropping name in favor of just Allocation and an
  41. // Assets-style key() function for AllocationSet.
  42. type Allocation struct {
  43. Name string `json:"name"`
  44. Properties *AllocationProperties `json:"properties,omitempty"`
  45. Window Window `json:"window"`
  46. Start time.Time `json:"start"`
  47. End time.Time `json:"end"`
  48. CPUCoreHours float64 `json:"cpuCoreHours"`
  49. CPUCoreRequestAverage float64 `json:"cpuCoreRequestAverage"`
  50. CPUCoreUsageAverage float64 `json:"cpuCoreUsageAverage"`
  51. CPUCost float64 `json:"cpuCost"`
  52. CPUCostAdjustment float64 `json:"cpuCostAdjustment"`
  53. GPUHours float64 `json:"gpuHours"`
  54. GPUCost float64 `json:"gpuCost"`
  55. GPUCostAdjustment float64 `json:"gpuCostAdjustment"`
  56. NetworkTransferBytes float64 `json:"networkTransferBytes"`
  57. NetworkReceiveBytes float64 `json:"networkReceiveBytes"`
  58. NetworkCost float64 `json:"networkCost"`
  59. NetworkCrossZoneCost float64 `json:"networkCrossZoneCost"` // @bingen:field[version=16]
  60. NetworkCrossRegionCost float64 `json:"networkCrossRegionCost"` // @bingen:field[version=16]
  61. NetworkInternetCost float64 `json:"networkInternetCost"` // @bingen:field[version=16]
  62. NetworkCostAdjustment float64 `json:"networkCostAdjustment"`
  63. LoadBalancerCost float64 `json:"loadBalancerCost"`
  64. LoadBalancerCostAdjustment float64 `json:"loadBalancerCostAdjustment"`
  65. PVs PVAllocations `json:"pvs"`
  66. PVCostAdjustment float64 `json:"pvCostAdjustment"`
  67. RAMByteHours float64 `json:"ramByteHours"`
  68. RAMBytesRequestAverage float64 `json:"ramByteRequestAverage"`
  69. RAMBytesUsageAverage float64 `json:"ramByteUsageAverage"`
  70. RAMCost float64 `json:"ramCost"`
  71. RAMCostAdjustment float64 `json:"ramCostAdjustment"`
  72. SharedCost float64 `json:"sharedCost"`
  73. ExternalCost float64 `json:"externalCost"`
  74. // RawAllocationOnly is a pointer so if it is not present it will be
  75. // marshalled as null rather than as an object with Go default values.
  76. RawAllocationOnly *RawAllocationOnlyData `json:"rawAllocationOnly"`
  77. // ProportionalAssetResourceCost represents the per-resource costs of the
  78. // allocation as a percentage of the per-resource total cost of the
  79. // asset on which the allocation was run. It is optionally computed
  80. // and appended to an Allocation, and so by default is is nil.
  81. ProportionalAssetResourceCosts ProportionalAssetResourceCosts `json:"proportionalAssetResourceCosts"` //@bingen:field[ignore]
  82. SharedCostBreakdown SharedCostBreakdowns `json:"sharedCostBreakdown"` //@bingen:field[ignore]
  83. LoadBalancers LbAllocations `json:"LoadBalancers"` //@bingen:field[version=18]
  84. // UnmountedPVCost is used to track how much of the cost in PVs is for an
  85. // unmounted PV. It is not additive of PVCost() and need not be sent in API
  86. // responses.
  87. UnmountedPVCost float64 `json:"-"` //@bingen:field[ignore]
  88. }
  89. type LbAllocations map[string]*LbAllocation
  90. func (orig LbAllocations) Clone() LbAllocations {
  91. if orig == nil {
  92. return nil
  93. }
  94. newAllocs := LbAllocations{}
  95. for key, lbAlloc := range orig {
  96. newAllocs[key] = &LbAllocation{
  97. Service: lbAlloc.Service,
  98. Cost: lbAlloc.Cost,
  99. Private: lbAlloc.Private,
  100. Ip: lbAlloc.Ip,
  101. Hours: lbAlloc.Hours,
  102. }
  103. }
  104. return newAllocs
  105. }
  106. type LbAllocation struct {
  107. Service string `json:"service"`
  108. Cost float64 `json:"cost"`
  109. Private bool `json:"private"`
  110. Ip string `json:"ip"` //@bingen:field[version=19]
  111. Hours float64 `json:"hours"` //@bingen:field[version=21]
  112. Adjustment float64 `json:"adjustment"` //@bingen:field[ignore]
  113. }
  114. func (lba *LbAllocation) SanitizeNaN() {
  115. if lba == nil {
  116. return
  117. }
  118. if math.IsNaN(lba.Cost) {
  119. log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Cost service:%s", lba.Service)
  120. lba.Cost = 0
  121. }
  122. if math.IsNaN(lba.Hours) {
  123. log.DedupedWarningf(5, "LBAllocation: Unexpected NaN found for Hours service:%s", lba.Service)
  124. lba.Hours = 0
  125. }
  126. }
  127. // RawAllocationOnlyData is information that only belong in "raw" Allocations,
  128. // those which have not undergone aggregation, accumulation, or any other form
  129. // of combination to produce a new Allocation from other Allocations.
  130. //
  131. // Max usage data belongs here because computing the overall maximum from two
  132. // or more Allocations is a non-trivial operation that cannot be defined without
  133. // maintaining a large amount of state. Consider the following example:
  134. // _______________________________________________
  135. //
  136. // A1 Using 3 CPU ---- ----- ------
  137. // A2 Using 2 CPU ---- ----- ----
  138. // A3 Using 1 CPU --- --
  139. // _______________________________________________
  140. //
  141. // Time ---->
  142. //
  143. // The logical maximum CPU usage is 5, but this cannot be calculated iteratively,
  144. // which is how we calculate aggregations and accumulations of Allocations currently.
  145. // This becomes a problem I could call "maximum sum of overlapping intervals" and is
  146. // essentially a variant of an interval scheduling algorithm.
  147. //
  148. // If we had types to differentiate between regular Allocations and AggregatedAllocations
  149. // then this type would be unnecessary and its fields would go into the regular Allocation
  150. // and not in the AggregatedAllocation.
  151. type RawAllocationOnlyData struct {
  152. CPUCoreUsageMax float64 `json:"cpuCoreUsageMax"`
  153. RAMBytesUsageMax float64 `json:"ramByteUsageMax"`
  154. }
  155. // Clone returns a deep copy of the given RawAllocationOnlyData
  156. func (r *RawAllocationOnlyData) Clone() *RawAllocationOnlyData {
  157. if r == nil {
  158. return nil
  159. }
  160. return &RawAllocationOnlyData{
  161. CPUCoreUsageMax: r.CPUCoreUsageMax,
  162. RAMBytesUsageMax: r.RAMBytesUsageMax,
  163. }
  164. }
  165. // Equal returns true if the RawAllocationOnlyData is approximately equal
  166. func (r *RawAllocationOnlyData) Equal(that *RawAllocationOnlyData) bool {
  167. if r == nil && that == nil {
  168. return true
  169. }
  170. if r == nil || that == nil {
  171. return false
  172. }
  173. return util.IsApproximately(r.CPUCoreUsageMax, that.CPUCoreUsageMax) &&
  174. util.IsApproximately(r.RAMBytesUsageMax, that.RAMBytesUsageMax)
  175. }
  176. func (r *RawAllocationOnlyData) SanitizeNaN() {
  177. if r == nil {
  178. return
  179. }
  180. if math.IsNaN(r.CPUCoreUsageMax) {
  181. log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for CPUCoreUsageMax")
  182. r.CPUCoreUsageMax = 0
  183. }
  184. if math.IsNaN(r.RAMBytesUsageMax) {
  185. log.DedupedWarningf(5, "RawAllocationOnlyData: Unexpected NaN found for RAMBytesUsageMax")
  186. r.RAMBytesUsageMax = 0
  187. }
  188. }
  189. // PVAllocations is a map of Disk Asset Identifiers to the
  190. // usage of them by an Allocation as recorded in a PVAllocation
  191. type PVAllocations map[PVKey]*PVAllocation
  192. // Clone creates a deep copy of a PVAllocations
  193. func (pv PVAllocations) Clone() PVAllocations {
  194. if pv == nil {
  195. return nil
  196. }
  197. clonePV := make(map[PVKey]*PVAllocation, len(pv))
  198. for k, v := range pv {
  199. clonePV[k] = &PVAllocation{
  200. ByteHours: v.ByteHours,
  201. Cost: v.Cost,
  202. ProviderID: v.ProviderID,
  203. }
  204. }
  205. return clonePV
  206. }
  207. // Add adds contents of that to the calling PVAllocations
  208. func (pv PVAllocations) Add(that PVAllocations) PVAllocations {
  209. apv := pv.Clone()
  210. if that != nil {
  211. if apv == nil {
  212. apv = PVAllocations{}
  213. }
  214. for pvKey, thatPVAlloc := range that {
  215. apvAlloc, ok := apv[pvKey]
  216. if !ok {
  217. apvAlloc = &PVAllocation{}
  218. }
  219. apvAlloc.Cost += thatPVAlloc.Cost
  220. apvAlloc.ByteHours += thatPVAlloc.ByteHours
  221. apv[pvKey] = apvAlloc
  222. if apvAlloc.ProviderID == thatPVAlloc.ProviderID {
  223. apv[pvKey].ProviderID = apvAlloc.ProviderID
  224. } else {
  225. apv[pvKey].ProviderID = ""
  226. }
  227. }
  228. }
  229. return apv
  230. }
  231. // Equal returns true if the two PVAllocations are equal in length and contain the same keys
  232. // and values.
  233. func (this PVAllocations) Equal(that PVAllocations) bool {
  234. if this == nil && that == nil {
  235. return true
  236. }
  237. if this == nil || that == nil {
  238. return false
  239. }
  240. if len(this) != len(that) {
  241. return false
  242. }
  243. for k, pv := range this {
  244. tv, ok := that[k]
  245. if !ok || !pv.Equal(tv) {
  246. return false
  247. }
  248. }
  249. return true
  250. }
  251. func (pvs PVAllocations) SanitizeNaN() {
  252. for _, pv := range pvs {
  253. pv.SanitizeNaN()
  254. }
  255. }
  256. // PVKey for identifying Disk type assets
  257. type PVKey struct {
  258. Cluster string `json:"cluster"`
  259. Name string `json:"name"`
  260. }
  261. func (pvk *PVKey) String() string {
  262. return fmt.Sprintf("cluster=%s:name=%s", pvk.Cluster, pvk.Name)
  263. }
  264. // FromString populates PVKey fields from string
  265. func (pvk *PVKey) FromString(key string) error {
  266. splitKey := strings.Split(key, ":")
  267. if len(splitKey) != 2 {
  268. return fmt.Errorf("PVKey string '%s' has the incorrect format", key)
  269. }
  270. pvk.Cluster = strings.TrimPrefix(splitKey[0], "cluster=")
  271. pvk.Name = strings.TrimPrefix(splitKey[1], "name=")
  272. return nil
  273. }
  274. // PVAllocation contains the byte hour usage
  275. // and cost of an Allocation for a single PV
  276. type PVAllocation struct {
  277. ByteHours float64 `json:"byteHours"`
  278. Cost float64 `json:"cost"`
  279. ProviderID string `json:"providerID"` // @bingen:field[version=20]
  280. Adjustment float64 `json:"adjustment"` //@bingen:field[ignore]
  281. }
  282. // Equal returns true if the two PVAllocation instances contain approximately the same
  283. // values.
  284. func (pva *PVAllocation) Equal(that *PVAllocation) bool {
  285. if pva == nil && that == nil {
  286. return true
  287. }
  288. if pva == nil || that == nil {
  289. return false
  290. }
  291. return util.IsApproximately(pva.ByteHours, that.ByteHours) &&
  292. util.IsApproximately(pva.Cost, that.Cost)
  293. }
  294. func (pva *PVAllocation) SanitizeNaN() {
  295. if pva == nil {
  296. return
  297. }
  298. if math.IsNaN(pva.ByteHours) {
  299. log.DedupedWarningf(5, "PVAllocation: Unexpected NaN found for ByteHours")
  300. pva.ByteHours = 0
  301. }
  302. if math.IsNaN(pva.Cost) {
  303. log.DedupedWarningf(5, "PVAllocation: Unexpected NaN found for Cost")
  304. pva.Cost = 0
  305. }
  306. }
  307. type ProportionalAssetResourceCost struct {
  308. Cluster string `json:"cluster"`
  309. Name string `json:"name,omitempty"`
  310. Type string `json:"type,omitempty"`
  311. ProviderID string `json:"providerID,omitempty"`
  312. CPUPercentage float64 `json:"cpuPercentage"`
  313. GPUPercentage float64 `json:"gpuPercentage"`
  314. RAMPercentage float64 `json:"ramPercentage"`
  315. LoadBalancerPercentage float64 `json:"loadBalancerPercentage"`
  316. PVPercentage float64 `json:"pvPercentage"`
  317. NodeResourceCostPercentage float64 `json:"nodeResourceCostPercentage"`
  318. GPUTotalCost float64 `json:"-"`
  319. GPUProportionalCost float64 `json:"-"`
  320. CPUTotalCost float64 `json:"-"`
  321. CPUProportionalCost float64 `json:"-"`
  322. RAMTotalCost float64 `json:"-"`
  323. RAMProportionalCost float64 `json:"-"`
  324. LoadBalancerProportionalCost float64 `json:"-"`
  325. LoadBalancerTotalCost float64 `json:"-"`
  326. PVProportionalCost float64 `json:"-"`
  327. PVTotalCost float64 `json:"-"`
  328. }
  329. func (parc ProportionalAssetResourceCost) Key(insertByName bool) string {
  330. if insertByName {
  331. return parc.Cluster + "," + parc.Name
  332. } else {
  333. return parc.Cluster
  334. }
  335. }
  336. type ProportionalAssetResourceCosts map[string]ProportionalAssetResourceCost
  337. func (parcs ProportionalAssetResourceCosts) Clone() ProportionalAssetResourceCosts {
  338. cloned := ProportionalAssetResourceCosts{}
  339. for key, parc := range parcs {
  340. cloned[key] = parc
  341. }
  342. return cloned
  343. }
  344. func (parcs ProportionalAssetResourceCosts) Insert(parc ProportionalAssetResourceCost, insertByName bool) {
  345. if !insertByName {
  346. parc.Name = ""
  347. parc.Type = ""
  348. parc.ProviderID = ""
  349. }
  350. if curr, ok := parcs[parc.Key(insertByName)]; ok {
  351. toInsert := ProportionalAssetResourceCost{
  352. Name: curr.Name,
  353. Type: curr.Type,
  354. Cluster: curr.Cluster,
  355. ProviderID: curr.ProviderID,
  356. CPUProportionalCost: curr.CPUProportionalCost + parc.CPUProportionalCost,
  357. RAMProportionalCost: curr.RAMProportionalCost + parc.RAMProportionalCost,
  358. GPUProportionalCost: curr.GPUProportionalCost + parc.GPUProportionalCost,
  359. PVProportionalCost: curr.PVProportionalCost + parc.PVProportionalCost,
  360. LoadBalancerProportionalCost: curr.LoadBalancerProportionalCost + parc.LoadBalancerProportionalCost,
  361. }
  362. ComputePercentages(&toInsert)
  363. parcs[parc.Key(insertByName)] = toInsert
  364. } else {
  365. ComputePercentages(&parc)
  366. parcs[parc.Key(insertByName)] = parc
  367. }
  368. }
  369. func ComputePercentages(toInsert *ProportionalAssetResourceCost) {
  370. totalNodeCost := toInsert.RAMTotalCost + toInsert.CPUTotalCost + toInsert.GPUTotalCost
  371. if toInsert.CPUTotalCost > 0 {
  372. toInsert.CPUPercentage = toInsert.CPUProportionalCost / toInsert.CPUTotalCost
  373. }
  374. if toInsert.GPUTotalCost > 0 {
  375. toInsert.GPUPercentage = toInsert.GPUProportionalCost / toInsert.GPUTotalCost
  376. }
  377. if toInsert.LoadBalancerTotalCost > 0 {
  378. toInsert.LoadBalancerPercentage = toInsert.LoadBalancerProportionalCost / toInsert.LoadBalancerTotalCost
  379. }
  380. if toInsert.RAMTotalCost > 0 {
  381. toInsert.RAMPercentage = toInsert.RAMProportionalCost / toInsert.RAMTotalCost
  382. }
  383. if toInsert.PVTotalCost > 0 {
  384. toInsert.PVPercentage = toInsert.PVProportionalCost / toInsert.PVTotalCost
  385. }
  386. ramFraction := toInsert.RAMTotalCost / totalNodeCost
  387. if ramFraction != ramFraction || ramFraction < 0 {
  388. ramFraction = 0
  389. }
  390. cpuFraction := toInsert.CPUTotalCost / totalNodeCost
  391. if cpuFraction != cpuFraction || cpuFraction < 0 {
  392. cpuFraction = 0
  393. }
  394. gpuFraction := toInsert.GPUTotalCost / totalNodeCost
  395. if gpuFraction != gpuFraction || gpuFraction < 0 {
  396. gpuFraction = 0
  397. }
  398. toInsert.NodeResourceCostPercentage = (toInsert.RAMPercentage * ramFraction) +
  399. (toInsert.CPUPercentage * cpuFraction) + (toInsert.GPUPercentage * gpuFraction)
  400. }
  401. func (parcs ProportionalAssetResourceCosts) Add(that ProportionalAssetResourceCosts) {
  402. for _, parc := range that {
  403. // if name field is empty, we know this is a cluster level PARC aggregation
  404. insertByName := true
  405. if parc.Name == "" {
  406. insertByName = false
  407. }
  408. parcs.Insert(parc, insertByName)
  409. }
  410. }
  411. func (parcs ProportionalAssetResourceCosts) SanitizeNaN() {
  412. for key, parc := range parcs {
  413. if math.IsNaN(parc.CPUPercentage) {
  414. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUPercentage name:%s", parc.Name)
  415. parc.CPUPercentage = 0
  416. }
  417. if math.IsNaN(parc.GPUPercentage) {
  418. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUPercentage name:%s", parc.Name)
  419. parc.GPUPercentage = 0
  420. }
  421. if math.IsNaN(parc.RAMPercentage) {
  422. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMPercentage name:%s", parc.Name)
  423. parc.RAMPercentage = 0
  424. }
  425. if math.IsNaN(parc.LoadBalancerPercentage) {
  426. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerPercentage name:%s", parc.Name)
  427. parc.LoadBalancerPercentage = 0
  428. }
  429. if math.IsNaN(parc.PVPercentage) {
  430. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVPercentage name:%s", parc.Name)
  431. parc.PVPercentage = 0
  432. }
  433. if math.IsNaN(parc.NodeResourceCostPercentage) {
  434. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for NodeResourceCostPercentage name:%s", parc.Name)
  435. parc.NodeResourceCostPercentage = 0
  436. }
  437. if math.IsNaN(parc.GPUTotalCost) {
  438. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUTotalCost name:%s", parc.Name)
  439. parc.GPUTotalCost = 0
  440. }
  441. if math.IsNaN(parc.GPUProportionalCost) {
  442. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for GPUProportionalCost name:%s", parc.Name)
  443. parc.GPUProportionalCost = 0
  444. }
  445. if math.IsNaN(parc.CPUTotalCost) {
  446. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUTotalCost name:%s", parc.Name)
  447. parc.CPUTotalCost = 0
  448. }
  449. if math.IsNaN(parc.CPUProportionalCost) {
  450. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for CPUProportionalCost name:%s", parc.Name)
  451. parc.CPUProportionalCost = 0
  452. }
  453. if math.IsNaN(parc.RAMTotalCost) {
  454. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMTotalCost name:%s", parc.Name)
  455. parc.RAMTotalCost = 0
  456. }
  457. if math.IsNaN(parc.RAMProportionalCost) {
  458. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for RAMProportionalCost name:%s", parc.Name)
  459. parc.RAMProportionalCost = 0
  460. }
  461. if math.IsNaN(parc.LoadBalancerProportionalCost) {
  462. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerProportionalCost name:%s", parc.Name)
  463. parc.LoadBalancerProportionalCost = 0
  464. }
  465. if math.IsNaN(parc.LoadBalancerTotalCost) {
  466. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for LoadBalancerTotalCost name:%s", parc.Name)
  467. parc.LoadBalancerTotalCost = 0
  468. }
  469. if math.IsNaN(parc.PVProportionalCost) {
  470. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVProportionalCost name:%s", parc.Name)
  471. parc.PVProportionalCost = 0
  472. }
  473. if math.IsNaN(parc.PVTotalCost) {
  474. log.DedupedWarningf(5, "ProportionalAssetResourceCosts: Unexpected NaN found for PVTotalCost name:%s", parc.Name)
  475. parc.PVTotalCost = 0
  476. }
  477. parcs[key] = parc
  478. }
  479. }
  480. type SharedCostBreakdown struct {
  481. Name string `json:"name"`
  482. TotalCost float64 `json:"totalCost"`
  483. CPUCost float64 `json:"cpuCost,omitempty"`
  484. GPUCost float64 `json:"gpuCost,omitempty"`
  485. RAMCost float64 `json:"ramCost,omitempty"`
  486. PVCost float64 `json:"pvCost,omitempty"`
  487. NetworkCost float64 `json:"networkCost,omitempty"`
  488. LBCost float64 `json:"loadBalancerCost,omitempty"`
  489. ExternalCost float64 `json:"externalCost,omitempty"`
  490. }
  491. type SharedCostBreakdowns map[string]SharedCostBreakdown
  492. func (scbs SharedCostBreakdowns) Clone() SharedCostBreakdowns {
  493. cloned := SharedCostBreakdowns{}
  494. for key, scb := range scbs {
  495. cloned[key] = scb
  496. }
  497. return cloned
  498. }
  499. func (scbs SharedCostBreakdowns) Insert(scb SharedCostBreakdown) {
  500. if curr, ok := scbs[scb.Name]; ok {
  501. scbs[scb.Name] = SharedCostBreakdown{
  502. Name: curr.Name,
  503. TotalCost: curr.TotalCost + scb.TotalCost,
  504. CPUCost: curr.CPUCost + scb.CPUCost,
  505. GPUCost: curr.GPUCost + scb.GPUCost,
  506. RAMCost: curr.RAMCost + scb.RAMCost,
  507. PVCost: curr.PVCost + scb.PVCost,
  508. NetworkCost: curr.NetworkCost + scb.NetworkCost,
  509. LBCost: curr.LBCost + scb.LBCost,
  510. ExternalCost: curr.ExternalCost + scb.ExternalCost,
  511. }
  512. } else {
  513. scbs[scb.Name] = scb
  514. }
  515. }
  516. func (scbs SharedCostBreakdowns) Add(that SharedCostBreakdowns) {
  517. for _, scb := range that {
  518. scbs.Insert(scb)
  519. }
  520. }
  521. func (scbs SharedCostBreakdowns) SanitizeNaN() {
  522. for key, scb := range scbs {
  523. if math.IsNaN(scb.CPUCost) {
  524. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for CPUCost name:%s", scb.Name)
  525. scb.CPUCost = 0
  526. }
  527. if math.IsNaN(scb.GPUCost) {
  528. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for GPUCost name:%s", scb.Name)
  529. scb.GPUCost = 0
  530. }
  531. if math.IsNaN(scb.RAMCost) {
  532. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for RAMCost name:%s", scb.Name)
  533. scb.RAMCost = 0
  534. }
  535. if math.IsNaN(scb.PVCost) {
  536. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for PVCost name:%s", scb.Name)
  537. scb.PVCost = 0
  538. }
  539. if math.IsNaN(scb.NetworkCost) {
  540. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for NetworkCost name:%s", scb.Name)
  541. scb.NetworkCost = 0
  542. }
  543. if math.IsNaN(scb.LBCost) {
  544. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for LBCost name:%s", scb.Name)
  545. scb.LBCost = 0
  546. }
  547. if math.IsNaN(scb.ExternalCost) {
  548. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for ExternalCost name:%s", scb.Name)
  549. scb.ExternalCost = 0
  550. }
  551. if math.IsNaN(scb.TotalCost) {
  552. log.DedupedWarningf(5, "SharedCostBreakdown: Unexpected NaN found for TotalCost name:%s", scb.Name)
  553. scb.TotalCost = 0
  554. }
  555. scbs[key] = scb
  556. }
  557. }
  558. // GetWindow returns the window of the struct
  559. func (a *Allocation) GetWindow() Window {
  560. return a.Window
  561. }
  562. // AllocationMatchFunc is a function that can be used to match Allocations by
  563. // returning true for any given Allocation if a condition is met.
  564. type AllocationMatchFunc func(*Allocation) bool
  565. // Add returns the result of summing the two given Allocations, which sums the
  566. // summary fields (e.g. costs, resources) and recomputes efficiency. Neither of
  567. // the two original Allocations are mutated in the process.
  568. func (a *Allocation) Add(that *Allocation) (*Allocation, error) {
  569. if a == nil {
  570. return that.Clone(), nil
  571. }
  572. if that == nil {
  573. return a.Clone(), nil
  574. }
  575. // Note: no need to clone "that", as add only mutates the receiver
  576. agg := a.Clone()
  577. agg.add(that)
  578. return agg, nil
  579. }
  580. // Clone returns a deep copy of the given Allocation
  581. func (a *Allocation) Clone() *Allocation {
  582. if a == nil {
  583. return nil
  584. }
  585. return &Allocation{
  586. Name: a.Name,
  587. Properties: a.Properties.Clone(),
  588. Window: a.Window.Clone(),
  589. Start: a.Start,
  590. End: a.End,
  591. CPUCoreHours: a.CPUCoreHours,
  592. CPUCoreRequestAverage: a.CPUCoreRequestAverage,
  593. CPUCoreUsageAverage: a.CPUCoreUsageAverage,
  594. CPUCost: a.CPUCost,
  595. CPUCostAdjustment: a.CPUCostAdjustment,
  596. GPUHours: a.GPUHours,
  597. GPUCost: a.GPUCost,
  598. GPUCostAdjustment: a.GPUCostAdjustment,
  599. NetworkTransferBytes: a.NetworkTransferBytes,
  600. NetworkReceiveBytes: a.NetworkReceiveBytes,
  601. NetworkCost: a.NetworkCost,
  602. NetworkCrossZoneCost: a.NetworkCrossZoneCost,
  603. NetworkCrossRegionCost: a.NetworkCrossRegionCost,
  604. NetworkInternetCost: a.NetworkInternetCost,
  605. NetworkCostAdjustment: a.NetworkCostAdjustment,
  606. LoadBalancerCost: a.LoadBalancerCost,
  607. LoadBalancerCostAdjustment: a.LoadBalancerCostAdjustment,
  608. PVs: a.PVs.Clone(),
  609. PVCostAdjustment: a.PVCostAdjustment,
  610. RAMByteHours: a.RAMByteHours,
  611. RAMBytesRequestAverage: a.RAMBytesRequestAverage,
  612. RAMBytesUsageAverage: a.RAMBytesUsageAverage,
  613. RAMCost: a.RAMCost,
  614. RAMCostAdjustment: a.RAMCostAdjustment,
  615. SharedCost: a.SharedCost,
  616. ExternalCost: a.ExternalCost,
  617. RawAllocationOnly: a.RawAllocationOnly.Clone(),
  618. ProportionalAssetResourceCosts: a.ProportionalAssetResourceCosts.Clone(),
  619. SharedCostBreakdown: a.SharedCostBreakdown.Clone(),
  620. LoadBalancers: a.LoadBalancers.Clone(),
  621. UnmountedPVCost: a.UnmountedPVCost,
  622. }
  623. }
  624. // Equal returns true if the values held in the given Allocation precisely
  625. // match those of the receiving Allocation. nil does not match nil. Floating
  626. // point values need to match according to util.IsApproximately, which accounts
  627. // for small, reasonable floating point error margins.
  628. func (a *Allocation) Equal(that *Allocation) bool {
  629. if a == nil || that == nil {
  630. return false
  631. }
  632. if a.Name != that.Name {
  633. return false
  634. }
  635. if !a.Properties.Equal(that.Properties) {
  636. return false
  637. }
  638. if !a.Window.Equal(that.Window) {
  639. return false
  640. }
  641. if !a.Start.Equal(that.Start) {
  642. return false
  643. }
  644. if !a.End.Equal(that.End) {
  645. return false
  646. }
  647. if !util.IsApproximately(a.CPUCoreHours, that.CPUCoreHours) {
  648. return false
  649. }
  650. if !util.IsApproximately(a.CPUCost, that.CPUCost) {
  651. return false
  652. }
  653. if !util.IsApproximately(a.CPUCostAdjustment, that.CPUCostAdjustment) {
  654. return false
  655. }
  656. if !util.IsApproximately(a.GPUHours, that.GPUHours) {
  657. return false
  658. }
  659. if !util.IsApproximately(a.GPUCost, that.GPUCost) {
  660. return false
  661. }
  662. if !util.IsApproximately(a.GPUCostAdjustment, that.GPUCostAdjustment) {
  663. return false
  664. }
  665. if !util.IsApproximately(a.NetworkTransferBytes, that.NetworkTransferBytes) {
  666. return false
  667. }
  668. if !util.IsApproximately(a.NetworkReceiveBytes, that.NetworkReceiveBytes) {
  669. return false
  670. }
  671. if !util.IsApproximately(a.NetworkCost, that.NetworkCost) {
  672. return false
  673. }
  674. if !util.IsApproximately(a.NetworkCrossZoneCost, that.NetworkCrossZoneCost) {
  675. return false
  676. }
  677. if !util.IsApproximately(a.NetworkCrossRegionCost, that.NetworkCrossRegionCost) {
  678. return false
  679. }
  680. if !util.IsApproximately(a.NetworkInternetCost, that.NetworkInternetCost) {
  681. return false
  682. }
  683. if !util.IsApproximately(a.NetworkCostAdjustment, that.NetworkCostAdjustment) {
  684. return false
  685. }
  686. if !util.IsApproximately(a.LoadBalancerCost, that.LoadBalancerCost) {
  687. return false
  688. }
  689. if !util.IsApproximately(a.LoadBalancerCostAdjustment, that.LoadBalancerCostAdjustment) {
  690. return false
  691. }
  692. if !util.IsApproximately(a.PVCostAdjustment, that.PVCostAdjustment) {
  693. return false
  694. }
  695. if !util.IsApproximately(a.RAMByteHours, that.RAMByteHours) {
  696. return false
  697. }
  698. if !util.IsApproximately(a.RAMCost, that.RAMCost) {
  699. return false
  700. }
  701. if !util.IsApproximately(a.RAMCostAdjustment, that.RAMCostAdjustment) {
  702. return false
  703. }
  704. if !util.IsApproximately(a.SharedCost, that.SharedCost) {
  705. return false
  706. }
  707. if !util.IsApproximately(a.ExternalCost, that.ExternalCost) {
  708. return false
  709. }
  710. if !a.RawAllocationOnly.Equal(that.RawAllocationOnly) {
  711. return false
  712. }
  713. if !a.PVs.Equal(that.PVs) {
  714. return false
  715. }
  716. if !util.IsApproximately(a.UnmountedPVCost, that.UnmountedPVCost) {
  717. return false
  718. }
  719. return true
  720. }
  721. // TotalCost is the total cost of the Allocation including adjustments
  722. func (a *Allocation) TotalCost() float64 {
  723. if a == nil {
  724. return 0.0
  725. }
  726. return a.CPUTotalCost() + a.GPUTotalCost() + a.RAMTotalCost() + a.PVTotalCost() + a.NetworkTotalCost() + a.LBTotalCost() + a.SharedTotalCost() + a.ExternalCost
  727. }
  728. // CPUTotalCost calculates total CPU cost of Allocation including adjustment
  729. func (a *Allocation) CPUTotalCost() float64 {
  730. if a == nil {
  731. return 0.0
  732. }
  733. return a.CPUCost + a.CPUCostAdjustment
  734. }
  735. // GPUTotalCost calculates total GPU cost of Allocation including adjustment
  736. func (a *Allocation) GPUTotalCost() float64 {
  737. if a == nil {
  738. return 0.0
  739. }
  740. return a.GPUCost + a.GPUCostAdjustment
  741. }
  742. // RAMTotalCost calculates total RAM cost of Allocation including adjustment
  743. func (a *Allocation) RAMTotalCost() float64 {
  744. if a == nil {
  745. return 0.0
  746. }
  747. return a.RAMCost + a.RAMCostAdjustment
  748. }
  749. // PVTotalCost calculates total PV cost of Allocation including adjustment
  750. func (a *Allocation) PVTotalCost() float64 {
  751. if a == nil {
  752. return 0.0
  753. }
  754. return a.PVCost() + a.PVCostAdjustment
  755. }
  756. // NetworkTotalCost calculates total Network cost of Allocation including adjustment
  757. func (a *Allocation) NetworkTotalCost() float64 {
  758. if a == nil {
  759. return 0.0
  760. }
  761. return a.NetworkCost + a.NetworkCostAdjustment
  762. }
  763. // LBTotalCost calculates total LB cost of Allocation including adjustment
  764. // TODO deprecate
  765. func (a *Allocation) LBTotalCost() float64 {
  766. return a.LoadBalancerTotalCost()
  767. }
  768. // LoadBalancerTotalCost calculates total LB cost of Allocation including adjustment
  769. func (a *Allocation) LoadBalancerTotalCost() float64 {
  770. if a == nil {
  771. return 0.0
  772. }
  773. return a.LoadBalancerCost + a.LoadBalancerCostAdjustment
  774. }
  775. // SharedTotalCost calculates total shared cost of Allocation including adjustment
  776. func (a *Allocation) SharedTotalCost() float64 {
  777. if a == nil {
  778. return 0.0
  779. }
  780. return a.SharedCost
  781. }
  782. // PVCost calculate cumulative cost of all PVs that Allocation is attached to
  783. func (a *Allocation) PVCost() float64 {
  784. if a == nil {
  785. return 0.0
  786. }
  787. cost := 0.0
  788. for _, pv := range a.PVs {
  789. cost += pv.Cost
  790. }
  791. return cost
  792. }
  793. // PVByteHours calculate cumulative ByteHours of all PVs that Allocation is attached to
  794. func (a *Allocation) PVByteHours() float64 {
  795. if a == nil {
  796. return 0.0
  797. }
  798. byteHours := 0.0
  799. for _, pv := range a.PVs {
  800. byteHours += pv.ByteHours
  801. }
  802. return byteHours
  803. }
  804. // CPUEfficiency is the ratio of usage to request. If there is no request and
  805. // no usage or cost, then efficiency is zero. If there is no request, but there
  806. // is usage or cost, then efficiency is 100%.
  807. func (a *Allocation) CPUEfficiency() float64 {
  808. if a == nil {
  809. return 0.0
  810. }
  811. if a.CPUCoreRequestAverage > 0 {
  812. return a.CPUCoreUsageAverage / a.CPUCoreRequestAverage
  813. }
  814. if a.CPUCoreUsageAverage == 0.0 || a.CPUCost == 0.0 {
  815. return 0.0
  816. }
  817. return 1.0
  818. }
  819. // RAMEfficiency is the ratio of usage to request. If there is no request and
  820. // no usage or cost, then efficiency is zero. If there is no request, but there
  821. // is usage or cost, then efficiency is 100%.
  822. func (a *Allocation) RAMEfficiency() float64 {
  823. if a == nil {
  824. return 0.0
  825. }
  826. if a.RAMBytesRequestAverage > 0 {
  827. return a.RAMBytesUsageAverage / a.RAMBytesRequestAverage
  828. }
  829. if a.RAMBytesUsageAverage == 0.0 || a.RAMCost == 0.0 {
  830. return 0.0
  831. }
  832. return 1.0
  833. }
  834. // TotalEfficiency is the cost-weighted average of CPU and RAM efficiency. If
  835. // there is no cost at all, then efficiency is zero.
  836. func (a *Allocation) TotalEfficiency() float64 {
  837. if a == nil {
  838. return 0.0
  839. }
  840. if a.RAMTotalCost()+a.CPUTotalCost() > 0 {
  841. ramCostEff := a.RAMEfficiency() * a.RAMTotalCost()
  842. cpuCostEff := a.CPUEfficiency() * a.CPUTotalCost()
  843. return (ramCostEff + cpuCostEff) / (a.CPUTotalCost() + a.RAMTotalCost())
  844. }
  845. return 0.0
  846. }
  847. // CPUCores converts the Allocation's CPUCoreHours into average CPUCores
  848. func (a *Allocation) CPUCores() float64 {
  849. if a.Minutes() <= 0.0 {
  850. return 0.0
  851. }
  852. return a.CPUCoreHours / (a.Minutes() / 60.0)
  853. }
  854. // RAMBytes converts the Allocation's RAMByteHours into average RAMBytes
  855. func (a *Allocation) RAMBytes() float64 {
  856. if a.Minutes() <= 0.0 {
  857. return 0.0
  858. }
  859. return a.RAMByteHours / (a.Minutes() / 60.0)
  860. }
  861. // GPUs converts the Allocation's GPUHours into average GPUs
  862. func (a *Allocation) GPUs() float64 {
  863. if a.Minutes() <= 0.0 {
  864. return 0.0
  865. }
  866. return a.GPUHours / (a.Minutes() / 60.0)
  867. }
  868. // PVBytes converts the Allocation's PVByteHours into average PVBytes
  869. func (a *Allocation) PVBytes() float64 {
  870. if a.Minutes() <= 0.0 {
  871. return 0.0
  872. }
  873. return a.PVByteHours() / (a.Minutes() / 60.0)
  874. }
  875. // ResetAdjustments sets all cost adjustment fields to zero
  876. func (a *Allocation) ResetAdjustments() {
  877. if a == nil {
  878. return
  879. }
  880. a.CPUCostAdjustment = 0.0
  881. a.GPUCostAdjustment = 0.0
  882. a.RAMCostAdjustment = 0.0
  883. a.PVCostAdjustment = 0.0
  884. a.NetworkCostAdjustment = 0.0
  885. a.LoadBalancerCostAdjustment = 0.0
  886. }
  887. // Resolution returns the duration of time covered by the Allocation
  888. func (a *Allocation) Resolution() time.Duration {
  889. return a.End.Sub(a.Start)
  890. }
  891. // IsAggregated is true if the given Allocation has been aggregated, which we
  892. // define by a lack of AllocationProperties.
  893. func (a *Allocation) IsAggregated() bool {
  894. return a == nil || a.Properties == nil
  895. }
  896. // IsExternal is true if the given Allocation represents external costs.
  897. func (a *Allocation) IsExternal() bool {
  898. if a == nil {
  899. return false
  900. }
  901. return strings.Contains(a.Name, ExternalSuffix)
  902. }
  903. // IsIdle is true if the given Allocation represents idle costs.
  904. func (a *Allocation) IsIdle() bool {
  905. if a == nil {
  906. return false
  907. }
  908. return strings.Contains(a.Name, IdleSuffix)
  909. }
  910. // IsUnallocated is true if the given Allocation represents unallocated costs.
  911. func (a *Allocation) IsUnallocated() bool {
  912. if a == nil {
  913. return false
  914. }
  915. return strings.Contains(a.Name, UnallocatedSuffix)
  916. }
  917. // IsUnmounted is true if the given Allocation represents unmounted volume costs.
  918. func (a *Allocation) IsUnmounted() bool {
  919. if a == nil {
  920. return false
  921. }
  922. return strings.Contains(a.Name, UnmountedSuffix)
  923. }
  924. // Minutes returns the number of minutes the Allocation represents, as defined
  925. // by the difference between the end and start times.
  926. func (a *Allocation) Minutes() float64 {
  927. if a == nil {
  928. return 0.0
  929. }
  930. return a.End.Sub(a.Start).Minutes()
  931. }
  932. // SetUnmountedPVCost determines if the Allocation is unmounted and, if so, it
  933. // sets the UnmountedPVCost field appropriately.
  934. func (a *Allocation) SetUnmountedPVCost() float64 {
  935. if a == nil {
  936. return 0.0
  937. }
  938. if a.IsUnmounted() {
  939. a.UnmountedPVCost = a.PVTotalCost()
  940. return a.UnmountedPVCost
  941. }
  942. return 0.0
  943. }
  944. // Share adds the TotalCost of the given Allocation to the SharedCost of the
  945. // receiving Allocation. No Start, End, Window, or AllocationProperties are considered.
  946. // Neither Allocation is mutated; a new Allocation is always returned.
  947. func (a *Allocation) Share(that *Allocation) (*Allocation, error) {
  948. if that == nil {
  949. return a.Clone(), nil
  950. }
  951. if a == nil {
  952. return nil, fmt.Errorf("cannot share with nil Allocation")
  953. }
  954. agg := a.Clone()
  955. agg.SharedCost += that.TotalCost()
  956. return agg, nil
  957. }
  958. // String represents the given Allocation as a string
  959. func (a *Allocation) String() string {
  960. if a == nil {
  961. return "<nil>"
  962. }
  963. return fmt.Sprintf("%s%s=%.2f", a.Name, NewWindow(&a.Start, &a.End), a.TotalCost())
  964. }
  965. func (a *Allocation) add(that *Allocation) {
  966. if a == nil {
  967. log.Warnf("Allocation.AggregateBy: trying to add a nil receiver")
  968. return
  969. }
  970. // Generate keys for each allocation to allow for special logic to set the controller
  971. // in the case of keys matching but controllers not matching.
  972. aggByForKey := []string{"cluster", "node", "namespace", "pod", "container"}
  973. leftKey := a.generateKey(aggByForKey, nil)
  974. rightKey := that.generateKey(aggByForKey, nil)
  975. leftProperties := a.Properties
  976. rightProperties := that.Properties
  977. // Preserve string properties that are matching between the two allocations
  978. a.Properties = a.Properties.Intersection(that.Properties)
  979. // If both Allocations have ProportionalAssetResourceCosts, then
  980. // add those from the given Allocation into the receiver.
  981. if a.ProportionalAssetResourceCosts != nil || that.ProportionalAssetResourceCosts != nil {
  982. // init empty PARCs if either operand has nil PARCs
  983. if a.ProportionalAssetResourceCosts == nil {
  984. a.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  985. }
  986. if that.ProportionalAssetResourceCosts == nil {
  987. that.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  988. }
  989. a.ProportionalAssetResourceCosts.Add(that.ProportionalAssetResourceCosts)
  990. }
  991. // If both Allocations have SharedCostBreakdowns, then
  992. // add those from the given Allocation into the receiver.
  993. if a.SharedCostBreakdown != nil || that.SharedCostBreakdown != nil {
  994. if a.SharedCostBreakdown == nil {
  995. a.SharedCostBreakdown = SharedCostBreakdowns{}
  996. }
  997. if that.SharedCostBreakdown == nil {
  998. that.SharedCostBreakdown = SharedCostBreakdowns{}
  999. }
  1000. a.SharedCostBreakdown.Add(that.SharedCostBreakdown)
  1001. }
  1002. // Overwrite regular intersection logic for the controller name property in the
  1003. // case that the Allocation keys are the same but the controllers are not.
  1004. if leftKey == rightKey &&
  1005. leftProperties != nil &&
  1006. rightProperties != nil &&
  1007. leftProperties.Controller != rightProperties.Controller {
  1008. if leftProperties.Controller == "" {
  1009. a.Properties.Controller = rightProperties.Controller
  1010. } else if rightProperties.Controller == "" {
  1011. a.Properties.Controller = leftProperties.Controller
  1012. } else {
  1013. controllers := []string{
  1014. leftProperties.Controller,
  1015. rightProperties.Controller,
  1016. }
  1017. sort.Strings(controllers)
  1018. a.Properties.Controller = controllers[0]
  1019. }
  1020. }
  1021. // Expand the window to encompass both Allocations
  1022. a.Window = a.Window.Expand(that.Window)
  1023. // Sum non-cumulative fields by turning them into cumulative, adding them,
  1024. // and then converting them back into averages after minutes have been
  1025. // combined (just below).
  1026. cpuReqCoreMins := a.CPUCoreRequestAverage * a.Minutes()
  1027. cpuReqCoreMins += that.CPUCoreRequestAverage * that.Minutes()
  1028. cpuUseCoreMins := a.CPUCoreUsageAverage * a.Minutes()
  1029. cpuUseCoreMins += that.CPUCoreUsageAverage * that.Minutes()
  1030. ramReqByteMins := a.RAMBytesRequestAverage * a.Minutes()
  1031. ramReqByteMins += that.RAMBytesRequestAverage * that.Minutes()
  1032. ramUseByteMins := a.RAMBytesUsageAverage * a.Minutes()
  1033. ramUseByteMins += that.RAMBytesUsageAverage * that.Minutes()
  1034. // Expand Start and End to be the "max" of among the given Allocations
  1035. if that.Start.Before(a.Start) {
  1036. a.Start = that.Start
  1037. }
  1038. if that.End.After(a.End) {
  1039. a.End = that.End
  1040. }
  1041. // Convert cumulative request and usage back into rates
  1042. // TODO:TEST write a unit test that fails if this is done incorrectly
  1043. if a.Minutes() > 0 {
  1044. a.CPUCoreRequestAverage = cpuReqCoreMins / a.Minutes()
  1045. a.CPUCoreUsageAverage = cpuUseCoreMins / a.Minutes()
  1046. a.RAMBytesRequestAverage = ramReqByteMins / a.Minutes()
  1047. a.RAMBytesUsageAverage = ramUseByteMins / a.Minutes()
  1048. } else {
  1049. a.CPUCoreRequestAverage = 0.0
  1050. a.CPUCoreUsageAverage = 0.0
  1051. a.RAMBytesRequestAverage = 0.0
  1052. a.RAMBytesUsageAverage = 0.0
  1053. }
  1054. // Sum all cumulative resource fields
  1055. a.CPUCoreHours += that.CPUCoreHours
  1056. a.GPUHours += that.GPUHours
  1057. a.RAMByteHours += that.RAMByteHours
  1058. a.NetworkTransferBytes += that.NetworkTransferBytes
  1059. a.NetworkReceiveBytes += that.NetworkReceiveBytes
  1060. // Sum all cumulative cost fields
  1061. a.CPUCost += that.CPUCost
  1062. a.GPUCost += that.GPUCost
  1063. a.RAMCost += that.RAMCost
  1064. a.NetworkCost += that.NetworkCost
  1065. a.NetworkCrossZoneCost += that.NetworkCrossZoneCost
  1066. a.NetworkCrossRegionCost += that.NetworkCrossRegionCost
  1067. a.NetworkInternetCost += that.NetworkInternetCost
  1068. a.LoadBalancerCost += that.LoadBalancerCost
  1069. a.SharedCost += that.SharedCost
  1070. a.ExternalCost += that.ExternalCost
  1071. a.UnmountedPVCost += that.UnmountedPVCost
  1072. // Sum PVAllocations
  1073. a.PVs = a.PVs.Add(that.PVs)
  1074. // Sum all cumulative adjustment fields
  1075. a.CPUCostAdjustment += that.CPUCostAdjustment
  1076. a.RAMCostAdjustment += that.RAMCostAdjustment
  1077. a.GPUCostAdjustment += that.GPUCostAdjustment
  1078. a.PVCostAdjustment += that.PVCostAdjustment
  1079. a.NetworkCostAdjustment += that.NetworkCostAdjustment
  1080. a.LoadBalancerCostAdjustment += that.LoadBalancerCostAdjustment
  1081. // Sum LoadBalancer Allocations
  1082. a.LoadBalancers = a.LoadBalancers.Add(that.LoadBalancers)
  1083. // Any data that is in a "raw allocation only" is not valid in any
  1084. // sort of cumulative Allocation (like one that is added).
  1085. a.RawAllocationOnly = nil
  1086. }
  1087. func (thisLbAllocs LbAllocations) Add(thatLbAllocs LbAllocations) LbAllocations {
  1088. // loop through both sets of LB allocations, building a new LBAllocations that has the summed set
  1089. mergedLbAllocs := thisLbAllocs.Clone()
  1090. if thatLbAllocs != nil {
  1091. if mergedLbAllocs == nil {
  1092. mergedLbAllocs = LbAllocations{}
  1093. }
  1094. for lbKey, thatlbAlloc := range thatLbAllocs {
  1095. thisLbAlloc, ok := mergedLbAllocs[lbKey]
  1096. if !ok {
  1097. thisLbAlloc = &LbAllocation{
  1098. Service: thatlbAlloc.Service,
  1099. Cost: thatlbAlloc.Cost,
  1100. Hours: thatlbAlloc.Hours,
  1101. }
  1102. mergedLbAllocs[lbKey] = thisLbAlloc
  1103. } else {
  1104. thisLbAlloc.Cost += thatlbAlloc.Cost
  1105. thisLbAlloc.Hours += thatlbAlloc.Hours
  1106. }
  1107. }
  1108. }
  1109. return mergedLbAllocs
  1110. }
  1111. func (thisLbAllocs LbAllocations) SanitizeNaN() {
  1112. for _, lba := range thisLbAllocs {
  1113. lba.SanitizeNaN()
  1114. }
  1115. }
  1116. // AllocationSet stores a set of Allocations, each with a unique name, that share
  1117. // a window. An AllocationSet is mutable, so treat it like a threadsafe map.
  1118. type AllocationSet struct {
  1119. Allocations map[string]*Allocation
  1120. ExternalKeys map[string]bool
  1121. IdleKeys map[string]bool
  1122. FromSource string // stores the name of the source used to compute the data
  1123. Window Window
  1124. Warnings []string
  1125. Errors []string
  1126. }
  1127. // NewAllocationSet instantiates a new AllocationSet and, optionally, inserts
  1128. // the given list of Allocations
  1129. func NewAllocationSet(start, end time.Time, allocs ...*Allocation) *AllocationSet {
  1130. as := &AllocationSet{
  1131. Allocations: map[string]*Allocation{},
  1132. ExternalKeys: map[string]bool{},
  1133. IdleKeys: map[string]bool{},
  1134. Window: NewWindow(&start, &end),
  1135. }
  1136. for _, a := range allocs {
  1137. as.Insert(a)
  1138. }
  1139. return as
  1140. }
  1141. // AllocationAggregationOptions provide advanced functionality to AggregateBy, including
  1142. // filtering results and sharing allocations. FilterFuncs are a list of match
  1143. // functions such that, if any function fails, the allocation is ignored.
  1144. // ShareFuncs are a list of match functions such that, if any function
  1145. // succeeds, the allocation is marked as a shared resource. ShareIdle is a
  1146. // simple flag for sharing idle resources.
  1147. type AllocationAggregationOptions struct {
  1148. AllocationTotalsStore AllocationTotalsStore
  1149. Filter filter.Filter
  1150. IdleByNode bool
  1151. IncludeProportionalAssetResourceCosts bool
  1152. LabelConfig *LabelConfig
  1153. MergeUnallocated bool
  1154. Reconcile bool
  1155. ReconcileNetwork bool
  1156. Share filter.Filter
  1157. SharedNamespaces []string
  1158. SharedLabels map[string][]string
  1159. ShareIdle string
  1160. ShareSplit string
  1161. SharedHourlyCosts map[string]float64
  1162. IncludeSharedCostBreakdown bool
  1163. SplitIdle bool
  1164. IncludeAggregatedMetadata bool
  1165. }
  1166. func isFilterEmpty(filter AllocationMatcher) bool {
  1167. if _, isAllPass := filter.(*matcher.AllPass[*Allocation]); isAllPass {
  1168. return true
  1169. }
  1170. return false
  1171. }
  1172. // AggregateBy aggregates the Allocations in the given AllocationSet by the given
  1173. // AllocationProperty. This will only be legal if the AllocationSet is divisible by the
  1174. // given AllocationProperty; e.g. Containers can be divided by Namespace, but not vice-a-versa.
  1175. func (as *AllocationSet) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  1176. // The order of operations for aggregating allocations is as follows:
  1177. //
  1178. // 1. Partition external, idle, and shared allocations into separate sets.
  1179. // Also, create the aggSet into which the results will be aggregated.
  1180. //
  1181. // 2. Compute sharing coefficients for idle and shared resources
  1182. // a) if idle allocation is to be shared, or if proportional asset
  1183. // resource costs are to be included, then compute idle coefficients
  1184. // (proportional asset resource costs are derived from idle coefficients)
  1185. // b) if proportional asset costs are to be included, derive them from
  1186. // idle coefficients and add them to the allocations.
  1187. // c) if idle allocation is NOT shared, but filters are present, compute
  1188. // idle filtration coefficients for the purpose of only returning the
  1189. // portion of idle allocation that would have been shared with the
  1190. // unfiltered results. (See unit tests 5.a,b,c)
  1191. // d) generate shared allocation for them given shared overhead, which
  1192. // must happen after (2a) and (2b)
  1193. // e) if there are shared resources, compute share coefficients
  1194. //
  1195. // 3. Drop any allocation that fails any of the filters
  1196. //
  1197. // 4. Distribute idle allocations according to the idle coefficients
  1198. //
  1199. // 5. Generate aggregation key and insert allocation into the output set
  1200. //
  1201. // 6. If idle is shared and resources are shared, some idle might be shared
  1202. // with a shared resource. Distribute that to the shared resources
  1203. // prior to sharing them with the aggregated results.
  1204. //
  1205. // 7. Apply idle filtration coefficients from step (2b)
  1206. //
  1207. // 8. Distribute shared allocations according to the share coefficients.
  1208. //
  1209. // 9. If there are external allocations that can be aggregated into
  1210. // the output (i.e. they can be used to generate a valid key for
  1211. // the given properties) then aggregate; otherwise... ignore them?
  1212. //
  1213. // 10. Distribute any undistributed idle, in the case that idle
  1214. // coefficients end up being zero and some idle is not shared.
  1215. //
  1216. // 11. If the merge idle option is enabled, merge any remaining idle
  1217. // allocations into a single idle allocation. If there was any idle
  1218. // whose costs were not distributed because there was no usage of a
  1219. // specific resource type, re-add the idle to the aggregation with
  1220. // only that type.
  1221. if as.IsEmpty() {
  1222. return nil
  1223. }
  1224. if options == nil {
  1225. options = &AllocationAggregationOptions{}
  1226. }
  1227. if options.LabelConfig == nil {
  1228. options.LabelConfig = NewLabelConfig()
  1229. }
  1230. // idleFiltrationCoefficients relies on this being explicitly set
  1231. if options.ShareIdle != ShareWeighted {
  1232. options.ShareIdle = ShareNone
  1233. }
  1234. var filter AllocationMatcher
  1235. if options.Filter == nil {
  1236. filter = &matcher.AllPass[*Allocation]{}
  1237. } else {
  1238. compiler := NewAllocationMatchCompiler(options.LabelConfig)
  1239. var err error
  1240. filter, err = compiler.Compile(options.Filter)
  1241. if err != nil {
  1242. return fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(options.Filter), err)
  1243. }
  1244. }
  1245. if filter == nil {
  1246. return fmt.Errorf("unexpected nil filter")
  1247. }
  1248. var sharer AllocationMatcher
  1249. if options.Share != nil {
  1250. compiler := NewAllocationMatchCompiler(options.LabelConfig)
  1251. var err error
  1252. sharer, err = compiler.Compile(options.Share)
  1253. if err != nil {
  1254. return fmt.Errorf("compiling sharer '%s': %w", ast.ToPreOrderShortString(options.Filter), err)
  1255. }
  1256. }
  1257. var allocatedTotalsMap map[string]map[string]float64
  1258. // If aggregateBy is nil, we don't aggregate anything. On the other hand,
  1259. // an empty slice implies that we should aggregate everything. See
  1260. // generateKey for why that makes sense.
  1261. shouldAggregate := aggregateBy != nil
  1262. shouldFilter := !isFilterEmpty(filter)
  1263. shouldShare := len(options.SharedHourlyCosts) > 0 || sharer != nil
  1264. if !shouldAggregate && !shouldFilter && !shouldShare && options.ShareIdle == ShareNone && !options.IncludeProportionalAssetResourceCosts {
  1265. // There is nothing for AggregateBy to do, so simply return nil
  1266. // before returning, set aggregated metadata inclusion in properties
  1267. if options.IncludeAggregatedMetadata {
  1268. for index := range as.Allocations {
  1269. as.Allocations[index].Properties.AggregatedMetadata = true
  1270. }
  1271. }
  1272. return nil
  1273. }
  1274. // aggSet will collect the aggregated allocations
  1275. aggSet := &AllocationSet{
  1276. Window: as.Window.Clone(),
  1277. }
  1278. // externalSet will collect external allocations
  1279. externalSet := &AllocationSet{
  1280. Window: as.Window.Clone(),
  1281. }
  1282. // idleSet will be shared among aggSet after initial aggregation
  1283. // is complete
  1284. idleSet := &AllocationSet{
  1285. Window: as.Window.Clone(),
  1286. }
  1287. // parcSet is used to compute proportionalAssetResourceCosts
  1288. // for surfacing in the API
  1289. parcSet := &AllocationSet{
  1290. Window: as.Window.Clone(),
  1291. }
  1292. // shareSet will be shared among aggSet after initial aggregation
  1293. // is complete
  1294. shareSet := &AllocationSet{
  1295. Window: as.Window.Clone(),
  1296. }
  1297. // (1) Loop and find all of the external, idle, and shared allocations. Add
  1298. // them to their respective sets, removing them from the set of allocations
  1299. // to aggregate.
  1300. for _, alloc := range as.Allocations {
  1301. alloc.Properties.AggregatedMetadata = options.IncludeAggregatedMetadata
  1302. // build a parallel set of allocations to only be used
  1303. // for computing PARCs
  1304. if options.IncludeProportionalAssetResourceCosts {
  1305. parcSet.Insert(alloc.Clone())
  1306. }
  1307. // External allocations get aggregated post-hoc (see step 6) and do
  1308. // not necessarily contain complete sets of properties, so they are
  1309. // moved to a separate AllocationSet.
  1310. if alloc.IsExternal() {
  1311. delete(as.ExternalKeys, alloc.Name)
  1312. delete(as.Allocations, alloc.Name)
  1313. externalSet.Insert(alloc)
  1314. continue
  1315. }
  1316. // Idle allocations should be separated into idleSet if they are to be
  1317. // shared later on. If they are not to be shared, then add them to the
  1318. // aggSet like any other allocation.
  1319. if alloc.IsIdle() {
  1320. delete(as.IdleKeys, alloc.Name)
  1321. delete(as.Allocations, alloc.Name)
  1322. if options.ShareIdle == ShareEven || options.ShareIdle == ShareWeighted {
  1323. idleSet.Insert(alloc)
  1324. } else {
  1325. aggSet.Insert(alloc)
  1326. }
  1327. continue
  1328. }
  1329. // Shared allocations must be identified and separated prior to
  1330. // aggregation and filtering. That is, if any of the ShareFuncs return
  1331. // true for the allocation, then move it to shareSet.
  1332. if sharer != nil && sharer.Matches(alloc) {
  1333. delete(as.IdleKeys, alloc.Name)
  1334. delete(as.Allocations, alloc.Name)
  1335. shareSet.Insert(alloc)
  1336. continue
  1337. }
  1338. }
  1339. // It's possible that no more un-shared, non-idle, non-external allocations
  1340. // remain at this point. This always results in an emptySet, so return early.
  1341. if len(as.Allocations) == 0 {
  1342. emptySet := &AllocationSet{
  1343. Window: as.Window.Clone(),
  1344. }
  1345. as.Allocations = emptySet.Allocations
  1346. return nil
  1347. }
  1348. // (2) In order to correctly share idle and shared costs, we first compute
  1349. // sharing coefficients, which represent the proportion of each cost to
  1350. // share with each allocation. Idle allocations are shared per-cluster or per-node,
  1351. // per-allocation, and per-resource, while shared resources are shared per-
  1352. // allocation only.
  1353. //
  1354. // For an idleCoefficient example, the entries:
  1355. // [cluster1][cluster1/namespace1/pod1/container1][cpu] = 0.166667
  1356. // [cluster1][cluster1/namespace1/pod1/container1][gpu] = 0.166667
  1357. // [cluster1][cluster1/namespace1/pod1/container1][ram] = 0.687500
  1358. // mean that the allocation "cluster1/namespace1/pod1/container1" will
  1359. // receive 16.67% of cluster1's idle CPU and GPU costs and 68.75% of its
  1360. // RAM costs.
  1361. //
  1362. // For a shareCoefficient example, the entries:
  1363. // [namespace2] = 0.666667
  1364. // [__filtered__] = 0.333333
  1365. // mean that the post-aggregation allocation "namespace2" will receive
  1366. // 66.67% of the shared resource costs, while the remaining 33.33% will
  1367. // be filtered out, as they were shared with allocations that did not pass
  1368. // one of the given filters.
  1369. //
  1370. // In order to maintain stable results when multiple operations are being
  1371. // carried out (e.g. sharing idle, sharing resources, and filtering) these
  1372. // coefficients are computed for the full set of allocations prior to
  1373. // adding shared overhead and prior to applying filters.
  1374. var err error
  1375. // (2a) If there are idle costs to be shared, compute the coefficients for
  1376. // sharing them among the non-idle, non-aggregated allocations (including
  1377. // the shared allocations).
  1378. var idleCoefficients map[string]map[string]map[string]float64
  1379. if idleSet.Length() > 0 && options.ShareIdle != ShareNone {
  1380. idleCoefficients, allocatedTotalsMap, err = computeIdleCoeffs(options, as, shareSet)
  1381. if err != nil {
  1382. log.Warnf("AllocationSet.AggregateBy: compute idle coeff: %s", err)
  1383. return fmt.Errorf("error computing idle coefficients: %s", err)
  1384. }
  1385. }
  1386. // (2b) If proportional asset resource costs are to be included, compute them
  1387. // and add them to the allocations.
  1388. if options.IncludeProportionalAssetResourceCosts {
  1389. err := deriveProportionalAssetResourceCosts(options, as, shareSet, parcSet)
  1390. if err != nil {
  1391. log.Debugf("AggregateBy: failed to derive proportional asset resource costs from idle coefficients: %s", err)
  1392. return fmt.Errorf("AggregateBy: failed to derive proportional asset resource costs from idle coefficients: %s", err)
  1393. }
  1394. }
  1395. // (2c) If idle costs are not to be shared, but there are filters, then we
  1396. // need to track the amount of each idle allocation to "filter" in order to
  1397. // maintain parity with the results when idle is shared. That is, we want
  1398. // to return only the idle costs that would have been shared with the given
  1399. // results, even if the filter had not been applied.
  1400. //
  1401. // For example, consider these results from aggregating by namespace with
  1402. // two clusters:
  1403. //
  1404. // namespace1: 25.00
  1405. // namespace2: 30.00
  1406. // namespace3: 15.00
  1407. // idle: 30.00
  1408. //
  1409. // When we then filter by cluster==cluster1, namespaces 2 and 3 are
  1410. // reduced by the amount that existed on cluster2. Then, idle must also be
  1411. // reduced by the relevant amount:
  1412. //
  1413. // namespace1: 25.00
  1414. // namespace2: 15.00
  1415. // idle: 20.00
  1416. //
  1417. // Note that this can happen for any field, not just cluster, so we again
  1418. // need to track this on a per-cluster or per-node, per-allocation, per-resource basis.
  1419. var idleFiltrationCoefficients map[string]map[string]map[string]float64
  1420. if shouldFilter && options.ShareIdle == ShareNone {
  1421. idleFiltrationCoefficients, _, err = computeIdleCoeffs(options, as, shareSet)
  1422. if err != nil {
  1423. return fmt.Errorf("error computing idle filtration coefficients: %s", err)
  1424. }
  1425. }
  1426. // (2d) Convert SharedHourlyCosts to Allocations in the shareSet. This must
  1427. // come after idle coefficients are computed so that allocations generated
  1428. // by shared overhead do not skew the idle coefficient computation.
  1429. for name, cost := range options.SharedHourlyCosts {
  1430. if cost > 0.0 {
  1431. hours := as.Resolution().Hours()
  1432. // If set ends in the future, adjust hours accordingly
  1433. diff := time.Since(as.End())
  1434. if diff < 0.0 {
  1435. hours += diff.Hours()
  1436. }
  1437. totalSharedCost := cost * hours
  1438. shareSet.Insert(&Allocation{
  1439. Name: fmt.Sprintf("%s/%s", name, SharedSuffix),
  1440. Start: as.Start(),
  1441. End: as.End(),
  1442. SharedCost: totalSharedCost,
  1443. Properties: &AllocationProperties{Cluster: SharedSuffix}, // The allocation needs to belong to a cluster,but it really doesn't matter which one, so just make it clear.
  1444. })
  1445. }
  1446. }
  1447. // (2e) Compute share coefficients for shared resources. These are computed
  1448. // after idle coefficients, and are computed for the aggregated allocations
  1449. // of the main allocation set. See above for details and an example.
  1450. var shareCoefficients map[string]float64
  1451. if shareSet.Length() > 0 {
  1452. shareCoefficients, err = computeShareCoeffs(aggregateBy, options, as)
  1453. if err != nil {
  1454. return fmt.Errorf("error computing share coefficients: %s", err)
  1455. }
  1456. }
  1457. // (3-5) Filter, distribute idle cost, and aggregate (in that order)
  1458. for _, alloc := range as.Allocations {
  1459. idleId, err := alloc.getIdleId(options)
  1460. if err != nil {
  1461. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  1462. }
  1463. // (3) If the allocation does not match the filter, immediately skip the
  1464. // allocation.
  1465. if !filter.Matches(alloc) {
  1466. // If we are tracking idle filtration coefficients, delete the
  1467. // entry corresponding to the filtered allocation. (Deleting the
  1468. // entry will result in that proportional amount being removed
  1469. // from the idle allocation at the end of the process.)
  1470. if idleFiltrationCoefficients != nil {
  1471. if ifcc, ok := idleFiltrationCoefficients[idleId]; ok {
  1472. delete(ifcc, alloc.Name)
  1473. }
  1474. }
  1475. continue
  1476. }
  1477. // (4) Distribute idle allocations according to the idle coefficients
  1478. // NOTE: if idle allocation is off (i.e. ShareIdle == ShareNone) then
  1479. // all idle allocations will be in the aggSet at this point, so idleSet
  1480. // will be empty and we won't enter this block.
  1481. if idleSet.Length() > 0 {
  1482. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1483. for _, idleAlloc := range idleSet.Allocations {
  1484. // Only share idle if the idleId matches; i.e. the allocation
  1485. // is from the same idleId as the idle costs
  1486. iaidleId, err := idleAlloc.getIdleId(options)
  1487. if err != nil {
  1488. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1489. return err
  1490. }
  1491. if iaidleId != idleId {
  1492. continue
  1493. }
  1494. // Make sure idle coefficients exist
  1495. if _, ok := idleCoefficients[idleId]; !ok {
  1496. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1497. continue
  1498. }
  1499. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1500. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1501. continue
  1502. }
  1503. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1504. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1505. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1506. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1507. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1508. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1509. alloc.CPUCost += idleCPUCost
  1510. alloc.GPUCost += idleGPUCost
  1511. alloc.RAMCost += idleRAMCost
  1512. }
  1513. }
  1514. // (5) generate key to use for aggregation-by-key and allocation name
  1515. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1516. alloc.Name = key
  1517. if options.MergeUnallocated && alloc.IsUnallocated() {
  1518. alloc.Name = UnallocatedSuffix
  1519. }
  1520. // Inserting the allocation with the generated key for a name will
  1521. // perform the actual basic aggregation step.
  1522. aggSet.Insert(alloc)
  1523. }
  1524. // (6) If idle is shared and resources are shared, it's possible that some
  1525. // amount of idle cost will be shared with a shared resource. Distribute
  1526. // that idle allocation, if it exists, to the respective shared allocations
  1527. // before sharing with the aggregated allocations.
  1528. if idleSet.Length() > 0 && shareSet.Length() > 0 {
  1529. for _, alloc := range shareSet.Allocations {
  1530. idleId, err := alloc.getIdleId(options)
  1531. if err != nil {
  1532. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  1533. }
  1534. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1535. for _, idleAlloc := range idleSet.Allocations {
  1536. // Only share idle if the idleId matches; i.e. the allocation
  1537. // is from the same idleId as the idle costs
  1538. iaidleId, _ := idleAlloc.getIdleId(options)
  1539. if iaidleId != idleId {
  1540. continue
  1541. }
  1542. // Make sure idle coefficients exist
  1543. if _, ok := idleCoefficients[idleId]; !ok {
  1544. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1545. continue
  1546. }
  1547. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1548. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1549. continue
  1550. }
  1551. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1552. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1553. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1554. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1555. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1556. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1557. alloc.CPUCost += idleCPUCost
  1558. alloc.GPUCost += idleGPUCost
  1559. alloc.RAMCost += idleRAMCost
  1560. }
  1561. }
  1562. }
  1563. // groupingIdleFiltrationCoeffs is used to track per-resource idle
  1564. // coefficients on a cluster-by-cluster or node-by-node basis depending
  1565. // on the IdleByNode option. It is, essentially, an aggregation of
  1566. // idleFiltrationCoefficients after they have been
  1567. // filtered above (in step 3)
  1568. var groupingIdleFiltrationCoeffs map[string]map[string]float64
  1569. if idleFiltrationCoefficients != nil {
  1570. groupingIdleFiltrationCoeffs = map[string]map[string]float64{}
  1571. for idleId, m := range idleFiltrationCoefficients {
  1572. if _, ok := groupingIdleFiltrationCoeffs[idleId]; !ok {
  1573. groupingIdleFiltrationCoeffs[idleId] = map[string]float64{
  1574. "cpu": 0.0,
  1575. "gpu": 0.0,
  1576. "ram": 0.0,
  1577. }
  1578. }
  1579. for _, n := range m {
  1580. for resource, val := range n {
  1581. groupingIdleFiltrationCoeffs[idleId][resource] += val
  1582. }
  1583. }
  1584. }
  1585. }
  1586. // (7) If we have both un-shared idle allocations and idle filtration
  1587. // coefficients then apply those. See step (2b) for an example.
  1588. if len(aggSet.IdleKeys) > 0 && groupingIdleFiltrationCoeffs != nil {
  1589. for idleKey := range aggSet.IdleKeys {
  1590. idleAlloc := aggSet.Get(idleKey)
  1591. iaidleId, err := idleAlloc.getIdleId(options)
  1592. if err != nil {
  1593. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1594. return err
  1595. }
  1596. if resourceCoeffs, ok := groupingIdleFiltrationCoeffs[iaidleId]; ok {
  1597. idleAlloc.CPUCost *= resourceCoeffs["cpu"]
  1598. idleAlloc.CPUCoreHours *= resourceCoeffs["cpu"]
  1599. idleAlloc.RAMCost *= resourceCoeffs["ram"]
  1600. idleAlloc.RAMByteHours *= resourceCoeffs["ram"]
  1601. idleAlloc.GPUCost *= resourceCoeffs["gpu"]
  1602. idleAlloc.GPUHours *= resourceCoeffs["gpu"]
  1603. }
  1604. }
  1605. }
  1606. // (8) Distribute shared allocations according to the share coefficients.
  1607. if shareSet.Length() > 0 {
  1608. for _, alloc := range aggSet.Allocations {
  1609. for _, sharedAlloc := range shareSet.Allocations {
  1610. if _, ok := shareCoefficients[alloc.Name]; !ok {
  1611. if !alloc.IsIdle() && !alloc.IsUnmounted() {
  1612. log.Warnf("AllocationSet.AggregateBy: error getting share coefficient for '%s'", alloc.Name)
  1613. }
  1614. continue
  1615. }
  1616. if options.IncludeSharedCostBreakdown {
  1617. if alloc.SharedCostBreakdown == nil {
  1618. alloc.SharedCostBreakdown = map[string]SharedCostBreakdown{}
  1619. }
  1620. sharedCostName, err := sharedAlloc.determineSharingName(options)
  1621. if err != nil {
  1622. return fmt.Errorf("failed to group shared costs: %s", err)
  1623. }
  1624. // check if current allocation is a shared flat overhead cost
  1625. if strings.Contains(sharedAlloc.Name, SharedSuffix) {
  1626. sharedCostName = "overheadCost"
  1627. }
  1628. scb := SharedCostBreakdown{
  1629. Name: sharedCostName,
  1630. TotalCost: sharedAlloc.TotalCost() * shareCoefficients[alloc.Name],
  1631. CPUCost: sharedAlloc.CPUTotalCost() * shareCoefficients[alloc.Name],
  1632. GPUCost: sharedAlloc.GPUTotalCost() * shareCoefficients[alloc.Name],
  1633. RAMCost: sharedAlloc.RAMTotalCost() * shareCoefficients[alloc.Name],
  1634. PVCost: sharedAlloc.PVCost() * shareCoefficients[alloc.Name],
  1635. NetworkCost: sharedAlloc.NetworkTotalCost() * shareCoefficients[alloc.Name],
  1636. LBCost: sharedAlloc.LBTotalCost() * shareCoefficients[alloc.Name],
  1637. ExternalCost: sharedAlloc.ExternalCost * shareCoefficients[alloc.Name],
  1638. }
  1639. // fmt.Printf("shared cost: %+v", scb)
  1640. alloc.SharedCostBreakdown.Insert(scb)
  1641. }
  1642. alloc.SharedCost += sharedAlloc.TotalCost() * shareCoefficients[alloc.Name]
  1643. }
  1644. }
  1645. }
  1646. // (9) Aggregate external allocations into aggregated allocations. This may
  1647. // not be possible for every external allocation, but attempt to find an
  1648. // exact key match, given each external allocation's proerties, and
  1649. // aggregate if an exact match is found.
  1650. for _, alloc := range externalSet.Allocations {
  1651. if filter.Matches(alloc) {
  1652. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1653. alloc.Name = key
  1654. aggSet.Insert(alloc)
  1655. }
  1656. }
  1657. // (10) In the edge case that some idle has not been distributed because
  1658. // there is no usage of that resource type, add idle back to
  1659. // aggregations with only that cost applied.
  1660. // E.g. in the case where we have a result that looks like this on the
  1661. // frontend:
  1662. // Name CPU GPU RAM
  1663. // __idle__ $10 $12 $6
  1664. // kubecost $2 $0 $1
  1665. // Sharing idle weighted would result in no idle GPU cost being
  1666. // distributed, because the coefficient for the kubecost GPU cost would
  1667. // be zero. Thus, instead we re-add idle to the aggSet with distributed
  1668. // costs zeroed out but the undistributed costs left in.
  1669. // Name CPU GPU RAM
  1670. // __idle__ $0 $12 $0
  1671. // kubecost $12 $0 $7
  1672. if idleSet.Length() > 0 {
  1673. for _, idleAlloc := range idleSet.Allocations {
  1674. // if the idle does not apply to the non-filtered values, skip it
  1675. if !filter.Matches(idleAlloc) {
  1676. continue
  1677. }
  1678. idleId, err := idleAlloc.getIdleId(options)
  1679. if err != nil {
  1680. log.Errorf("AllocationSet.AggregateBy: idle allocation is missing idleId %s", idleAlloc.Name)
  1681. continue
  1682. }
  1683. hasUndistributableCost := false
  1684. if idleAlloc.CPUCost > 0 && allocatedTotalsMap[idleId]["cpu"] == 0 {
  1685. hasUndistributableCost = true
  1686. } else {
  1687. idleAlloc.CPUCost = 0
  1688. }
  1689. if idleAlloc.GPUCost > 0 && allocatedTotalsMap[idleId]["gpu"] == 0 {
  1690. hasUndistributableCost = true
  1691. } else {
  1692. idleAlloc.GPUCost = 0
  1693. }
  1694. if idleAlloc.RAMCost > 0 && allocatedTotalsMap[idleId]["ram"] == 0 {
  1695. hasUndistributableCost = true
  1696. } else {
  1697. idleAlloc.RAMCost = 0
  1698. }
  1699. if hasUndistributableCost {
  1700. idleAlloc.Name = fmt.Sprintf("%s/%s", idleId, IdleSuffix)
  1701. aggSet.Insert(idleAlloc)
  1702. }
  1703. }
  1704. }
  1705. // (11) Combine all idle allocations into a single "__idle__" allocation
  1706. if !options.SplitIdle {
  1707. for _, idleAlloc := range aggSet.IdleAllocations() {
  1708. aggSet.Delete(idleAlloc.Name)
  1709. idleAlloc.Name = IdleSuffix
  1710. aggSet.Insert(idleAlloc)
  1711. }
  1712. }
  1713. // TODO revisit this (ideally we just remove sharing from this function!)
  1714. // If filters and shared resources and shared idle are all enabled then
  1715. // we will over-count idle by exactly the portion that gets shared with the
  1716. // filtered allocations -- and idle filtration will miss this because it
  1717. // only filters the non-idle filtered costs.
  1718. //
  1719. // Consider the following example, from unit tests:
  1720. // - namespace1 28.000
  1721. // - namespace2 36.000
  1722. // - namespace3 18.000
  1723. // - cluster1/idle 20.000
  1724. // - cluster2/idle 10.000
  1725. //
  1726. // Now, we want to share namespace1, filter namespace2, and share idle:
  1727. //
  1728. // 1. Distribute idle
  1729. // ns1 ns2 ns3
  1730. // non-idle 28.000 36.000 18.000
  1731. // idle 14.688 10.312 5.000
  1732. //
  1733. // 2. Share namespace1
  1734. //
  1735. // ns2 ns3
  1736. // non-idle 36.000 18.000
  1737. // idle 10.312 5.000
  1738. // shared non-idle 18.667 9.333
  1739. // shared idle 9.792 4.896 (***)
  1740. //
  1741. // 3. Filter out all but namespace2
  1742. //
  1743. // ns2 = 36.000 + 10.312 + 18.667 + 9.792 = 74.771
  1744. //
  1745. // So, if we had NOT shared idle, we would expect something like this:
  1746. //
  1747. // ns2 = 36.000 + 18.667 = 54.667
  1748. // idle = 10.312 + 9.792 = 20.104
  1749. //
  1750. // But we will instead get this:
  1751. //
  1752. // ns2 = 36.000 + 18.667 = 54.667
  1753. // idle = 10.312 + 14.688 = 25.000
  1754. //
  1755. // Which over-shoots idle by 4.896 (***), i.e. precisely the amount of idle
  1756. // cost corresponding to namespace1 AND shared with namespace3. Phew.
  1757. //
  1758. // I originally wanted to fix this, but after 2 days, I'm punting with the
  1759. // recommendation that we rewrite this function soon. Too difficult.
  1760. // - Niko
  1761. as.Allocations = aggSet.Allocations
  1762. return nil
  1763. }
  1764. func computeShareCoeffs(aggregateBy []string, options *AllocationAggregationOptions, as *AllocationSet) (map[string]float64, error) {
  1765. // Compute coeffs by totalling per-allocation, then dividing by the total.
  1766. coeffs := map[string]float64{}
  1767. // Compute totals for all allocations
  1768. total := 0.0
  1769. // ShareEven counts each aggregation with even weight, whereas ShareWeighted
  1770. // counts each aggregation proportionally to its respective costs
  1771. shareType := options.ShareSplit
  1772. var filter AllocationMatcher
  1773. if options.Filter == nil {
  1774. filter = &matcher.AllPass[*Allocation]{}
  1775. } else {
  1776. compiler := NewAllocationMatchCompiler(options.LabelConfig)
  1777. var err error
  1778. filter, err = compiler.Compile(options.Filter)
  1779. if err != nil {
  1780. return nil, fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(options.Filter), err)
  1781. }
  1782. }
  1783. if filter == nil {
  1784. return nil, fmt.Errorf("unexpected nil filter")
  1785. }
  1786. // Record allocation values first, then normalize by totals to get percentages
  1787. for _, alloc := range as.Allocations {
  1788. if alloc.IsIdle() {
  1789. // Skip idle allocations in coefficient calculation
  1790. continue
  1791. }
  1792. if alloc.IsUnmounted() {
  1793. // Skip unmounted allocations in coefficient calculation
  1794. continue
  1795. }
  1796. // Determine the post-aggregation key under which the allocation will
  1797. // be shared.
  1798. name := alloc.generateKey(aggregateBy, options.LabelConfig)
  1799. // If the current allocation will be filtered out in step 3, contribute
  1800. // its share of the shared coefficient to a "__filtered__" bin, which
  1801. // will ultimately be dropped. This step ensures that the shared cost
  1802. // of a non-filtered allocation will be conserved even when the filter
  1803. // is removed. (Otherwise, all the shared cost will get redistributed
  1804. // over the unfiltered results, inflating their shared costs.)
  1805. if !filter.Matches(alloc) {
  1806. name = "__filtered__"
  1807. }
  1808. if shareType == ShareEven {
  1809. // Even distribution is not additive - set to 1.0 for everything
  1810. coeffs[name] = 1.0
  1811. // Total for even distribution is always the number of coefficients
  1812. total = float64(len(coeffs))
  1813. } else {
  1814. // Both are additive for weighted distribution, where each
  1815. // cumulative coefficient will be divided by the total.
  1816. coeffs[name] += alloc.TotalCost() - alloc.SharedCost - alloc.UnmountedPVCost
  1817. total += alloc.TotalCost() - alloc.SharedCost - alloc.UnmountedPVCost
  1818. }
  1819. }
  1820. // Normalize coefficients by totals
  1821. for a := range coeffs {
  1822. if coeffs[a] > 0 && total > 0 {
  1823. coeffs[a] /= total
  1824. } else {
  1825. log.Warnf("ETL: invalid values for shared coefficients: %v, %v", coeffs[a], total)
  1826. coeffs[a] = 0.0
  1827. }
  1828. }
  1829. return coeffs, nil
  1830. }
  1831. func computeIdleCoeffs(options *AllocationAggregationOptions, as *AllocationSet, shareSet *AllocationSet) (map[string]map[string]map[string]float64, map[string]map[string]float64, error) {
  1832. types := []string{"cpu", "gpu", "ram"}
  1833. // Compute idle coefficients, then save them in AllocationAggregationOptions
  1834. // [idle_id][allocation name][resource] = [coeff]
  1835. coeffs := map[string]map[string]map[string]float64{}
  1836. // Compute totals per resource for CPU, GPU, RAM, and PV
  1837. // [idle_id][resource] = [total]
  1838. totals := map[string]map[string]float64{}
  1839. // Record allocation values first, then normalize by totals to get percentages
  1840. for _, alloc := range as.Allocations {
  1841. if alloc.IsIdle() {
  1842. // Skip idle allocations in coefficient calculation
  1843. continue
  1844. }
  1845. idleId, err := alloc.getIdleId(options)
  1846. if err != nil {
  1847. log.DedupedWarningf(3, "Missing Idle Key for %s", alloc.Name)
  1848. }
  1849. // get the name key for the allocation
  1850. name := alloc.Name
  1851. // Create key based tables if they don't exist
  1852. if _, ok := coeffs[idleId]; !ok {
  1853. coeffs[idleId] = map[string]map[string]float64{}
  1854. }
  1855. if _, ok := totals[idleId]; !ok {
  1856. totals[idleId] = map[string]float64{}
  1857. }
  1858. if _, ok := coeffs[idleId][name]; !ok {
  1859. coeffs[idleId][name] = map[string]float64{}
  1860. }
  1861. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1862. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1863. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1864. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1865. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1866. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1867. }
  1868. // Do the same for shared allocations
  1869. for _, alloc := range shareSet.Allocations {
  1870. if alloc.IsIdle() {
  1871. // Skip idle allocations in coefficient calculation
  1872. continue
  1873. }
  1874. // idleId will be providerId or cluster
  1875. idleId, err := alloc.getIdleId(options)
  1876. if err != nil {
  1877. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1878. }
  1879. // get the name key for the allocation
  1880. name := alloc.Name
  1881. // Create idleId based tables if they don't exist
  1882. if _, ok := coeffs[idleId]; !ok {
  1883. coeffs[idleId] = map[string]map[string]float64{}
  1884. }
  1885. if _, ok := totals[idleId]; !ok {
  1886. totals[idleId] = map[string]float64{}
  1887. }
  1888. if _, ok := coeffs[idleId][name]; !ok {
  1889. coeffs[idleId][name] = map[string]float64{}
  1890. }
  1891. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1892. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1893. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1894. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1895. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1896. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1897. }
  1898. // Normalize coefficients by totals
  1899. for id := range coeffs {
  1900. for a := range coeffs[id] {
  1901. for _, r := range types {
  1902. if coeffs[id][a][r] > 0 && totals[id][r] > 0 {
  1903. coeffs[id][a][r] /= totals[id][r]
  1904. }
  1905. }
  1906. }
  1907. }
  1908. return coeffs, totals, nil
  1909. }
  1910. func deriveProportionalAssetResourceCosts(options *AllocationAggregationOptions, as *AllocationSet, shareSet, parcsSet *AllocationSet) error {
  1911. // Compute idle coefficients, then save them in AllocationAggregationOptions
  1912. // [idle_id][allocation name][resource] = [coeff]
  1913. coeffs := map[string]map[string]map[string]float64{}
  1914. // Compute totals per resource for CPU, GPU, RAM, and PV
  1915. // [idle_id][resource] = [total]
  1916. totals := map[string]map[string]float64{}
  1917. // Record allocation values first, then normalize by totals to get percentages
  1918. for _, alloc := range parcsSet.Allocations {
  1919. idleId, err := alloc.getIdleId(options)
  1920. if err != nil {
  1921. log.DedupedWarningf(3, "Missing Idle Key for %s", alloc.Name)
  1922. }
  1923. // get the name key for the allocation
  1924. name := alloc.Name
  1925. // Create key based tables if they don't exist
  1926. if _, ok := coeffs[idleId]; !ok {
  1927. coeffs[idleId] = map[string]map[string]float64{}
  1928. }
  1929. if _, ok := totals[idleId]; !ok {
  1930. totals[idleId] = map[string]float64{}
  1931. }
  1932. if _, ok := coeffs[idleId][name]; !ok {
  1933. coeffs[idleId][name] = map[string]float64{}
  1934. }
  1935. // idle IDs for load balancers are their services
  1936. for key := range alloc.LoadBalancers {
  1937. if _, ok := totals[key]; !ok {
  1938. totals[key] = map[string]float64{}
  1939. }
  1940. if _, ok := coeffs[key]; !ok {
  1941. coeffs[key] = map[string]map[string]float64{}
  1942. }
  1943. if _, ok := coeffs[key][name]; !ok {
  1944. coeffs[key][name] = map[string]float64{}
  1945. }
  1946. coeffs[key][name]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1947. totals[key]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1948. }
  1949. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1950. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1951. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1952. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1953. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1954. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1955. }
  1956. // Do the same for shared allocations
  1957. for _, alloc := range shareSet.Allocations {
  1958. if alloc.IsIdle() {
  1959. // Skip idle allocations in coefficient calculation
  1960. continue
  1961. }
  1962. // idleId will be providerId or cluster
  1963. idleId, err := alloc.getIdleId(options)
  1964. if err != nil {
  1965. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1966. }
  1967. // get the name key for the allocation
  1968. name := alloc.Name
  1969. // Create idleId based tables if they don't exist
  1970. if _, ok := coeffs[idleId]; !ok {
  1971. coeffs[idleId] = map[string]map[string]float64{}
  1972. }
  1973. if _, ok := totals[idleId]; !ok {
  1974. totals[idleId] = map[string]float64{}
  1975. }
  1976. if _, ok := coeffs[idleId][name]; !ok {
  1977. coeffs[idleId][name] = map[string]float64{}
  1978. }
  1979. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1980. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1981. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1982. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1983. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1984. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1985. // idle IDs for load balancers are their services
  1986. for key := range alloc.LoadBalancers {
  1987. if _, ok := totals[key]; !ok {
  1988. totals[key] = map[string]float64{}
  1989. }
  1990. if _, ok := coeffs[key]; !ok {
  1991. coeffs[key] = map[string]map[string]float64{}
  1992. }
  1993. if _, ok := coeffs[key][name]; !ok {
  1994. coeffs[key][name] = map[string]float64{}
  1995. }
  1996. coeffs[key][name]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1997. totals[key]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1998. }
  1999. }
  2000. // after totals are computed, loop through and set parcs on allocations
  2001. for _, alloc := range as.Allocations {
  2002. idleId, err := alloc.getIdleId(options)
  2003. if err != nil {
  2004. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  2005. }
  2006. alloc.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  2007. alloc.ProportionalAssetResourceCosts.Insert(ProportionalAssetResourceCost{
  2008. Cluster: alloc.Properties.Cluster,
  2009. Name: alloc.Properties.Node,
  2010. Type: "Node",
  2011. ProviderID: alloc.Properties.ProviderID,
  2012. GPUProportionalCost: coeffs[idleId][alloc.Name]["gpu"],
  2013. CPUProportionalCost: coeffs[idleId][alloc.Name]["cpu"],
  2014. RAMProportionalCost: coeffs[idleId][alloc.Name]["ram"],
  2015. }, options.IdleByNode)
  2016. // insert a separate PARC for the load balancer
  2017. if alloc.LoadBalancerCost != 0 {
  2018. for key, svc := range alloc.LoadBalancers {
  2019. alloc.ProportionalAssetResourceCosts.Insert(ProportionalAssetResourceCost{
  2020. Cluster: alloc.Properties.Cluster,
  2021. Name: svc.Service,
  2022. Type: "LoadBalancer",
  2023. LoadBalancerProportionalCost: coeffs[key][alloc.Name]["loadbalancer"],
  2024. }, options.IdleByNode)
  2025. }
  2026. }
  2027. for name, pvAlloc := range alloc.PVs {
  2028. // insert a separate PARC for each PV attached
  2029. alloc.ProportionalAssetResourceCosts.Insert(ProportionalAssetResourceCost{
  2030. Cluster: name.Cluster,
  2031. Name: name.Name,
  2032. Type: "PV",
  2033. ProviderID: pvAlloc.ProviderID,
  2034. PVProportionalCost: pvAlloc.Cost,
  2035. }, options.IdleByNode)
  2036. }
  2037. }
  2038. return nil
  2039. }
  2040. func (a *Allocation) determineSharingName(options *AllocationAggregationOptions) (string, error) {
  2041. if a == nil {
  2042. return "", fmt.Errorf("determineSharingName called on nil Allocation")
  2043. } else if options == nil {
  2044. return "unknown", nil
  2045. }
  2046. // grab SharedLabels keys and sort them, to keep this function deterministic
  2047. var labelKeys []string
  2048. for labelKey := range options.SharedLabels {
  2049. labelKeys = append(labelKeys, labelKey)
  2050. }
  2051. slices.Sort(labelKeys)
  2052. var sharedAggregateBy []string
  2053. var sharedLabels [][]string
  2054. for _, labelKey := range labelKeys {
  2055. sharedAgg := fmt.Sprintf("label:%s", labelKey)
  2056. if !slices.Contains(sharedAggregateBy, sharedAgg) {
  2057. sharedAggregateBy = append(sharedAggregateBy, sharedAgg)
  2058. }
  2059. sharedLabels = append(sharedLabels, options.SharedLabels[labelKey])
  2060. }
  2061. if len(options.SharedNamespaces) > 0 {
  2062. sharedAggregateBy = append(sharedAggregateBy, "namespace")
  2063. }
  2064. sharedCostName := a.generateKey(sharedAggregateBy, options.LabelConfig)
  2065. // get each value in the generated key, then reset the name
  2066. sharedCostNameValues := strings.Split(sharedCostName, "/")
  2067. sharedCostName = ""
  2068. // if we don't have as many values as aggregateBys, something went wrong in generateKey
  2069. if len(sharedCostNameValues) != len(sharedAggregateBy) {
  2070. log.Warnf("Unable to determine share cost group for allocation \"%s\"", a.Name)
  2071. } else {
  2072. // try to match to the first label
  2073. for i, sharedLabelValues := range sharedLabels {
  2074. allocLabel := sharedCostNameValues[i]
  2075. if slices.Contains(sharedLabelValues, allocLabel) {
  2076. return allocLabel, nil
  2077. }
  2078. }
  2079. // if we didn't match to a label, try to match to a namespace
  2080. if len(options.SharedNamespaces) > 0 {
  2081. // namespace will always be the last value, if SharedNamespaces is set
  2082. allocNamespace := sharedCostNameValues[len(sharedCostNameValues)-1]
  2083. if slices.Contains(options.SharedNamespaces, allocNamespace) {
  2084. return allocNamespace, nil
  2085. }
  2086. }
  2087. // if neither the labels nor the namespaces matched, we log a warning and mark this allocation
  2088. // as unknown
  2089. if len(sharedCostName) == 0 {
  2090. log.Warnf("Failed to determine shared cost grouping for allocation \"%s\"", a.Name)
  2091. }
  2092. }
  2093. return "unknown", nil
  2094. }
  2095. // getIdleId returns the providerId or cluster of an Allocation depending on the IdleByNode
  2096. // option in the AllocationAggregationOptions and an error if the respective field is missing
  2097. func (a *Allocation) getIdleId(options *AllocationAggregationOptions) (string, error) {
  2098. var idleId string
  2099. if options.IdleByNode {
  2100. // Key allocations to ProviderId to match against node
  2101. idleId = fmt.Sprintf("%s/%s", a.Properties.Cluster, a.Properties.Node)
  2102. if idleId == "" {
  2103. return idleId, fmt.Errorf("ProviderId is not set")
  2104. }
  2105. } else {
  2106. // key the allocations by cluster id
  2107. idleId = a.Properties.Cluster
  2108. if idleId == "" {
  2109. return idleId, fmt.Errorf("ClusterProp is not set")
  2110. }
  2111. }
  2112. return idleId, nil
  2113. }
  2114. func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig) string {
  2115. if a == nil {
  2116. return ""
  2117. }
  2118. return a.Properties.GenerateKey(aggregateBy, labelConfig)
  2119. }
  2120. func (a *Allocation) StringProperty(property string) (string, error) {
  2121. switch property {
  2122. case AllocationClusterProp:
  2123. if a.Properties == nil {
  2124. return "", nil
  2125. }
  2126. return a.Properties.Cluster, nil
  2127. case AllocationNodeProp:
  2128. if a.Properties == nil {
  2129. return "", nil
  2130. }
  2131. return a.Properties.Node, nil
  2132. case AllocationContainerProp:
  2133. if a.Properties == nil {
  2134. return "", nil
  2135. }
  2136. return a.Properties.Container, nil
  2137. case AllocationControllerProp:
  2138. if a.Properties == nil {
  2139. return "", nil
  2140. }
  2141. return a.Properties.Controller, nil
  2142. case AllocationControllerKindProp:
  2143. if a.Properties == nil {
  2144. return "", nil
  2145. }
  2146. return a.Properties.ControllerKind, nil
  2147. case AllocationNamespaceProp:
  2148. if a.Properties == nil {
  2149. return "", nil
  2150. }
  2151. return a.Properties.Namespace, nil
  2152. case AllocationPodProp:
  2153. if a.Properties == nil {
  2154. return "", nil
  2155. }
  2156. return a.Properties.Pod, nil
  2157. case AllocationProviderIDProp:
  2158. if a.Properties == nil {
  2159. return "", nil
  2160. }
  2161. return a.Properties.ProviderID, nil
  2162. default:
  2163. return "", fmt.Errorf("Allocation: StringProperty: invalid property name: %s", property)
  2164. }
  2165. }
  2166. func (a *Allocation) StringSliceProperty(property string) ([]string, error) {
  2167. switch property {
  2168. case AllocationServiceProp:
  2169. if a.Properties == nil {
  2170. return nil, nil
  2171. }
  2172. return a.Properties.Services, nil
  2173. default:
  2174. return nil, fmt.Errorf("Allocation: StringSliceProperty: invalid property name: %s", property)
  2175. }
  2176. }
  2177. func (a *Allocation) StringMapProperty(property string) (map[string]string, error) {
  2178. switch property {
  2179. case AllocationLabelProp:
  2180. if a.Properties == nil {
  2181. return nil, nil
  2182. }
  2183. return a.Properties.Labels, nil
  2184. case AllocationAnnotationProp:
  2185. if a.Properties == nil {
  2186. return nil, nil
  2187. }
  2188. return a.Properties.Annotations, nil
  2189. default:
  2190. return nil, fmt.Errorf("Allocation: StringMapProperty: invalid property name: %s", property)
  2191. }
  2192. }
  2193. func (a *Allocation) SanitizeNaN() {
  2194. if a == nil {
  2195. return
  2196. }
  2197. if math.IsNaN(a.CPUCost) {
  2198. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for CPUCost: name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2199. a.CPUCost = 0
  2200. }
  2201. if math.IsNaN(a.CPUCoreRequestAverage) {
  2202. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for CPUCoreRequestAverage: name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2203. a.CPUCoreRequestAverage = 0
  2204. }
  2205. if math.IsNaN(a.CPUCoreHours) {
  2206. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for CPUCoreHours: name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2207. a.CPUCoreHours = 0
  2208. }
  2209. if math.IsNaN(a.CPUCoreUsageAverage) {
  2210. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for CPUCoreUsageAverage name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2211. a.CPUCoreUsageAverage = 0
  2212. }
  2213. if math.IsNaN(a.CPUCostAdjustment) {
  2214. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for CPUCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2215. a.CPUCostAdjustment = 0
  2216. }
  2217. if math.IsNaN(a.GPUHours) {
  2218. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for GPUHours name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2219. a.GPUHours = 0
  2220. }
  2221. if math.IsNaN(a.GPUCost) {
  2222. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for GPUCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2223. a.GPUCost = 0
  2224. }
  2225. if math.IsNaN(a.GPUCostAdjustment) {
  2226. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for GPUCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2227. a.GPUCostAdjustment = 0
  2228. }
  2229. if math.IsNaN(a.NetworkTransferBytes) {
  2230. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkTransferBytes name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2231. a.NetworkTransferBytes = 0
  2232. }
  2233. if math.IsNaN(a.NetworkReceiveBytes) {
  2234. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkReceiveBytes name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2235. a.NetworkReceiveBytes = 0
  2236. }
  2237. if math.IsNaN(a.NetworkCost) {
  2238. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2239. a.NetworkCost = 0
  2240. }
  2241. if math.IsNaN(a.NetworkCrossZoneCost) {
  2242. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkCrossZoneCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2243. a.NetworkCrossZoneCost = 0
  2244. }
  2245. if math.IsNaN(a.NetworkCrossRegionCost) {
  2246. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkCrossRegionCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2247. a.NetworkCrossRegionCost = 0
  2248. }
  2249. if math.IsNaN(a.NetworkInternetCost) {
  2250. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkInternetCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2251. a.NetworkInternetCost = 0
  2252. }
  2253. if math.IsNaN(a.NetworkCostAdjustment) {
  2254. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for NetworkCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2255. a.NetworkCostAdjustment = 0
  2256. }
  2257. if math.IsNaN(a.LoadBalancerCost) {
  2258. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for LoadBalancerCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2259. a.LoadBalancerCost = 0
  2260. }
  2261. if math.IsNaN(a.LoadBalancerCostAdjustment) {
  2262. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for LoadBalancerCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2263. a.LoadBalancerCostAdjustment = 0
  2264. }
  2265. if math.IsNaN(a.PVCostAdjustment) {
  2266. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for PVCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2267. a.PVCostAdjustment = 0
  2268. }
  2269. if math.IsNaN(a.RAMByteHours) {
  2270. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for RAMByteHours name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2271. a.RAMByteHours = 0
  2272. }
  2273. if math.IsNaN(a.RAMBytesRequestAverage) {
  2274. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for RAMBytesRequestAverage name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2275. a.RAMBytesRequestAverage = 0
  2276. }
  2277. if math.IsNaN(a.RAMBytesUsageAverage) {
  2278. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for RAMBytesUsageAverage name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2279. a.RAMBytesUsageAverage = 0
  2280. }
  2281. if math.IsNaN(a.RAMCost) {
  2282. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for RAMCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2283. a.RAMCost = 0
  2284. }
  2285. if math.IsNaN(a.RAMCostAdjustment) {
  2286. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for RAMCostAdjustment name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2287. a.RAMCostAdjustment = 0
  2288. }
  2289. if math.IsNaN(a.SharedCost) {
  2290. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for SharedCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2291. a.SharedCost = 0
  2292. }
  2293. if math.IsNaN(a.ExternalCost) {
  2294. log.DedupedWarningf(5, "Allocation: Unexpected NaN found for ExternalCost name:%s, window:%s, properties:%s", a.Name, a.Window.String(), a.Properties.String())
  2295. a.ExternalCost = 0
  2296. }
  2297. a.PVs.SanitizeNaN()
  2298. a.RawAllocationOnly.SanitizeNaN()
  2299. a.ProportionalAssetResourceCosts.SanitizeNaN()
  2300. a.SharedCostBreakdown.SanitizeNaN()
  2301. a.LoadBalancers.SanitizeNaN()
  2302. }
  2303. // Clone returns a new AllocationSet with a deep copy of the given
  2304. // AllocationSet's allocations.
  2305. func (as *AllocationSet) Clone() *AllocationSet {
  2306. if as == nil {
  2307. return nil
  2308. }
  2309. allocs := make(map[string]*Allocation, len(as.Allocations))
  2310. for k, v := range as.Allocations {
  2311. allocs[k] = v.Clone()
  2312. }
  2313. externalKeys := make(map[string]bool, len(as.ExternalKeys))
  2314. for k, v := range as.ExternalKeys {
  2315. externalKeys[k] = v
  2316. }
  2317. idleKeys := make(map[string]bool, len(as.IdleKeys))
  2318. for k, v := range as.IdleKeys {
  2319. idleKeys[k] = v
  2320. }
  2321. var errors []string
  2322. var warnings []string
  2323. if as.Errors != nil {
  2324. errors = make([]string, len(as.Errors))
  2325. copy(errors, as.Errors)
  2326. } else {
  2327. errors = nil
  2328. }
  2329. if as.Warnings != nil {
  2330. warnings := make([]string, len(as.Warnings))
  2331. copy(warnings, as.Warnings)
  2332. } else {
  2333. warnings = nil
  2334. }
  2335. return &AllocationSet{
  2336. Allocations: allocs,
  2337. ExternalKeys: externalKeys,
  2338. IdleKeys: idleKeys,
  2339. Window: as.Window.Clone(),
  2340. Errors: errors,
  2341. Warnings: warnings,
  2342. }
  2343. }
  2344. // Delete removes the allocation with the given name from the set
  2345. func (as *AllocationSet) Delete(name string) {
  2346. if as == nil {
  2347. return
  2348. }
  2349. delete(as.ExternalKeys, name)
  2350. delete(as.IdleKeys, name)
  2351. delete(as.Allocations, name)
  2352. }
  2353. // End returns the End time of the AllocationSet window
  2354. func (as *AllocationSet) End() time.Time {
  2355. if as == nil {
  2356. log.Warnf("AllocationSet: calling End on nil AllocationSet")
  2357. return time.Unix(0, 0)
  2358. }
  2359. if as.Window.End() == nil {
  2360. log.Warnf("AllocationSet: AllocationSet with illegal window: End is nil; len(as.allocations)=%d", len(as.Allocations))
  2361. return time.Unix(0, 0)
  2362. }
  2363. return *as.Window.End()
  2364. }
  2365. // Get returns the Allocation at the given key in the AllocationSet
  2366. func (as *AllocationSet) Get(key string) *Allocation {
  2367. if alloc, ok := as.Allocations[key]; ok {
  2368. return alloc
  2369. }
  2370. return nil
  2371. }
  2372. // ExternalAllocations returns a map of the external allocations in the set.
  2373. // Returns clones of the actual Allocations, so mutability is not a problem.
  2374. func (as *AllocationSet) ExternalAllocations() map[string]*Allocation {
  2375. externals := map[string]*Allocation{}
  2376. if as.IsEmpty() {
  2377. return externals
  2378. }
  2379. for key := range as.ExternalKeys {
  2380. if alloc, ok := as.Allocations[key]; ok {
  2381. externals[key] = alloc.Clone()
  2382. }
  2383. }
  2384. return externals
  2385. }
  2386. // ExternalCost returns the total aggregated external costs of the set
  2387. func (as *AllocationSet) ExternalCost() float64 {
  2388. if as.IsEmpty() {
  2389. return 0.0
  2390. }
  2391. externalCost := 0.0
  2392. for _, alloc := range as.Allocations {
  2393. externalCost += alloc.ExternalCost
  2394. }
  2395. return externalCost
  2396. }
  2397. // IdleAllocations returns a map of the idle allocations in the AllocationSet.
  2398. // Returns clones of the actual Allocations, so mutability is not a problem.
  2399. func (as *AllocationSet) IdleAllocations() map[string]*Allocation {
  2400. idles := map[string]*Allocation{}
  2401. if as.IsEmpty() {
  2402. return idles
  2403. }
  2404. for key := range as.IdleKeys {
  2405. if alloc, ok := as.Allocations[key]; ok {
  2406. idles[key] = alloc.Clone()
  2407. }
  2408. }
  2409. return idles
  2410. }
  2411. // Insert aggregates the current entry in the AllocationSet by the given Allocation,
  2412. // but only if the Allocation is valid, i.e. matches the AllocationSet's window. If
  2413. // there is no existing entry, one is created. Nil error response indicates success.
  2414. func (as *AllocationSet) Insert(that *Allocation) error {
  2415. if as == nil {
  2416. return fmt.Errorf("cannot insert into nil AllocationSet")
  2417. }
  2418. if as.Allocations == nil {
  2419. as.Allocations = map[string]*Allocation{}
  2420. }
  2421. if as.ExternalKeys == nil {
  2422. as.ExternalKeys = map[string]bool{}
  2423. }
  2424. if as.IdleKeys == nil {
  2425. as.IdleKeys = map[string]bool{}
  2426. }
  2427. // Add the given Allocation to the existing entry, if there is one;
  2428. // otherwise just set directly into allocations
  2429. if _, ok := as.Allocations[that.Name]; !ok {
  2430. as.Allocations[that.Name] = that
  2431. } else {
  2432. as.Allocations[that.Name].add(that)
  2433. }
  2434. // If the given Allocation is an external one, record that
  2435. if that.IsExternal() {
  2436. as.ExternalKeys[that.Name] = true
  2437. }
  2438. // If the given Allocation is an idle one, record that
  2439. if that.IsIdle() {
  2440. as.IdleKeys[that.Name] = true
  2441. }
  2442. // Expand the window, just to be safe. It's possible that the Allocation will
  2443. // be set into the map without expanding it to the AllocationSet's window.
  2444. as.Allocations[that.Name].Window = as.Allocations[that.Name].Window.Expand(as.Window)
  2445. return nil
  2446. }
  2447. // IsEmpty returns true if the AllocationSet is nil, or if it contains
  2448. // zero allocations.
  2449. func (as *AllocationSet) IsEmpty() bool {
  2450. if as == nil || len(as.Allocations) == 0 {
  2451. return true
  2452. }
  2453. return false
  2454. }
  2455. // Length returns the number of Allocations in the set
  2456. func (as *AllocationSet) Length() int {
  2457. if as == nil {
  2458. return 0
  2459. }
  2460. return len(as.Allocations)
  2461. }
  2462. // ResetAdjustments sets all cost adjustment fields to zero
  2463. func (as *AllocationSet) ResetAdjustments() {
  2464. if as == nil {
  2465. return
  2466. }
  2467. for _, a := range as.Allocations {
  2468. a.ResetAdjustments()
  2469. }
  2470. }
  2471. // Resolution returns the AllocationSet's window duration
  2472. func (as *AllocationSet) Resolution() time.Duration {
  2473. return as.Window.Duration()
  2474. }
  2475. // GetWindow returns the AllocationSet's window
  2476. func (as *AllocationSet) GetWindow() Window {
  2477. return as.Window
  2478. }
  2479. // Set uses the given Allocation to overwrite the existing entry in the
  2480. // AllocationSet under the Allocation's name.
  2481. func (as *AllocationSet) Set(alloc *Allocation) error {
  2482. if as.IsEmpty() {
  2483. as.Allocations = map[string]*Allocation{}
  2484. as.ExternalKeys = map[string]bool{}
  2485. as.IdleKeys = map[string]bool{}
  2486. }
  2487. as.Allocations[alloc.Name] = alloc
  2488. // If the given Allocation is an external one, record that
  2489. if alloc.IsExternal() {
  2490. as.ExternalKeys[alloc.Name] = true
  2491. }
  2492. // If the given Allocation is an idle one, record that
  2493. if alloc.IsIdle() {
  2494. as.IdleKeys[alloc.Name] = true
  2495. }
  2496. return nil
  2497. }
  2498. // GetUnmountedPVCost returns the sum of all UnmountedPVCost fields across all
  2499. // allocations in the set.
  2500. func (as *AllocationSet) GetUnmountedPVCost() float64 {
  2501. upvc := 0.0
  2502. for _, a := range as.Allocations {
  2503. upvc += a.UnmountedPVCost
  2504. }
  2505. return upvc
  2506. }
  2507. // SetUnmountedPVCost sets the UnmountedPVCost field for all allocations in the
  2508. // set.
  2509. func (as *AllocationSet) SetUnmountedPVCost() float64 {
  2510. upvc := 0.0
  2511. for _, a := range as.Allocations {
  2512. upvc += a.SetUnmountedPVCost()
  2513. }
  2514. return upvc
  2515. }
  2516. // Start returns the Start time of the AllocationSet window
  2517. func (as *AllocationSet) Start() time.Time {
  2518. if as == nil {
  2519. log.Warnf("AllocationSet: calling Start on nil AllocationSet")
  2520. return time.Unix(0, 0)
  2521. }
  2522. if as.Window.Start() == nil {
  2523. log.Warnf("AllocationSet: AllocationSet with illegal window: Start is nil; len(as.allocations)=%d", len(as.Allocations))
  2524. return time.Unix(0, 0)
  2525. }
  2526. return *as.Window.Start()
  2527. }
  2528. // String represents the given Allocation as a string
  2529. func (as *AllocationSet) String() string {
  2530. if as == nil {
  2531. return "<nil>"
  2532. }
  2533. return fmt.Sprintf(
  2534. "AllocationSet{length: %d; window: %s; totalCost: %.2f}",
  2535. as.Length(),
  2536. as.Window,
  2537. as.TotalCost())
  2538. }
  2539. // TotalCost returns the sum of all TotalCosts of the allocations contained
  2540. func (as *AllocationSet) TotalCost() float64 {
  2541. if as.IsEmpty() {
  2542. return 0.0
  2543. }
  2544. tc := 0.0
  2545. for _, a := range as.Allocations {
  2546. tc += a.TotalCost()
  2547. }
  2548. return tc
  2549. }
  2550. // UTCOffset returns the AllocationSet's configured UTCOffset.
  2551. func (as *AllocationSet) UTCOffset() time.Duration {
  2552. _, zone := as.Start().Zone()
  2553. return time.Duration(zone) * time.Second
  2554. }
  2555. func (as *AllocationSet) Accumulate(that *AllocationSet) (*AllocationSet, error) {
  2556. if as.IsEmpty() {
  2557. return that.Clone(), nil
  2558. }
  2559. if that.IsEmpty() {
  2560. return as.Clone(), nil
  2561. }
  2562. // Set start, end to min(start), max(end)
  2563. start := as.Start()
  2564. end := as.End()
  2565. if that.Start().Before(start) {
  2566. start = that.Start()
  2567. }
  2568. if that.End().After(end) {
  2569. end = that.End()
  2570. }
  2571. acc := NewAllocationSet(start, end)
  2572. for _, alloc := range as.Allocations {
  2573. err := acc.Insert(alloc)
  2574. if err != nil {
  2575. return nil, err
  2576. }
  2577. }
  2578. for _, alloc := range that.Allocations {
  2579. err := acc.Insert(alloc)
  2580. if err != nil {
  2581. return nil, err
  2582. }
  2583. }
  2584. return acc, nil
  2585. }
  2586. func (as *AllocationSet) SanitizeNaN() {
  2587. if as == nil {
  2588. return
  2589. }
  2590. for _, a := range as.Allocations {
  2591. a.SanitizeNaN()
  2592. }
  2593. }
  2594. // AllocationSetRange is a thread-safe slice of AllocationSets. It is meant to
  2595. // be used such that the AllocationSets held are consecutive and coherent with
  2596. // respect to using the same aggregation properties, UTC offset, and
  2597. // resolution. However these rules are not necessarily enforced, so use wisely.
  2598. type AllocationSetRange struct {
  2599. Allocations []*AllocationSet
  2600. FromStore string // stores the name of the store used to retrieve the data
  2601. }
  2602. // NewAllocationSetRange instantiates a new range composed of the given
  2603. // AllocationSets in the order provided.
  2604. func NewAllocationSetRange(allocs ...*AllocationSet) *AllocationSetRange {
  2605. return &AllocationSetRange{
  2606. Allocations: allocs,
  2607. }
  2608. }
  2609. // Get safely retrieves the AllocationSet at the given index of the range.
  2610. func (asr *AllocationSetRange) Get(i int) (*AllocationSet, error) {
  2611. if i < 0 || i >= len(asr.Allocations) {
  2612. return nil, fmt.Errorf("AllocationSetRange: index out of range: %d", i)
  2613. }
  2614. return asr.Allocations[i], nil
  2615. }
  2616. // Accumulate sums each AllocationSet in the given range, returning a single cumulative
  2617. // AllocationSet for the entire range.
  2618. func (asr *AllocationSetRange) accumulate() (*AllocationSet, error) {
  2619. var allocSet *AllocationSet
  2620. var err error
  2621. for _, as := range asr.Allocations {
  2622. allocSet, err = allocSet.Accumulate(as)
  2623. if err != nil {
  2624. return nil, err
  2625. }
  2626. }
  2627. return allocSet, nil
  2628. }
  2629. // newAccumulation clones the first available AllocationSet to use as the data structure to
  2630. // Accumulate the remaining data. This leaves the original AllocationSetRange intact.
  2631. func (asr *AllocationSetRange) newAccumulation() (*AllocationSet, error) {
  2632. // NOTE: Adding this API for consistency across SummaryAllocation and Assets, but this
  2633. // NOTE: implementation is almost identical to regular Accumulate(). The Accumulate() method
  2634. // NOTE: for Allocation returns Clone() of the input, which is required for Accumulate
  2635. // NOTE: support (unit tests are great for verifying this information).
  2636. var allocSet *AllocationSet
  2637. var err error
  2638. if asr == nil {
  2639. return nil, fmt.Errorf("nil AllocationSetRange in accumulation")
  2640. }
  2641. if len(asr.Allocations) == 0 {
  2642. return nil, fmt.Errorf("AllocationSetRange has empty AssetSet in accumulation")
  2643. }
  2644. for _, as := range asr.Allocations {
  2645. if allocSet == nil {
  2646. allocSet = as.Clone()
  2647. continue
  2648. }
  2649. var allocSetCopy *AllocationSet = nil
  2650. if as != nil {
  2651. allocSetCopy = as.Clone()
  2652. }
  2653. allocSet, err = allocSet.Accumulate(allocSetCopy)
  2654. if err != nil {
  2655. return nil, err
  2656. }
  2657. }
  2658. return allocSet, nil
  2659. }
  2660. // Accumulate sums AllocationSets based on the AccumulateOption (calendar week or calendar month).
  2661. // The accumulated set is determined by the start of the window of the allocation set.
  2662. func (asr *AllocationSetRange) Accumulate(accumulateBy AccumulateOption) (*AllocationSetRange, error) {
  2663. switch accumulateBy {
  2664. case AccumulateOptionNone:
  2665. return asr.accumulateByNone()
  2666. case AccumulateOptionAll:
  2667. return asr.accumulateByAll()
  2668. case AccumulateOptionHour:
  2669. return asr.accumulateByHour()
  2670. case AccumulateOptionDay:
  2671. return asr.accumulateByDay()
  2672. case AccumulateOptionWeek:
  2673. return asr.accumulateByWeek()
  2674. case AccumulateOptionMonth:
  2675. return asr.accumulateByMonth()
  2676. default:
  2677. // ideally, this should never happen
  2678. return nil, fmt.Errorf("unexpected error, invalid accumulateByType: %s", accumulateBy)
  2679. }
  2680. }
  2681. func (asr *AllocationSetRange) accumulateByAll() (*AllocationSetRange, error) {
  2682. var err error
  2683. var as *AllocationSet
  2684. as, err = asr.newAccumulation()
  2685. if err != nil {
  2686. return nil, fmt.Errorf("error accumulating all:%s", err)
  2687. }
  2688. accumulated := NewAllocationSetRange(as)
  2689. return accumulated, nil
  2690. }
  2691. func (asr *AllocationSetRange) accumulateByNone() (*AllocationSetRange, error) {
  2692. return asr.Clone(), nil
  2693. }
  2694. func (asr *AllocationSetRange) accumulateByHour() (*AllocationSetRange, error) {
  2695. // ensure that the summary allocation sets have a 1-hour window, if a set exists
  2696. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() != time.Hour {
  2697. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", asr.Allocations[0].Window.Duration())
  2698. }
  2699. return asr.Clone(), nil
  2700. }
  2701. func (asr *AllocationSetRange) accumulateByDay() (*AllocationSetRange, error) {
  2702. // if the allocation set window is 1-day, just return the existing allocation set range
  2703. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() == time.Hour*24 {
  2704. return asr, nil
  2705. }
  2706. var toAccumulate *AllocationSetRange
  2707. result := NewAllocationSetRange()
  2708. for i, as := range asr.Allocations {
  2709. if as.Window.Duration() != time.Hour {
  2710. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", as.Window.Duration())
  2711. }
  2712. hour := as.Window.Start().Hour()
  2713. if toAccumulate == nil {
  2714. toAccumulate = NewAllocationSetRange()
  2715. as = as.Clone()
  2716. }
  2717. toAccumulate.Append(as)
  2718. asAccumulated, err := toAccumulate.accumulate()
  2719. if err != nil {
  2720. return nil, fmt.Errorf("error accumulating result: %s", err)
  2721. }
  2722. toAccumulate = NewAllocationSetRange(asAccumulated)
  2723. if hour == 23 || i == len(asr.Allocations)-1 {
  2724. if length := len(toAccumulate.Allocations); length != 1 {
  2725. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2726. }
  2727. result.Append(toAccumulate.Allocations[0])
  2728. toAccumulate = nil
  2729. }
  2730. }
  2731. return result, nil
  2732. }
  2733. func (asr *AllocationSetRange) accumulateByMonth() (*AllocationSetRange, error) {
  2734. var toAccumulate *AllocationSetRange
  2735. result := NewAllocationSetRange()
  2736. for i, as := range asr.Allocations {
  2737. if as.Window.Duration() != time.Hour*24 {
  2738. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", as.Window.Duration())
  2739. }
  2740. _, month, _ := as.Window.Start().Date()
  2741. _, nextDayMonth, _ := as.Window.Start().Add(time.Hour * 24).Date()
  2742. if toAccumulate == nil {
  2743. toAccumulate = NewAllocationSetRange()
  2744. as = as.Clone()
  2745. }
  2746. toAccumulate.Append(as)
  2747. asAccumulated, err := toAccumulate.accumulate()
  2748. if err != nil {
  2749. return nil, fmt.Errorf("error accumulating result: %s", err)
  2750. }
  2751. toAccumulate = NewAllocationSetRange(asAccumulated)
  2752. // either the month has ended, or there are no more allocation sets
  2753. if month != nextDayMonth || i == len(asr.Allocations)-1 {
  2754. if length := len(toAccumulate.Allocations); length != 1 {
  2755. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2756. }
  2757. result.Append(toAccumulate.Allocations[0])
  2758. toAccumulate = nil
  2759. }
  2760. }
  2761. return result, nil
  2762. }
  2763. func (asr *AllocationSetRange) accumulateByWeek() (*AllocationSetRange, error) {
  2764. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() == timeutil.Week {
  2765. return asr, nil
  2766. }
  2767. var toAccumulate *AllocationSetRange
  2768. result := NewAllocationSetRange()
  2769. for i, as := range asr.Allocations {
  2770. if as.Window.Duration() != time.Hour*24 {
  2771. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", as.Window.Duration())
  2772. }
  2773. dayOfWeek := as.Window.Start().Weekday()
  2774. if toAccumulate == nil {
  2775. toAccumulate = NewAllocationSetRange()
  2776. as = as.Clone()
  2777. }
  2778. toAccumulate.Append(as)
  2779. asAccumulated, err := toAccumulate.accumulate()
  2780. if err != nil {
  2781. return nil, fmt.Errorf("error accumulating result: %s", err)
  2782. }
  2783. toAccumulate = NewAllocationSetRange(asAccumulated)
  2784. // current assumption is the week always ends on Saturday, or there are no more allocation sets
  2785. if dayOfWeek == time.Saturday || i == len(asr.Allocations)-1 {
  2786. if length := len(toAccumulate.Allocations); length != 1 {
  2787. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2788. }
  2789. result.Append(toAccumulate.Allocations[0])
  2790. toAccumulate = nil
  2791. }
  2792. }
  2793. return result, nil
  2794. }
  2795. // AggregateBy aggregates each AllocationSet in the range by the given
  2796. // properties and options.
  2797. func (asr *AllocationSetRange) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  2798. aggRange := &AllocationSetRange{Allocations: []*AllocationSet{}}
  2799. for _, as := range asr.Allocations {
  2800. err := as.AggregateBy(aggregateBy, options)
  2801. if err != nil {
  2802. return err
  2803. }
  2804. aggRange.Allocations = append(aggRange.Allocations, as)
  2805. }
  2806. asr.Allocations = aggRange.Allocations
  2807. return nil
  2808. }
  2809. // Append appends the given AllocationSet to the end of the range. It does not
  2810. // validate whether or not that violates window continuity.
  2811. func (asr *AllocationSetRange) Append(that *AllocationSet) {
  2812. asr.Allocations = append(asr.Allocations, that)
  2813. }
  2814. // InsertRange merges the given AllocationSetRange into the receiving one by
  2815. // lining up sets with matching windows, then inserting each allocation from
  2816. // the given ASR into the respective set in the receiving ASR. If the given
  2817. // ASR contains an AllocationSet from a window that does not exist in the
  2818. // receiving ASR, then an error is returned. However, the given ASR does not
  2819. // need to cover the full range of the receiver.
  2820. func (asr *AllocationSetRange) InsertRange(that *AllocationSetRange) error {
  2821. if asr == nil {
  2822. return fmt.Errorf("cannot insert range into nil AllocationSetRange")
  2823. }
  2824. // Providing an empty or nil set range is a no-op
  2825. if that == nil {
  2826. return nil
  2827. }
  2828. // keys maps window to index in asr
  2829. keys := map[string]int{}
  2830. for i, as := range asr.Allocations {
  2831. if as == nil {
  2832. continue
  2833. }
  2834. keys[as.Window.String()] = i
  2835. }
  2836. // Nothing to merge, so simply return
  2837. if len(keys) == 0 {
  2838. return nil
  2839. }
  2840. var err error
  2841. for _, thatAS := range that.Allocations {
  2842. if thatAS == nil || err != nil {
  2843. continue
  2844. }
  2845. // Find matching AllocationSet in asr
  2846. i, ok := keys[thatAS.Window.String()]
  2847. if !ok {
  2848. err = fmt.Errorf("cannot merge AllocationSet into window that does not exist: %s", thatAS.Window.String())
  2849. continue
  2850. }
  2851. as, err := asr.Get(i)
  2852. if err != nil {
  2853. err = fmt.Errorf("AllocationSetRange index does not exist: %d", i)
  2854. continue
  2855. }
  2856. // Insert each Allocation from the given set
  2857. for _, alloc := range thatAS.Allocations {
  2858. err = as.Insert(alloc)
  2859. if err != nil {
  2860. err = fmt.Errorf("error inserting allocation: %s", err)
  2861. continue
  2862. }
  2863. }
  2864. }
  2865. // err might be nil
  2866. return err
  2867. }
  2868. // Length returns the length of the range, which is zero if nil
  2869. func (asr *AllocationSetRange) Length() int {
  2870. if asr == nil || asr.Allocations == nil {
  2871. return 0
  2872. }
  2873. return len(asr.Allocations)
  2874. }
  2875. // Slice copies the underlying slice of AllocationSets, maintaining order,
  2876. // and returns the copied slice.
  2877. func (asr *AllocationSetRange) Slice() []*AllocationSet {
  2878. if asr == nil || asr.Allocations == nil {
  2879. return nil
  2880. }
  2881. copy := []*AllocationSet{}
  2882. for _, as := range asr.Allocations {
  2883. copy = append(copy, as.Clone())
  2884. }
  2885. return copy
  2886. }
  2887. // String represents the given AllocationSetRange as a string
  2888. func (asr *AllocationSetRange) String() string {
  2889. if asr == nil {
  2890. return "<nil>"
  2891. }
  2892. return fmt.Sprintf("AllocationSetRange{length: %d}", asr.Length())
  2893. }
  2894. // UTCOffset returns the detected UTCOffset of the AllocationSets within the
  2895. // range. Defaults to 0 if the range is nil or empty. Does not warn if there
  2896. // are sets with conflicting UTCOffsets (just returns the first).
  2897. func (asr *AllocationSetRange) UTCOffset() time.Duration {
  2898. if asr.Length() == 0 {
  2899. return 0
  2900. }
  2901. as, err := asr.Get(0)
  2902. if err != nil {
  2903. return 0
  2904. }
  2905. return as.UTCOffset()
  2906. }
  2907. // Window returns the full window that the AllocationSetRange spans, from the
  2908. // start of the first AllocationSet to the end of the last one.
  2909. func (asr *AllocationSetRange) Window() Window {
  2910. if asr == nil || asr.Length() == 0 {
  2911. return NewWindow(nil, nil)
  2912. }
  2913. start := asr.Allocations[0].Start()
  2914. end := asr.Allocations[asr.Length()-1].End()
  2915. return NewWindow(&start, &end)
  2916. }
  2917. // Start returns the earliest start of all Allocations in the AllocationSetRange.
  2918. // It returns an error if there are no allocations.
  2919. func (asr *AllocationSetRange) Start() (time.Time, error) {
  2920. start := time.Time{}
  2921. if asr == nil {
  2922. return start, fmt.Errorf("had no data to compute a start from")
  2923. }
  2924. firstStartNotSet := true
  2925. for _, as := range asr.Allocations {
  2926. for _, a := range as.Allocations {
  2927. if firstStartNotSet {
  2928. start = a.Start
  2929. firstStartNotSet = false
  2930. }
  2931. if a.Start.Before(start) {
  2932. start = a.Start
  2933. }
  2934. }
  2935. }
  2936. if firstStartNotSet {
  2937. return start, fmt.Errorf("had no data to compute a start from")
  2938. }
  2939. return start, nil
  2940. }
  2941. // End returns the latest end of all Allocations in the AllocationSetRange.
  2942. // It returns an error if there are no allocations.
  2943. func (asr *AllocationSetRange) End() (time.Time, error) {
  2944. end := time.Time{}
  2945. if asr == nil {
  2946. return end, fmt.Errorf("had no data to compute an end from")
  2947. }
  2948. firstEndNotSet := true
  2949. for _, as := range asr.Allocations {
  2950. for _, a := range as.Allocations {
  2951. if firstEndNotSet {
  2952. end = a.End
  2953. firstEndNotSet = false
  2954. }
  2955. if a.End.After(end) {
  2956. end = a.End
  2957. }
  2958. }
  2959. }
  2960. if firstEndNotSet {
  2961. return end, fmt.Errorf("had no data to compute an end from")
  2962. }
  2963. return end, nil
  2964. }
  2965. // StartAndEnd iterates over all AssetSets in the AssetSetRange and returns the earliest start and
  2966. // latest end over the entire range
  2967. func (asr *AllocationSetRange) StartAndEnd() (time.Time, time.Time, error) {
  2968. start := time.Time{}
  2969. end := time.Time{}
  2970. if asr == nil {
  2971. return start, end, fmt.Errorf("had no data to compute a start and end from")
  2972. }
  2973. firstStartNotSet := true
  2974. firstEndNotSet := true
  2975. for _, as := range asr.Allocations {
  2976. for _, a := range as.Allocations {
  2977. if firstStartNotSet {
  2978. start = a.Start
  2979. firstStartNotSet = false
  2980. }
  2981. if a.Start.Before(start) {
  2982. start = a.Start
  2983. }
  2984. if firstEndNotSet {
  2985. end = a.End
  2986. firstEndNotSet = false
  2987. }
  2988. if a.End.After(end) {
  2989. end = a.End
  2990. }
  2991. }
  2992. }
  2993. if firstStartNotSet {
  2994. return start, end, fmt.Errorf("had no data to compute a start from")
  2995. }
  2996. if firstEndNotSet {
  2997. return start, end, fmt.Errorf("had no data to compute an end from")
  2998. }
  2999. return start, end, nil
  3000. }
  3001. // Minutes returns the duration, in minutes, between the earliest start
  3002. // and the latest end of all assets in the AllocationSetRange.
  3003. func (asr *AllocationSetRange) Minutes() float64 {
  3004. if asr == nil {
  3005. return 0
  3006. }
  3007. start, end, err := asr.StartAndEnd()
  3008. if err != nil {
  3009. return 0
  3010. }
  3011. duration := end.Sub(start)
  3012. return duration.Minutes()
  3013. }
  3014. // TotalCost returns the sum of all TotalCosts of the allocations contained
  3015. func (asr *AllocationSetRange) TotalCost() float64 {
  3016. if asr == nil || len(asr.Allocations) == 0 {
  3017. return 0.0
  3018. }
  3019. tc := 0.0
  3020. for _, as := range asr.Allocations {
  3021. tc += as.TotalCost()
  3022. }
  3023. return tc
  3024. }
  3025. // Clone returns a new AllocationSetRange cloned from the existing ASR
  3026. func (asr *AllocationSetRange) Clone() *AllocationSetRange {
  3027. sasrClone := NewAllocationSetRange()
  3028. sasrClone.FromStore = asr.FromStore
  3029. for _, as := range asr.Allocations {
  3030. asClone := as.Clone()
  3031. sasrClone.Append(asClone)
  3032. }
  3033. return sasrClone
  3034. }