2
0

usage.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package models
  2. import (
  3. "time"
  4. "github.com/porter-dev/porter/api/types"
  5. "gorm.io/gorm"
  6. )
  7. // ProjectUsage keeps track of the usage limits for a project
  8. type ProjectUsage struct {
  9. gorm.Model
  10. // The project ID that this model refers to
  11. ProjectID uint
  12. // The CPU usage, in vCPUs
  13. ResourceCPU uint
  14. // The memory usage, in bytes
  15. ResourceMemory uint
  16. // The number of clusters
  17. Clusters uint
  18. // The number of users
  19. Users uint
  20. }
  21. // ToProjectUsageType converts the project usage model to a project usage API type
  22. func (p *ProjectUsage) ToProjectUsageType() *types.ProjectUsage {
  23. return &types.ProjectUsage{
  24. ResourceCPU: p.ResourceCPU,
  25. ResourceMemory: p.ResourceMemory,
  26. Clusters: p.Clusters,
  27. Users: p.Users,
  28. }
  29. }
  30. // ProjectUsageCache stores the latest cache of the resource usage for a project,
  31. // for fields that are expensive to compute
  32. type ProjectUsageCache struct {
  33. gorm.Model
  34. // The project ID that this model refers to
  35. ProjectID uint
  36. // The CPU usage, in vCPUs
  37. ResourceCPU uint
  38. // The memory usage, in bytes
  39. ResourceMemory uint
  40. // The number of clusters
  41. Clusters uint
  42. // The number of users
  43. Users uint
  44. // Whether the user is exceeding usage
  45. Exceeded bool
  46. // How long the user has been exceeding resource limits
  47. ExceededSince *time.Time
  48. }
  49. func (p *ProjectUsageCache) Is1HrOld() bool {
  50. timeSince := time.Now().Sub(p.UpdatedAt)
  51. return timeSince > 1*time.Hour
  52. }