asset.go 74 KB

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