types.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. func NewCostResponse(ccs *CustomCostSet) *CostResponse {
  52. costResponse := &CostResponse{
  53. Window: ccs.Window,
  54. CustomCosts: []*CustomCost{},
  55. }
  56. for _, cc := range ccs.CustomCosts {
  57. costResponse.TotalBilledCost += cc.BilledCost
  58. costResponse.TotalListCost += cc.ListCost
  59. costResponse.CustomCosts = append(costResponse.CustomCosts, cc)
  60. }
  61. return costResponse
  62. }
  63. func ParseCustomCostResponse(ccResponse *pb.CustomCostResponse) []*CustomCost {
  64. costs := ccResponse.GetCosts()
  65. customCosts := make([]*CustomCost, len(costs))
  66. for i, cost := range costs {
  67. customCosts[i] = &CustomCost{
  68. Id: cost.GetId(),
  69. Zone: cost.GetZone(),
  70. AccountName: cost.GetAccountName(),
  71. ChargeCategory: cost.GetChargeCategory(),
  72. Description: cost.GetDescription(),
  73. ResourceName: cost.GetResourceName(),
  74. ResourceType: cost.GetResourceType(),
  75. ProviderId: cost.GetProviderId(),
  76. BilledCost: cost.GetBilledCost(),
  77. ListCost: cost.GetListCost(),
  78. ListUnitPrice: cost.GetListUnitPrice(),
  79. UsageQuantity: cost.GetUsageQuantity(),
  80. UsageUnit: cost.GetUsageUnit(),
  81. Domain: ccResponse.GetDomain(),
  82. }
  83. }
  84. return customCosts
  85. }
  86. func (cc *CustomCost) Add(other *CustomCost) {
  87. cc.BilledCost += other.BilledCost
  88. cc.ListCost += other.ListCost
  89. cc.ListUnitPrice += other.ListUnitPrice
  90. cc.UsageQuantity += other.UsageQuantity
  91. }
  92. type CustomCostSet struct {
  93. CustomCosts []*CustomCost
  94. Window opencost.Window
  95. }
  96. func NewCustomCostSet(window opencost.Window) *CustomCostSet {
  97. return &CustomCostSet{
  98. CustomCosts: []*CustomCost{},
  99. Window: window,
  100. }
  101. }
  102. func (ccs *CustomCostSet) Add(customCosts []*CustomCost) {
  103. ccs.CustomCosts = append(ccs.CustomCosts, customCosts...)
  104. }
  105. func (ccs *CustomCostSet) Aggregate(aggregateBy []string) error {
  106. if len(aggregateBy) == 0 {
  107. return fmt.Errorf("found empty aggregateBy")
  108. }
  109. aggMap := make(map[string]*CustomCost)
  110. for _, cc := range ccs.CustomCosts {
  111. aggKey, err := generateAggKey(cc, aggregateBy)
  112. if err != nil {
  113. return fmt.Errorf("failed to aggregate CustomCostSet: %w", err)
  114. }
  115. cc.Aggregate = aggKey
  116. if existing, ok := aggMap[aggKey]; ok {
  117. existing.Add(cc)
  118. } else {
  119. aggMap[aggKey] = cc
  120. }
  121. }
  122. var newCustomCosts []*CustomCost
  123. for _, customCost := range aggMap {
  124. newCustomCosts = append(newCustomCosts, customCost)
  125. }
  126. ccs.CustomCosts = newCustomCosts
  127. return nil
  128. }
  129. func generateAggKey(cc *CustomCost, aggregateBy []string) (string, error) {
  130. var aggKeys []string
  131. for _, agg := range aggregateBy {
  132. // TODO only domain is supported currently
  133. if agg == string(CustomCostDomainProp) {
  134. aggKeys = append(aggKeys, cc.Domain)
  135. } else {
  136. return "", fmt.Errorf("unsupported aggregation type: %s", agg)
  137. }
  138. }
  139. aggKey := strings.Join(aggKeys, "/")
  140. return aggKey, nil
  141. }