asset.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065
  1. package kubecost
  2. import (
  3. "bytes"
  4. "encoding"
  5. "encoding/json"
  6. "fmt"
  7. "math"
  8. "strings"
  9. "sync"
  10. "time"
  11. "github.com/kubecost/cost-model/pkg/log"
  12. )
  13. const timeFmt = "2006-01-02T15:04:05-0700"
  14. // UndefinedKey is used in composing Asset group keys if the group does not have that property defined.
  15. // E.g. if aggregating on Cluster, Assets in the AssetSet where Asset has no cluster will be grouped under key "__undefined__"
  16. const UndefinedKey = "__undefined__"
  17. // Asset defines an entity within a cluster that has a defined cost over a
  18. // given period of time.
  19. type Asset interface {
  20. // Type identifies the kind of Asset, which must always exist and should
  21. // be defined by the underlying type implementing the interface.
  22. Type() AssetType
  23. // Properties are a map of predefined traits, which may or may not exist,
  24. // but must conform to the AssetProperty schema
  25. Properties() *AssetProperties
  26. SetProperties(*AssetProperties)
  27. // Labels are a map of undefined string-to-string values
  28. Labels() AssetLabels
  29. SetLabels(AssetLabels)
  30. // Monetary values
  31. Adjustment() float64
  32. SetAdjustment(float64)
  33. TotalCost() float64
  34. // Temporal values
  35. Start() time.Time
  36. End() time.Time
  37. Minutes() float64
  38. Window() Window
  39. ExpandWindow(Window)
  40. SetStartEnd(time.Time, time.Time)
  41. // Operations and comparisons
  42. Add(Asset) Asset
  43. Clone() Asset
  44. Equal(Asset) bool
  45. // Representations
  46. encoding.BinaryMarshaler
  47. encoding.BinaryUnmarshaler
  48. json.Marshaler
  49. fmt.Stringer
  50. }
  51. // AssetToExternalAllocation converts the given asset to an Allocation, given
  52. // the properties to use to aggregate, and the mapping from Allocation property
  53. // to Asset label. For example, consider this asset:
  54. //
  55. // CURRENT: Asset ETL stores its data ALREADY MAPPED from label to k8s concept. This isn't ideal-- see the TOOD.
  56. // Cloud {
  57. // TotalCost: 10.00,
  58. // Labels{
  59. // "kubernetes_namespace":"monitoring",
  60. // "env":"prod"
  61. // }
  62. // }
  63. //
  64. // Given the following parameters, we expect to return:
  65. //
  66. // 1) single-prop full match
  67. // aggregateBy = ["namespace"]
  68. // => Allocation{Name: "monitoring", ExternalCost: 10.00, TotalCost: 10.00}, nil
  69. //
  70. // 2) multi-prop full match
  71. // aggregateBy = ["namespace", "label:env"]
  72. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  73. // => Allocation{Name: "monitoring/env=prod", ExternalCost: 10.00, TotalCost: 10.00}, nil
  74. //
  75. // 3) multi-prop partial match
  76. // aggregateBy = ["namespace", "label:foo"]
  77. // => Allocation{Name: "monitoring/__unallocated__", ExternalCost: 10.00, TotalCost: 10.00}, nil
  78. //
  79. // 4) no match
  80. // aggregateBy = ["cluster"]
  81. // => nil, err
  82. //
  83. // TODO:
  84. // Cloud {
  85. // TotalCost: 10.00,
  86. // Labels{
  87. // "kubernetes_namespace":"monitoring",
  88. // "env":"prod"
  89. // }
  90. // }
  91. //
  92. // Given the following parameters, we expect to return:
  93. //
  94. // 1) single-prop full match
  95. // aggregateBy = ["namespace"]
  96. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  97. // => Allocation{Name: "monitoring", ExternalCost: 10.00, TotalCost: 10.00}, nil
  98. //
  99. // 2) multi-prop full match
  100. // aggregateBy = ["namespace", "label:env"]
  101. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  102. // => Allocation{Name: "monitoring/env=prod", ExternalCost: 10.00, TotalCost: 10.00}, nil
  103. //
  104. // 3) multi-prop partial match
  105. // aggregateBy = ["namespace", "label:foo"]
  106. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  107. // => Allocation{Name: "monitoring/__unallocated__", ExternalCost: 10.00, TotalCost: 10.00}, nil
  108. //
  109. // 4) no match
  110. // aggregateBy = ["cluster"]
  111. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  112. // => nil, err
  113. //
  114. // (See asset_test.go for assertions of these examples and more.)
  115. func AssetToExternalAllocation(asset Asset, aggregateBy []string) (*Allocation, error) {
  116. if asset == nil {
  117. return nil, fmt.Errorf("asset is nil")
  118. }
  119. // names will collect the slash-separated names accrued by iterating over
  120. // aggregateBy and checking the relevant labels.
  121. names := []string{}
  122. // match records whether or not a match was found in the Asset labels,
  123. // such that is can genuinely be turned into an external Allocation.
  124. match := false
  125. // props records the relevant Properties to set on the resultant Allocation
  126. props := Properties{}
  127. for _, aggBy := range aggregateBy {
  128. // labelName should be derived from the mapping of properties to
  129. // label names, unless the aggBy is explicitly a label, in which
  130. // case we should pull the label name from the aggBy string.
  131. labelName := aggBy
  132. if strings.HasPrefix(aggBy, "label:") {
  133. labelName = strings.TrimPrefix(aggBy, "label:")
  134. }
  135. if labelName == "" {
  136. // No matching label has been defined in the cost-analyzer label config
  137. // relating to the given aggregateBy property.
  138. names = append(names, UnallocatedSuffix)
  139. continue
  140. }
  141. if value := asset.Labels()[labelName]; value != "" {
  142. // Valid label value was found for one of the aggregation properties,
  143. // so add it to the name.
  144. if strings.HasPrefix(aggBy, "label:") {
  145. // Use naming convention labelName=labelValue for labels
  146. // e.g. aggBy="label:env", value="prod" => "env=prod"
  147. names = append(names, fmt.Sprintf("%s=%s", strings.TrimPrefix(aggBy, "label:"), value))
  148. match = true
  149. // Set the corresponding label in props
  150. labels, err := props.GetLabels()
  151. if err != nil {
  152. labels = map[string]string{}
  153. }
  154. labels[labelName] = value
  155. props.SetLabels(labels)
  156. } else {
  157. names = append(names, value)
  158. match = true
  159. // Set the corresponding property on props
  160. switch aggBy {
  161. case ClusterProp.String():
  162. props.SetCluster(value)
  163. case NodeProp.String():
  164. props.SetNode(value)
  165. case NamespaceProp.String():
  166. props.SetNamespace(value)
  167. case ControllerKindProp.String():
  168. props.SetControllerKind(value)
  169. case ControllerProp.String():
  170. props.SetController(value)
  171. case PodProp.String():
  172. props.SetPod(value)
  173. case ContainerProp.String():
  174. props.SetContainer(value)
  175. case ServiceProp.String():
  176. // TODO: external allocation: how to do this? multi-service?
  177. props.SetServices([]string{value})
  178. }
  179. }
  180. } else {
  181. // No value label value was found on the Asset; consider it
  182. // unallocated. Note that this case is only truly relevant if at
  183. // least one other property matches (e.g. case 3 in the examples)
  184. // because if there are no matches, then an error is returned.
  185. names = append(names, UnallocatedSuffix)
  186. }
  187. }
  188. if !match {
  189. return nil, fmt.Errorf("asset does not qualify as an external allocation")
  190. }
  191. names = append(names, ExternalSuffix)
  192. // TODO: external allocation: efficiency?
  193. // TODO: external allocation: resource totals?
  194. return &Allocation{
  195. Name: strings.Join(names, "/"),
  196. Properties: props,
  197. ExternalCost: asset.TotalCost(),
  198. TotalCost: asset.TotalCost(),
  199. }, nil
  200. }
  201. // key is used to determine uniqueness of an Asset, for instance during Insert
  202. // to determine if two Assets should be combined. Passing `nil` `aggregateBy` indicates
  203. // that all available `AssetProperty` keys should be used. Passing empty `aggregateBy` indicates that
  204. // no key should be used (e.g. to aggregate all assets). Passing one or more `aggregateBy`
  205. // values will key by only those values.
  206. // Valid values of `aggregateBy` elements are strings which are an `AssetProperty`, and strings prefixed
  207. // with `"label:"`.
  208. func key(a Asset, aggregateBy []string) (string, error) {
  209. keys := []string{}
  210. if aggregateBy == nil {
  211. aggregateBy = []string{
  212. string(AssetProviderProp),
  213. string(AssetAccountProp),
  214. string(AssetProjectProp),
  215. string(AssetCategoryProp),
  216. string(AssetClusterProp),
  217. string(AssetTypeProp),
  218. string(AssetServiceProp),
  219. string(AssetProviderIDProp),
  220. string(AssetNameProp),
  221. }
  222. }
  223. for _, s := range aggregateBy {
  224. key := ""
  225. switch true {
  226. case s == string(AssetProviderProp):
  227. key = a.Properties().Provider
  228. case s == string(AssetAccountProp):
  229. key = a.Properties().Account
  230. case s == string(AssetProjectProp):
  231. key = a.Properties().Project
  232. case s == string(AssetClusterProp):
  233. key = a.Properties().Cluster
  234. case s == string(AssetCategoryProp):
  235. key = a.Properties().Category
  236. case s == string(AssetTypeProp):
  237. key = a.Type().String()
  238. case s == string(AssetServiceProp):
  239. key = a.Properties().Service
  240. case s == string(AssetProviderIDProp):
  241. key = a.Properties().ProviderID
  242. case s == string(AssetNameProp):
  243. key = a.Properties().Name
  244. case strings.HasPrefix(s, "label:"):
  245. if labelKey := strings.TrimPrefix(s, "label:"); labelKey != "" {
  246. labelVal := a.Labels()[labelKey]
  247. if labelVal == "" {
  248. key = "__undefined__"
  249. } else {
  250. key = fmt.Sprintf("%s=%s", labelKey, labelVal)
  251. }
  252. } else {
  253. // Don't allow aggregating on label ""
  254. return "", fmt.Errorf("Attempted to aggregate on invalid key: %s", s)
  255. }
  256. default:
  257. return "", fmt.Errorf("Attempted to aggregate on invalid key: %s", s)
  258. }
  259. if key != "" {
  260. keys = append(keys, key)
  261. } else {
  262. keys = append(keys, UndefinedKey)
  263. }
  264. }
  265. return strings.Join(keys, "/"), nil
  266. }
  267. func toString(a Asset) string {
  268. return fmt.Sprintf("%s{%s}%s=%.2f", a.Type().String(), a.Properties(), a.Window(), a.TotalCost())
  269. }
  270. // AssetLabels is a schema-free mapping of key/value pairs that can be
  271. // attributed to an Asset as a flexible a
  272. type AssetLabels map[string]string
  273. // Clone returns a cloned map of labels
  274. func (al AssetLabels) Clone() AssetLabels {
  275. clone := make(AssetLabels, len(al))
  276. for label, value := range al {
  277. clone[label] = value
  278. }
  279. return clone
  280. }
  281. // Equal returns true only if the two set of labels are exact matches
  282. func (al AssetLabels) Equal(that AssetLabels) bool {
  283. if len(al) != len(that) {
  284. return false
  285. }
  286. for label, value := range al {
  287. if thatValue, ok := that[label]; !ok || thatValue != value {
  288. return false
  289. }
  290. }
  291. return true
  292. }
  293. // Merge retains only the labels shared with the given AssetLabels
  294. func (al AssetLabels) Merge(that AssetLabels) AssetLabels {
  295. result := AssetLabels{}
  296. for label, value := range al {
  297. if thatValue, ok := that[label]; ok && thatValue == value {
  298. result[label] = value
  299. }
  300. }
  301. return result
  302. }
  303. // AssetMatchFunc is a function that can be used to match Assets by
  304. // returning true for any given Asset if a condition is met.
  305. type AssetMatchFunc func(Asset) bool
  306. // AssetType identifies a type of Asset
  307. type AssetType int
  308. const (
  309. // AnyAssetType describes the Any AssetType
  310. AnyAssetType AssetType = iota
  311. // CloudAssetType describes the Cloud AssetType
  312. CloudAssetType
  313. // ClusterManagementAssetType describes the ClusterManagement AssetType
  314. ClusterManagementAssetType
  315. // DiskAssetType describes the Disk AssetType
  316. DiskAssetType
  317. // LoadBalancerAssetType describes the LoadBalancer AssetType
  318. LoadBalancerAssetType
  319. // NetworkAssetType describes the Network AssetType
  320. NetworkAssetType
  321. // NodeAssetType describes the Node AssetType
  322. NodeAssetType
  323. // SharedAssetType describes the Shared AssetType
  324. SharedAssetType
  325. )
  326. // ParseAssetType attempts to parse the given string into an AssetType
  327. func ParseAssetType(text string) (AssetType, error) {
  328. switch strings.TrimSpace(strings.ToLower(text)) {
  329. case "cloud":
  330. return CloudAssetType, nil
  331. case "clustermanagement":
  332. return ClusterManagementAssetType, nil
  333. case "disk":
  334. return DiskAssetType, nil
  335. case "loadbalancer":
  336. return LoadBalancerAssetType, nil
  337. case "network":
  338. return NetworkAssetType, nil
  339. case "node":
  340. return NodeAssetType, nil
  341. case "shared":
  342. return SharedAssetType, nil
  343. }
  344. return AnyAssetType, fmt.Errorf("invalid asset type: %s", text)
  345. }
  346. // String converts the given AssetType to a string
  347. func (at AssetType) String() string {
  348. return [...]string{
  349. "Asset",
  350. "Cloud",
  351. "ClusterManagement",
  352. "Disk",
  353. "LoadBalancer",
  354. "Network",
  355. "Node",
  356. "Shared",
  357. }[at]
  358. }
  359. // Any is the most general Asset, which is usually created as a result of
  360. // adding two Assets of different types.
  361. type Any struct {
  362. labels AssetLabels
  363. properties *AssetProperties
  364. start time.Time
  365. end time.Time
  366. window Window
  367. adjustment float64
  368. Cost float64
  369. }
  370. // NewAsset creates a new Any-type Asset for the given period of time
  371. func NewAsset(start, end time.Time, window Window) *Any {
  372. return &Any{
  373. labels: AssetLabels{},
  374. properties: &AssetProperties{},
  375. start: start,
  376. end: end,
  377. window: window.Clone(),
  378. }
  379. }
  380. // Type returns the Asset's type
  381. func (a *Any) Type() AssetType {
  382. return AnyAssetType
  383. }
  384. // Properties returns the Asset's properties
  385. func (a *Any) Properties() *AssetProperties {
  386. return a.properties
  387. }
  388. // SetProperties sets the Asset's properties
  389. func (a *Any) SetProperties(props *AssetProperties) {
  390. a.properties = props
  391. }
  392. // Labels returns the Asset's labels
  393. func (a *Any) Labels() AssetLabels {
  394. return a.labels
  395. }
  396. // SetLabels sets the Asset's labels
  397. func (a *Any) SetLabels(labels AssetLabels) {
  398. a.labels = labels
  399. }
  400. // Adjustment returns the Asset's cost adjustment
  401. func (a *Any) Adjustment() float64 {
  402. return a.adjustment
  403. }
  404. // SetAdjustment sets the Asset's cost adjustment
  405. func (a *Any) SetAdjustment(adj float64) {
  406. a.adjustment = adj
  407. }
  408. // TotalCost returns the Asset's TotalCost
  409. func (a *Any) TotalCost() float64 {
  410. return a.Cost + a.adjustment
  411. }
  412. // Start returns the Asset's start time within the window
  413. func (a *Any) Start() time.Time {
  414. return a.start
  415. }
  416. // End returns the Asset's end time within the window
  417. func (a *Any) End() time.Time {
  418. return a.end
  419. }
  420. // Minutes returns the number of minutes the Asset was active within the window
  421. func (a *Any) Minutes() float64 {
  422. return a.End().Sub(a.Start()).Minutes()
  423. }
  424. // Window returns the Asset's window
  425. func (a *Any) Window() Window {
  426. return a.window
  427. }
  428. // ExpandWindow expands the Asset's window by the given window
  429. func (a *Any) ExpandWindow(window Window) {
  430. a.window = a.window.Expand(window)
  431. }
  432. // SetStartEnd sets the Asset's Start and End fields
  433. func (a *Any) SetStartEnd(start, end time.Time) {
  434. if a.Window().Contains(start) {
  435. a.start = start
  436. } else {
  437. log.Warningf("Any.SetStartEnd: start %s not in %s", start, a.Window())
  438. }
  439. if a.Window().Contains(end) {
  440. a.end = end
  441. } else {
  442. log.Warningf("Any.SetStartEnd: end %s not in %s", end, a.Window())
  443. }
  444. }
  445. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  446. // as much relevant information as possible (i.e. type, properties, labels).
  447. func (a *Any) Add(that Asset) Asset {
  448. this := a.Clone().(*Any)
  449. props := a.Properties().Merge(that.Properties())
  450. labels := a.Labels().Merge(that.Labels())
  451. start := a.Start()
  452. if that.Start().Before(start) {
  453. start = that.Start()
  454. }
  455. end := a.End()
  456. if that.End().After(end) {
  457. end = that.End()
  458. }
  459. window := a.Window().Expand(that.Window())
  460. this.start = start
  461. this.end = end
  462. this.window = window
  463. this.SetProperties(props)
  464. this.SetLabels(labels)
  465. this.adjustment += that.Adjustment()
  466. this.Cost += (that.TotalCost() - that.Adjustment())
  467. return this
  468. }
  469. // Clone returns a cloned instance of the Asset
  470. func (a *Any) Clone() Asset {
  471. return &Any{
  472. labels: a.labels.Clone(),
  473. properties: a.properties.Clone(),
  474. start: a.start,
  475. end: a.end,
  476. window: a.window.Clone(),
  477. adjustment: a.adjustment,
  478. Cost: a.Cost,
  479. }
  480. }
  481. // Equal returns true if the given Asset is an exact match of the receiver
  482. func (a *Any) Equal(that Asset) bool {
  483. t, ok := that.(*Any)
  484. if !ok {
  485. return false
  486. }
  487. if !a.Labels().Equal(that.Labels()) {
  488. return false
  489. }
  490. if !a.Properties().Equal(that.Properties()) {
  491. return false
  492. }
  493. if !a.start.Equal(t.start) {
  494. return false
  495. }
  496. if !a.end.Equal(t.end) {
  497. return false
  498. }
  499. if !a.window.Equal(t.window) {
  500. return false
  501. }
  502. if a.Cost != t.Cost {
  503. return false
  504. }
  505. return true
  506. }
  507. // MarshalJSON implements json.Marshaler
  508. func (a *Any) MarshalJSON() ([]byte, error) {
  509. buffer := bytes.NewBufferString("{")
  510. jsonEncode(buffer, "properties", a.Properties(), ",")
  511. jsonEncode(buffer, "labels", a.Labels(), ",")
  512. jsonEncodeString(buffer, "window", a.Window().String(), ",")
  513. jsonEncodeString(buffer, "start", a.Start().Format(timeFmt), ",")
  514. jsonEncodeString(buffer, "end", a.End().Format(timeFmt), ",")
  515. jsonEncodeFloat64(buffer, "minutes", a.Minutes(), ",")
  516. jsonEncodeFloat64(buffer, "adjustment", a.Adjustment(), ",")
  517. jsonEncodeFloat64(buffer, "totalCost", a.TotalCost(), "")
  518. buffer.WriteString("}")
  519. return buffer.Bytes(), nil
  520. }
  521. // String implements fmt.Stringer
  522. func (a *Any) String() string {
  523. return toString(a)
  524. }
  525. // Cloud describes a cloud asset
  526. type Cloud struct {
  527. labels AssetLabels
  528. properties *AssetProperties
  529. start time.Time
  530. end time.Time
  531. window Window
  532. adjustment float64
  533. Cost float64
  534. Credit float64 // Credit is a negative value representing dollars credited back to a given line-item
  535. }
  536. // NewCloud returns a new Cloud Asset
  537. func NewCloud(category, providerID string, start, end time.Time, window Window) *Cloud {
  538. properties := &AssetProperties{
  539. Category: category,
  540. ProviderID: providerID,
  541. }
  542. return &Cloud{
  543. labels: AssetLabels{},
  544. properties: properties,
  545. start: start,
  546. end: end,
  547. window: window.Clone(),
  548. }
  549. }
  550. // Type returns the AssetType
  551. func (ca *Cloud) Type() AssetType {
  552. return CloudAssetType
  553. }
  554. // Properties returns the AssetProperties
  555. func (ca *Cloud) Properties() *AssetProperties {
  556. return ca.properties
  557. }
  558. // SetProperties sets the Asset's properties
  559. func (ca *Cloud) SetProperties(props *AssetProperties) {
  560. ca.properties = props
  561. }
  562. // Labels returns the AssetLabels
  563. func (ca *Cloud) Labels() AssetLabels {
  564. return ca.labels
  565. }
  566. // SetLabels sets the Asset's labels
  567. func (ca *Cloud) SetLabels(labels AssetLabels) {
  568. ca.labels = labels
  569. }
  570. // Adjustment returns the Asset's adjustment value
  571. func (ca *Cloud) Adjustment() float64 {
  572. return ca.adjustment
  573. }
  574. // SetAdjustment sets the Asset's adjustment value
  575. func (ca *Cloud) SetAdjustment(adj float64) {
  576. ca.adjustment = adj
  577. }
  578. // TotalCost returns the Asset's total cost
  579. func (ca *Cloud) TotalCost() float64 {
  580. return ca.Cost + ca.adjustment + ca.Credit
  581. }
  582. // Start returns the Asset's precise start time within the window
  583. func (ca *Cloud) Start() time.Time {
  584. return ca.start
  585. }
  586. // End returns the Asset's precise end time within the window
  587. func (ca *Cloud) End() time.Time {
  588. return ca.end
  589. }
  590. // Minutes returns the number of Minutes the Asset ran
  591. func (ca *Cloud) Minutes() float64 {
  592. return ca.End().Sub(ca.Start()).Minutes()
  593. }
  594. // Window returns the window within which the Asset ran
  595. func (ca *Cloud) Window() Window {
  596. return ca.window
  597. }
  598. // ExpandWindow expands the Asset's window by the given window
  599. func (ca *Cloud) ExpandWindow(window Window) {
  600. ca.window = ca.window.Expand(window)
  601. }
  602. // SetStartEnd sets the Asset's Start and End fields
  603. func (ca *Cloud) SetStartEnd(start, end time.Time) {
  604. if ca.Window().Contains(start) {
  605. ca.start = start
  606. } else {
  607. log.Warningf("Cloud.SetStartEnd: start %s not in %s", start, ca.Window())
  608. }
  609. if ca.Window().Contains(end) {
  610. ca.end = end
  611. } else {
  612. log.Warningf("Cloud.SetStartEnd: end %s not in %s", end, ca.Window())
  613. }
  614. }
  615. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  616. // as much relevant information as possible (i.e. type, properties, labels).
  617. func (ca *Cloud) Add(a Asset) Asset {
  618. // Cloud + Cloud = Cloud
  619. if that, ok := a.(*Cloud); ok {
  620. this := ca.Clone().(*Cloud)
  621. this.add(that)
  622. return this
  623. }
  624. props := ca.Properties().Merge(a.Properties())
  625. labels := ca.Labels().Merge(a.Labels())
  626. start := ca.Start()
  627. if a.Start().Before(start) {
  628. start = a.Start()
  629. }
  630. end := ca.End()
  631. if a.End().After(end) {
  632. end = a.End()
  633. }
  634. window := ca.Window().Expand(a.Window())
  635. // Cloud + !Cloud = Any
  636. any := NewAsset(start, end, window)
  637. any.SetProperties(props)
  638. any.SetLabels(labels)
  639. any.adjustment = ca.Adjustment() + a.Adjustment()
  640. any.Cost = (ca.TotalCost() - ca.Adjustment() - ca.Credit) + (a.TotalCost() - a.Adjustment() - ca.Credit)
  641. return any
  642. }
  643. func (ca *Cloud) add(that *Cloud) {
  644. if ca == nil {
  645. ca = that
  646. return
  647. }
  648. props := ca.Properties().Merge(that.Properties())
  649. labels := ca.Labels().Merge(that.Labels())
  650. start := ca.Start()
  651. if that.Start().Before(start) {
  652. start = that.Start()
  653. }
  654. end := ca.End()
  655. if that.End().After(end) {
  656. end = that.End()
  657. }
  658. window := ca.Window().Expand(that.Window())
  659. ca.start = start
  660. ca.end = end
  661. ca.window = window
  662. ca.SetProperties(props)
  663. ca.SetLabels(labels)
  664. ca.adjustment += that.adjustment
  665. ca.Cost += that.Cost
  666. ca.Credit += that.Credit
  667. }
  668. // Clone returns a cloned instance of the Asset
  669. func (ca *Cloud) Clone() Asset {
  670. return &Cloud{
  671. labels: ca.labels.Clone(),
  672. properties: ca.properties.Clone(),
  673. start: ca.start,
  674. end: ca.end,
  675. window: ca.window.Clone(),
  676. adjustment: ca.adjustment,
  677. Cost: ca.Cost,
  678. Credit: ca.Credit,
  679. }
  680. }
  681. // Equal returns true if the given Asset precisely equals the Asset
  682. func (ca *Cloud) Equal(a Asset) bool {
  683. that, ok := a.(*Cloud)
  684. if !ok {
  685. return false
  686. }
  687. if !ca.Labels().Equal(that.Labels()) {
  688. return false
  689. }
  690. if !ca.Properties().Equal(that.Properties()) {
  691. return false
  692. }
  693. if !ca.start.Equal(that.start) {
  694. return false
  695. }
  696. if !ca.end.Equal(that.end) {
  697. return false
  698. }
  699. if !ca.window.Equal(that.window) {
  700. return false
  701. }
  702. if ca.adjustment != that.adjustment {
  703. return false
  704. }
  705. if ca.Cost != that.Cost {
  706. return false
  707. }
  708. if ca.Credit != that.Credit {
  709. return false
  710. }
  711. return true
  712. }
  713. // MarshalJSON implements json.Marshaler
  714. func (ca *Cloud) MarshalJSON() ([]byte, error) {
  715. buffer := bytes.NewBufferString("{")
  716. jsonEncodeString(buffer, "type", ca.Type().String(), ",")
  717. jsonEncode(buffer, "properties", ca.Properties(), ",")
  718. jsonEncode(buffer, "labels", ca.Labels(), ",")
  719. jsonEncodeString(buffer, "window", ca.Window().String(), ",")
  720. jsonEncodeString(buffer, "start", ca.Start().Format(timeFmt), ",")
  721. jsonEncodeString(buffer, "end", ca.End().Format(timeFmt), ",")
  722. jsonEncodeFloat64(buffer, "minutes", ca.Minutes(), ",")
  723. jsonEncodeFloat64(buffer, "adjustment", ca.Adjustment(), ",")
  724. jsonEncodeFloat64(buffer, "credit", ca.Credit, ",")
  725. jsonEncodeFloat64(buffer, "totalCost", ca.TotalCost(), "")
  726. buffer.WriteString("}")
  727. return buffer.Bytes(), nil
  728. }
  729. // String implements fmt.Stringer
  730. func (ca *Cloud) String() string {
  731. return toString(ca)
  732. }
  733. // ClusterManagement describes a provider's cluster management fee
  734. type ClusterManagement struct {
  735. labels AssetLabels
  736. properties *AssetProperties
  737. window Window
  738. Cost float64
  739. }
  740. // NewClusterManagement creates and returns a new ClusterManagement instance
  741. func NewClusterManagement(provider, cluster string, window Window) *ClusterManagement {
  742. properties := &AssetProperties{
  743. Category: ManagementCategory,
  744. Provider: ParseProvider(provider),
  745. Cluster: cluster,
  746. Service: KubernetesService,
  747. }
  748. return &ClusterManagement{
  749. labels: AssetLabels{},
  750. properties: properties,
  751. window: window.Clone(),
  752. }
  753. }
  754. // Type returns the Asset's type
  755. func (cm *ClusterManagement) Type() AssetType {
  756. return ClusterManagementAssetType
  757. }
  758. // Properties returns the Asset's properties
  759. func (cm *ClusterManagement) Properties() *AssetProperties {
  760. return cm.properties
  761. }
  762. // SetProperties sets the Asset's properties
  763. func (cm *ClusterManagement) SetProperties(props *AssetProperties) {
  764. cm.properties = props
  765. }
  766. // Labels returns the Asset's labels
  767. func (cm *ClusterManagement) Labels() AssetLabels {
  768. return cm.labels
  769. }
  770. // SetLabels sets the Asset's properties
  771. func (cm *ClusterManagement) SetLabels(props AssetLabels) {
  772. cm.labels = props
  773. }
  774. // Adjustment does not apply to ClusterManagement
  775. func (cm *ClusterManagement) Adjustment() float64 {
  776. return 0.0
  777. }
  778. // SetAdjustment does not apply to ClusterManagement
  779. func (cm *ClusterManagement) SetAdjustment(float64) {
  780. return
  781. }
  782. // TotalCost returns the Asset's total cost
  783. func (cm *ClusterManagement) TotalCost() float64 {
  784. return cm.Cost
  785. }
  786. // Start returns the Asset's precise start time within the window
  787. func (cm *ClusterManagement) Start() time.Time {
  788. return *cm.window.Start()
  789. }
  790. // End returns the Asset's precise end time within the window
  791. func (cm *ClusterManagement) End() time.Time {
  792. return *cm.window.End()
  793. }
  794. // Minutes returns the number of minutes the Asset ran
  795. func (cm *ClusterManagement) Minutes() float64 {
  796. return cm.Window().Minutes()
  797. }
  798. // Window return the Asset's window
  799. func (cm *ClusterManagement) Window() Window {
  800. return cm.window
  801. }
  802. // ExpandWindow expands the Asset's window by the given window
  803. func (cm *ClusterManagement) ExpandWindow(window Window) {
  804. cm.window = cm.window.Expand(window)
  805. }
  806. // SetStartEnd sets the Asset's Start and End fields (not applicable here)
  807. func (cm *ClusterManagement) SetStartEnd(start, end time.Time) {
  808. return
  809. }
  810. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  811. // as much relevant information as possible (i.e. type, properties, labels).
  812. func (cm *ClusterManagement) Add(a Asset) Asset {
  813. // ClusterManagement + ClusterManagement = ClusterManagement
  814. if that, ok := a.(*ClusterManagement); ok {
  815. this := cm.Clone().(*ClusterManagement)
  816. this.add(that)
  817. return this
  818. }
  819. props := cm.Properties().Merge(a.Properties())
  820. labels := cm.Labels().Merge(a.Labels())
  821. start := cm.Start()
  822. if a.Start().Before(start) {
  823. start = a.Start()
  824. }
  825. end := cm.End()
  826. if a.End().After(end) {
  827. end = a.End()
  828. }
  829. window := cm.Window().Expand(a.Window())
  830. // ClusterManagement + !ClusterManagement = Any
  831. any := NewAsset(start, end, window)
  832. any.SetProperties(props)
  833. any.SetLabels(labels)
  834. any.adjustment = cm.Adjustment() + a.Adjustment()
  835. any.Cost = (cm.TotalCost() - cm.Adjustment()) + (a.TotalCost() - a.Adjustment())
  836. return any
  837. }
  838. func (cm *ClusterManagement) add(that *ClusterManagement) {
  839. if cm == nil {
  840. cm = that
  841. return
  842. }
  843. props := cm.Properties().Merge(that.Properties())
  844. labels := cm.Labels().Merge(that.Labels())
  845. window := cm.Window().Expand(that.Window())
  846. cm.window = window
  847. cm.SetProperties(props)
  848. cm.SetLabels(labels)
  849. cm.Cost += that.Cost
  850. }
  851. // Clone returns a cloned instance of the Asset
  852. func (cm *ClusterManagement) Clone() Asset {
  853. return &ClusterManagement{
  854. labels: cm.labels.Clone(),
  855. properties: cm.properties.Clone(),
  856. window: cm.window.Clone(),
  857. Cost: cm.Cost,
  858. }
  859. }
  860. // Equal returns true if the given Asset exactly matches the Asset
  861. func (cm *ClusterManagement) Equal(a Asset) bool {
  862. that, ok := a.(*ClusterManagement)
  863. if !ok {
  864. return false
  865. }
  866. if !cm.Labels().Equal(that.Labels()) {
  867. return false
  868. }
  869. if !cm.Properties().Equal(that.Properties()) {
  870. return false
  871. }
  872. if !cm.window.Equal(that.window) {
  873. return false
  874. }
  875. if cm.Cost != that.Cost {
  876. return false
  877. }
  878. return true
  879. }
  880. // MarshalJSON implements json.Marshler
  881. func (cm *ClusterManagement) MarshalJSON() ([]byte, error) {
  882. buffer := bytes.NewBufferString("{")
  883. jsonEncodeString(buffer, "type", cm.Type().String(), ",")
  884. jsonEncode(buffer, "properties", cm.Properties(), ",")
  885. jsonEncode(buffer, "labels", cm.Labels(), ",")
  886. jsonEncodeString(buffer, "window", cm.Window().String(), ",")
  887. jsonEncodeString(buffer, "start", cm.Start().Format(timeFmt), ",")
  888. jsonEncodeString(buffer, "end", cm.End().Format(timeFmt), ",")
  889. jsonEncodeFloat64(buffer, "minutes", cm.Minutes(), ",")
  890. jsonEncodeFloat64(buffer, "totalCost", cm.TotalCost(), "")
  891. buffer.WriteString("}")
  892. return buffer.Bytes(), nil
  893. }
  894. // String implements fmt.Stringer
  895. func (cm *ClusterManagement) String() string {
  896. return toString(cm)
  897. }
  898. // Disk represents an in-cluster disk Asset
  899. type Disk struct {
  900. labels AssetLabels
  901. properties *AssetProperties
  902. start time.Time
  903. end time.Time
  904. window Window
  905. adjustment float64
  906. Cost float64
  907. ByteHours float64
  908. Local float64
  909. Breakdown *Breakdown
  910. }
  911. // NewDisk creates and returns a new Disk Asset
  912. func NewDisk(name, cluster, providerID string, start, end time.Time, window Window) *Disk {
  913. properties := &AssetProperties{
  914. Category: StorageCategory,
  915. Name: name,
  916. Cluster: cluster,
  917. ProviderID: providerID,
  918. Service: KubernetesService,
  919. }
  920. return &Disk{
  921. labels: AssetLabels{},
  922. properties: properties,
  923. start: start,
  924. end: end,
  925. window: window,
  926. Breakdown: &Breakdown{},
  927. }
  928. }
  929. // Type returns the AssetType of the Asset
  930. func (d *Disk) Type() AssetType {
  931. return DiskAssetType
  932. }
  933. // Properties returns the Asset's properties
  934. func (d *Disk) Properties() *AssetProperties {
  935. return d.properties
  936. }
  937. // SetProperties sets the Asset's properties
  938. func (d *Disk) SetProperties(props *AssetProperties) {
  939. d.properties = props
  940. }
  941. // Labels returns the Asset's labels
  942. func (d *Disk) Labels() AssetLabels {
  943. return d.labels
  944. }
  945. // SetLabels sets the Asset's labels
  946. func (d *Disk) SetLabels(labels AssetLabels) {
  947. d.labels = labels
  948. }
  949. // Adjustment returns the Asset's cost adjustment
  950. func (d *Disk) Adjustment() float64 {
  951. return d.adjustment
  952. }
  953. // SetAdjustment sets the Asset's cost adjustment
  954. func (d *Disk) SetAdjustment(adj float64) {
  955. d.adjustment = adj
  956. }
  957. // TotalCost returns the Asset's total cost
  958. func (d *Disk) TotalCost() float64 {
  959. return d.Cost + d.adjustment
  960. }
  961. // Start returns the precise start time of the Asset within the window
  962. func (d *Disk) Start() time.Time {
  963. return d.start
  964. }
  965. // End returns the precise start time of the Asset within the window
  966. func (d *Disk) End() time.Time {
  967. return d.end
  968. }
  969. // Minutes returns the number of minutes the Asset ran
  970. func (d *Disk) Minutes() float64 {
  971. diskMins := d.end.Sub(d.start).Minutes()
  972. windowMins := d.window.Minutes()
  973. if diskMins > windowMins {
  974. log.Warningf("Asset ETL: Disk.Minutes exceeds window: %.2f > %.2f", diskMins, windowMins)
  975. diskMins = windowMins
  976. }
  977. if diskMins < 0 {
  978. diskMins = 0
  979. }
  980. return diskMins
  981. }
  982. // Window returns the window within which the Asset
  983. func (d *Disk) Window() Window {
  984. return d.window
  985. }
  986. // ExpandWindow expands the Asset's window by the given window
  987. func (d *Disk) ExpandWindow(window Window) {
  988. d.window = d.window.Expand(window)
  989. }
  990. // SetStartEnd sets the Asset's Start and End fields
  991. func (d *Disk) SetStartEnd(start, end time.Time) {
  992. if d.Window().Contains(start) {
  993. d.start = start
  994. } else {
  995. log.Warningf("Disk.SetStartEnd: start %s not in %s", start, d.Window())
  996. }
  997. if d.Window().Contains(end) {
  998. d.end = end
  999. } else {
  1000. log.Warningf("Disk.SetStartEnd: end %s not in %s", end, d.Window())
  1001. }
  1002. }
  1003. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  1004. // as much relevant information as possible (i.e. type, properties, labels).
  1005. func (d *Disk) Add(a Asset) Asset {
  1006. // Disk + Disk = Disk
  1007. if that, ok := a.(*Disk); ok {
  1008. this := d.Clone().(*Disk)
  1009. this.add(that)
  1010. return this
  1011. }
  1012. props := d.Properties().Merge(a.Properties())
  1013. labels := d.Labels().Merge(a.Labels())
  1014. start := d.Start()
  1015. if a.Start().Before(start) {
  1016. start = a.Start()
  1017. }
  1018. end := d.End()
  1019. if a.End().After(end) {
  1020. end = a.End()
  1021. }
  1022. window := d.Window().Expand(a.Window())
  1023. // Disk + !Disk = Any
  1024. any := NewAsset(start, end, window)
  1025. any.SetProperties(props)
  1026. any.SetLabels(labels)
  1027. any.adjustment = d.Adjustment() + a.Adjustment()
  1028. any.Cost = (d.TotalCost() - d.Adjustment()) + (a.TotalCost() - a.Adjustment())
  1029. return any
  1030. }
  1031. func (d *Disk) add(that *Disk) {
  1032. if d == nil {
  1033. d = that
  1034. return
  1035. }
  1036. props := d.Properties().Merge(that.Properties())
  1037. labels := d.Labels().Merge(that.Labels())
  1038. d.SetProperties(props)
  1039. d.SetLabels(labels)
  1040. start := d.Start()
  1041. if that.Start().Before(start) {
  1042. start = that.Start()
  1043. }
  1044. end := d.End()
  1045. if that.End().After(end) {
  1046. end = that.End()
  1047. }
  1048. window := d.Window().Expand(that.Window())
  1049. d.start = start
  1050. d.end = end
  1051. d.window = window
  1052. totalCost := d.Cost + that.Cost
  1053. if totalCost > 0.0 {
  1054. d.Breakdown.Idle = (d.Breakdown.Idle*d.Cost + that.Breakdown.Idle*that.Cost) / totalCost
  1055. d.Breakdown.Other = (d.Breakdown.Other*d.Cost + that.Breakdown.Other*that.Cost) / totalCost
  1056. d.Breakdown.System = (d.Breakdown.System*d.Cost + that.Breakdown.System*that.Cost) / totalCost
  1057. d.Breakdown.User = (d.Breakdown.User*d.Cost + that.Breakdown.User*that.Cost) / totalCost
  1058. d.Local = (d.TotalCost()*d.Local + that.TotalCost()*that.Local) / (d.TotalCost() + that.TotalCost())
  1059. } else {
  1060. d.Local = (d.Local + that.Local) / 2.0
  1061. }
  1062. d.adjustment += that.adjustment
  1063. d.Cost += that.Cost
  1064. d.ByteHours += that.ByteHours
  1065. }
  1066. // Clone returns a cloned instance of the Asset
  1067. func (d *Disk) Clone() Asset {
  1068. return &Disk{
  1069. properties: d.properties.Clone(),
  1070. labels: d.labels.Clone(),
  1071. start: d.start,
  1072. end: d.end,
  1073. window: d.window.Clone(),
  1074. adjustment: d.adjustment,
  1075. Cost: d.Cost,
  1076. ByteHours: d.ByteHours,
  1077. Local: d.Local,
  1078. Breakdown: d.Breakdown.Clone(),
  1079. }
  1080. }
  1081. // Equal returns true if the two Assets match exactly
  1082. func (d *Disk) Equal(a Asset) bool {
  1083. that, ok := a.(*Disk)
  1084. if !ok {
  1085. return false
  1086. }
  1087. if !d.Labels().Equal(that.Labels()) {
  1088. return false
  1089. }
  1090. if !d.Properties().Equal(that.Properties()) {
  1091. return false
  1092. }
  1093. if !d.Start().Equal(that.Start()) {
  1094. return false
  1095. }
  1096. if !d.End().Equal(that.End()) {
  1097. return false
  1098. }
  1099. if !d.window.Equal(that.window) {
  1100. return false
  1101. }
  1102. if d.adjustment != that.adjustment {
  1103. return false
  1104. }
  1105. if d.Cost != that.Cost {
  1106. return false
  1107. }
  1108. if d.ByteHours != that.ByteHours {
  1109. return false
  1110. }
  1111. if d.Local != that.Local {
  1112. return false
  1113. }
  1114. if !d.Breakdown.Equal(that.Breakdown) {
  1115. return false
  1116. }
  1117. return true
  1118. }
  1119. // MarshalJSON implements the json.Marshaler interface
  1120. func (d *Disk) MarshalJSON() ([]byte, error) {
  1121. buffer := bytes.NewBufferString("{")
  1122. jsonEncodeString(buffer, "type", d.Type().String(), ",")
  1123. jsonEncode(buffer, "properties", d.Properties(), ",")
  1124. jsonEncode(buffer, "labels", d.Labels(), ",")
  1125. jsonEncodeString(buffer, "window", d.Window().String(), ",")
  1126. jsonEncodeString(buffer, "start", d.Start().Format(timeFmt), ",")
  1127. jsonEncodeString(buffer, "end", d.End().Format(timeFmt), ",")
  1128. jsonEncodeFloat64(buffer, "minutes", d.Minutes(), ",")
  1129. jsonEncodeFloat64(buffer, "byteHours", d.ByteHours, ",")
  1130. jsonEncodeFloat64(buffer, "bytes", d.Bytes(), ",")
  1131. jsonEncode(buffer, "breakdown", d.Breakdown, ",")
  1132. jsonEncodeFloat64(buffer, "adjustment", d.Adjustment(), ",")
  1133. jsonEncodeFloat64(buffer, "totalCost", d.TotalCost(), "")
  1134. buffer.WriteString("}")
  1135. return buffer.Bytes(), nil
  1136. }
  1137. // String implements fmt.Stringer
  1138. func (d *Disk) String() string {
  1139. return toString(d)
  1140. }
  1141. // Bytes returns the number of bytes belonging to the disk. This could be
  1142. // fractional because it's the number of byte*hours divided by the number of
  1143. // hours running; e.g. the sum of a 100GiB disk running for the first 10 hours
  1144. // and a 30GiB disk running for the last 20 hours of the same 24-hour window
  1145. // would produce:
  1146. // (100*10 + 30*20) / 24 = 66.667GiB
  1147. // However, any number of disks running for the full span of a window will
  1148. // report the actual number of bytes of the static disk; e.g. the above
  1149. // scenario for one entire 24-hour window:
  1150. // (100*24 + 30*24) / 24 = (100 + 30) = 130GiB
  1151. func (d *Disk) Bytes() float64 {
  1152. // [b*hr]*([min/hr]*[1/min]) = [b*hr]/[hr] = b
  1153. return d.ByteHours * (60.0 / d.Minutes())
  1154. }
  1155. // Breakdown describes a resource's use as a percentage of various usage types
  1156. type Breakdown struct {
  1157. Idle float64 `json:"idle"`
  1158. Other float64 `json:"other"`
  1159. System float64 `json:"system"`
  1160. User float64 `json:"user"`
  1161. }
  1162. // Clone returns a cloned instance of the Breakdown
  1163. func (b *Breakdown) Clone() *Breakdown {
  1164. if b == nil {
  1165. return nil
  1166. }
  1167. return &Breakdown{
  1168. Idle: b.Idle,
  1169. Other: b.Other,
  1170. System: b.System,
  1171. User: b.User,
  1172. }
  1173. }
  1174. // Equal returns true if the two Breakdowns are exact matches
  1175. func (b *Breakdown) Equal(that *Breakdown) bool {
  1176. if b == nil || that == nil {
  1177. return false
  1178. }
  1179. if b.Idle != that.Idle {
  1180. return false
  1181. }
  1182. if b.Other != that.Other {
  1183. return false
  1184. }
  1185. if b.System != that.System {
  1186. return false
  1187. }
  1188. if b.User != that.User {
  1189. return false
  1190. }
  1191. return true
  1192. }
  1193. // Network is an Asset representing a single node's network costs
  1194. type Network struct {
  1195. properties *AssetProperties
  1196. labels AssetLabels
  1197. start time.Time
  1198. end time.Time
  1199. window Window
  1200. adjustment float64
  1201. Cost float64
  1202. }
  1203. // NewNetwork creates and returns a new Network Asset
  1204. func NewNetwork(name, cluster, providerID string, start, end time.Time, window Window) *Network {
  1205. properties := &AssetProperties{
  1206. Category: NetworkCategory,
  1207. Name: name,
  1208. Cluster: cluster,
  1209. ProviderID: providerID,
  1210. Service: KubernetesService,
  1211. }
  1212. return &Network{
  1213. properties: properties,
  1214. labels: AssetLabels{},
  1215. start: start,
  1216. end: end,
  1217. window: window.Clone(),
  1218. }
  1219. }
  1220. // Type returns the AssetType of the Asset
  1221. func (n *Network) Type() AssetType {
  1222. return NetworkAssetType
  1223. }
  1224. // Properties returns the Asset's properties
  1225. func (n *Network) Properties() *AssetProperties {
  1226. return n.properties
  1227. }
  1228. // SetProperties sets the Asset's properties
  1229. func (n *Network) SetProperties(props *AssetProperties) {
  1230. n.properties = props
  1231. }
  1232. // Labels returns the Asset's labels
  1233. func (n *Network) Labels() AssetLabels {
  1234. return n.labels
  1235. }
  1236. // SetLabels sets the Asset's labels
  1237. func (n *Network) SetLabels(labels AssetLabels) {
  1238. n.labels = labels
  1239. }
  1240. // Adjustment returns the Asset's cost adjustment
  1241. func (n *Network) Adjustment() float64 {
  1242. return n.adjustment
  1243. }
  1244. // SetAdjustment sets the Asset's cost adjustment
  1245. func (n *Network) SetAdjustment(adj float64) {
  1246. n.adjustment = adj
  1247. }
  1248. // TotalCost returns the Asset's total cost
  1249. func (n *Network) TotalCost() float64 {
  1250. return n.Cost + n.adjustment
  1251. }
  1252. // Start returns the precise start time of the Asset within the window
  1253. func (n *Network) Start() time.Time {
  1254. return n.start
  1255. }
  1256. // End returns the precise end time of the Asset within the window
  1257. func (n *Network) End() time.Time {
  1258. return n.end
  1259. }
  1260. // Minutes returns the number of minutes the Asset ran within the window
  1261. func (n *Network) Minutes() float64 {
  1262. netMins := n.end.Sub(n.start).Minutes()
  1263. windowMins := n.window.Minutes()
  1264. if netMins > windowMins {
  1265. log.Warningf("Asset ETL: Network.Minutes exceeds window: %.2f > %.2f", netMins, windowMins)
  1266. netMins = windowMins
  1267. }
  1268. if netMins < 0 {
  1269. netMins = 0
  1270. }
  1271. return netMins
  1272. }
  1273. // Window returns the window within which the Asset ran
  1274. func (n *Network) Window() Window {
  1275. return n.window
  1276. }
  1277. // ExpandWindow expands the Asset's window by the given window
  1278. func (n *Network) ExpandWindow(window Window) {
  1279. n.window = n.window.Expand(window)
  1280. }
  1281. // SetStartEnd sets the Asset's Start and End fields
  1282. func (n *Network) SetStartEnd(start, end time.Time) {
  1283. if n.Window().Contains(start) {
  1284. n.start = start
  1285. } else {
  1286. log.Warningf("Disk.SetStartEnd: start %s not in %s", start, n.Window())
  1287. }
  1288. if n.Window().Contains(end) {
  1289. n.end = end
  1290. } else {
  1291. log.Warningf("Disk.SetStartEnd: end %s not in %s", end, n.Window())
  1292. }
  1293. }
  1294. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  1295. // as much relevant information as possible (i.e. type, properties, labels).
  1296. func (n *Network) Add(a Asset) Asset {
  1297. // Network + Network = Network
  1298. if that, ok := a.(*Network); ok {
  1299. this := n.Clone().(*Network)
  1300. this.add(that)
  1301. return this
  1302. }
  1303. props := n.Properties().Merge(a.Properties())
  1304. labels := n.Labels().Merge(a.Labels())
  1305. start := n.Start()
  1306. if a.Start().Before(start) {
  1307. start = a.Start()
  1308. }
  1309. end := n.End()
  1310. if a.End().After(end) {
  1311. end = a.End()
  1312. }
  1313. window := n.Window().Expand(a.Window())
  1314. // Network + !Network = Any
  1315. any := NewAsset(start, end, window)
  1316. any.SetProperties(props)
  1317. any.SetLabels(labels)
  1318. any.adjustment = n.Adjustment() + a.Adjustment()
  1319. any.Cost = (n.TotalCost() - n.Adjustment()) + (a.TotalCost() - a.Adjustment())
  1320. return any
  1321. }
  1322. func (n *Network) add(that *Network) {
  1323. if n == nil {
  1324. n = that
  1325. return
  1326. }
  1327. props := n.Properties().Merge(that.Properties())
  1328. labels := n.Labels().Merge(that.Labels())
  1329. n.SetProperties(props)
  1330. n.SetLabels(labels)
  1331. start := n.Start()
  1332. if that.Start().Before(start) {
  1333. start = that.Start()
  1334. }
  1335. end := n.End()
  1336. if that.End().After(end) {
  1337. end = that.End()
  1338. }
  1339. window := n.Window().Expand(that.Window())
  1340. n.start = start
  1341. n.end = end
  1342. n.window = window
  1343. n.Cost += that.Cost
  1344. n.adjustment += that.adjustment
  1345. }
  1346. // Clone returns a deep copy of the given Network
  1347. func (n *Network) Clone() Asset {
  1348. if n == nil {
  1349. return nil
  1350. }
  1351. return &Network{
  1352. properties: n.properties.Clone(),
  1353. labels: n.labels.Clone(),
  1354. start: n.start,
  1355. end: n.end,
  1356. window: n.window.Clone(),
  1357. adjustment: n.adjustment,
  1358. Cost: n.Cost,
  1359. }
  1360. }
  1361. // Equal returns true if the tow Assets match exactly
  1362. func (n *Network) Equal(a Asset) bool {
  1363. that, ok := a.(*Network)
  1364. if !ok {
  1365. return false
  1366. }
  1367. if !n.Labels().Equal(that.Labels()) {
  1368. return false
  1369. }
  1370. if !n.Properties().Equal(that.Properties()) {
  1371. return false
  1372. }
  1373. if !n.Start().Equal(that.Start()) {
  1374. return false
  1375. }
  1376. if !n.End().Equal(that.End()) {
  1377. return false
  1378. }
  1379. if !n.window.Equal(that.window) {
  1380. return false
  1381. }
  1382. if n.adjustment != that.adjustment {
  1383. return false
  1384. }
  1385. if n.Cost != that.Cost {
  1386. return false
  1387. }
  1388. return true
  1389. }
  1390. // MarshalJSON implements json.Marshal interface
  1391. func (n *Network) MarshalJSON() ([]byte, error) {
  1392. buffer := bytes.NewBufferString("{")
  1393. jsonEncodeString(buffer, "type", n.Type().String(), ",")
  1394. jsonEncode(buffer, "properties", n.Properties(), ",")
  1395. jsonEncode(buffer, "labels", n.Labels(), ",")
  1396. jsonEncodeString(buffer, "window", n.Window().String(), ",")
  1397. jsonEncodeString(buffer, "start", n.Start().Format(timeFmt), ",")
  1398. jsonEncodeString(buffer, "end", n.End().Format(timeFmt), ",")
  1399. jsonEncodeFloat64(buffer, "minutes", n.Minutes(), ",")
  1400. jsonEncodeFloat64(buffer, "adjustment", n.Adjustment(), ",")
  1401. jsonEncodeFloat64(buffer, "totalCost", n.TotalCost(), "")
  1402. buffer.WriteString("}")
  1403. return buffer.Bytes(), nil
  1404. }
  1405. // String implements fmt.Stringer
  1406. func (n *Network) String() string {
  1407. return toString(n)
  1408. }
  1409. // Node is an Asset representing a single node in a cluster
  1410. type Node struct {
  1411. properties *AssetProperties
  1412. labels AssetLabels
  1413. start time.Time
  1414. end time.Time
  1415. window Window
  1416. adjustment float64
  1417. NodeType string
  1418. CPUCoreHours float64
  1419. RAMByteHours float64
  1420. CPUBreakdown *Breakdown
  1421. RAMBreakdown *Breakdown
  1422. CPUCost float64
  1423. GPUCost float64
  1424. RAMCost float64
  1425. Discount float64
  1426. Preemptible float64
  1427. }
  1428. // NewNode creates and returns a new Node Asset
  1429. func NewNode(name, cluster, providerID string, start, end time.Time, window Window) *Node {
  1430. properties := &AssetProperties{
  1431. Category: ComputeCategory,
  1432. Name: name,
  1433. Cluster: cluster,
  1434. ProviderID: providerID,
  1435. Service: KubernetesService,
  1436. }
  1437. return &Node{
  1438. properties: properties,
  1439. labels: AssetLabels{},
  1440. start: start,
  1441. end: end,
  1442. window: window.Clone(),
  1443. CPUBreakdown: &Breakdown{},
  1444. RAMBreakdown: &Breakdown{},
  1445. }
  1446. }
  1447. // Type returns the AssetType of the Asset
  1448. func (n *Node) Type() AssetType {
  1449. return NodeAssetType
  1450. }
  1451. // Properties returns the Asset's properties
  1452. func (n *Node) Properties() *AssetProperties {
  1453. return n.properties
  1454. }
  1455. // SetProperties sets the Asset's properties
  1456. func (n *Node) SetProperties(props *AssetProperties) {
  1457. n.properties = props
  1458. }
  1459. // Labels returns the Asset's labels
  1460. func (n *Node) Labels() AssetLabels {
  1461. return n.labels
  1462. }
  1463. // SetLabels sets the Asset's labels
  1464. func (n *Node) SetLabels(labels AssetLabels) {
  1465. n.labels = labels
  1466. }
  1467. // Adjustment returns the Asset's cost adjustment
  1468. func (n *Node) Adjustment() float64 {
  1469. return n.adjustment
  1470. }
  1471. // SetAdjustment sets the Asset's cost adjustment
  1472. func (n *Node) SetAdjustment(adj float64) {
  1473. n.adjustment = adj
  1474. }
  1475. // TotalCost returns the Asset's total cost
  1476. func (n *Node) TotalCost() float64 {
  1477. return ((n.CPUCost + n.RAMCost) * (1.0 - n.Discount)) + n.GPUCost + n.adjustment
  1478. }
  1479. // Start returns the precise start time of the Asset within the window
  1480. func (n *Node) Start() time.Time {
  1481. return n.start
  1482. }
  1483. // End returns the precise end time of the Asset within the window
  1484. func (n *Node) End() time.Time {
  1485. return n.end
  1486. }
  1487. // Minutes returns the number of minutes the Asset ran within the window
  1488. func (n *Node) Minutes() float64 {
  1489. nodeMins := n.end.Sub(n.start).Minutes()
  1490. windowMins := n.window.Minutes()
  1491. if nodeMins > windowMins {
  1492. log.Warningf("Asset ETL: Node.Minutes exceeds window: %.2f > %.2f", nodeMins, windowMins)
  1493. nodeMins = windowMins
  1494. }
  1495. if nodeMins < 0 {
  1496. nodeMins = 0
  1497. }
  1498. return nodeMins
  1499. }
  1500. // Window returns the window within which the Asset ran
  1501. func (n *Node) Window() Window {
  1502. return n.window
  1503. }
  1504. // ExpandWindow expands the Asset's window by the given window
  1505. func (n *Node) ExpandWindow(window Window) {
  1506. n.window = n.window.Expand(window)
  1507. }
  1508. // SetStartEnd sets the Asset's Start and End fields
  1509. func (n *Node) SetStartEnd(start, end time.Time) {
  1510. if n.Window().Contains(start) {
  1511. n.start = start
  1512. } else {
  1513. log.Warningf("Disk.SetStartEnd: start %s not in %s", start, n.Window())
  1514. }
  1515. if n.Window().Contains(end) {
  1516. n.end = end
  1517. } else {
  1518. log.Warningf("Disk.SetStartEnd: end %s not in %s", end, n.Window())
  1519. }
  1520. }
  1521. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  1522. // as much relevant information as possible (i.e. type, properties, labels).
  1523. func (n *Node) Add(a Asset) Asset {
  1524. // Node + Node = Node
  1525. if that, ok := a.(*Node); ok {
  1526. this := n.Clone().(*Node)
  1527. this.add(that)
  1528. return this
  1529. }
  1530. props := n.Properties().Merge(a.Properties())
  1531. labels := n.Labels().Merge(a.Labels())
  1532. start := n.Start()
  1533. if a.Start().Before(start) {
  1534. start = a.Start()
  1535. }
  1536. end := n.End()
  1537. if a.End().After(end) {
  1538. end = a.End()
  1539. }
  1540. window := n.Window().Expand(a.Window())
  1541. // Node + !Node = Any
  1542. any := NewAsset(start, end, window)
  1543. any.SetProperties(props)
  1544. any.SetLabels(labels)
  1545. any.adjustment = n.Adjustment() + a.Adjustment()
  1546. any.Cost = (n.TotalCost() - n.Adjustment()) + (a.TotalCost() - a.Adjustment())
  1547. return any
  1548. }
  1549. func (n *Node) add(that *Node) {
  1550. if n == nil {
  1551. n = that
  1552. return
  1553. }
  1554. props := n.Properties().Merge(that.Properties())
  1555. labels := n.Labels().Merge(that.Labels())
  1556. n.SetProperties(props)
  1557. n.SetLabels(labels)
  1558. if n.NodeType != that.NodeType {
  1559. n.NodeType = ""
  1560. }
  1561. start := n.Start()
  1562. if that.Start().Before(start) {
  1563. start = that.Start()
  1564. }
  1565. end := n.End()
  1566. if that.End().After(end) {
  1567. end = that.End()
  1568. }
  1569. window := n.Window().Expand(that.Window())
  1570. n.start = start
  1571. n.end = end
  1572. n.window = window
  1573. // Order of operations for node costs is:
  1574. // Discount(CPU + RAM) + GPU + Adjustment
  1575. // Combining discounts, then involves weighting each discount by each
  1576. // respective (CPU + RAM) cost. Combining preemptible, on the other
  1577. // hand, is done with all three (but not Adjustment, which can change
  1578. // without triggering a re-computation of Preemtible).
  1579. disc := (n.CPUCost+n.RAMCost)*(1.0-n.Discount) + (that.CPUCost+that.RAMCost)*(1.0-that.Discount)
  1580. nonDisc := (n.CPUCost + n.RAMCost) + (that.CPUCost + that.RAMCost)
  1581. if nonDisc > 0 {
  1582. n.Discount = 1.0 - (disc / nonDisc)
  1583. } else {
  1584. n.Discount = (n.Discount + that.Discount) / 2.0
  1585. }
  1586. nNoAdj := n.TotalCost() - n.Adjustment()
  1587. thatNoAdj := that.TotalCost() - that.Adjustment()
  1588. if (nNoAdj + thatNoAdj) > 0 {
  1589. n.Preemptible = (nNoAdj*n.Preemptible + thatNoAdj*that.Preemptible) / (nNoAdj + thatNoAdj)
  1590. } else {
  1591. n.Preemptible = (n.Preemptible + that.Preemptible) / 2.0
  1592. }
  1593. totalCPUCost := n.CPUCost + that.CPUCost
  1594. if totalCPUCost > 0.0 {
  1595. n.CPUBreakdown.Idle = (n.CPUBreakdown.Idle*n.CPUCost + that.CPUBreakdown.Idle*that.CPUCost) / totalCPUCost
  1596. n.CPUBreakdown.Other = (n.CPUBreakdown.Other*n.CPUCost + that.CPUBreakdown.Other*that.CPUCost) / totalCPUCost
  1597. n.CPUBreakdown.System = (n.CPUBreakdown.System*n.CPUCost + that.CPUBreakdown.System*that.CPUCost) / totalCPUCost
  1598. n.CPUBreakdown.User = (n.CPUBreakdown.User*n.CPUCost + that.CPUBreakdown.User*that.CPUCost) / totalCPUCost
  1599. }
  1600. totalRAMCost := n.RAMCost + that.RAMCost
  1601. if totalRAMCost > 0.0 {
  1602. n.RAMBreakdown.Idle = (n.RAMBreakdown.Idle*n.RAMCost + that.RAMBreakdown.Idle*that.RAMCost) / totalRAMCost
  1603. n.RAMBreakdown.Other = (n.RAMBreakdown.Other*n.RAMCost + that.RAMBreakdown.Other*that.RAMCost) / totalRAMCost
  1604. n.RAMBreakdown.System = (n.RAMBreakdown.System*n.RAMCost + that.RAMBreakdown.System*that.RAMCost) / totalRAMCost
  1605. n.RAMBreakdown.User = (n.RAMBreakdown.User*n.RAMCost + that.RAMBreakdown.User*that.RAMCost) / totalRAMCost
  1606. }
  1607. n.CPUCoreHours += that.CPUCoreHours
  1608. n.RAMByteHours += that.RAMByteHours
  1609. n.CPUCost += that.CPUCost
  1610. n.GPUCost += that.GPUCost
  1611. n.RAMCost += that.RAMCost
  1612. n.adjustment += that.adjustment
  1613. }
  1614. // Clone returns a deep copy of the given Node
  1615. func (n *Node) Clone() Asset {
  1616. if n == nil {
  1617. return nil
  1618. }
  1619. return &Node{
  1620. properties: n.properties.Clone(),
  1621. labels: n.labels.Clone(),
  1622. start: n.start,
  1623. end: n.end,
  1624. window: n.window.Clone(),
  1625. adjustment: n.adjustment,
  1626. NodeType: n.NodeType,
  1627. CPUCoreHours: n.CPUCoreHours,
  1628. RAMByteHours: n.RAMByteHours,
  1629. CPUBreakdown: n.CPUBreakdown.Clone(),
  1630. RAMBreakdown: n.RAMBreakdown.Clone(),
  1631. CPUCost: n.CPUCost,
  1632. GPUCost: n.GPUCost,
  1633. RAMCost: n.RAMCost,
  1634. Preemptible: n.Preemptible,
  1635. Discount: n.Discount,
  1636. }
  1637. }
  1638. // Equal returns true if the tow Assets match exactly
  1639. func (n *Node) Equal(a Asset) bool {
  1640. that, ok := a.(*Node)
  1641. if !ok {
  1642. return false
  1643. }
  1644. if !n.Labels().Equal(that.Labels()) {
  1645. return false
  1646. }
  1647. if !n.Properties().Equal(that.Properties()) {
  1648. return false
  1649. }
  1650. if !n.Start().Equal(that.Start()) {
  1651. return false
  1652. }
  1653. if !n.End().Equal(that.End()) {
  1654. return false
  1655. }
  1656. if !n.window.Equal(that.window) {
  1657. return false
  1658. }
  1659. if n.adjustment != that.adjustment {
  1660. return false
  1661. }
  1662. if n.NodeType != that.NodeType {
  1663. return false
  1664. }
  1665. if n.CPUCoreHours != that.CPUCoreHours {
  1666. return false
  1667. }
  1668. if n.RAMByteHours != that.RAMByteHours {
  1669. return false
  1670. }
  1671. if !n.CPUBreakdown.Equal(that.CPUBreakdown) {
  1672. return false
  1673. }
  1674. if !n.RAMBreakdown.Equal(that.RAMBreakdown) {
  1675. return false
  1676. }
  1677. if n.CPUCost != that.CPUCost {
  1678. return false
  1679. }
  1680. if n.GPUCost != that.GPUCost {
  1681. return false
  1682. }
  1683. if n.RAMCost != that.RAMCost {
  1684. return false
  1685. }
  1686. if n.Discount != that.Discount {
  1687. return false
  1688. }
  1689. if n.Preemptible != that.Preemptible {
  1690. return false
  1691. }
  1692. return true
  1693. }
  1694. // MarshalJSON implements json.Marshal interface
  1695. func (n *Node) MarshalJSON() ([]byte, error) {
  1696. buffer := bytes.NewBufferString("{")
  1697. jsonEncodeString(buffer, "type", n.Type().String(), ",")
  1698. jsonEncode(buffer, "properties", n.Properties(), ",")
  1699. jsonEncode(buffer, "labels", n.Labels(), ",")
  1700. jsonEncodeString(buffer, "window", n.Window().String(), ",")
  1701. jsonEncodeString(buffer, "start", n.Start().Format(timeFmt), ",")
  1702. jsonEncodeString(buffer, "end", n.End().Format(timeFmt), ",")
  1703. jsonEncodeFloat64(buffer, "minutes", n.Minutes(), ",")
  1704. jsonEncodeString(buffer, "nodeType", n.NodeType, ",")
  1705. jsonEncodeFloat64(buffer, "cpuCores", n.CPUCores(), ",")
  1706. jsonEncodeFloat64(buffer, "ramBytes", n.RAMBytes(), ",")
  1707. jsonEncodeFloat64(buffer, "cpuCoreHours", n.CPUCoreHours, ",")
  1708. jsonEncodeFloat64(buffer, "ramByteHours", n.RAMByteHours, ",")
  1709. jsonEncode(buffer, "cpuBreakdown", n.CPUBreakdown, ",")
  1710. jsonEncode(buffer, "ramBreakdown", n.RAMBreakdown, ",")
  1711. jsonEncodeFloat64(buffer, "preemptible", n.Preemptible, ",")
  1712. jsonEncodeFloat64(buffer, "discount", n.Discount, ",")
  1713. jsonEncodeFloat64(buffer, "cpuCost", n.CPUCost, ",")
  1714. jsonEncodeFloat64(buffer, "gpuCost", n.GPUCost, ",")
  1715. jsonEncodeFloat64(buffer, "ramCost", n.RAMCost, ",")
  1716. jsonEncodeFloat64(buffer, "adjustment", n.Adjustment(), ",")
  1717. jsonEncodeFloat64(buffer, "totalCost", n.TotalCost(), "")
  1718. buffer.WriteString("}")
  1719. return buffer.Bytes(), nil
  1720. }
  1721. // String implements fmt.Stringer
  1722. func (n *Node) String() string {
  1723. return toString(n)
  1724. }
  1725. // IsPreemptible returns true if the node is 100% preemptible. It's possible
  1726. // to be "partially preemptible" by adding a preemptible node with a
  1727. // non-preemptible node.
  1728. func (n *Node) IsPreemptible() bool {
  1729. return n.Preemptible == 1.0
  1730. }
  1731. // CPUCores returns the number of cores belonging to the node. This could be
  1732. // fractional because it's the number of core*hours divided by the number of
  1733. // hours running; e.g. the sum of a 4-core node running for the first 10 hours
  1734. // and a 3-core node running for the last 20 hours of the same 24-hour window
  1735. // would produce:
  1736. // (4*10 + 3*20) / 24 = 4.167 cores
  1737. // However, any number of cores running for the full span of a window will
  1738. // report the actual number of cores of the static node; e.g. the above
  1739. // scenario for one entire 24-hour window:
  1740. // (4*24 + 3*24) / 24 = (4 + 3) = 7 cores
  1741. func (n *Node) CPUCores() float64 {
  1742. // [core*hr]*([min/hr]*[1/min]) = [core*hr]/[hr] = core
  1743. return n.CPUCoreHours * (60.0 / n.Minutes())
  1744. }
  1745. // RAMBytes returns the amount of RAM belonging to the node. This could be
  1746. // fractional because it's the number of byte*hours divided by the number of
  1747. // hours running; e.g. the sum of a 12GiB-RAM node running for the first 10 hours
  1748. // and a 16GiB-RAM node running for the last 20 hours of the same 24-hour window
  1749. // would produce:
  1750. // (12*10 + 16*20) / 24 = 18.333GiB RAM
  1751. // However, any number of cores running for the full span of a window will
  1752. // report the actual number of cores of the static node; e.g. the above
  1753. // scenario for one entire 24-hour window:
  1754. // (12*24 + 16*24) / 24 = (12 + 16) = 28 cores
  1755. func (n *Node) RAMBytes() float64 {
  1756. // [b*hr]*([min/hr]*[1/min]) = [b*hr]/[hr] = b
  1757. return n.RAMByteHours * (60.0 / n.Minutes())
  1758. }
  1759. // LoadBalancer is an Asset representing a single load balancer in a cluster
  1760. // TODO: add GB of ingress processed, numForwardingRules once we start recording those to prometheus metric
  1761. type LoadBalancer struct {
  1762. properties *AssetProperties
  1763. labels AssetLabels
  1764. start time.Time
  1765. end time.Time
  1766. window Window
  1767. adjustment float64
  1768. Cost float64
  1769. }
  1770. // NewLoadBalancer instantiates and returns a new LoadBalancer
  1771. func NewLoadBalancer(name, cluster, providerID string, start, end time.Time, window Window) *LoadBalancer {
  1772. properties := &AssetProperties{
  1773. Category: NetworkCategory,
  1774. Name: name,
  1775. Cluster: cluster,
  1776. ProviderID: providerID,
  1777. Service: KubernetesService,
  1778. }
  1779. return &LoadBalancer{
  1780. properties: properties,
  1781. labels: AssetLabels{},
  1782. start: start,
  1783. end: end,
  1784. window: window,
  1785. }
  1786. }
  1787. // Type returns the AssetType of the Asset
  1788. func (lb *LoadBalancer) Type() AssetType {
  1789. return LoadBalancerAssetType
  1790. }
  1791. // Properties returns the Asset's properties
  1792. func (lb *LoadBalancer) Properties() *AssetProperties {
  1793. return lb.properties
  1794. }
  1795. // SetProperties sets the Asset's properties
  1796. func (lb *LoadBalancer) SetProperties(props *AssetProperties) {
  1797. lb.properties = props
  1798. }
  1799. // Labels returns the Asset's labels
  1800. func (lb *LoadBalancer) Labels() AssetLabels {
  1801. return lb.labels
  1802. }
  1803. // SetLabels sets the Asset's labels
  1804. func (lb *LoadBalancer) SetLabels(labels AssetLabels) {
  1805. lb.labels = labels
  1806. }
  1807. // Adjustment returns the Asset's cost adjustment
  1808. func (lb *LoadBalancer) Adjustment() float64 {
  1809. return lb.adjustment
  1810. }
  1811. // SetAdjustment sets the Asset's cost adjustment
  1812. func (lb *LoadBalancer) SetAdjustment(adj float64) {
  1813. lb.adjustment = adj
  1814. }
  1815. // TotalCost returns the total cost of the Asset
  1816. func (lb *LoadBalancer) TotalCost() float64 {
  1817. return lb.Cost + lb.adjustment
  1818. }
  1819. // Start returns the preceise start point of the Asset within the window
  1820. func (lb *LoadBalancer) Start() time.Time {
  1821. return lb.start
  1822. }
  1823. // End returns the preceise end point of the Asset within the window
  1824. func (lb *LoadBalancer) End() time.Time {
  1825. return lb.end
  1826. }
  1827. // Minutes returns the number of minutes the Asset ran within the window
  1828. func (lb *LoadBalancer) Minutes() float64 {
  1829. return lb.end.Sub(lb.start).Minutes()
  1830. }
  1831. // Window returns the window within which the Asset ran
  1832. func (lb *LoadBalancer) Window() Window {
  1833. return lb.window
  1834. }
  1835. // ExpandWindow expands the Asset's window by the given window
  1836. func (lb *LoadBalancer) ExpandWindow(w Window) {
  1837. lb.window = lb.window.Expand(w)
  1838. }
  1839. // SetStartEnd sets the Asset's Start and End fields
  1840. func (lb *LoadBalancer) SetStartEnd(start, end time.Time) {
  1841. if lb.Window().Contains(start) {
  1842. lb.start = start
  1843. } else {
  1844. log.Warningf("Disk.SetStartEnd: start %s not in %s", start, lb.Window())
  1845. }
  1846. if lb.Window().Contains(end) {
  1847. lb.end = end
  1848. } else {
  1849. log.Warningf("Disk.SetStartEnd: end %s not in %s", end, lb.Window())
  1850. }
  1851. }
  1852. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  1853. // as much relevant information as possible (i.e. type, properties, labels).
  1854. func (lb *LoadBalancer) Add(a Asset) Asset {
  1855. // LoadBalancer + LoadBalancer = LoadBalancer
  1856. if that, ok := a.(*LoadBalancer); ok {
  1857. this := lb.Clone().(*LoadBalancer)
  1858. this.add(that)
  1859. return this
  1860. }
  1861. props := lb.Properties().Merge(a.Properties())
  1862. labels := lb.Labels().Merge(a.Labels())
  1863. start := lb.Start()
  1864. if a.Start().Before(start) {
  1865. start = a.Start()
  1866. }
  1867. end := lb.End()
  1868. if a.End().After(end) {
  1869. end = a.End()
  1870. }
  1871. window := lb.Window().Expand(a.Window())
  1872. // LoadBalancer + !LoadBalancer = Any
  1873. any := NewAsset(start, end, window)
  1874. any.SetProperties(props)
  1875. any.SetLabels(labels)
  1876. any.adjustment = lb.Adjustment() + a.Adjustment()
  1877. any.Cost = (lb.TotalCost() - lb.Adjustment()) + (a.TotalCost() - a.Adjustment())
  1878. return any
  1879. }
  1880. func (lb *LoadBalancer) add(that *LoadBalancer) {
  1881. if lb == nil {
  1882. lb = that
  1883. return
  1884. }
  1885. props := lb.Properties().Merge(that.Properties())
  1886. labels := lb.Labels().Merge(that.Labels())
  1887. lb.SetProperties(props)
  1888. lb.SetLabels(labels)
  1889. start := lb.Start()
  1890. if that.Start().Before(start) {
  1891. start = that.Start()
  1892. }
  1893. end := lb.End()
  1894. if that.End().After(end) {
  1895. end = that.End()
  1896. }
  1897. window := lb.Window().Expand(that.Window())
  1898. lb.start = start
  1899. lb.end = end
  1900. lb.window = window
  1901. lb.Cost += that.Cost
  1902. lb.adjustment += that.adjustment
  1903. }
  1904. // Clone returns a cloned instance of the given Asset
  1905. func (lb *LoadBalancer) Clone() Asset {
  1906. return &LoadBalancer{
  1907. properties: lb.properties.Clone(),
  1908. labels: lb.labels.Clone(),
  1909. start: lb.start,
  1910. end: lb.end,
  1911. window: lb.window.Clone(),
  1912. adjustment: lb.adjustment,
  1913. Cost: lb.Cost,
  1914. }
  1915. }
  1916. // Equal returns true if the tow Assets match precisely
  1917. func (lb *LoadBalancer) Equal(a Asset) bool {
  1918. that, ok := a.(*LoadBalancer)
  1919. if !ok {
  1920. return false
  1921. }
  1922. if !lb.Labels().Equal(that.Labels()) {
  1923. return false
  1924. }
  1925. if !lb.Properties().Equal(that.Properties()) {
  1926. return false
  1927. }
  1928. if !lb.Start().Equal(that.Start()) {
  1929. return false
  1930. }
  1931. if !lb.End().Equal(that.End()) {
  1932. return false
  1933. }
  1934. if !lb.window.Equal(that.window) {
  1935. return false
  1936. }
  1937. if lb.adjustment != that.adjustment {
  1938. return false
  1939. }
  1940. if lb.Cost != that.Cost {
  1941. return false
  1942. }
  1943. return true
  1944. }
  1945. // MarshalJSON implements json.Marshal
  1946. func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
  1947. buffer := bytes.NewBufferString("{")
  1948. jsonEncodeString(buffer, "type", lb.Type().String(), ",")
  1949. jsonEncode(buffer, "properties", lb.Properties(), ",")
  1950. jsonEncode(buffer, "labels", lb.Labels(), ",")
  1951. jsonEncodeString(buffer, "window", lb.Window().String(), ",")
  1952. jsonEncodeString(buffer, "start", lb.Start().Format(timeFmt), ",")
  1953. jsonEncodeString(buffer, "end", lb.End().Format(timeFmt), ",")
  1954. jsonEncodeFloat64(buffer, "minutes", lb.Minutes(), ",")
  1955. jsonEncodeFloat64(buffer, "adjustment", lb.Adjustment(), ",")
  1956. jsonEncodeFloat64(buffer, "totalCost", lb.TotalCost(), "")
  1957. buffer.WriteString("}")
  1958. return buffer.Bytes(), nil
  1959. }
  1960. // String implements fmt.Stringer
  1961. func (lb *LoadBalancer) String() string {
  1962. return toString(lb)
  1963. }
  1964. // SharedAsset is an Asset representing a shared cost
  1965. type SharedAsset struct {
  1966. properties *AssetProperties
  1967. labels AssetLabels
  1968. window Window
  1969. Cost float64
  1970. }
  1971. // NewSharedAsset creates and returns a new SharedAsset
  1972. func NewSharedAsset(name string, window Window) *SharedAsset {
  1973. properties := &AssetProperties{
  1974. Name: name,
  1975. Category: SharedCategory,
  1976. Service: OtherCategory,
  1977. }
  1978. return &SharedAsset{
  1979. properties: properties,
  1980. labels: AssetLabels{},
  1981. window: window.Clone(),
  1982. }
  1983. }
  1984. // Type returns the AssetType of the Asset
  1985. func (sa *SharedAsset) Type() AssetType {
  1986. return SharedAssetType
  1987. }
  1988. // Properties returns the Asset's properties
  1989. func (sa *SharedAsset) Properties() *AssetProperties {
  1990. return sa.properties
  1991. }
  1992. // SetProperties sets the Asset's properties
  1993. func (sa *SharedAsset) SetProperties(props *AssetProperties) {
  1994. sa.properties = props
  1995. }
  1996. // Labels returns the Asset's labels
  1997. func (sa *SharedAsset) Labels() AssetLabels {
  1998. return sa.labels
  1999. }
  2000. // SetLabels sets the Asset's labels
  2001. func (sa *SharedAsset) SetLabels(labels AssetLabels) {
  2002. sa.labels = labels
  2003. }
  2004. // Adjustment is not relevant to SharedAsset, but required to implement Asset
  2005. func (sa *SharedAsset) Adjustment() float64 {
  2006. return 0.0
  2007. }
  2008. // SetAdjustment is not relevant to SharedAsset, but required to implement Asset
  2009. func (sa *SharedAsset) SetAdjustment(float64) {
  2010. return
  2011. }
  2012. // TotalCost returns the Asset's total cost
  2013. func (sa *SharedAsset) TotalCost() float64 {
  2014. return sa.Cost
  2015. }
  2016. // Start returns the start time of the Asset
  2017. func (sa *SharedAsset) Start() time.Time {
  2018. return *sa.window.start
  2019. }
  2020. // End returns the end time of the Asset
  2021. func (sa *SharedAsset) End() time.Time {
  2022. return *sa.window.end
  2023. }
  2024. // Minutes returns the number of minutes the SharedAsset ran within the window
  2025. func (sa *SharedAsset) Minutes() float64 {
  2026. return sa.window.Minutes()
  2027. }
  2028. // Window returns the window within the SharedAsset ran
  2029. func (sa *SharedAsset) Window() Window {
  2030. return sa.window
  2031. }
  2032. // ExpandWindow expands the Asset's window
  2033. func (sa *SharedAsset) ExpandWindow(w Window) {
  2034. sa.window = sa.window.Expand(w)
  2035. }
  2036. // SetStartEnd sets the Asset's Start and End fields (not applicable here)
  2037. func (sa *SharedAsset) SetStartEnd(start, end time.Time) {
  2038. return
  2039. }
  2040. // Add sums the Asset with the given Asset to produce a new Asset, maintaining
  2041. // as much relevant information as possible (i.e. type, properties, labels).
  2042. func (sa *SharedAsset) Add(a Asset) Asset {
  2043. // SharedAsset + SharedAsset = SharedAsset
  2044. if that, ok := a.(*SharedAsset); ok {
  2045. this := sa.Clone().(*SharedAsset)
  2046. this.add(that)
  2047. return this
  2048. }
  2049. props := sa.Properties().Merge(a.Properties())
  2050. labels := sa.Labels().Merge(a.Labels())
  2051. start := sa.Start()
  2052. if a.Start().Before(start) {
  2053. start = a.Start()
  2054. }
  2055. end := sa.End()
  2056. if a.End().After(end) {
  2057. end = a.End()
  2058. }
  2059. window := sa.Window().Expand(a.Window())
  2060. // SharedAsset + !SharedAsset = Any
  2061. any := NewAsset(start, end, window)
  2062. any.SetProperties(props)
  2063. any.SetLabels(labels)
  2064. any.adjustment = sa.Adjustment() + a.Adjustment()
  2065. any.Cost = (sa.TotalCost() - sa.Adjustment()) + (a.TotalCost() - a.Adjustment())
  2066. return any
  2067. }
  2068. func (sa *SharedAsset) add(that *SharedAsset) {
  2069. if sa == nil {
  2070. sa = that
  2071. return
  2072. }
  2073. props := sa.Properties().Merge(that.Properties())
  2074. labels := sa.Labels().Merge(that.Labels())
  2075. sa.SetProperties(props)
  2076. sa.SetLabels(labels)
  2077. window := sa.Window().Expand(that.Window())
  2078. sa.window = window
  2079. sa.Cost += that.Cost
  2080. }
  2081. // Clone returns a deep copy of the given SharedAsset
  2082. func (sa *SharedAsset) Clone() Asset {
  2083. if sa == nil {
  2084. return nil
  2085. }
  2086. return &SharedAsset{
  2087. properties: sa.properties.Clone(),
  2088. labels: sa.labels.Clone(),
  2089. window: sa.window.Clone(),
  2090. Cost: sa.Cost,
  2091. }
  2092. }
  2093. // Equal returns true if the two Assets are exact matches
  2094. func (sa *SharedAsset) Equal(a Asset) bool {
  2095. that, ok := a.(*SharedAsset)
  2096. if !ok {
  2097. return false
  2098. }
  2099. if !sa.Labels().Equal(that.Labels()) {
  2100. return false
  2101. }
  2102. if !sa.Properties().Equal(that.Properties()) {
  2103. return false
  2104. }
  2105. if !sa.Start().Equal(that.Start()) {
  2106. return false
  2107. }
  2108. if !sa.End().Equal(that.End()) {
  2109. return false
  2110. }
  2111. if !sa.window.Equal(that.window) {
  2112. return false
  2113. }
  2114. if sa.Cost != that.Cost {
  2115. return false
  2116. }
  2117. return true
  2118. }
  2119. // MarshalJSON implements json.Marshaler
  2120. func (sa *SharedAsset) MarshalJSON() ([]byte, error) {
  2121. buffer := bytes.NewBufferString("{")
  2122. jsonEncodeString(buffer, "type", sa.Type().String(), ",")
  2123. jsonEncode(buffer, "properties", sa.Properties(), ",")
  2124. jsonEncode(buffer, "labels", sa.Labels(), ",")
  2125. jsonEncode(buffer, "properties", sa.Properties(), ",")
  2126. jsonEncode(buffer, "labels", sa.Labels(), ",")
  2127. jsonEncodeString(buffer, "window", sa.Window().String(), ",")
  2128. jsonEncodeString(buffer, "start", sa.Start().Format(timeFmt), ",")
  2129. jsonEncodeString(buffer, "end", sa.End().Format(timeFmt), ",")
  2130. jsonEncodeFloat64(buffer, "minutes", sa.Minutes(), ",")
  2131. jsonEncodeFloat64(buffer, "totalCost", sa.TotalCost(), "")
  2132. buffer.WriteString("}")
  2133. return buffer.Bytes(), nil
  2134. }
  2135. // String implements fmt.Stringer
  2136. func (sa *SharedAsset) String() string {
  2137. return toString(sa)
  2138. }
  2139. // AssetSet stores a set of Assets, each with a unique name, that share
  2140. // a window. An AssetSet is mutable, so treat it like a threadsafe map.
  2141. type AssetSet struct {
  2142. sync.RWMutex
  2143. aggregateBy []string
  2144. assets map[string]Asset
  2145. Window Window
  2146. Warnings []string
  2147. Errors []string
  2148. }
  2149. // NewAssetSet instantiates a new AssetSet and, optionally, inserts
  2150. // the given list of Assets
  2151. func NewAssetSet(start, end time.Time, assets ...Asset) *AssetSet {
  2152. as := &AssetSet{
  2153. assets: map[string]Asset{},
  2154. Window: NewWindow(&start, &end),
  2155. }
  2156. for _, a := range assets {
  2157. as.Insert(a)
  2158. }
  2159. return as
  2160. }
  2161. // AggregateBy aggregates the Assets in the AssetSet by the given list of
  2162. // AssetProperties, such that each asset is binned by a key determined by its
  2163. // relevant property values.
  2164. func (as *AssetSet) AggregateBy(aggregateBy []string, opts *AssetAggregationOptions) error {
  2165. if opts == nil {
  2166. opts = &AssetAggregationOptions{}
  2167. }
  2168. if as.IsEmpty() && len(opts.SharedHourlyCosts) == 0 {
  2169. return nil
  2170. }
  2171. as.Lock()
  2172. defer as.Unlock()
  2173. aggSet := NewAssetSet(as.Start(), as.End())
  2174. aggSet.aggregateBy = aggregateBy
  2175. // Compute hours of the given AssetSet, and if it ends in the future,
  2176. // adjust the hours accordingly
  2177. hours := as.Window.Minutes() / 60.0
  2178. diff := time.Now().Sub(as.End())
  2179. if diff < 0.0 {
  2180. hours += diff.Hours()
  2181. }
  2182. // Insert a shared asset for each shared cost
  2183. for name, hourlyCost := range opts.SharedHourlyCosts {
  2184. sa := NewSharedAsset(name, as.Window.Clone())
  2185. sa.Cost = hourlyCost * hours
  2186. err := aggSet.Insert(sa)
  2187. if err != nil {
  2188. return err
  2189. }
  2190. }
  2191. // Delete the Assets that don't pass each filter
  2192. for _, ff := range opts.FilterFuncs {
  2193. for key, asset := range as.assets {
  2194. if !ff(asset) {
  2195. delete(as.assets, key)
  2196. }
  2197. }
  2198. }
  2199. // Insert each asset into the new set, which will be keyed by the `aggregateBy`
  2200. // on aggSet, resulting in aggregation.
  2201. for _, asset := range as.assets {
  2202. err := aggSet.Insert(asset)
  2203. if err != nil {
  2204. return err
  2205. }
  2206. }
  2207. // Assign the aggregated values back to the original set
  2208. as.assets = aggSet.assets
  2209. as.aggregateBy = aggregateBy
  2210. return nil
  2211. }
  2212. // Clone returns a new AssetSet with a deep copy of the given
  2213. // AssetSet's assets.
  2214. func (as *AssetSet) Clone() *AssetSet {
  2215. if as == nil {
  2216. return nil
  2217. }
  2218. as.RLock()
  2219. defer as.RUnlock()
  2220. var aggregateBy []string
  2221. if as.aggregateBy != nil {
  2222. aggregateBy := []string{}
  2223. for _, s := range as.aggregateBy {
  2224. aggregateBy = append(aggregateBy, s)
  2225. }
  2226. }
  2227. assets := map[string]Asset{}
  2228. for k, v := range as.assets {
  2229. assets[k] = v.Clone()
  2230. }
  2231. s := as.Start()
  2232. e := as.End()
  2233. return &AssetSet{
  2234. Window: NewWindow(&s, &e),
  2235. aggregateBy: aggregateBy,
  2236. assets: assets,
  2237. }
  2238. }
  2239. // Each invokes the given function for each Asset in the set
  2240. func (as *AssetSet) Each(f func(string, Asset)) {
  2241. if as == nil {
  2242. return
  2243. }
  2244. for k, a := range as.assets {
  2245. f(k, a)
  2246. }
  2247. }
  2248. // End returns the end time of the AssetSet's window
  2249. func (as *AssetSet) End() time.Time {
  2250. return *as.Window.End()
  2251. }
  2252. // FindMatch attempts to find a match in the AssetSet for the given Asset on
  2253. // the provided properties and labels. If a match is not found, FindMatch
  2254. // returns nil and a Not Found error.
  2255. func (as *AssetSet) FindMatch(query Asset, aggregateBy []string) (Asset, error) {
  2256. as.RLock()
  2257. defer as.RUnlock()
  2258. matchKey, err := key(query, aggregateBy)
  2259. if err != nil {
  2260. return nil, err
  2261. }
  2262. for _, asset := range as.assets {
  2263. if k, err := key(asset, aggregateBy); err != nil {
  2264. return nil, err
  2265. } else if k == matchKey {
  2266. return asset, nil
  2267. }
  2268. }
  2269. return nil, fmt.Errorf("Asset not found to match %s on %v", query, aggregateBy)
  2270. }
  2271. // ReconciliationMatch attempts to find an exact match in the AssetSet on
  2272. // (Category, ProviderID). If a match is found, it returns the Asset with the
  2273. // intent to adjust it. If no match exists, it attempts to find one on only
  2274. // (ProviderID). If that match is found, it returns the Asset with the intent
  2275. // to insert the associated Cloud cost.
  2276. func (as *AssetSet) ReconciliationMatch(query Asset) (Asset, bool, error) {
  2277. as.RLock()
  2278. defer as.RUnlock()
  2279. // Full match means matching on (Category, ProviderID)
  2280. fullMatchProps := []string{string(AssetCategoryProp), string(AssetProviderIDProp)}
  2281. fullMatchKey, err := key(query, fullMatchProps)
  2282. // This should never happen because we are using enumerated properties,
  2283. // but the check is here in case that changes
  2284. if err != nil {
  2285. return nil, false, err
  2286. }
  2287. // Partial match means matching only on (ProviderID)
  2288. providerIDMatchProps := []string{string(AssetProviderIDProp)}
  2289. providerIDMatchKey, err := key(query, providerIDMatchProps)
  2290. // This should never happen because we are using enumerated properties,
  2291. // but the check is here in case that changes
  2292. if err != nil {
  2293. return nil, false, err
  2294. }
  2295. var providerIDMatch Asset
  2296. for _, asset := range as.assets {
  2297. if k, err := key(asset, fullMatchProps); err != nil {
  2298. return nil, false, err
  2299. } else if k == fullMatchKey {
  2300. log.DedupedInfof(10, "Asset ETL: Reconciliation[rcnw]: ReconcileRange Match: %s", fullMatchKey)
  2301. return asset, true, nil
  2302. }
  2303. if k, err := key(asset, providerIDMatchProps); err != nil {
  2304. return nil, false, err
  2305. } else if k == providerIDMatchKey {
  2306. // Found a partial match. Save it until after all other options
  2307. // have been checked for full matches.
  2308. providerIDMatch = asset
  2309. }
  2310. }
  2311. // No full match was found, so return partial match, if found.
  2312. if providerIDMatch != nil {
  2313. return providerIDMatch, false, nil
  2314. }
  2315. return nil, false, fmt.Errorf("Asset not found to match %s", query)
  2316. }
  2317. // Get returns the Asset in the AssetSet at the given key, or nil and false
  2318. // if no Asset exists for the given key
  2319. func (as *AssetSet) Get(key string) (Asset, bool) {
  2320. as.RLock()
  2321. defer as.RUnlock()
  2322. if a, ok := as.assets[key]; ok {
  2323. return a, true
  2324. }
  2325. return nil, false
  2326. }
  2327. // Insert inserts the given Asset into the AssetSet, using the AssetSet's
  2328. // configured properties to determine the key under which the Asset will
  2329. // be inserted.
  2330. func (as *AssetSet) Insert(asset Asset) error {
  2331. if as.IsEmpty() {
  2332. as.Lock()
  2333. as.assets = map[string]Asset{}
  2334. as.Unlock()
  2335. }
  2336. as.Lock()
  2337. defer as.Unlock()
  2338. // Determine key into which to Insert the Asset.
  2339. k, err := key(asset, as.aggregateBy)
  2340. if err != nil {
  2341. return err
  2342. }
  2343. // Add the given Asset to the existing entry, if there is one;
  2344. // otherwise just set directly into assets
  2345. if _, ok := as.assets[k]; !ok {
  2346. as.assets[k] = asset
  2347. } else {
  2348. as.assets[k] = as.assets[k].Add(asset)
  2349. }
  2350. // Expand the window, just to be safe. It's possible that the asset will
  2351. // be set into the map without expanding it to the AssetSet's window.
  2352. as.assets[k].ExpandWindow(as.Window)
  2353. return nil
  2354. }
  2355. // IsEmpty returns true if the AssetSet is nil, or if it contains
  2356. // zero assets.
  2357. func (as *AssetSet) IsEmpty() bool {
  2358. if as == nil || len(as.assets) == 0 {
  2359. return true
  2360. }
  2361. as.RLock()
  2362. defer as.RUnlock()
  2363. return as.assets == nil || len(as.assets) == 0
  2364. }
  2365. func (as *AssetSet) Length() int {
  2366. if as == nil {
  2367. return 0
  2368. }
  2369. as.RLock()
  2370. defer as.RUnlock()
  2371. return len(as.assets)
  2372. }
  2373. // Map clones and returns a map of the AssetSet's Assets
  2374. func (as *AssetSet) Map() map[string]Asset {
  2375. if as.IsEmpty() {
  2376. return map[string]Asset{}
  2377. }
  2378. return as.Clone().assets
  2379. }
  2380. // MarshalJSON JSON-encodes the AssetSet
  2381. func (as *AssetSet) MarshalJSON() ([]byte, error) {
  2382. as.RLock()
  2383. defer as.RUnlock()
  2384. return json.Marshal(as.assets)
  2385. }
  2386. func (as *AssetSet) Set(asset Asset, aggregateBy []string) error {
  2387. if as.IsEmpty() {
  2388. as.Lock()
  2389. as.assets = map[string]Asset{}
  2390. as.Unlock()
  2391. }
  2392. as.Lock()
  2393. defer as.Unlock()
  2394. // Expand the window to match the AssetSet, then set it
  2395. asset.ExpandWindow(as.Window)
  2396. k, err := key(asset, aggregateBy)
  2397. if err != nil {
  2398. return err
  2399. }
  2400. as.assets[k] = asset
  2401. return nil
  2402. }
  2403. func (as *AssetSet) Start() time.Time {
  2404. return *as.Window.Start()
  2405. }
  2406. func (as *AssetSet) TotalCost() float64 {
  2407. tc := 0.0
  2408. as.Lock()
  2409. defer as.Unlock()
  2410. for _, a := range as.assets {
  2411. tc += a.TotalCost()
  2412. }
  2413. return tc
  2414. }
  2415. func (as *AssetSet) UTCOffset() time.Duration {
  2416. _, zone := as.Start().Zone()
  2417. return time.Duration(zone) * time.Second
  2418. }
  2419. func (as *AssetSet) accumulate(that *AssetSet) (*AssetSet, error) {
  2420. if as == nil {
  2421. return that, nil
  2422. }
  2423. if that == nil {
  2424. return as, nil
  2425. }
  2426. // In the case of an AssetSetRange with empty entries, we may end up with
  2427. // an incoming `as` without an `aggregateBy`, even though we are tring to
  2428. // aggregate here. This handles that case by assigning the correct `aggregateBy`.
  2429. if !sameContents(as.aggregateBy, that.aggregateBy) {
  2430. if len(as.aggregateBy) == 0 {
  2431. as.aggregateBy = that.aggregateBy
  2432. }
  2433. }
  2434. // Set start, end to min(start), max(end)
  2435. start := as.Start()
  2436. end := as.End()
  2437. if that.Start().Before(start) {
  2438. start = that.Start()
  2439. }
  2440. if that.End().After(end) {
  2441. end = that.End()
  2442. }
  2443. if as.IsEmpty() && that.IsEmpty() {
  2444. return NewAssetSet(start, end), nil
  2445. }
  2446. acc := NewAssetSet(start, end)
  2447. acc.aggregateBy = as.aggregateBy
  2448. as.RLock()
  2449. defer as.RUnlock()
  2450. that.RLock()
  2451. defer that.RUnlock()
  2452. for _, asset := range as.assets {
  2453. err := acc.Insert(asset)
  2454. if err != nil {
  2455. return nil, err
  2456. }
  2457. }
  2458. for _, asset := range that.assets {
  2459. err := acc.Insert(asset)
  2460. if err != nil {
  2461. return nil, err
  2462. }
  2463. }
  2464. return acc, nil
  2465. }
  2466. type AssetSetRange struct {
  2467. sync.RWMutex
  2468. assets []*AssetSet
  2469. }
  2470. func NewAssetSetRange(assets ...*AssetSet) *AssetSetRange {
  2471. return &AssetSetRange{
  2472. assets: assets,
  2473. }
  2474. }
  2475. // Accumulate sums each AssetSet in the given range, returning a single cumulative
  2476. // AssetSet for the entire range.
  2477. func (asr *AssetSetRange) Accumulate() (*AssetSet, error) {
  2478. var assetSet *AssetSet
  2479. var err error
  2480. asr.RLock()
  2481. defer asr.RUnlock()
  2482. for _, as := range asr.assets {
  2483. assetSet, err = assetSet.accumulate(as)
  2484. if err != nil {
  2485. return nil, err
  2486. }
  2487. }
  2488. return assetSet, nil
  2489. }
  2490. type AssetAggregationOptions struct {
  2491. SharedHourlyCosts map[string]float64
  2492. FilterFuncs []AssetMatchFunc
  2493. }
  2494. func (asr *AssetSetRange) AggregateBy(aggregateBy []string, opts *AssetAggregationOptions) error {
  2495. aggRange := &AssetSetRange{assets: []*AssetSet{}}
  2496. asr.Lock()
  2497. defer asr.Unlock()
  2498. for _, as := range asr.assets {
  2499. err := as.AggregateBy(aggregateBy, opts)
  2500. if err != nil {
  2501. return err
  2502. }
  2503. aggRange.assets = append(aggRange.assets, as)
  2504. }
  2505. asr.assets = aggRange.assets
  2506. return nil
  2507. }
  2508. func (asr *AssetSetRange) Append(that *AssetSet) {
  2509. asr.Lock()
  2510. defer asr.Unlock()
  2511. asr.assets = append(asr.assets, that)
  2512. }
  2513. // Each invokes the given function for each AssetSet in the range
  2514. func (asr *AssetSetRange) Each(f func(int, *AssetSet)) {
  2515. if asr == nil {
  2516. return
  2517. }
  2518. for i, as := range asr.assets {
  2519. f(i, as)
  2520. }
  2521. }
  2522. func (asr *AssetSetRange) Get(i int) (*AssetSet, error) {
  2523. if i < 0 || i >= len(asr.assets) {
  2524. return nil, fmt.Errorf("AssetSetRange: index out of range: %d", i)
  2525. }
  2526. asr.RLock()
  2527. defer asr.RUnlock()
  2528. return asr.assets[i], nil
  2529. }
  2530. func (asr *AssetSetRange) Length() int {
  2531. if asr == nil || asr.assets == nil {
  2532. return 0
  2533. }
  2534. asr.RLock()
  2535. defer asr.RUnlock()
  2536. return len(asr.assets)
  2537. }
  2538. func (asr *AssetSetRange) MarshalJSON() ([]byte, error) {
  2539. asr.RLock()
  2540. asr.RUnlock()
  2541. return json.Marshal(asr.assets)
  2542. }
  2543. func (asr *AssetSetRange) UTCOffset() time.Duration {
  2544. if asr.Length() == 0 {
  2545. return 0
  2546. }
  2547. as, err := asr.Get(0)
  2548. if err != nil {
  2549. return 0
  2550. }
  2551. return as.UTCOffset()
  2552. }
  2553. // Window returns the full window that the AssetSetRange spans, from the
  2554. // start of the first AssetSet to the end of the last one.
  2555. func (asr *AssetSetRange) Window() Window {
  2556. if asr == nil || asr.Length() == 0 {
  2557. return NewWindow(nil, nil)
  2558. }
  2559. start := asr.assets[0].Start()
  2560. end := asr.assets[asr.Length()-1].End()
  2561. return NewWindow(&start, &end)
  2562. }
  2563. // TODO move everything below to a separate package
  2564. func jsonEncodeFloat64(buffer *bytes.Buffer, name string, val float64, comma string) {
  2565. var encoding string
  2566. if math.IsNaN(val) {
  2567. encoding = fmt.Sprintf("\"%s\":null%s", name, comma)
  2568. } else {
  2569. encoding = fmt.Sprintf("\"%s\":%f%s", name, val, comma)
  2570. }
  2571. buffer.WriteString(encoding)
  2572. }
  2573. func jsonEncodeString(buffer *bytes.Buffer, name, val, comma string) {
  2574. buffer.WriteString(fmt.Sprintf("\"%s\":\"%s\"%s", name, val, comma))
  2575. }
  2576. func jsonEncode(buffer *bytes.Buffer, name string, obj interface{}, comma string) {
  2577. buffer.WriteString(fmt.Sprintf("\"%s\":", name))
  2578. if bytes, err := json.Marshal(obj); err != nil {
  2579. buffer.WriteString("null")
  2580. } else {
  2581. buffer.Write(bytes)
  2582. }
  2583. buffer.WriteString(comma)
  2584. }
  2585. // Returns true if string slices a and b contain all of the same strings, in any order.
  2586. func sameContents(a, b []string) bool {
  2587. if len(a) != len(b) {
  2588. return false
  2589. }
  2590. for i := range a {
  2591. if !contains(b, a[i]) {
  2592. return false
  2593. }
  2594. }
  2595. return true
  2596. }
  2597. func contains(slice []string, item string) bool {
  2598. for _, element := range slice {
  2599. if element == item {
  2600. return true
  2601. }
  2602. }
  2603. return false
  2604. }