mock.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package model
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/model/pb"
  7. "google.golang.org/protobuf/types/known/timestamppb"
  8. )
  9. func createCustomCost(postfix string) *pb.CustomCost {
  10. n := func(a string) string {
  11. return fmt.Sprintf("%s_%s", a, postfix)
  12. }
  13. cost := rand.Float32() * 250.0
  14. return &pb.CustomCost{
  15. Metadata: map[string]string{
  16. n("custom_cost"): n("metadata"),
  17. },
  18. Zone: "zone-a",
  19. AccountName: n("account"),
  20. ChargeCategory: n("charge"),
  21. Description: "this is a test cost description(" + postfix + ")",
  22. ResourceName: "test-custom-cost-" + postfix,
  23. ResourceType: "custom",
  24. Id: n("id"),
  25. ProviderId: "gke",
  26. BilledCost: cost,
  27. ListCost: cost,
  28. ListUnitPrice: cost,
  29. UsageQuantity: 1.0,
  30. UsageUnit: n("unit"),
  31. Labels: map[string]string{
  32. n("label"): n("value"),
  33. },
  34. }
  35. }
  36. func GenerateMockCustomCostSet(start, end time.Time) *pb.CustomCostResponse {
  37. costs := []*pb.CustomCost{}
  38. for i := 0; i < 50; i++ {
  39. costs = append(costs, createCustomCost(fmt.Sprintf("%d", i)))
  40. }
  41. return &pb.CustomCostResponse{
  42. Metadata: map[string]string{
  43. "key1": "value1",
  44. "test": "1, 2, 3",
  45. },
  46. CostSource: "none",
  47. Domain: "testing",
  48. Version: "v1",
  49. Currency: "USD",
  50. Start: timestamppb.New(start),
  51. End: timestamppb.New(end),
  52. Costs: costs,
  53. }
  54. }
  55. func GenerateMockLabelResponse(start time.Time, res pb.Resolution) *pb.LabelsResponse {
  56. return &pb.LabelsResponse{
  57. Type: "account-labels",
  58. GroupId: "billing_account_xzy",
  59. Window: &pb.Window{
  60. Resolution: res,
  61. Start: timestamppb.New(start),
  62. },
  63. LabelSets: map[string]*pb.LabelSet{
  64. "account1": {Labels: map[string]string{
  65. "account": "account1",
  66. "test": "test1",
  67. }},
  68. "account2": {Labels: map[string]string{
  69. "account": "account2",
  70. "test": "test2",
  71. }},
  72. },
  73. }
  74. }