types.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package customcost
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/filter"
  7. "github.com/opencost/opencost/core/pkg/model/pb"
  8. "github.com/opencost/opencost/core/pkg/opencost"
  9. )
  10. type CostTotalRequest struct {
  11. Start time.Time
  12. End time.Time
  13. AggregateBy []string
  14. Step time.Duration
  15. Filter filter.Filter
  16. }
  17. type CostTimeseriesRequest struct {
  18. Start time.Time
  19. End time.Time
  20. AggregateBy []string
  21. Step time.Duration
  22. Filter filter.Filter
  23. }
  24. type CostResponse struct {
  25. Window opencost.Window `json:"window"`
  26. TotalBilledCost float32 `json:"totalBilledCost"`
  27. TotalListCost float32 `json:"totalListCost"`
  28. CustomCosts []*CustomCost `json:"customCosts"`
  29. }
  30. type CustomCost struct {
  31. Id string `json:"id"`
  32. Zone string `json:"zone"`
  33. AccountName string `json:"account_name"`
  34. ChargeCategory string `json:"charge_category"`
  35. Description string `json:"description"`
  36. ResourceName string `json:"resource_name"`
  37. ResourceType string `json:"resource_type"`
  38. ProviderId string `json:"provider_id"`
  39. BilledCost float32 `json:"billedCost"`
  40. ListCost float32 `json:"listCost"`
  41. ListUnitPrice float32 `json:"list_unit_price"`
  42. UsageQuantity float32 `json:"usage_quantity"`
  43. UsageUnit string `json:"usage_unit"`
  44. Domain string `json:"domain"`
  45. Aggregate string `json:"aggregate"`
  46. }
  47. type CostTimeseriesResponse struct {
  48. Window opencost.Window `json:"window"`
  49. Timeseries []*CostResponse `json:"timeseries"`
  50. }
  51. // --------------
  52. func NewCostResponse(ccs *CustomCostSet) *CostResponse {
  53. costResponse := &CostResponse{
  54. Window: ccs.Window,
  55. CustomCosts: []*CustomCost{},
  56. }
  57. for _, cc := range ccs.CustomCosts {
  58. costResponse.TotalBilledCost += cc.BilledCost
  59. costResponse.TotalListCost += cc.ListCost
  60. costResponse.CustomCosts = append(costResponse.CustomCosts, cc)
  61. }
  62. return costResponse
  63. }
  64. // --------------
  65. func ParseCustomCostResponse(ccResponse *pb.CustomCostResponse) []*CustomCost {
  66. costs := ccResponse.GetCosts()
  67. customCosts := make([]*CustomCost, len(costs))
  68. for i, cost := range costs {
  69. customCosts[i] = &CustomCost{
  70. Id: cost.GetId(),
  71. Zone: cost.GetZone(),
  72. AccountName: cost.GetAccountName(),
  73. ChargeCategory: cost.GetChargeCategory(),
  74. Description: cost.GetDescription(),
  75. ResourceName: cost.GetResourceName(),
  76. ResourceType: cost.GetResourceType(),
  77. ProviderId: cost.GetProviderId(),
  78. BilledCost: cost.GetBilledCost(),
  79. ListCost: cost.GetListCost(),
  80. ListUnitPrice: cost.GetListUnitPrice(),
  81. UsageQuantity: cost.GetUsageQuantity(),
  82. UsageUnit: cost.GetUsageUnit(),
  83. Domain: ccResponse.GetDomain(),
  84. }
  85. }
  86. return customCosts
  87. }
  88. func (cc *CustomCost) Add(other *CustomCost) {
  89. cc.BilledCost += other.BilledCost
  90. cc.ListCost += other.ListCost
  91. cc.ListUnitPrice += other.ListUnitPrice
  92. cc.UsageQuantity += other.UsageQuantity
  93. }
  94. type CustomCostSet struct {
  95. CustomCosts []*CustomCost
  96. Window opencost.Window
  97. }
  98. func NewCustomCostSet(window opencost.Window) *CustomCostSet {
  99. return &CustomCostSet{
  100. CustomCosts: []*CustomCost{},
  101. Window: window,
  102. }
  103. }
  104. func (ccs *CustomCostSet) Add(customCosts []*CustomCost) {
  105. ccs.CustomCosts = append(ccs.CustomCosts, customCosts...)
  106. }
  107. func (ccs *CustomCostSet) Aggregate(aggregateBy []string) error {
  108. if len(aggregateBy) == 0 {
  109. return fmt.Errorf("found empty aggregateBy")
  110. }
  111. aggMap := make(map[string]*CustomCost)
  112. for _, cc := range ccs.CustomCosts {
  113. aggKey, err := generateAggKey(cc, aggregateBy)
  114. if err != nil {
  115. return fmt.Errorf("failed to aggregate CustomCostSet: %w", err)
  116. }
  117. cc.Aggregate = aggKey
  118. if existing, ok := aggMap[aggKey]; ok {
  119. existing.Add(cc)
  120. } else {
  121. aggMap[aggKey] = cc
  122. }
  123. }
  124. var newCustomCosts []*CustomCost
  125. for _, customCost := range aggMap {
  126. newCustomCosts = append(newCustomCosts, customCost)
  127. }
  128. ccs.CustomCosts = newCustomCosts
  129. return nil
  130. }
  131. func generateAggKey(cc *CustomCost, aggregateBy []string) (string, error) {
  132. var aggKeys []string
  133. for _, agg := range aggregateBy {
  134. // TODO only domain is supported currently
  135. if agg == string(CustomCostDomainProp) {
  136. aggKeys = append(aggKeys, cc.Domain)
  137. } else {
  138. return "", fmt.Errorf("unsupported aggregation type: %s", agg)
  139. }
  140. }
  141. aggKey := strings.Join(aggKeys, "/")
  142. return aggKey, nil
  143. }