node.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package pricing
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/model/shared"
  8. )
  9. type NodePricing struct {
  10. Properties NodePricingProperties `json:"properties" yaml:"properties"`
  11. Prices Prices `json:"prices" yaml:"prices"`
  12. }
  13. func (np *NodePricing) String() string {
  14. return np.Properties.String() + "|" + np.Prices.canonical()
  15. }
  16. type NodePricingProperties struct {
  17. Provider shared.Provider `json:"provider,omitempty" yaml:"provider,omitempty"`
  18. Region string `json:"region,omitempty" yaml:"region,omitempty"`
  19. InstanceType string `json:"instanceType,omitempty" yaml:"instanceType,omitempty"`
  20. Provisioning ProvisioningType `json:"provisioning,omitempty" yaml:"provisioning,omitempty"`
  21. Commitment CommitmentType `json:"commitment,omitempty" yaml:"commitment,omitempty"`
  22. Cluster string `json:"cluster,omitempty" yaml:"cluster,omitempty"`
  23. ProviderID string `json:"providerID,omitempty" yaml:"providerID,omitempty"`
  24. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
  25. Start *time.Time `json:"start,omitempty" yaml:"start,omitempty"`
  26. End *time.Time `json:"end,omitempty" yaml:"end,omitempty"`
  27. }
  28. func (np *NodePricingProperties) String() string {
  29. return fmt.Sprintf("%s:%s:%s:%s:%s:%s:%s:%s:%s",
  30. np.Provider,
  31. np.Region,
  32. np.InstanceType,
  33. np.Provisioning,
  34. np.Commitment,
  35. np.Cluster,
  36. np.ProviderID,
  37. np.labelsKey(),
  38. np.timeKey(),
  39. )
  40. }
  41. func (np *NodePricingProperties) labelsKey() string {
  42. if len(np.Labels) == 0 {
  43. return ""
  44. }
  45. keys := make([]string, 0, len(np.Labels))
  46. for k := range np.Labels {
  47. keys = append(keys, k)
  48. }
  49. sort.Strings(keys)
  50. var b strings.Builder
  51. for i, k := range keys {
  52. if i > 0 {
  53. b.WriteByte(':')
  54. }
  55. b.WriteString(k)
  56. b.WriteByte('=')
  57. b.WriteString(np.Labels[k])
  58. }
  59. return b.String()
  60. }
  61. func (np *NodePricingProperties) timeKey() string {
  62. s := "nil"
  63. e := "nil"
  64. if np.Start != nil {
  65. s = np.Start.UTC().Format(time.RFC3339Nano)
  66. }
  67. if np.End != nil {
  68. e = np.End.UTC().Format(time.RFC3339Nano)
  69. }
  70. return fmt.Sprintf("%s:%s", s, e)
  71. }