Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

generator.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const fs = require('fs')
  2. const mockPath = './backend/db/_generated.js'
  3. // Insert here how many users you would like to generate:
  4. const total = 100
  5. // Amount of responses for a complete survey
  6. const questions = 13
  7. // Seekers per 100 profiles
  8. const percentageOfSeekers = 95
  9. // Values for responses
  10. const possibleResponses = {
  11. not_important: '120',
  12. some_what_important: '140',
  13. important: '160',
  14. very_important: '180',
  15. extremely_important: '200',
  16. mandatory: '400',
  17. }
  18. const possibleZipcodes = [
  19. '90065', // Glassel
  20. '90012', // Chinatown
  21. '90240', // Downey
  22. '91030', // South Pasadena
  23. '91201', // Glendale
  24. '91399', // Woodland Hills
  25. '91401', // Van Nuys
  26. '90840', // Long Beach
  27. '91001', // Altadena
  28. '91011', // La Canada Flintridge
  29. '97075', // Beaverton
  30. ]
  31. // Helper functions
  32. const randomNumber = max => {
  33. return Math.floor(Math.random() * max) < 1
  34. ? 1
  35. : Math.floor(Math.random() * max)
  36. }
  37. const randomValFrom = arr => arr[randomNumber(arr.length)]
  38. const generate = (classObj, amount, meta) => {
  39. const instances = []
  40. for (let i = 0; i < amount; i++) {
  41. instances.push(new classObj(i + 1, meta))
  42. }
  43. return instances
  44. }
  45. class User {
  46. constructor(id) {
  47. this.user_id = id
  48. this.user_name = ''
  49. this.user_email = ''
  50. this.is_admin = false
  51. this.is_poster = false
  52. }
  53. }
  54. class Profile {
  55. constructor(id) {
  56. this.user_id = id
  57. this.profile_id = id
  58. }
  59. }
  60. class Response {
  61. constructor(id) {
  62. this.response_id = id
  63. this.profile_id = null
  64. this.response_key_id = null
  65. this.val = null
  66. }
  67. }
  68. const users = generate(User, total)
  69. users.forEach(
  70. user => (user.is_poster = randomNumber(100) > percentageOfSeekers ? 1 : 0),
  71. )
  72. const profiles = generate(Profile, total)
  73. // Generate responses, then fill in details
  74. const responses = generate(Response, total * questions)
  75. profiles.forEach((profile, i) => {
  76. const startingIndex = i * questions
  77. for (let k = 0; k < questions; k++) {
  78. const resToEdit = responses[startingIndex + k]
  79. resToEdit.response_key_id = k + 1
  80. resToEdit.profile_id = profile.profile_id
  81. resToEdit.val =
  82. k + 1 == questions
  83. ? randomValFrom(possibleZipcodes)
  84. : randomValFrom(Object.values(possibleResponses))
  85. }
  86. })
  87. // console.log(responses)
  88. // console.log(users)
  89. const jobPosters = users.filter(user => user.is_poster > 0).length
  90. const write = async () => {
  91. await fs.writeFile(mockPath, '', () => {})
  92. fs.appendFile(
  93. mockPath,
  94. `/**\n * Generated ${total} profiles @${Date.now()}\n * ${jobPosters}:${
  95. total - jobPosters
  96. } posters|seekers\n * ---\n */\n\n` +
  97. 'module.exports = ' +
  98. JSON.stringify({ users, responses }),
  99. err => {
  100. if (err) {
  101. console.error(err)
  102. return
  103. }
  104. },
  105. )
  106. }
  107. write()