allocation.go 97 KB

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