SplitTest.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const _ = require('lodash')
  4. const MIN_NAME_LENGTH = 3
  5. const MAX_NAME_LENGTH = 200
  6. const MIN_VARIANT_NAME_LENGTH = 3
  7. const MAX_VARIANT_NAME_LENGTH = 255
  8. const NAME_REGEX = /^[a-z0-9-]+$/
  9. const RolloutPercentType = {
  10. type: Number,
  11. default: 0,
  12. min: [0, 'Rollout percentage must be between 0 and 100, got {VALUE}'],
  13. max: [100, 'Rollout percentage must be between 0 and 100, got {VALUE}'],
  14. required: true,
  15. }
  16. const VariantSchema = new Schema(
  17. {
  18. name: {
  19. type: String,
  20. minLength: MIN_VARIANT_NAME_LENGTH,
  21. maxLength: MAX_VARIANT_NAME_LENGTH,
  22. required: true,
  23. validate: {
  24. validator: function (input) {
  25. return input !== null && input !== 'default' && NAME_REGEX.test(input)
  26. },
  27. message: `invalid, cannot be 'default' and must match: ${NAME_REGEX}, got {VALUE}`,
  28. },
  29. },
  30. rolloutPercent: RolloutPercentType,
  31. rolloutStripes: [
  32. {
  33. start: RolloutPercentType,
  34. end: RolloutPercentType,
  35. },
  36. ],
  37. },
  38. { _id: false }
  39. )
  40. const VersionSchema = new Schema(
  41. {
  42. versionNumber: {
  43. type: Number,
  44. default: 1,
  45. min: [1, 'must be 1 or higher, got {VALUE}'],
  46. required: true,
  47. },
  48. phase: {
  49. type: String,
  50. default: 'alpha',
  51. enum: ['alpha', 'beta', 'release'],
  52. required: true,
  53. },
  54. active: {
  55. type: Boolean,
  56. default: true,
  57. required: true,
  58. },
  59. variants: [VariantSchema],
  60. createdAt: {
  61. type: Date,
  62. default: Date.now,
  63. },
  64. },
  65. { _id: false }
  66. )
  67. const SplitTestSchema = new Schema({
  68. name: {
  69. type: String,
  70. minLength: MIN_NAME_LENGTH,
  71. maxlength: MAX_NAME_LENGTH,
  72. required: true,
  73. unique: true,
  74. validate: {
  75. validator: function (input) {
  76. return input !== null && NAME_REGEX.test(input)
  77. },
  78. message: `invalid, must match: ${NAME_REGEX}`,
  79. },
  80. },
  81. versions: [VersionSchema],
  82. forbidReleasePhase: {
  83. type: Boolean,
  84. required: false,
  85. },
  86. description: {
  87. type: String,
  88. required: false,
  89. },
  90. expectedEndDate: {
  91. type: Date,
  92. required: false,
  93. },
  94. ticketUrl: {
  95. type: String,
  96. required: false,
  97. },
  98. reportsUrls: {
  99. type: [String],
  100. required: false,
  101. default: [],
  102. },
  103. winningVariant: {
  104. type: String,
  105. required: false,
  106. },
  107. })
  108. SplitTestSchema.methods.getCurrentVersion = function () {
  109. if (this.versions && this.versions.length > 0) {
  110. return _.maxBy(this.versions, 'versionNumber')
  111. } else {
  112. return undefined
  113. }
  114. }
  115. SplitTestSchema.methods.getVersion = function (versionNumber) {
  116. return _.find(this.versions || [], {
  117. versionNumber,
  118. })
  119. }
  120. module.exports = {
  121. SplitTest: mongoose.model('SplitTest', SplitTestSchema),
  122. SplitTestSchema,
  123. }