track_scopes.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package analytics
  2. import (
  3. "fmt"
  4. )
  5. // UserScopedTrack is a track that automatically adds a user id to the tracking
  6. type UserScopedTrack struct {
  7. *defaultSegmentTrack
  8. userID uint
  9. }
  10. // UserScopedTrackOpts are the options for created a new user-scoped track
  11. type UserScopedTrackOpts struct {
  12. *defaultTrackOpts
  13. UserID uint
  14. }
  15. // GetUserScopedTrackOpts is a helper method to getting the UserScopedTrackOpts
  16. func GetUserScopedTrackOpts(userID uint) *UserScopedTrackOpts {
  17. return &UserScopedTrackOpts{
  18. UserID: userID,
  19. }
  20. }
  21. func (u *UserScopedTrack) getUserId() string {
  22. return fmt.Sprintf("%d", u.userID)
  23. }
  24. func getSegmentUserTrack(opts *UserScopedTrackOpts, track *defaultSegmentTrack) *UserScopedTrack {
  25. return &UserScopedTrack{
  26. defaultSegmentTrack: track,
  27. userID: opts.UserID,
  28. }
  29. }
  30. // ProjectScopedTrack is a track that automatically adds a project id to the track
  31. type ProjectScopedTrack struct {
  32. *UserScopedTrack
  33. projectID uint
  34. }
  35. // ProjectScopedTrackOpts are the options for created a new project-scoped track
  36. type ProjectScopedTrackOpts struct {
  37. *UserScopedTrackOpts
  38. ProjectID uint
  39. }
  40. // GetProjectScopedTrackOpts is a helper method to getting the ProjectScopedTrackOpts
  41. func GetProjectScopedTrackOpts(userID, projID uint) *ProjectScopedTrackOpts {
  42. return &ProjectScopedTrackOpts{
  43. UserScopedTrackOpts: GetUserScopedTrackOpts(userID),
  44. ProjectID: projID,
  45. }
  46. }
  47. func getSegmentProjectTrack(opts *ProjectScopedTrackOpts, track *defaultSegmentTrack) *ProjectScopedTrack {
  48. track.properties.addProjectProperties(opts)
  49. return &ProjectScopedTrack{
  50. UserScopedTrack: getSegmentUserTrack(opts.UserScopedTrackOpts, track),
  51. projectID: opts.ProjectID,
  52. }
  53. }
  54. // RegistryScopedTrack is a track that automatically adds a registry id to the track
  55. type RegistryScopedTrack struct {
  56. *ProjectScopedTrack
  57. registryID uint
  58. }
  59. // RegistryScopedTrackOpts are the options for created a new registry-scoped track
  60. type RegistryScopedTrackOpts struct {
  61. *ProjectScopedTrackOpts
  62. RegistryID uint
  63. }
  64. // GetRegistryScopedTrackOpts is a helper method to getting the RegistryScopedTrackOpts
  65. func GetRegistryScopedTrackOpts(userID, projID, regID uint) *RegistryScopedTrackOpts {
  66. return &RegistryScopedTrackOpts{
  67. ProjectScopedTrackOpts: GetProjectScopedTrackOpts(userID, projID),
  68. RegistryID: regID,
  69. }
  70. }
  71. func getSegmentRegistryTrack(opts *RegistryScopedTrackOpts, track *defaultSegmentTrack) *RegistryScopedTrack {
  72. track.properties.addRegistryProperties(opts)
  73. return &RegistryScopedTrack{
  74. ProjectScopedTrack: getSegmentProjectTrack(opts.ProjectScopedTrackOpts, track),
  75. registryID: opts.RegistryID,
  76. }
  77. }
  78. // ClusterScopedTrack is a track that automatically adds a cluster id to the track
  79. type ClusterScopedTrack struct {
  80. *ProjectScopedTrack
  81. clusterID uint
  82. }
  83. // ClusterScopedTrackOpts are the options for created a new cluster-scoped track
  84. type ClusterScopedTrackOpts struct {
  85. *ProjectScopedTrackOpts
  86. ClusterID uint
  87. }
  88. // GetClusterScopedTrackOpts is a helper method to getting the ClusterScopedTrackOpts
  89. func GetClusterScopedTrackOpts(userID, projID, clusterID uint) *ClusterScopedTrackOpts {
  90. return &ClusterScopedTrackOpts{
  91. ProjectScopedTrackOpts: GetProjectScopedTrackOpts(userID, projID),
  92. ClusterID: clusterID,
  93. }
  94. }
  95. func getSegmentClusterTrack(opts *ClusterScopedTrackOpts, track *defaultSegmentTrack) *ClusterScopedTrack {
  96. track.properties.addClusterProperties(opts)
  97. return &ClusterScopedTrack{
  98. ProjectScopedTrack: getSegmentProjectTrack(opts.ProjectScopedTrackOpts, track),
  99. clusterID: opts.ClusterID,
  100. }
  101. }
  102. // ApplicationScopedTrack is a track that automatically adds an app name and namespace to the track
  103. type ApplicationScopedTrack struct {
  104. *ClusterScopedTrack
  105. name string
  106. namespace string
  107. }
  108. // ApplicationScopedTrackOpts are the options for created a new app-scoped track
  109. type ApplicationScopedTrackOpts struct {
  110. *ClusterScopedTrackOpts
  111. Name string
  112. Namespace string
  113. ChartName string
  114. }
  115. // GetApplicationScopedTrackOpts is a helper method to getting the ApplicationScopedTrackOpts
  116. func GetApplicationScopedTrackOpts(userID, projID, clusterID uint, name, namespace, chartName string) *ApplicationScopedTrackOpts {
  117. return &ApplicationScopedTrackOpts{
  118. ClusterScopedTrackOpts: GetClusterScopedTrackOpts(userID, projID, clusterID),
  119. Name: name,
  120. Namespace: namespace,
  121. ChartName: chartName,
  122. }
  123. }
  124. func getSegmentApplicationTrack(opts *ApplicationScopedTrackOpts, track *defaultSegmentTrack) *ApplicationScopedTrack {
  125. track.properties.addApplicationProperties(opts)
  126. return &ApplicationScopedTrack{
  127. ClusterScopedTrack: getSegmentClusterTrack(opts.ClusterScopedTrackOpts, track),
  128. name: opts.Name,
  129. namespace: opts.Namespace,
  130. }
  131. }