allocation.go 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  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. func (a *Allocation) IsUnmounted() bool {
  740. if a == nil {
  741. return false
  742. }
  743. return strings.Contains(a.Name, UnmountedSuffix)
  744. }
  745. // Minutes returns the number of minutes the Allocation represents, as defined
  746. // by the difference between the end and start times.
  747. func (a *Allocation) Minutes() float64 {
  748. if a == nil {
  749. return 0.0
  750. }
  751. return a.End.Sub(a.Start).Minutes()
  752. }
  753. // Share adds the TotalCost of the given Allocation to the SharedCost of the
  754. // receiving Allocation. No Start, End, Window, or AllocationProperties are considered.
  755. // Neither Allocation is mutated; a new Allocation is always returned.
  756. func (a *Allocation) Share(that *Allocation) (*Allocation, error) {
  757. if that == nil {
  758. return a.Clone(), nil
  759. }
  760. if a == nil {
  761. return nil, fmt.Errorf("cannot share with nil Allocation")
  762. }
  763. agg := a.Clone()
  764. agg.SharedCost += that.TotalCost()
  765. return agg, nil
  766. }
  767. // String represents the given Allocation as a string
  768. func (a *Allocation) String() string {
  769. if a == nil {
  770. return "<nil>"
  771. }
  772. return fmt.Sprintf("%s%s=%.2f", a.Name, NewWindow(&a.Start, &a.End), a.TotalCost())
  773. }
  774. func (a *Allocation) add(that *Allocation) {
  775. if a == nil {
  776. log.Warnf("Allocation.AggregateBy: trying to add a nil receiver")
  777. return
  778. }
  779. // Generate keys for each allocation to allow for special logic to set the controller
  780. // in the case of keys matching but controllers not matching.
  781. aggByForKey := []string{"cluster", "node", "namespace", "pod", "container"}
  782. leftKey := a.generateKey(aggByForKey, nil)
  783. rightKey := that.generateKey(aggByForKey, nil)
  784. leftProperties := a.Properties
  785. rightProperties := that.Properties
  786. // Preserve string properties that are matching between the two allocations
  787. a.Properties = a.Properties.Intersection(that.Properties)
  788. // If both Allocations have ProportionalAssetResourceCosts, then
  789. // add those from the given Allocation into the receiver.
  790. if a.ProportionalAssetResourceCosts != nil || that.ProportionalAssetResourceCosts != nil {
  791. // init empty PARCs if either operand has nil PARCs
  792. if a.ProportionalAssetResourceCosts == nil {
  793. a.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  794. }
  795. if that.ProportionalAssetResourceCosts == nil {
  796. that.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  797. }
  798. a.ProportionalAssetResourceCosts.Add(that.ProportionalAssetResourceCosts)
  799. }
  800. // If both Allocations have SharedCostBreakdowns, then
  801. // add those from the given Allocation into the receiver.
  802. if a.SharedCostBreakdown != nil || that.SharedCostBreakdown != nil {
  803. if a.SharedCostBreakdown == nil {
  804. a.SharedCostBreakdown = SharedCostBreakdowns{}
  805. }
  806. if that.SharedCostBreakdown == nil {
  807. that.SharedCostBreakdown = SharedCostBreakdowns{}
  808. }
  809. a.SharedCostBreakdown.Add(that.SharedCostBreakdown)
  810. }
  811. // Overwrite regular intersection logic for the controller name property in the
  812. // case that the Allocation keys are the same but the controllers are not.
  813. if leftKey == rightKey &&
  814. leftProperties != nil &&
  815. rightProperties != nil &&
  816. leftProperties.Controller != rightProperties.Controller {
  817. if leftProperties.Controller == "" {
  818. a.Properties.Controller = rightProperties.Controller
  819. } else if rightProperties.Controller == "" {
  820. a.Properties.Controller = leftProperties.Controller
  821. } else {
  822. controllers := []string{
  823. leftProperties.Controller,
  824. rightProperties.Controller,
  825. }
  826. sort.Strings(controllers)
  827. a.Properties.Controller = controllers[0]
  828. }
  829. }
  830. // Expand the window to encompass both Allocations
  831. a.Window = a.Window.Expand(that.Window)
  832. // Sum non-cumulative fields by turning them into cumulative, adding them,
  833. // and then converting them back into averages after minutes have been
  834. // combined (just below).
  835. cpuReqCoreMins := a.CPUCoreRequestAverage * a.Minutes()
  836. cpuReqCoreMins += that.CPUCoreRequestAverage * that.Minutes()
  837. cpuUseCoreMins := a.CPUCoreUsageAverage * a.Minutes()
  838. cpuUseCoreMins += that.CPUCoreUsageAverage * that.Minutes()
  839. ramReqByteMins := a.RAMBytesRequestAverage * a.Minutes()
  840. ramReqByteMins += that.RAMBytesRequestAverage * that.Minutes()
  841. ramUseByteMins := a.RAMBytesUsageAverage * a.Minutes()
  842. ramUseByteMins += that.RAMBytesUsageAverage * that.Minutes()
  843. // Expand Start and End to be the "max" of among the given Allocations
  844. if that.Start.Before(a.Start) {
  845. a.Start = that.Start
  846. }
  847. if that.End.After(a.End) {
  848. a.End = that.End
  849. }
  850. // Convert cumulative request and usage back into rates
  851. // TODO:TEST write a unit test that fails if this is done incorrectly
  852. if a.Minutes() > 0 {
  853. a.CPUCoreRequestAverage = cpuReqCoreMins / a.Minutes()
  854. a.CPUCoreUsageAverage = cpuUseCoreMins / a.Minutes()
  855. a.RAMBytesRequestAverage = ramReqByteMins / a.Minutes()
  856. a.RAMBytesUsageAverage = ramUseByteMins / a.Minutes()
  857. } else {
  858. a.CPUCoreRequestAverage = 0.0
  859. a.CPUCoreUsageAverage = 0.0
  860. a.RAMBytesRequestAverage = 0.0
  861. a.RAMBytesUsageAverage = 0.0
  862. }
  863. // Sum all cumulative resource fields
  864. a.CPUCoreHours += that.CPUCoreHours
  865. a.GPUHours += that.GPUHours
  866. a.RAMByteHours += that.RAMByteHours
  867. a.NetworkTransferBytes += that.NetworkTransferBytes
  868. a.NetworkReceiveBytes += that.NetworkReceiveBytes
  869. // Sum all cumulative cost fields
  870. a.CPUCost += that.CPUCost
  871. a.GPUCost += that.GPUCost
  872. a.RAMCost += that.RAMCost
  873. a.NetworkCost += that.NetworkCost
  874. a.NetworkCrossZoneCost += that.NetworkCrossZoneCost
  875. a.NetworkCrossRegionCost += that.NetworkCrossRegionCost
  876. a.NetworkInternetCost += that.NetworkInternetCost
  877. a.LoadBalancerCost += that.LoadBalancerCost
  878. a.SharedCost += that.SharedCost
  879. a.ExternalCost += that.ExternalCost
  880. // Sum PVAllocations
  881. a.PVs = a.PVs.Add(that.PVs)
  882. // Sum all cumulative adjustment fields
  883. a.CPUCostAdjustment += that.CPUCostAdjustment
  884. a.RAMCostAdjustment += that.RAMCostAdjustment
  885. a.GPUCostAdjustment += that.GPUCostAdjustment
  886. a.PVCostAdjustment += that.PVCostAdjustment
  887. a.NetworkCostAdjustment += that.NetworkCostAdjustment
  888. a.LoadBalancerCostAdjustment += that.LoadBalancerCostAdjustment
  889. // Sum LoadBalancer Allocations
  890. a.LoadBalancers = a.LoadBalancers.Add(that.LoadBalancers)
  891. // Any data that is in a "raw allocation only" is not valid in any
  892. // sort of cumulative Allocation (like one that is added).
  893. a.RawAllocationOnly = nil
  894. }
  895. func (thisLbAllocs LbAllocations) Add(thatLbAllocs LbAllocations) LbAllocations {
  896. // loop through both sets of LB allocations, building a new LBAllocations that has the summed set
  897. mergedLbAllocs := thisLbAllocs.Clone()
  898. if thatLbAllocs != nil {
  899. if mergedLbAllocs == nil {
  900. mergedLbAllocs = LbAllocations{}
  901. }
  902. for lbKey, thatlbAlloc := range thatLbAllocs {
  903. thisLbAlloc, ok := mergedLbAllocs[lbKey]
  904. if !ok {
  905. thisLbAlloc = &LbAllocation{
  906. Service: thatlbAlloc.Service,
  907. Cost: thatlbAlloc.Cost,
  908. }
  909. mergedLbAllocs[lbKey] = thisLbAlloc
  910. } else {
  911. thisLbAlloc.Cost += thatlbAlloc.Cost
  912. }
  913. }
  914. }
  915. return mergedLbAllocs
  916. }
  917. // AllocationSet stores a set of Allocations, each with a unique name, that share
  918. // a window. An AllocationSet is mutable, so treat it like a threadsafe map.
  919. type AllocationSet struct {
  920. Allocations map[string]*Allocation
  921. ExternalKeys map[string]bool
  922. IdleKeys map[string]bool
  923. FromSource string // stores the name of the source used to compute the data
  924. Window Window
  925. Warnings []string
  926. Errors []string
  927. }
  928. // NewAllocationSet instantiates a new AllocationSet and, optionally, inserts
  929. // the given list of Allocations
  930. func NewAllocationSet(start, end time.Time, allocs ...*Allocation) *AllocationSet {
  931. as := &AllocationSet{
  932. Allocations: map[string]*Allocation{},
  933. ExternalKeys: map[string]bool{},
  934. IdleKeys: map[string]bool{},
  935. Window: NewWindow(&start, &end),
  936. }
  937. for _, a := range allocs {
  938. as.Insert(a)
  939. }
  940. return as
  941. }
  942. // AllocationAggregationOptions provide advanced functionality to AggregateBy, including
  943. // filtering results and sharing allocations. FilterFuncs are a list of match
  944. // functions such that, if any function fails, the allocation is ignored.
  945. // ShareFuncs are a list of match functions such that, if any function
  946. // succeeds, the allocation is marked as a shared resource. ShareIdle is a
  947. // simple flag for sharing idle resources.
  948. type AllocationAggregationOptions struct {
  949. AllocationTotalsStore AllocationTotalsStore
  950. Filter filter21.Filter
  951. IdleByNode bool
  952. IncludeProportionalAssetResourceCosts bool
  953. LabelConfig *LabelConfig
  954. MergeUnallocated bool
  955. Reconcile bool
  956. ReconcileNetwork bool
  957. ShareFuncs []AllocationMatchFunc
  958. SharedNamespaces []string
  959. SharedLabels map[string][]string
  960. ShareIdle string
  961. ShareSplit string
  962. SharedHourlyCosts map[string]float64
  963. IncludeSharedCostBreakdown bool
  964. SplitIdle bool
  965. IncludeAggregatedMetadata bool
  966. }
  967. func isFilterEmpty(filter AllocationMatcher) bool {
  968. if _, isAllPass := filter.(*matcher.AllPass[*Allocation]); isAllPass {
  969. return true
  970. }
  971. return false
  972. }
  973. // AggregateBy aggregates the Allocations in the given AllocationSet by the given
  974. // AllocationProperty. This will only be legal if the AllocationSet is divisible by the
  975. // given AllocationProperty; e.g. Containers can be divided by Namespace, but not vice-a-versa.
  976. func (as *AllocationSet) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  977. // The order of operations for aggregating allocations is as follows:
  978. //
  979. // 1. Partition external, idle, and shared allocations into separate sets.
  980. // Also, create the aggSet into which the results will be aggregated.
  981. //
  982. // 2. Compute sharing coefficients for idle and shared resources
  983. // a) if idle allocation is to be shared, or if proportional asset
  984. // resource costs are to be included, then compute idle coefficients
  985. // (proportional asset resource costs are derived from idle coefficients)
  986. // b) if proportional asset costs are to be included, derive them from
  987. // idle coefficients and add them to the allocations.
  988. // c) if idle allocation is NOT shared, but filters are present, compute
  989. // idle filtration coefficients for the purpose of only returning the
  990. // portion of idle allocation that would have been shared with the
  991. // unfiltered results. (See unit tests 5.a,b,c)
  992. // d) generate shared allocation for them given shared overhead, which
  993. // must happen after (2a) and (2b)
  994. // e) if there are shared resources, compute share coefficients
  995. //
  996. // 3. Drop any allocation that fails any of the filters
  997. //
  998. // 4. Distribute idle allocations according to the idle coefficients
  999. //
  1000. // 5. Generate aggregation key and insert allocation into the output set
  1001. //
  1002. // 6. If idle is shared and resources are shared, some idle might be shared
  1003. // with a shared resource. Distribute that to the shared resources
  1004. // prior to sharing them with the aggregated results.
  1005. //
  1006. // 7. Apply idle filtration coefficients from step (2b)
  1007. //
  1008. // 8. Distribute shared allocations according to the share coefficients.
  1009. //
  1010. // 9. If there are external allocations that can be aggregated into
  1011. // the output (i.e. they can be used to generate a valid key for
  1012. // the given properties) then aggregate; otherwise... ignore them?
  1013. //
  1014. // 10. Distribute any undistributed idle, in the case that idle
  1015. // coefficients end up being zero and some idle is not shared.
  1016. //
  1017. // 11. If the merge idle option is enabled, merge any remaining idle
  1018. // allocations into a single idle allocation. If there was any idle
  1019. // whose costs were not distributed because there was no usage of a
  1020. // specific resource type, re-add the idle to the aggregation with
  1021. // only that type.
  1022. if as.IsEmpty() {
  1023. return nil
  1024. }
  1025. if options == nil {
  1026. options = &AllocationAggregationOptions{}
  1027. }
  1028. if options.LabelConfig == nil {
  1029. options.LabelConfig = NewLabelConfig()
  1030. }
  1031. // idleFiltrationCoefficients relies on this being explicitly set
  1032. if options.ShareIdle != ShareWeighted {
  1033. options.ShareIdle = ShareNone
  1034. }
  1035. var filter AllocationMatcher
  1036. if options.Filter == nil {
  1037. filter = &matcher.AllPass[*Allocation]{}
  1038. } else {
  1039. compiler := NewAllocationMatchCompiler(options.LabelConfig)
  1040. var err error
  1041. filter, err = compiler.Compile(options.Filter)
  1042. if err != nil {
  1043. return fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(options.Filter), err)
  1044. }
  1045. }
  1046. if filter == nil {
  1047. return fmt.Errorf("unexpected nil filter")
  1048. }
  1049. var allocatedTotalsMap map[string]map[string]float64
  1050. // If aggregateBy is nil, we don't aggregate anything. On the other hand,
  1051. // an empty slice implies that we should aggregate everything. See
  1052. // generateKey for why that makes sense.
  1053. shouldAggregate := aggregateBy != nil
  1054. shouldFilter := !isFilterEmpty(filter)
  1055. shouldShare := len(options.SharedHourlyCosts) > 0 || len(options.ShareFuncs) > 0
  1056. if !shouldAggregate && !shouldFilter && !shouldShare && options.ShareIdle == ShareNone && !options.IncludeProportionalAssetResourceCosts {
  1057. // There is nothing for AggregateBy to do, so simply return nil
  1058. // before returning, set aggregated metadata inclusion in properties
  1059. if options.IncludeAggregatedMetadata {
  1060. for index := range as.Allocations {
  1061. as.Allocations[index].Properties.AggregatedMetadata = true
  1062. }
  1063. }
  1064. return nil
  1065. }
  1066. // aggSet will collect the aggregated allocations
  1067. aggSet := &AllocationSet{
  1068. Window: as.Window.Clone(),
  1069. }
  1070. // externalSet will collect external allocations
  1071. externalSet := &AllocationSet{
  1072. Window: as.Window.Clone(),
  1073. }
  1074. // idleSet will be shared among aggSet after initial aggregation
  1075. // is complete
  1076. idleSet := &AllocationSet{
  1077. Window: as.Window.Clone(),
  1078. }
  1079. // parcSet is used to compute proportionalAssetResourceCosts
  1080. // for surfacing in the API
  1081. parcSet := &AllocationSet{
  1082. Window: as.Window.Clone(),
  1083. }
  1084. // shareSet will be shared among aggSet after initial aggregation
  1085. // is complete
  1086. shareSet := &AllocationSet{
  1087. Window: as.Window.Clone(),
  1088. }
  1089. // (1) Loop and find all of the external, idle, and shared allocations. Add
  1090. // them to their respective sets, removing them from the set of allocations
  1091. // to aggregate.
  1092. for _, alloc := range as.Allocations {
  1093. alloc.Properties.AggregatedMetadata = options.IncludeAggregatedMetadata
  1094. // build a parallel set of allocations to only be used
  1095. // for computing PARCs
  1096. if options.IncludeProportionalAssetResourceCosts {
  1097. parcSet.Insert(alloc.Clone())
  1098. }
  1099. // External allocations get aggregated post-hoc (see step 6) and do
  1100. // not necessarily contain complete sets of properties, so they are
  1101. // moved to a separate AllocationSet.
  1102. if alloc.IsExternal() {
  1103. delete(as.ExternalKeys, alloc.Name)
  1104. delete(as.Allocations, alloc.Name)
  1105. externalSet.Insert(alloc)
  1106. continue
  1107. }
  1108. // Idle allocations should be separated into idleSet if they are to be
  1109. // shared later on. If they are not to be shared, then add them to the
  1110. // aggSet like any other allocation.
  1111. if alloc.IsIdle() {
  1112. delete(as.IdleKeys, alloc.Name)
  1113. delete(as.Allocations, alloc.Name)
  1114. if options.ShareIdle == ShareEven || options.ShareIdle == ShareWeighted {
  1115. idleSet.Insert(alloc)
  1116. } else {
  1117. aggSet.Insert(alloc)
  1118. }
  1119. continue
  1120. }
  1121. // Shared allocations must be identified and separated prior to
  1122. // aggregation and filtering. That is, if any of the ShareFuncs return
  1123. // true for the allocation, then move it to shareSet.
  1124. for _, sf := range options.ShareFuncs {
  1125. if sf(alloc) {
  1126. delete(as.IdleKeys, alloc.Name)
  1127. delete(as.Allocations, alloc.Name)
  1128. shareSet.Insert(alloc)
  1129. break
  1130. }
  1131. }
  1132. }
  1133. // It's possible that no more un-shared, non-idle, non-external allocations
  1134. // remain at this point. This always results in an emptySet, so return early.
  1135. if len(as.Allocations) == 0 {
  1136. emptySet := &AllocationSet{
  1137. Window: as.Window.Clone(),
  1138. }
  1139. as.Allocations = emptySet.Allocations
  1140. return nil
  1141. }
  1142. // (2) In order to correctly share idle and shared costs, we first compute
  1143. // sharing coefficients, which represent the proportion of each cost to
  1144. // share with each allocation. Idle allocations are shared per-cluster or per-node,
  1145. // per-allocation, and per-resource, while shared resources are shared per-
  1146. // allocation only.
  1147. //
  1148. // For an idleCoefficient example, the entries:
  1149. // [cluster1][cluster1/namespace1/pod1/container1][cpu] = 0.166667
  1150. // [cluster1][cluster1/namespace1/pod1/container1][gpu] = 0.166667
  1151. // [cluster1][cluster1/namespace1/pod1/container1][ram] = 0.687500
  1152. // mean that the allocation "cluster1/namespace1/pod1/container1" will
  1153. // receive 16.67% of cluster1's idle CPU and GPU costs and 68.75% of its
  1154. // RAM costs.
  1155. //
  1156. // For a shareCoefficient example, the entries:
  1157. // [namespace2] = 0.666667
  1158. // [__filtered__] = 0.333333
  1159. // mean that the post-aggregation allocation "namespace2" will receive
  1160. // 66.67% of the shared resource costs, while the remaining 33.33% will
  1161. // be filtered out, as they were shared with allocations that did not pass
  1162. // one of the given filters.
  1163. //
  1164. // In order to maintain stable results when multiple operations are being
  1165. // carried out (e.g. sharing idle, sharing resources, and filtering) these
  1166. // coefficients are computed for the full set of allocations prior to
  1167. // adding shared overhead and prior to applying filters.
  1168. var err error
  1169. // (2a) If there are idle costs to be shared, compute the coefficients for
  1170. // sharing them among the non-idle, non-aggregated allocations (including
  1171. // the shared allocations).
  1172. var idleCoefficients map[string]map[string]map[string]float64
  1173. if idleSet.Length() > 0 && options.ShareIdle != ShareNone {
  1174. idleCoefficients, allocatedTotalsMap, err = computeIdleCoeffs(options, as, shareSet)
  1175. if err != nil {
  1176. log.Warnf("AllocationSet.AggregateBy: compute idle coeff: %s", err)
  1177. return fmt.Errorf("error computing idle coefficients: %s", err)
  1178. }
  1179. }
  1180. // (2b) If proportional asset resource costs are to be included, compute them
  1181. // and add them to the allocations.
  1182. if options.IncludeProportionalAssetResourceCosts {
  1183. err := deriveProportionalAssetResourceCosts(options, as, shareSet, parcSet)
  1184. if err != nil {
  1185. log.Debugf("AggregateBy: failed to derive proportional asset resource costs from idle coefficients: %s", err)
  1186. return fmt.Errorf("AggregateBy: failed to derive proportional asset resource costs from idle coefficients: %s", err)
  1187. }
  1188. }
  1189. // (2c) If idle costs are not to be shared, but there are filters, then we
  1190. // need to track the amount of each idle allocation to "filter" in order to
  1191. // maintain parity with the results when idle is shared. That is, we want
  1192. // to return only the idle costs that would have been shared with the given
  1193. // results, even if the filter had not been applied.
  1194. //
  1195. // For example, consider these results from aggregating by namespace with
  1196. // two clusters:
  1197. //
  1198. // namespace1: 25.00
  1199. // namespace2: 30.00
  1200. // namespace3: 15.00
  1201. // idle: 30.00
  1202. //
  1203. // When we then filter by cluster==cluster1, namespaces 2 and 3 are
  1204. // reduced by the amount that existed on cluster2. Then, idle must also be
  1205. // reduced by the relevant amount:
  1206. //
  1207. // namespace1: 25.00
  1208. // namespace2: 15.00
  1209. // idle: 20.00
  1210. //
  1211. // Note that this can happen for any field, not just cluster, so we again
  1212. // need to track this on a per-cluster or per-node, per-allocation, per-resource basis.
  1213. var idleFiltrationCoefficients map[string]map[string]map[string]float64
  1214. if shouldFilter && options.ShareIdle == ShareNone {
  1215. idleFiltrationCoefficients, _, err = computeIdleCoeffs(options, as, shareSet)
  1216. if err != nil {
  1217. return fmt.Errorf("error computing idle filtration coefficients: %s", err)
  1218. }
  1219. }
  1220. // (2d) Convert SharedHourlyCosts to Allocations in the shareSet. This must
  1221. // come after idle coefficients are computed so that allocations generated
  1222. // by shared overhead do not skew the idle coefficient computation.
  1223. for name, cost := range options.SharedHourlyCosts {
  1224. if cost > 0.0 {
  1225. hours := as.Resolution().Hours()
  1226. // If set ends in the future, adjust hours accordingly
  1227. diff := time.Since(as.End())
  1228. if diff < 0.0 {
  1229. hours += diff.Hours()
  1230. }
  1231. totalSharedCost := cost * hours
  1232. shareSet.Insert(&Allocation{
  1233. Name: fmt.Sprintf("%s/%s", name, SharedSuffix),
  1234. Start: as.Start(),
  1235. End: as.End(),
  1236. SharedCost: totalSharedCost,
  1237. 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.
  1238. })
  1239. }
  1240. }
  1241. // (2e) Compute share coefficients for shared resources. These are computed
  1242. // after idle coefficients, and are computed for the aggregated allocations
  1243. // of the main allocation set. See above for details and an example.
  1244. var shareCoefficients map[string]float64
  1245. if shareSet.Length() > 0 {
  1246. shareCoefficients, err = computeShareCoeffs(aggregateBy, options, as)
  1247. if err != nil {
  1248. return fmt.Errorf("error computing share coefficients: %s", err)
  1249. }
  1250. }
  1251. // (3-5) Filter, distribute idle cost, and aggregate (in that order)
  1252. for _, alloc := range as.Allocations {
  1253. idleId, err := alloc.getIdleId(options)
  1254. if err != nil {
  1255. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  1256. }
  1257. // (3) If the allocation does not match the filter, immediately skip the
  1258. // allocation.
  1259. if !filter.Matches(alloc) {
  1260. // If we are tracking idle filtration coefficients, delete the
  1261. // entry corresponding to the filtered allocation. (Deleting the
  1262. // entry will result in that proportional amount being removed
  1263. // from the idle allocation at the end of the process.)
  1264. if idleFiltrationCoefficients != nil {
  1265. if ifcc, ok := idleFiltrationCoefficients[idleId]; ok {
  1266. delete(ifcc, alloc.Name)
  1267. }
  1268. }
  1269. continue
  1270. }
  1271. // (4) Distribute idle allocations according to the idle coefficients
  1272. // NOTE: if idle allocation is off (i.e. ShareIdle == ShareNone) then
  1273. // all idle allocations will be in the aggSet at this point, so idleSet
  1274. // will be empty and we won't enter this block.
  1275. if idleSet.Length() > 0 {
  1276. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1277. for _, idleAlloc := range idleSet.Allocations {
  1278. // Only share idle if the idleId matches; i.e. the allocation
  1279. // is from the same idleId as the idle costs
  1280. iaidleId, err := idleAlloc.getIdleId(options)
  1281. if err != nil {
  1282. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1283. return err
  1284. }
  1285. if iaidleId != idleId {
  1286. continue
  1287. }
  1288. // Make sure idle coefficients exist
  1289. if _, ok := idleCoefficients[idleId]; !ok {
  1290. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1291. continue
  1292. }
  1293. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1294. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1295. continue
  1296. }
  1297. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1298. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1299. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1300. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1301. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1302. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1303. alloc.CPUCost += idleCPUCost
  1304. alloc.GPUCost += idleGPUCost
  1305. alloc.RAMCost += idleRAMCost
  1306. }
  1307. }
  1308. // (5) generate key to use for aggregation-by-key and allocation name
  1309. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1310. alloc.Name = key
  1311. if options.MergeUnallocated && alloc.IsUnallocated() {
  1312. alloc.Name = UnallocatedSuffix
  1313. }
  1314. // Inserting the allocation with the generated key for a name will
  1315. // perform the actual basic aggregation step.
  1316. aggSet.Insert(alloc)
  1317. }
  1318. // (6) If idle is shared and resources are shared, it's possible that some
  1319. // amount of idle cost will be shared with a shared resource. Distribute
  1320. // that idle allocation, if it exists, to the respective shared allocations
  1321. // before sharing with the aggregated allocations.
  1322. if idleSet.Length() > 0 && shareSet.Length() > 0 {
  1323. for _, alloc := range shareSet.Allocations {
  1324. idleId, err := alloc.getIdleId(options)
  1325. if err != nil {
  1326. log.DedupedWarningf(3, "AllocationSet.AggregateBy: missing idleId for allocation: %s", alloc.Name)
  1327. }
  1328. // Distribute idle allocations by coefficient per-idleId, per-allocation
  1329. for _, idleAlloc := range idleSet.Allocations {
  1330. // Only share idle if the idleId matches; i.e. the allocation
  1331. // is from the same idleId as the idle costs
  1332. iaidleId, _ := idleAlloc.getIdleId(options)
  1333. if iaidleId != idleId {
  1334. continue
  1335. }
  1336. // Make sure idle coefficients exist
  1337. if _, ok := idleCoefficients[idleId]; !ok {
  1338. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient: no idleId '%s' for '%s'", idleId, alloc.Name)
  1339. continue
  1340. }
  1341. if _, ok := idleCoefficients[idleId][alloc.Name]; !ok {
  1342. log.Warnf("AllocationSet.AggregateBy: error getting idle coefficient for '%s'", alloc.Name)
  1343. continue
  1344. }
  1345. alloc.CPUCoreHours += idleAlloc.CPUCoreHours * idleCoefficients[idleId][alloc.Name]["cpu"]
  1346. alloc.GPUHours += idleAlloc.GPUHours * idleCoefficients[idleId][alloc.Name]["gpu"]
  1347. alloc.RAMByteHours += idleAlloc.RAMByteHours * idleCoefficients[idleId][alloc.Name]["ram"]
  1348. idleCPUCost := idleAlloc.CPUCost * idleCoefficients[idleId][alloc.Name]["cpu"]
  1349. idleGPUCost := idleAlloc.GPUCost * idleCoefficients[idleId][alloc.Name]["gpu"]
  1350. idleRAMCost := idleAlloc.RAMCost * idleCoefficients[idleId][alloc.Name]["ram"]
  1351. alloc.CPUCost += idleCPUCost
  1352. alloc.GPUCost += idleGPUCost
  1353. alloc.RAMCost += idleRAMCost
  1354. }
  1355. }
  1356. }
  1357. // groupingIdleFiltrationCoeffs is used to track per-resource idle
  1358. // coefficients on a cluster-by-cluster or node-by-node basis depending
  1359. // on the IdleByNode option. It is, essentially, an aggregation of
  1360. // idleFiltrationCoefficients after they have been
  1361. // filtered above (in step 3)
  1362. var groupingIdleFiltrationCoeffs map[string]map[string]float64
  1363. if idleFiltrationCoefficients != nil {
  1364. groupingIdleFiltrationCoeffs = map[string]map[string]float64{}
  1365. for idleId, m := range idleFiltrationCoefficients {
  1366. if _, ok := groupingIdleFiltrationCoeffs[idleId]; !ok {
  1367. groupingIdleFiltrationCoeffs[idleId] = map[string]float64{
  1368. "cpu": 0.0,
  1369. "gpu": 0.0,
  1370. "ram": 0.0,
  1371. }
  1372. }
  1373. for _, n := range m {
  1374. for resource, val := range n {
  1375. groupingIdleFiltrationCoeffs[idleId][resource] += val
  1376. }
  1377. }
  1378. }
  1379. }
  1380. // (7) If we have both un-shared idle allocations and idle filtration
  1381. // coefficients then apply those. See step (2b) for an example.
  1382. if len(aggSet.IdleKeys) > 0 && groupingIdleFiltrationCoeffs != nil {
  1383. for idleKey := range aggSet.IdleKeys {
  1384. idleAlloc := aggSet.Get(idleKey)
  1385. iaidleId, err := idleAlloc.getIdleId(options)
  1386. if err != nil {
  1387. log.Errorf("AllocationSet.AggregateBy: Idle allocation is missing idleId %s", idleAlloc.Name)
  1388. return err
  1389. }
  1390. if resourceCoeffs, ok := groupingIdleFiltrationCoeffs[iaidleId]; ok {
  1391. idleAlloc.CPUCost *= resourceCoeffs["cpu"]
  1392. idleAlloc.CPUCoreHours *= resourceCoeffs["cpu"]
  1393. idleAlloc.RAMCost *= resourceCoeffs["ram"]
  1394. idleAlloc.RAMByteHours *= resourceCoeffs["ram"]
  1395. idleAlloc.GPUCost *= resourceCoeffs["gpu"]
  1396. idleAlloc.GPUHours *= resourceCoeffs["gpu"]
  1397. }
  1398. }
  1399. }
  1400. // (8) Distribute shared allocations according to the share coefficients.
  1401. if shareSet.Length() > 0 {
  1402. for _, alloc := range aggSet.Allocations {
  1403. for _, sharedAlloc := range shareSet.Allocations {
  1404. if _, ok := shareCoefficients[alloc.Name]; !ok {
  1405. if !alloc.IsIdle() && !alloc.IsUnmounted() {
  1406. log.Warnf("AllocationSet.AggregateBy: error getting share coefficient for '%s'", alloc.Name)
  1407. }
  1408. continue
  1409. }
  1410. if options.IncludeSharedCostBreakdown {
  1411. if alloc.SharedCostBreakdown == nil {
  1412. alloc.SharedCostBreakdown = map[string]SharedCostBreakdown{}
  1413. }
  1414. sharedCostName, err := sharedAlloc.determineSharingName(options)
  1415. if err != nil {
  1416. return fmt.Errorf("failed to group shared costs: %s", err)
  1417. }
  1418. // check if current allocation is a shared flat overhead cost
  1419. if strings.Contains(sharedAlloc.Name, SharedSuffix) {
  1420. sharedCostName = "overheadCost"
  1421. }
  1422. scb := SharedCostBreakdown{
  1423. Name: sharedCostName,
  1424. TotalCost: sharedAlloc.TotalCost() * shareCoefficients[alloc.Name],
  1425. CPUCost: sharedAlloc.CPUTotalCost() * shareCoefficients[alloc.Name],
  1426. GPUCost: sharedAlloc.GPUTotalCost() * shareCoefficients[alloc.Name],
  1427. RAMCost: sharedAlloc.RAMTotalCost() * shareCoefficients[alloc.Name],
  1428. PVCost: sharedAlloc.PVCost() * shareCoefficients[alloc.Name],
  1429. NetworkCost: sharedAlloc.NetworkTotalCost() * shareCoefficients[alloc.Name],
  1430. LBCost: sharedAlloc.LBTotalCost() * shareCoefficients[alloc.Name],
  1431. ExternalCost: sharedAlloc.ExternalCost * shareCoefficients[alloc.Name],
  1432. }
  1433. // fmt.Printf("shared cost: %+v", scb)
  1434. alloc.SharedCostBreakdown.Insert(scb)
  1435. }
  1436. alloc.SharedCost += sharedAlloc.TotalCost() * shareCoefficients[alloc.Name]
  1437. }
  1438. }
  1439. }
  1440. // (9) Aggregate external allocations into aggregated allocations. This may
  1441. // not be possible for every external allocation, but attempt to find an
  1442. // exact key match, given each external allocation's proerties, and
  1443. // aggregate if an exact match is found.
  1444. for _, alloc := range externalSet.Allocations {
  1445. if filter.Matches(alloc) {
  1446. key := alloc.generateKey(aggregateBy, options.LabelConfig)
  1447. alloc.Name = key
  1448. aggSet.Insert(alloc)
  1449. }
  1450. }
  1451. // (10) In the edge case that some idle has not been distributed because
  1452. // there is no usage of that resource type, add idle back to
  1453. // aggregations with only that cost applied.
  1454. // E.g. in the case where we have a result that looks like this on the
  1455. // frontend:
  1456. // Name CPU GPU RAM
  1457. // __idle__ $10 $12 $6
  1458. // kubecost $2 $0 $1
  1459. // Sharing idle weighted would result in no idle GPU cost being
  1460. // distributed, because the coefficient for the kubecost GPU cost would
  1461. // be zero. Thus, instead we re-add idle to the aggSet with distributed
  1462. // costs zeroed out but the undistributed costs left in.
  1463. // Name CPU GPU RAM
  1464. // __idle__ $0 $12 $0
  1465. // kubecost $12 $0 $7
  1466. if idleSet.Length() > 0 {
  1467. for _, idleAlloc := range idleSet.Allocations {
  1468. // if the idle does not apply to the non-filtered values, skip it
  1469. if !filter.Matches(idleAlloc) {
  1470. continue
  1471. }
  1472. idleId, err := idleAlloc.getIdleId(options)
  1473. if err != nil {
  1474. log.Errorf("AllocationSet.AggregateBy: idle allocation is missing idleId %s", idleAlloc.Name)
  1475. continue
  1476. }
  1477. hasUndistributableCost := false
  1478. if idleAlloc.CPUCost > 0 && allocatedTotalsMap[idleId]["cpu"] == 0 {
  1479. hasUndistributableCost = true
  1480. } else {
  1481. idleAlloc.CPUCost = 0
  1482. }
  1483. if idleAlloc.GPUCost > 0 && allocatedTotalsMap[idleId]["gpu"] == 0 {
  1484. hasUndistributableCost = true
  1485. } else {
  1486. idleAlloc.GPUCost = 0
  1487. }
  1488. if idleAlloc.RAMCost > 0 && allocatedTotalsMap[idleId]["ram"] == 0 {
  1489. hasUndistributableCost = true
  1490. } else {
  1491. idleAlloc.RAMCost = 0
  1492. }
  1493. if hasUndistributableCost {
  1494. idleAlloc.Name = fmt.Sprintf("%s/%s", idleId, IdleSuffix)
  1495. aggSet.Insert(idleAlloc)
  1496. }
  1497. }
  1498. }
  1499. // (11) Combine all idle allocations into a single "__idle__" allocation
  1500. if !options.SplitIdle {
  1501. for _, idleAlloc := range aggSet.IdleAllocations() {
  1502. aggSet.Delete(idleAlloc.Name)
  1503. idleAlloc.Name = IdleSuffix
  1504. aggSet.Insert(idleAlloc)
  1505. }
  1506. }
  1507. // TODO revisit this (ideally we just remove sharing from this function!)
  1508. // If filters and shared resources and shared idle are all enabled then
  1509. // we will over-count idle by exactly the portion that gets shared with the
  1510. // filtered allocations -- and idle filtration will miss this because it
  1511. // only filters the non-idle filtered costs.
  1512. //
  1513. // Consider the following example, from unit tests:
  1514. // - namespace1 28.000
  1515. // - namespace2 36.000
  1516. // - namespace3 18.000
  1517. // - cluster1/idle 20.000
  1518. // - cluster2/idle 10.000
  1519. //
  1520. // Now, we want to share namespace1, filter namespace2, and share idle:
  1521. //
  1522. // 1. Distribute idle
  1523. // ns1 ns2 ns3
  1524. // non-idle 28.000 36.000 18.000
  1525. // idle 14.688 10.312 5.000
  1526. //
  1527. // 2. Share namespace1
  1528. //
  1529. // ns2 ns3
  1530. // non-idle 36.000 18.000
  1531. // idle 10.312 5.000
  1532. // shared non-idle 18.667 9.333
  1533. // shared idle 9.792 4.896 (***)
  1534. //
  1535. // 3. Filter out all but namespace2
  1536. //
  1537. // ns2 = 36.000 + 10.312 + 18.667 + 9.792 = 74.771
  1538. //
  1539. // So, if we had NOT shared idle, we would expect something like this:
  1540. //
  1541. // ns2 = 36.000 + 18.667 = 54.667
  1542. // idle = 10.312 + 9.792 = 20.104
  1543. //
  1544. // But we will instead get this:
  1545. //
  1546. // ns2 = 36.000 + 18.667 = 54.667
  1547. // idle = 10.312 + 14.688 = 25.000
  1548. //
  1549. // Which over-shoots idle by 4.896 (***), i.e. precisely the amount of idle
  1550. // cost corresponding to namespace1 AND shared with namespace3. Phew.
  1551. //
  1552. // I originally wanted to fix this, but after 2 days, I'm punting with the
  1553. // recommendation that we rewrite this function soon. Too difficult.
  1554. // - Niko
  1555. as.Allocations = aggSet.Allocations
  1556. return nil
  1557. }
  1558. func computeShareCoeffs(aggregateBy []string, options *AllocationAggregationOptions, as *AllocationSet) (map[string]float64, error) {
  1559. // Compute coeffs by totalling per-allocation, then dividing by the total.
  1560. coeffs := map[string]float64{}
  1561. // Compute totals for all allocations
  1562. total := 0.0
  1563. // ShareEven counts each aggregation with even weight, whereas ShareWeighted
  1564. // counts each aggregation proportionally to its respective costs
  1565. shareType := options.ShareSplit
  1566. var filter AllocationMatcher
  1567. if options.Filter == nil {
  1568. filter = &matcher.AllPass[*Allocation]{}
  1569. } else {
  1570. compiler := NewAllocationMatchCompiler(options.LabelConfig)
  1571. var err error
  1572. filter, err = compiler.Compile(options.Filter)
  1573. if err != nil {
  1574. return nil, fmt.Errorf("compiling filter '%s': %w", ast.ToPreOrderShortString(options.Filter), err)
  1575. }
  1576. }
  1577. if filter == nil {
  1578. return nil, fmt.Errorf("unexpected nil filter")
  1579. }
  1580. // Record allocation values first, then normalize by totals to get percentages
  1581. for _, alloc := range as.Allocations {
  1582. if alloc.IsIdle() {
  1583. // Skip idle allocations in coefficient calculation
  1584. continue
  1585. }
  1586. if alloc.IsUnmounted() {
  1587. // Skip unmounted allocations in coefficient calculation
  1588. continue
  1589. }
  1590. // Determine the post-aggregation key under which the allocation will
  1591. // be shared.
  1592. name := alloc.generateKey(aggregateBy, options.LabelConfig)
  1593. // If the current allocation will be filtered out in step 3, contribute
  1594. // its share of the shared coefficient to a "__filtered__" bin, which
  1595. // will ultimately be dropped. This step ensures that the shared cost
  1596. // of a non-filtered allocation will be conserved even when the filter
  1597. // is removed. (Otherwise, all the shared cost will get redistributed
  1598. // over the unfiltered results, inflating their shared costs.)
  1599. if !filter.Matches(alloc) {
  1600. name = "__filtered__"
  1601. }
  1602. if shareType == ShareEven {
  1603. // Even distribution is not additive - set to 1.0 for everything
  1604. coeffs[name] = 1.0
  1605. // Total for even distribution is always the number of coefficients
  1606. total = float64(len(coeffs))
  1607. } else {
  1608. // Both are additive for weighted distribution, where each
  1609. // cumulative coefficient will be divided by the total.
  1610. coeffs[name] += alloc.TotalCost() - alloc.SharedCost
  1611. total += alloc.TotalCost() - alloc.SharedCost
  1612. }
  1613. }
  1614. // Normalize coefficients by totals
  1615. for a := range coeffs {
  1616. if coeffs[a] > 0 && total > 0 {
  1617. coeffs[a] /= total
  1618. } else {
  1619. log.Warnf("ETL: invalid values for shared coefficients: %v, %v", coeffs[a], total)
  1620. coeffs[a] = 0.0
  1621. }
  1622. }
  1623. return coeffs, nil
  1624. }
  1625. func computeIdleCoeffs(options *AllocationAggregationOptions, as *AllocationSet, shareSet *AllocationSet) (map[string]map[string]map[string]float64, map[string]map[string]float64, error) {
  1626. types := []string{"cpu", "gpu", "ram"}
  1627. // Compute idle coefficients, then save them in AllocationAggregationOptions
  1628. // [idle_id][allocation name][resource] = [coeff]
  1629. coeffs := map[string]map[string]map[string]float64{}
  1630. // Compute totals per resource for CPU, GPU, RAM, and PV
  1631. // [idle_id][resource] = [total]
  1632. totals := map[string]map[string]float64{}
  1633. // Record allocation values first, then normalize by totals to get percentages
  1634. for _, alloc := range as.Allocations {
  1635. if alloc.IsIdle() {
  1636. // Skip idle allocations in coefficient calculation
  1637. continue
  1638. }
  1639. idleId, err := alloc.getIdleId(options)
  1640. if err != nil {
  1641. log.DedupedWarningf(3, "Missing Idle Key for %s", alloc.Name)
  1642. }
  1643. // get the name key for the allocation
  1644. name := alloc.Name
  1645. // Create key based tables if they don't exist
  1646. if _, ok := coeffs[idleId]; !ok {
  1647. coeffs[idleId] = map[string]map[string]float64{}
  1648. }
  1649. if _, ok := totals[idleId]; !ok {
  1650. totals[idleId] = map[string]float64{}
  1651. }
  1652. if _, ok := coeffs[idleId][name]; !ok {
  1653. coeffs[idleId][name] = map[string]float64{}
  1654. }
  1655. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1656. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1657. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1658. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1659. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1660. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1661. }
  1662. // Do the same for shared allocations
  1663. for _, alloc := range shareSet.Allocations {
  1664. if alloc.IsIdle() {
  1665. // Skip idle allocations in coefficient calculation
  1666. continue
  1667. }
  1668. // idleId will be providerId or cluster
  1669. idleId, err := alloc.getIdleId(options)
  1670. if err != nil {
  1671. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1672. }
  1673. // get the name key for the allocation
  1674. name := alloc.Name
  1675. // Create idleId based tables if they don't exist
  1676. if _, ok := coeffs[idleId]; !ok {
  1677. coeffs[idleId] = map[string]map[string]float64{}
  1678. }
  1679. if _, ok := totals[idleId]; !ok {
  1680. totals[idleId] = map[string]float64{}
  1681. }
  1682. if _, ok := coeffs[idleId][name]; !ok {
  1683. coeffs[idleId][name] = map[string]float64{}
  1684. }
  1685. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1686. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1687. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1688. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1689. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1690. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1691. }
  1692. // Normalize coefficients by totals
  1693. for id := range coeffs {
  1694. for a := range coeffs[id] {
  1695. for _, r := range types {
  1696. if coeffs[id][a][r] > 0 && totals[id][r] > 0 {
  1697. coeffs[id][a][r] /= totals[id][r]
  1698. }
  1699. }
  1700. }
  1701. }
  1702. return coeffs, totals, nil
  1703. }
  1704. func deriveProportionalAssetResourceCosts(options *AllocationAggregationOptions, as *AllocationSet, shareSet, parcsSet *AllocationSet) error {
  1705. // Compute idle coefficients, then save them in AllocationAggregationOptions
  1706. // [idle_id][allocation name][resource] = [coeff]
  1707. coeffs := map[string]map[string]map[string]float64{}
  1708. // Compute totals per resource for CPU, GPU, RAM, and PV
  1709. // [idle_id][resource] = [total]
  1710. totals := map[string]map[string]float64{}
  1711. // Record allocation values first, then normalize by totals to get percentages
  1712. for _, alloc := range parcsSet.Allocations {
  1713. idleId, err := alloc.getIdleId(options)
  1714. if err != nil {
  1715. log.DedupedWarningf(3, "Missing Idle Key for %s", alloc.Name)
  1716. }
  1717. // get the name key for the allocation
  1718. name := alloc.Name
  1719. // Create key based tables if they don't exist
  1720. if _, ok := coeffs[idleId]; !ok {
  1721. coeffs[idleId] = map[string]map[string]float64{}
  1722. }
  1723. if _, ok := totals[idleId]; !ok {
  1724. totals[idleId] = map[string]float64{}
  1725. }
  1726. if _, ok := coeffs[idleId][name]; !ok {
  1727. coeffs[idleId][name] = map[string]float64{}
  1728. }
  1729. // idle IDs for load balancers are their services
  1730. for key := range alloc.LoadBalancers {
  1731. if _, ok := totals[key]; !ok {
  1732. totals[key] = map[string]float64{}
  1733. }
  1734. if _, ok := coeffs[key]; !ok {
  1735. coeffs[key] = map[string]map[string]float64{}
  1736. }
  1737. if _, ok := coeffs[key][name]; !ok {
  1738. coeffs[key][name] = map[string]float64{}
  1739. }
  1740. coeffs[key][name]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1741. totals[key]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1742. }
  1743. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1744. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1745. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1746. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1747. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1748. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1749. }
  1750. // Do the same for shared allocations
  1751. for _, alloc := range shareSet.Allocations {
  1752. if alloc.IsIdle() {
  1753. // Skip idle allocations in coefficient calculation
  1754. continue
  1755. }
  1756. // idleId will be providerId or cluster
  1757. idleId, err := alloc.getIdleId(options)
  1758. if err != nil {
  1759. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1760. }
  1761. // get the name key for the allocation
  1762. name := alloc.Name
  1763. // Create idleId based tables if they don't exist
  1764. if _, ok := coeffs[idleId]; !ok {
  1765. coeffs[idleId] = map[string]map[string]float64{}
  1766. }
  1767. if _, ok := totals[idleId]; !ok {
  1768. totals[idleId] = map[string]float64{}
  1769. }
  1770. if _, ok := coeffs[idleId][name]; !ok {
  1771. coeffs[idleId][name] = map[string]float64{}
  1772. }
  1773. coeffs[idleId][name]["cpu"] += alloc.CPUTotalCost()
  1774. coeffs[idleId][name]["gpu"] += alloc.GPUTotalCost()
  1775. coeffs[idleId][name]["ram"] += alloc.RAMTotalCost()
  1776. totals[idleId]["cpu"] += alloc.CPUTotalCost()
  1777. totals[idleId]["gpu"] += alloc.GPUTotalCost()
  1778. totals[idleId]["ram"] += alloc.RAMTotalCost()
  1779. // idle IDs for load balancers are their services
  1780. for key := range alloc.LoadBalancers {
  1781. if _, ok := totals[key]; !ok {
  1782. totals[key] = map[string]float64{}
  1783. }
  1784. if _, ok := coeffs[key]; !ok {
  1785. coeffs[key] = map[string]map[string]float64{}
  1786. }
  1787. if _, ok := coeffs[key][name]; !ok {
  1788. coeffs[key][name] = map[string]float64{}
  1789. }
  1790. coeffs[key][name]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1791. totals[key]["loadbalancer"] += alloc.LoadBalancerTotalCost()
  1792. }
  1793. }
  1794. // after totals are computed, loop through and set parcs on allocations
  1795. for _, alloc := range as.Allocations {
  1796. idleId, err := alloc.getIdleId(options)
  1797. if err != nil {
  1798. log.DedupedWarningf(3, "Missing Idle Key in share set for %s", alloc.Name)
  1799. }
  1800. alloc.ProportionalAssetResourceCosts = ProportionalAssetResourceCosts{}
  1801. alloc.ProportionalAssetResourceCosts.Insert(ProportionalAssetResourceCost{
  1802. Cluster: alloc.Properties.Cluster,
  1803. Name: alloc.Properties.Node,
  1804. Type: "Node",
  1805. ProviderID: alloc.Properties.ProviderID,
  1806. GPUProportionalCost: coeffs[idleId][alloc.Name]["gpu"],
  1807. CPUProportionalCost: coeffs[idleId][alloc.Name]["cpu"],
  1808. RAMProportionalCost: coeffs[idleId][alloc.Name]["ram"],
  1809. }, options.IdleByNode)
  1810. // insert a separate PARC for the load balancer
  1811. if alloc.LoadBalancerCost != 0 {
  1812. for key, svc := range alloc.LoadBalancers {
  1813. alloc.ProportionalAssetResourceCosts.Insert(ProportionalAssetResourceCost{
  1814. Cluster: alloc.Properties.Cluster,
  1815. Name: svc.Service,
  1816. Type: "LoadBalancer",
  1817. LoadBalancerProportionalCost: coeffs[key][alloc.Name]["loadbalancer"],
  1818. }, options.IdleByNode)
  1819. }
  1820. }
  1821. }
  1822. return nil
  1823. }
  1824. func (a *Allocation) determineSharingName(options *AllocationAggregationOptions) (string, error) {
  1825. if a == nil {
  1826. return "", fmt.Errorf("determineSharingName called on nil Allocation")
  1827. } else if options == nil {
  1828. return "unknown", nil
  1829. }
  1830. // grab SharedLabels keys and sort them, to keep this function deterministic
  1831. var labelKeys []string
  1832. for labelKey, _ := range options.SharedLabels {
  1833. labelKeys = append(labelKeys, labelKey)
  1834. }
  1835. slices.Sort(labelKeys)
  1836. var sharedAggregateBy []string
  1837. var sharedLabels [][]string
  1838. for _, labelKey := range labelKeys {
  1839. sharedAgg := fmt.Sprintf("label:%s", labelKey)
  1840. if !slices.Contains(sharedAggregateBy, sharedAgg) {
  1841. sharedAggregateBy = append(sharedAggregateBy, sharedAgg)
  1842. }
  1843. sharedLabels = append(sharedLabels, options.SharedLabels[labelKey])
  1844. }
  1845. if len(options.SharedNamespaces) > 0 {
  1846. sharedAggregateBy = append(sharedAggregateBy, "namespace")
  1847. }
  1848. sharedCostName := a.generateKey(sharedAggregateBy, options.LabelConfig)
  1849. // get each value in the generated key, then reset the name
  1850. sharedCostNameValues := strings.Split(sharedCostName, "/")
  1851. sharedCostName = ""
  1852. // if we don't have as many values as aggregateBys, something went wrong in generateKey
  1853. if len(sharedCostNameValues) != len(sharedAggregateBy) {
  1854. log.Warnf("Unable to determine share cost group for allocation \"%s\"", a.Name)
  1855. } else {
  1856. // try to match to the first label
  1857. for i, sharedLabelValues := range sharedLabels {
  1858. allocLabel := sharedCostNameValues[i]
  1859. if slices.Contains(sharedLabelValues, allocLabel) {
  1860. return allocLabel, nil
  1861. }
  1862. }
  1863. // if we didn't match to a label, try to match to a namespace
  1864. if len(options.SharedNamespaces) > 0 {
  1865. // namespace will always be the last value, if SharedNamespaces is set
  1866. allocNamespace := sharedCostNameValues[len(sharedCostNameValues)-1]
  1867. if slices.Contains(options.SharedNamespaces, allocNamespace) {
  1868. return allocNamespace, nil
  1869. }
  1870. }
  1871. // if neither the labels nor the namespaces matched, we log a warning and mark this allocation
  1872. // as unknown
  1873. if len(sharedCostName) == 0 {
  1874. log.Warnf("Failed to determine shared cost grouping for allocation \"%s\"", a.Name)
  1875. }
  1876. }
  1877. return "unknown", nil
  1878. }
  1879. // getIdleId returns the providerId or cluster of an Allocation depending on the IdleByNode
  1880. // option in the AllocationAggregationOptions and an error if the respective field is missing
  1881. func (a *Allocation) getIdleId(options *AllocationAggregationOptions) (string, error) {
  1882. var idleId string
  1883. if options.IdleByNode {
  1884. // Key allocations to ProviderId to match against node
  1885. idleId = fmt.Sprintf("%s/%s", a.Properties.Cluster, a.Properties.Node)
  1886. if idleId == "" {
  1887. return idleId, fmt.Errorf("ProviderId is not set")
  1888. }
  1889. } else {
  1890. // key the allocations by cluster id
  1891. idleId = a.Properties.Cluster
  1892. if idleId == "" {
  1893. return idleId, fmt.Errorf("ClusterProp is not set")
  1894. }
  1895. }
  1896. return idleId, nil
  1897. }
  1898. func (a *Allocation) generateKey(aggregateBy []string, labelConfig *LabelConfig) string {
  1899. if a == nil {
  1900. return ""
  1901. }
  1902. return a.Properties.GenerateKey(aggregateBy, labelConfig)
  1903. }
  1904. func (a *Allocation) StringProperty(property string) (string, error) {
  1905. switch property {
  1906. case AllocationClusterProp:
  1907. if a.Properties == nil {
  1908. return "", nil
  1909. }
  1910. return a.Properties.Cluster, nil
  1911. case AllocationNodeProp:
  1912. if a.Properties == nil {
  1913. return "", nil
  1914. }
  1915. return a.Properties.Node, nil
  1916. case AllocationContainerProp:
  1917. if a.Properties == nil {
  1918. return "", nil
  1919. }
  1920. return a.Properties.Container, nil
  1921. case AllocationControllerProp:
  1922. if a.Properties == nil {
  1923. return "", nil
  1924. }
  1925. return a.Properties.Controller, nil
  1926. case AllocationControllerKindProp:
  1927. if a.Properties == nil {
  1928. return "", nil
  1929. }
  1930. return a.Properties.ControllerKind, nil
  1931. case AllocationNamespaceProp:
  1932. if a.Properties == nil {
  1933. return "", nil
  1934. }
  1935. return a.Properties.Namespace, nil
  1936. case AllocationPodProp:
  1937. if a.Properties == nil {
  1938. return "", nil
  1939. }
  1940. return a.Properties.Pod, nil
  1941. case AllocationProviderIDProp:
  1942. if a.Properties == nil {
  1943. return "", nil
  1944. }
  1945. return a.Properties.ProviderID, nil
  1946. default:
  1947. return "", fmt.Errorf("Allocation: StringProperty: invalid property name: %s", property)
  1948. }
  1949. }
  1950. func (a *Allocation) StringSliceProperty(property string) ([]string, error) {
  1951. switch property {
  1952. case AllocationServiceProp:
  1953. if a.Properties == nil {
  1954. return nil, nil
  1955. }
  1956. return a.Properties.Services, nil
  1957. default:
  1958. return nil, fmt.Errorf("Allocation: StringSliceProperty: invalid property name: %s", property)
  1959. }
  1960. }
  1961. func (a *Allocation) StringMapProperty(property string) (map[string]string, error) {
  1962. switch property {
  1963. case AllocationLabelProp:
  1964. if a.Properties == nil {
  1965. return nil, nil
  1966. }
  1967. return a.Properties.Labels, nil
  1968. case AllocationAnnotationProp:
  1969. if a.Properties == nil {
  1970. return nil, nil
  1971. }
  1972. return a.Properties.Annotations, nil
  1973. default:
  1974. return nil, fmt.Errorf("Allocation: StringMapProperty: invalid property name: %s", property)
  1975. }
  1976. }
  1977. // Clone returns a new AllocationSet with a deep copy of the given
  1978. // AllocationSet's allocations.
  1979. func (as *AllocationSet) Clone() *AllocationSet {
  1980. if as == nil {
  1981. return nil
  1982. }
  1983. allocs := make(map[string]*Allocation, len(as.Allocations))
  1984. for k, v := range as.Allocations {
  1985. allocs[k] = v.Clone()
  1986. }
  1987. externalKeys := make(map[string]bool, len(as.ExternalKeys))
  1988. for k, v := range as.ExternalKeys {
  1989. externalKeys[k] = v
  1990. }
  1991. idleKeys := make(map[string]bool, len(as.IdleKeys))
  1992. for k, v := range as.IdleKeys {
  1993. idleKeys[k] = v
  1994. }
  1995. var errors []string
  1996. var warnings []string
  1997. if as.Errors != nil {
  1998. errors = make([]string, len(as.Errors))
  1999. copy(errors, as.Errors)
  2000. } else {
  2001. errors = nil
  2002. }
  2003. if as.Warnings != nil {
  2004. warnings := make([]string, len(as.Warnings))
  2005. copy(warnings, as.Warnings)
  2006. } else {
  2007. warnings = nil
  2008. }
  2009. return &AllocationSet{
  2010. Allocations: allocs,
  2011. ExternalKeys: externalKeys,
  2012. IdleKeys: idleKeys,
  2013. Window: as.Window.Clone(),
  2014. Errors: errors,
  2015. Warnings: warnings,
  2016. }
  2017. }
  2018. // Delete removes the allocation with the given name from the set
  2019. func (as *AllocationSet) Delete(name string) {
  2020. if as == nil {
  2021. return
  2022. }
  2023. delete(as.ExternalKeys, name)
  2024. delete(as.IdleKeys, name)
  2025. delete(as.Allocations, name)
  2026. }
  2027. // End returns the End time of the AllocationSet window
  2028. func (as *AllocationSet) End() time.Time {
  2029. if as == nil {
  2030. log.Warnf("AllocationSet: calling End on nil AllocationSet")
  2031. return time.Unix(0, 0)
  2032. }
  2033. if as.Window.End() == nil {
  2034. log.Warnf("AllocationSet: AllocationSet with illegal window: End is nil; len(as.allocations)=%d", len(as.Allocations))
  2035. return time.Unix(0, 0)
  2036. }
  2037. return *as.Window.End()
  2038. }
  2039. // Get returns the Allocation at the given key in the AllocationSet
  2040. func (as *AllocationSet) Get(key string) *Allocation {
  2041. if alloc, ok := as.Allocations[key]; ok {
  2042. return alloc
  2043. }
  2044. return nil
  2045. }
  2046. // ExternalAllocations returns a map of the external allocations in the set.
  2047. // Returns clones of the actual Allocations, so mutability is not a problem.
  2048. func (as *AllocationSet) ExternalAllocations() map[string]*Allocation {
  2049. externals := map[string]*Allocation{}
  2050. if as.IsEmpty() {
  2051. return externals
  2052. }
  2053. for key := range as.ExternalKeys {
  2054. if alloc, ok := as.Allocations[key]; ok {
  2055. externals[key] = alloc.Clone()
  2056. }
  2057. }
  2058. return externals
  2059. }
  2060. // ExternalCost returns the total aggregated external costs of the set
  2061. func (as *AllocationSet) ExternalCost() float64 {
  2062. if as.IsEmpty() {
  2063. return 0.0
  2064. }
  2065. externalCost := 0.0
  2066. for _, alloc := range as.Allocations {
  2067. externalCost += alloc.ExternalCost
  2068. }
  2069. return externalCost
  2070. }
  2071. // IdleAllocations returns a map of the idle allocations in the AllocationSet.
  2072. // Returns clones of the actual Allocations, so mutability is not a problem.
  2073. func (as *AllocationSet) IdleAllocations() map[string]*Allocation {
  2074. idles := map[string]*Allocation{}
  2075. if as.IsEmpty() {
  2076. return idles
  2077. }
  2078. for key := range as.IdleKeys {
  2079. if alloc, ok := as.Allocations[key]; ok {
  2080. idles[key] = alloc.Clone()
  2081. }
  2082. }
  2083. return idles
  2084. }
  2085. // Insert aggregates the current entry in the AllocationSet by the given Allocation,
  2086. // but only if the Allocation is valid, i.e. matches the AllocationSet's window. If
  2087. // there is no existing entry, one is created. Nil error response indicates success.
  2088. func (as *AllocationSet) Insert(that *Allocation) error {
  2089. if as == nil {
  2090. return fmt.Errorf("cannot insert into nil AllocationSet")
  2091. }
  2092. if as.Allocations == nil {
  2093. as.Allocations = map[string]*Allocation{}
  2094. }
  2095. if as.ExternalKeys == nil {
  2096. as.ExternalKeys = map[string]bool{}
  2097. }
  2098. if as.IdleKeys == nil {
  2099. as.IdleKeys = map[string]bool{}
  2100. }
  2101. // Add the given Allocation to the existing entry, if there is one;
  2102. // otherwise just set directly into allocations
  2103. if _, ok := as.Allocations[that.Name]; !ok {
  2104. as.Allocations[that.Name] = that
  2105. } else {
  2106. as.Allocations[that.Name].add(that)
  2107. }
  2108. // If the given Allocation is an external one, record that
  2109. if that.IsExternal() {
  2110. as.ExternalKeys[that.Name] = true
  2111. }
  2112. // If the given Allocation is an idle one, record that
  2113. if that.IsIdle() {
  2114. as.IdleKeys[that.Name] = true
  2115. }
  2116. // Expand the window, just to be safe. It's possible that the Allocation will
  2117. // be set into the map without expanding it to the AllocationSet's window.
  2118. as.Allocations[that.Name].Window = as.Allocations[that.Name].Window.Expand(as.Window)
  2119. return nil
  2120. }
  2121. // IsEmpty returns true if the AllocationSet is nil, or if it contains
  2122. // zero allocations.
  2123. func (as *AllocationSet) IsEmpty() bool {
  2124. if as == nil || len(as.Allocations) == 0 {
  2125. return true
  2126. }
  2127. return false
  2128. }
  2129. // Length returns the number of Allocations in the set
  2130. func (as *AllocationSet) Length() int {
  2131. if as == nil {
  2132. return 0
  2133. }
  2134. return len(as.Allocations)
  2135. }
  2136. // ResetAdjustments sets all cost adjustment fields to zero
  2137. func (as *AllocationSet) ResetAdjustments() {
  2138. if as == nil {
  2139. return
  2140. }
  2141. for _, a := range as.Allocations {
  2142. a.ResetAdjustments()
  2143. }
  2144. }
  2145. // Resolution returns the AllocationSet's window duration
  2146. func (as *AllocationSet) Resolution() time.Duration {
  2147. return as.Window.Duration()
  2148. }
  2149. // GetWindow returns the AllocationSet's window
  2150. func (as *AllocationSet) GetWindow() Window {
  2151. return as.Window
  2152. }
  2153. // Set uses the given Allocation to overwrite the existing entry in the
  2154. // AllocationSet under the Allocation's name.
  2155. func (as *AllocationSet) Set(alloc *Allocation) error {
  2156. if as.IsEmpty() {
  2157. as.Allocations = map[string]*Allocation{}
  2158. as.ExternalKeys = map[string]bool{}
  2159. as.IdleKeys = map[string]bool{}
  2160. }
  2161. as.Allocations[alloc.Name] = alloc
  2162. // If the given Allocation is an external one, record that
  2163. if alloc.IsExternal() {
  2164. as.ExternalKeys[alloc.Name] = true
  2165. }
  2166. // If the given Allocation is an idle one, record that
  2167. if alloc.IsIdle() {
  2168. as.IdleKeys[alloc.Name] = true
  2169. }
  2170. return nil
  2171. }
  2172. // Start returns the Start time of the AllocationSet window
  2173. func (as *AllocationSet) Start() time.Time {
  2174. if as == nil {
  2175. log.Warnf("AllocationSet: calling Start on nil AllocationSet")
  2176. return time.Unix(0, 0)
  2177. }
  2178. if as.Window.Start() == nil {
  2179. log.Warnf("AllocationSet: AllocationSet with illegal window: Start is nil; len(as.allocations)=%d", len(as.Allocations))
  2180. return time.Unix(0, 0)
  2181. }
  2182. return *as.Window.Start()
  2183. }
  2184. // String represents the given Allocation as a string
  2185. func (as *AllocationSet) String() string {
  2186. if as == nil {
  2187. return "<nil>"
  2188. }
  2189. return fmt.Sprintf(
  2190. "AllocationSet{length: %d; window: %s; totalCost: %.2f}",
  2191. as.Length(),
  2192. as.Window,
  2193. as.TotalCost())
  2194. }
  2195. // TotalCost returns the sum of all TotalCosts of the allocations contained
  2196. func (as *AllocationSet) TotalCost() float64 {
  2197. if as.IsEmpty() {
  2198. return 0.0
  2199. }
  2200. tc := 0.0
  2201. for _, a := range as.Allocations {
  2202. tc += a.TotalCost()
  2203. }
  2204. return tc
  2205. }
  2206. // UTCOffset returns the AllocationSet's configured UTCOffset.
  2207. func (as *AllocationSet) UTCOffset() time.Duration {
  2208. _, zone := as.Start().Zone()
  2209. return time.Duration(zone) * time.Second
  2210. }
  2211. func (as *AllocationSet) Accumulate(that *AllocationSet) (*AllocationSet, error) {
  2212. if as.IsEmpty() {
  2213. return that.Clone(), nil
  2214. }
  2215. if that.IsEmpty() {
  2216. return as.Clone(), nil
  2217. }
  2218. // Set start, end to min(start), max(end)
  2219. start := as.Start()
  2220. end := as.End()
  2221. if that.Start().Before(start) {
  2222. start = that.Start()
  2223. }
  2224. if that.End().After(end) {
  2225. end = that.End()
  2226. }
  2227. acc := NewAllocationSet(start, end)
  2228. for _, alloc := range as.Allocations {
  2229. err := acc.Insert(alloc)
  2230. if err != nil {
  2231. return nil, err
  2232. }
  2233. }
  2234. for _, alloc := range that.Allocations {
  2235. err := acc.Insert(alloc)
  2236. if err != nil {
  2237. return nil, err
  2238. }
  2239. }
  2240. return acc, nil
  2241. }
  2242. // AllocationSetRange is a thread-safe slice of AllocationSets. It is meant to
  2243. // be used such that the AllocationSets held are consecutive and coherent with
  2244. // respect to using the same aggregation properties, UTC offset, and
  2245. // resolution. However these rules are not necessarily enforced, so use wisely.
  2246. type AllocationSetRange struct {
  2247. Allocations []*AllocationSet
  2248. FromStore string // stores the name of the store used to retrieve the data
  2249. }
  2250. // NewAllocationSetRange instantiates a new range composed of the given
  2251. // AllocationSets in the order provided.
  2252. func NewAllocationSetRange(allocs ...*AllocationSet) *AllocationSetRange {
  2253. return &AllocationSetRange{
  2254. Allocations: allocs,
  2255. }
  2256. }
  2257. // Get safely retrieves the AllocationSet at the given index of the range.
  2258. func (asr *AllocationSetRange) Get(i int) (*AllocationSet, error) {
  2259. if i < 0 || i >= len(asr.Allocations) {
  2260. return nil, fmt.Errorf("AllocationSetRange: index out of range: %d", i)
  2261. }
  2262. return asr.Allocations[i], nil
  2263. }
  2264. // Accumulate sums each AllocationSet in the given range, returning a single cumulative
  2265. // AllocationSet for the entire range.
  2266. func (asr *AllocationSetRange) accumulate() (*AllocationSet, error) {
  2267. var allocSet *AllocationSet
  2268. var err error
  2269. for _, as := range asr.Allocations {
  2270. allocSet, err = allocSet.Accumulate(as)
  2271. if err != nil {
  2272. return nil, err
  2273. }
  2274. }
  2275. return allocSet, nil
  2276. }
  2277. // newAccumulation clones the first available AllocationSet to use as the data structure to
  2278. // Accumulate the remaining data. This leaves the original AllocationSetRange intact.
  2279. func (asr *AllocationSetRange) newAccumulation() (*AllocationSet, error) {
  2280. // NOTE: Adding this API for consistency across SummaryAllocation and Assets, but this
  2281. // NOTE: implementation is almost identical to regular Accumulate(). The Accumulate() method
  2282. // NOTE: for Allocation returns Clone() of the input, which is required for Accumulate
  2283. // NOTE: support (unit tests are great for verifying this information).
  2284. var allocSet *AllocationSet
  2285. var err error
  2286. if asr == nil {
  2287. return nil, fmt.Errorf("nil AllocationSetRange in accumulation")
  2288. }
  2289. if len(asr.Allocations) == 0 {
  2290. return nil, fmt.Errorf("AllocationSetRange has empty AssetSet in accumulation")
  2291. }
  2292. for _, as := range asr.Allocations {
  2293. if allocSet == nil {
  2294. allocSet = as.Clone()
  2295. continue
  2296. }
  2297. var allocSetCopy *AllocationSet = nil
  2298. if as != nil {
  2299. allocSetCopy = as.Clone()
  2300. }
  2301. allocSet, err = allocSet.Accumulate(allocSetCopy)
  2302. if err != nil {
  2303. return nil, err
  2304. }
  2305. }
  2306. return allocSet, nil
  2307. }
  2308. // Accumulate sums AllocationSets based on the AccumulateOption (calendar week or calendar month).
  2309. // The accumulated set is determined by the start of the window of the allocation set.
  2310. func (asr *AllocationSetRange) Accumulate(accumulateBy AccumulateOption) (*AllocationSetRange, error) {
  2311. switch accumulateBy {
  2312. case AccumulateOptionNone:
  2313. return asr.accumulateByNone()
  2314. case AccumulateOptionAll:
  2315. return asr.accumulateByAll()
  2316. case AccumulateOptionHour:
  2317. return asr.accumulateByHour()
  2318. case AccumulateOptionDay:
  2319. return asr.accumulateByDay()
  2320. case AccumulateOptionWeek:
  2321. return asr.accumulateByWeek()
  2322. case AccumulateOptionMonth:
  2323. return asr.accumulateByMonth()
  2324. default:
  2325. // ideally, this should never happen
  2326. return nil, fmt.Errorf("unexpected error, invalid accumulateByType: %s", accumulateBy)
  2327. }
  2328. }
  2329. func (asr *AllocationSetRange) accumulateByAll() (*AllocationSetRange, error) {
  2330. var err error
  2331. var as *AllocationSet
  2332. as, err = asr.newAccumulation()
  2333. if err != nil {
  2334. return nil, fmt.Errorf("error accumulating all:%s", err)
  2335. }
  2336. accumulated := NewAllocationSetRange(as)
  2337. return accumulated, nil
  2338. }
  2339. func (asr *AllocationSetRange) accumulateByNone() (*AllocationSetRange, error) {
  2340. return asr.Clone(), nil
  2341. }
  2342. func (asr *AllocationSetRange) accumulateByHour() (*AllocationSetRange, error) {
  2343. // ensure that the summary allocation sets have a 1-hour window, if a set exists
  2344. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() != time.Hour {
  2345. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", asr.Allocations[0].Window.Duration())
  2346. }
  2347. return asr.Clone(), nil
  2348. }
  2349. func (asr *AllocationSetRange) accumulateByDay() (*AllocationSetRange, error) {
  2350. // if the allocation set window is 1-day, just return the existing allocation set range
  2351. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() == time.Hour*24 {
  2352. return asr, nil
  2353. }
  2354. var toAccumulate *AllocationSetRange
  2355. result := NewAllocationSetRange()
  2356. for i, as := range asr.Allocations {
  2357. if as.Window.Duration() != time.Hour {
  2358. return nil, fmt.Errorf("window duration must equal 1 hour; got:%s", as.Window.Duration())
  2359. }
  2360. hour := as.Window.Start().Hour()
  2361. if toAccumulate == nil {
  2362. toAccumulate = NewAllocationSetRange()
  2363. as = as.Clone()
  2364. }
  2365. toAccumulate.Append(as)
  2366. asAccumulated, err := toAccumulate.accumulate()
  2367. if err != nil {
  2368. return nil, fmt.Errorf("error accumulating result: %s", err)
  2369. }
  2370. toAccumulate = NewAllocationSetRange(asAccumulated)
  2371. if hour == 23 || i == len(asr.Allocations)-1 {
  2372. if length := len(toAccumulate.Allocations); length != 1 {
  2373. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2374. }
  2375. result.Append(toAccumulate.Allocations[0])
  2376. toAccumulate = nil
  2377. }
  2378. }
  2379. return result, nil
  2380. }
  2381. func (asr *AllocationSetRange) accumulateByMonth() (*AllocationSetRange, error) {
  2382. var toAccumulate *AllocationSetRange
  2383. result := NewAllocationSetRange()
  2384. for i, as := range asr.Allocations {
  2385. if as.Window.Duration() != time.Hour*24 {
  2386. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", as.Window.Duration())
  2387. }
  2388. _, month, _ := as.Window.Start().Date()
  2389. _, nextDayMonth, _ := as.Window.Start().Add(time.Hour * 24).Date()
  2390. if toAccumulate == nil {
  2391. toAccumulate = NewAllocationSetRange()
  2392. as = as.Clone()
  2393. }
  2394. toAccumulate.Append(as)
  2395. asAccumulated, err := toAccumulate.accumulate()
  2396. if err != nil {
  2397. return nil, fmt.Errorf("error accumulating result: %s", err)
  2398. }
  2399. toAccumulate = NewAllocationSetRange(asAccumulated)
  2400. // either the month has ended, or there are no more allocation sets
  2401. if month != nextDayMonth || i == len(asr.Allocations)-1 {
  2402. if length := len(toAccumulate.Allocations); length != 1 {
  2403. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2404. }
  2405. result.Append(toAccumulate.Allocations[0])
  2406. toAccumulate = nil
  2407. }
  2408. }
  2409. return result, nil
  2410. }
  2411. func (asr *AllocationSetRange) accumulateByWeek() (*AllocationSetRange, error) {
  2412. if len(asr.Allocations) > 0 && asr.Allocations[0].Window.Duration() == timeutil.Week {
  2413. return asr, nil
  2414. }
  2415. var toAccumulate *AllocationSetRange
  2416. result := NewAllocationSetRange()
  2417. for i, as := range asr.Allocations {
  2418. if as.Window.Duration() != time.Hour*24 {
  2419. return nil, fmt.Errorf("window duration must equal 24 hours; got:%s", as.Window.Duration())
  2420. }
  2421. dayOfWeek := as.Window.Start().Weekday()
  2422. if toAccumulate == nil {
  2423. toAccumulate = NewAllocationSetRange()
  2424. as = as.Clone()
  2425. }
  2426. toAccumulate.Append(as)
  2427. asAccumulated, err := toAccumulate.accumulate()
  2428. if err != nil {
  2429. return nil, fmt.Errorf("error accumulating result: %s", err)
  2430. }
  2431. toAccumulate = NewAllocationSetRange(asAccumulated)
  2432. // current assumption is the week always ends on Saturday, or there are no more allocation sets
  2433. if dayOfWeek == time.Saturday || i == len(asr.Allocations)-1 {
  2434. if length := len(toAccumulate.Allocations); length != 1 {
  2435. return nil, fmt.Errorf("failed accumulation, detected %d sets instead of 1", length)
  2436. }
  2437. result.Append(toAccumulate.Allocations[0])
  2438. toAccumulate = nil
  2439. }
  2440. }
  2441. return result, nil
  2442. }
  2443. // AggregateBy aggregates each AllocationSet in the range by the given
  2444. // properties and options.
  2445. func (asr *AllocationSetRange) AggregateBy(aggregateBy []string, options *AllocationAggregationOptions) error {
  2446. aggRange := &AllocationSetRange{Allocations: []*AllocationSet{}}
  2447. for _, as := range asr.Allocations {
  2448. err := as.AggregateBy(aggregateBy, options)
  2449. if err != nil {
  2450. return err
  2451. }
  2452. aggRange.Allocations = append(aggRange.Allocations, as)
  2453. }
  2454. asr.Allocations = aggRange.Allocations
  2455. return nil
  2456. }
  2457. // Append appends the given AllocationSet to the end of the range. It does not
  2458. // validate whether or not that violates window continuity.
  2459. func (asr *AllocationSetRange) Append(that *AllocationSet) {
  2460. asr.Allocations = append(asr.Allocations, that)
  2461. }
  2462. // InsertRange merges the given AllocationSetRange into the receiving one by
  2463. // lining up sets with matching windows, then inserting each allocation from
  2464. // the given ASR into the respective set in the receiving ASR. If the given
  2465. // ASR contains an AllocationSet from a window that does not exist in the
  2466. // receiving ASR, then an error is returned. However, the given ASR does not
  2467. // need to cover the full range of the receiver.
  2468. func (asr *AllocationSetRange) InsertRange(that *AllocationSetRange) error {
  2469. if asr == nil {
  2470. return fmt.Errorf("cannot insert range into nil AllocationSetRange")
  2471. }
  2472. // Providing an empty or nil set range is a no-op
  2473. if that == nil {
  2474. return nil
  2475. }
  2476. // keys maps window to index in asr
  2477. keys := map[string]int{}
  2478. for i, as := range asr.Allocations {
  2479. if as == nil {
  2480. continue
  2481. }
  2482. keys[as.Window.String()] = i
  2483. }
  2484. // Nothing to merge, so simply return
  2485. if len(keys) == 0 {
  2486. return nil
  2487. }
  2488. var err error
  2489. for _, thatAS := range that.Allocations {
  2490. if thatAS == nil || err != nil {
  2491. continue
  2492. }
  2493. // Find matching AllocationSet in asr
  2494. i, ok := keys[thatAS.Window.String()]
  2495. if !ok {
  2496. err = fmt.Errorf("cannot merge AllocationSet into window that does not exist: %s", thatAS.Window.String())
  2497. continue
  2498. }
  2499. as, err := asr.Get(i)
  2500. if err != nil {
  2501. err = fmt.Errorf("AllocationSetRange index does not exist: %d", i)
  2502. continue
  2503. }
  2504. // Insert each Allocation from the given set
  2505. for _, alloc := range thatAS.Allocations {
  2506. err = as.Insert(alloc)
  2507. if err != nil {
  2508. err = fmt.Errorf("error inserting allocation: %s", err)
  2509. continue
  2510. }
  2511. }
  2512. }
  2513. // err might be nil
  2514. return err
  2515. }
  2516. // Length returns the length of the range, which is zero if nil
  2517. func (asr *AllocationSetRange) Length() int {
  2518. if asr == nil || asr.Allocations == nil {
  2519. return 0
  2520. }
  2521. return len(asr.Allocations)
  2522. }
  2523. // Slice copies the underlying slice of AllocationSets, maintaining order,
  2524. // and returns the copied slice.
  2525. func (asr *AllocationSetRange) Slice() []*AllocationSet {
  2526. if asr == nil || asr.Allocations == nil {
  2527. return nil
  2528. }
  2529. copy := []*AllocationSet{}
  2530. for _, as := range asr.Allocations {
  2531. copy = append(copy, as.Clone())
  2532. }
  2533. return copy
  2534. }
  2535. // String represents the given AllocationSetRange as a string
  2536. func (asr *AllocationSetRange) String() string {
  2537. if asr == nil {
  2538. return "<nil>"
  2539. }
  2540. return fmt.Sprintf("AllocationSetRange{length: %d}", asr.Length())
  2541. }
  2542. // UTCOffset returns the detected UTCOffset of the AllocationSets within the
  2543. // range. Defaults to 0 if the range is nil or empty. Does not warn if there
  2544. // are sets with conflicting UTCOffsets (just returns the first).
  2545. func (asr *AllocationSetRange) UTCOffset() time.Duration {
  2546. if asr.Length() == 0 {
  2547. return 0
  2548. }
  2549. as, err := asr.Get(0)
  2550. if err != nil {
  2551. return 0
  2552. }
  2553. return as.UTCOffset()
  2554. }
  2555. // Window returns the full window that the AllocationSetRange spans, from the
  2556. // start of the first AllocationSet to the end of the last one.
  2557. func (asr *AllocationSetRange) Window() Window {
  2558. if asr == nil || asr.Length() == 0 {
  2559. return NewWindow(nil, nil)
  2560. }
  2561. start := asr.Allocations[0].Start()
  2562. end := asr.Allocations[asr.Length()-1].End()
  2563. return NewWindow(&start, &end)
  2564. }
  2565. // Start returns the earliest start of all Allocations in the AllocationSetRange.
  2566. // It returns an error if there are no allocations.
  2567. func (asr *AllocationSetRange) Start() (time.Time, error) {
  2568. start := time.Time{}
  2569. if asr == nil {
  2570. return start, fmt.Errorf("had no data to compute a start from")
  2571. }
  2572. firstStartNotSet := true
  2573. for _, as := range asr.Allocations {
  2574. for _, a := range as.Allocations {
  2575. if firstStartNotSet {
  2576. start = a.Start
  2577. firstStartNotSet = false
  2578. }
  2579. if a.Start.Before(start) {
  2580. start = a.Start
  2581. }
  2582. }
  2583. }
  2584. if firstStartNotSet {
  2585. return start, fmt.Errorf("had no data to compute a start from")
  2586. }
  2587. return start, nil
  2588. }
  2589. // End returns the latest end of all Allocations in the AllocationSetRange.
  2590. // It returns an error if there are no allocations.
  2591. func (asr *AllocationSetRange) End() (time.Time, error) {
  2592. end := time.Time{}
  2593. if asr == nil {
  2594. return end, fmt.Errorf("had no data to compute an end from")
  2595. }
  2596. firstEndNotSet := true
  2597. for _, as := range asr.Allocations {
  2598. for _, a := range as.Allocations {
  2599. if firstEndNotSet {
  2600. end = a.End
  2601. firstEndNotSet = false
  2602. }
  2603. if a.End.After(end) {
  2604. end = a.End
  2605. }
  2606. }
  2607. }
  2608. if firstEndNotSet {
  2609. return end, fmt.Errorf("had no data to compute an end from")
  2610. }
  2611. return end, nil
  2612. }
  2613. // StartAndEnd iterates over all AssetSets in the AssetSetRange and returns the earliest start and
  2614. // latest end over the entire range
  2615. func (asr *AllocationSetRange) StartAndEnd() (time.Time, time.Time, error) {
  2616. start := time.Time{}
  2617. end := time.Time{}
  2618. if asr == nil {
  2619. return start, end, fmt.Errorf("had no data to compute a start and end from")
  2620. }
  2621. firstStartNotSet := true
  2622. firstEndNotSet := true
  2623. for _, as := range asr.Allocations {
  2624. for _, a := range as.Allocations {
  2625. if firstStartNotSet {
  2626. start = a.Start
  2627. firstStartNotSet = false
  2628. }
  2629. if a.Start.Before(start) {
  2630. start = a.Start
  2631. }
  2632. if firstEndNotSet {
  2633. end = a.End
  2634. firstEndNotSet = false
  2635. }
  2636. if a.End.After(end) {
  2637. end = a.End
  2638. }
  2639. }
  2640. }
  2641. if firstStartNotSet {
  2642. return start, end, fmt.Errorf("had no data to compute a start from")
  2643. }
  2644. if firstEndNotSet {
  2645. return start, end, fmt.Errorf("had no data to compute an end from")
  2646. }
  2647. return start, end, nil
  2648. }
  2649. // Minutes returns the duration, in minutes, between the earliest start
  2650. // and the latest end of all assets in the AllocationSetRange.
  2651. func (asr *AllocationSetRange) Minutes() float64 {
  2652. if asr == nil {
  2653. return 0
  2654. }
  2655. start, end, err := asr.StartAndEnd()
  2656. if err != nil {
  2657. return 0
  2658. }
  2659. duration := end.Sub(start)
  2660. return duration.Minutes()
  2661. }
  2662. // TotalCost returns the sum of all TotalCosts of the allocations contained
  2663. func (asr *AllocationSetRange) TotalCost() float64 {
  2664. if asr == nil || len(asr.Allocations) == 0 {
  2665. return 0.0
  2666. }
  2667. tc := 0.0
  2668. for _, as := range asr.Allocations {
  2669. tc += as.TotalCost()
  2670. }
  2671. return tc
  2672. }
  2673. // Clone returns a new AllocationSetRange cloned from the existing ASR
  2674. func (asr *AllocationSetRange) Clone() *AllocationSetRange {
  2675. sasrClone := NewAllocationSetRange()
  2676. sasrClone.FromStore = asr.FromStore
  2677. for _, as := range asr.Allocations {
  2678. asClone := as.Clone()
  2679. sasrClone.Append(asClone)
  2680. }
  2681. return sasrClone
  2682. }