Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

generator.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const fs = require('fs')
  2. const mockOutputPath = './db/_generated.js'
  3. // Insert here how many users you would like to generate:
  4. const total = 100
  5. let extraProfilesToGenerate = 0
  6. // Amount of responses for a complete survey
  7. const questions = 13
  8. // Seekers per 100 profiles
  9. const percentageOfSeekers = 90
  10. // Values for responses
  11. const possibleResponses = {
  12. not_important: '120',
  13. some_what_important: '140',
  14. important: '160',
  15. very_important: '180',
  16. extremely_important: '200',
  17. mandatory: '400',
  18. }
  19. const possibleZipcodes = [
  20. '90065', // Glassel
  21. '90012', // Chinatown
  22. '90240', // Downey
  23. '91030', // South Pasadena
  24. '91201', // Glendale
  25. '91399', // Woodland Hills
  26. '91401', // Van Nuys
  27. '90840', // Long Beach
  28. '91001', // Altadena
  29. '91011', // La Canada Flintridge
  30. '97075', // Beaverton
  31. ]
  32. // Helper functions
  33. const randomNumber = max => {
  34. return Math.floor(Math.random() * max) < 1
  35. ? 1
  36. : Math.floor(Math.random() * max)
  37. }
  38. const randomValFrom = arr => arr[randomNumber(arr.length)]
  39. const randomEmail = (length = 5) => {
  40. let chars =
  41. 'abcdefghijklmnopqrstuvwxyz-_abcdefghijklmnopqrstuvwxyz0123456789'
  42. let str = ''
  43. for (let i = 0; i < length + randomNumber(9); i++) {
  44. str += chars.charAt(Math.floor(Math.random() * chars.length))
  45. }
  46. const suffixs = [
  47. '@gmail.com',
  48. '@aol.com',
  49. '@yahoo.com',
  50. '@apple.com',
  51. '@hotmail.com',
  52. '@rocket-mail.com',
  53. '@mail.com',
  54. ]
  55. return str + randomValFrom(suffixs)
  56. }
  57. const randomName = (length = 4) => {
  58. let chars = 'aeiouaeiouabcdefghijklmnoprstuvwyabcdefghijklmnopqrstuvwxyz'
  59. let str = ''
  60. for (let i = 0; i < length + randomNumber(9); i++) {
  61. str += chars.charAt(Math.floor(Math.random() * chars.length))
  62. }
  63. return str
  64. }
  65. const generate = (classObj, amount, meta) => {
  66. const instances = []
  67. for (let i = 0; i < amount; i++) {
  68. instances.push(new classObj(i + 1, meta))
  69. }
  70. return instances
  71. }
  72. class User {
  73. constructor(id) {
  74. this.user_id = id
  75. this.user_name = ''
  76. this.user_email = ''
  77. this.is_admin = false
  78. this.is_poster = false
  79. }
  80. }
  81. class Profile {
  82. constructor(id, override) {
  83. this.user_id = override ? override.user_id : id
  84. this.profile_id = override ? override.profile_id + id : id
  85. }
  86. }
  87. class Response {
  88. constructor(id) {
  89. this.response_id = id
  90. this.profile_id = null
  91. this.response_key_id = null
  92. this.val = null
  93. }
  94. }
  95. const users = generate(User, total)
  96. users.forEach(user => {
  97. user.is_poster = randomNumber(100) > percentageOfSeekers ? 1 : 0
  98. if (user.is_poster) {
  99. extraProfilesToGenerate = extraProfilesToGenerate + randomNumber(2)
  100. }
  101. user.user_name = randomName() + ' ' + randomName()
  102. user.user_email = randomEmail()
  103. })
  104. let jobPosterIds = users
  105. .filter(user => user.is_poster > 0)
  106. .map(user => user.user_id)
  107. // Guarentee ONE job poster
  108. if (!jobPosterIds.length) {
  109. randomValFrom(users).is_poster = 1
  110. jobPosterIds = users
  111. .filter(user => user.is_poster > 0)
  112. .map(user => user.user_id)
  113. }
  114. const profiles = generate(Profile, total)
  115. // Generate extra job posting profiles
  116. // attributed to random user.is_poster === true
  117. // TODO: Clean this up. Hard to read...
  118. if (extraProfilesToGenerate > 0) {
  119. let extras = []
  120. for (let l = 0; l < extraProfilesToGenerate; l++) {
  121. const generatedExtraProfiles = generate(Profile, 1, {
  122. user_id:
  123. jobPosterIds.length > 1
  124. ? randomValFrom(jobPosterIds)
  125. : jobPosterIds[0],
  126. profile_id: profiles.length + l,
  127. })
  128. extras = [...extras, ...generatedExtraProfiles]
  129. }
  130. extras.forEach(profile => profiles.push(profile))
  131. }
  132. // Generate responses, then fill in details
  133. const responses = generate(
  134. Response,
  135. (total + extraProfilesToGenerate) * questions,
  136. )
  137. profiles.forEach((profile, i) => {
  138. const startingIndex = i * questions
  139. for (let k = 0; k < questions; k++) {
  140. const resToEdit = responses[startingIndex + k]
  141. resToEdit.response_key_id = k + 1
  142. resToEdit.profile_id = profile.profile_id
  143. resToEdit.val =
  144. k + 1 == questions
  145. ? randomValFrom(possibleZipcodes)
  146. : randomValFrom(Object.values(possibleResponses))
  147. }
  148. })
  149. /**
  150. * Our output format
  151. */
  152. const outputDataObject = { users, profiles, responses }
  153. const jobPostings = profiles.filter(profile =>
  154. jobPosterIds.includes(profile.user_id),
  155. ).length
  156. const jobPosters = users.filter(user => user.is_poster > 0).length
  157. const header = `/**
  158. * GENERATED MOCK SIIMEE DATA
  159. * Generated at: ${Date.now()}
  160. * ---
  161. * ${jobPostings} positions listed by ${jobPosters} job posters
  162. * ${total + extraProfilesToGenerate - jobPostings} candidate profiles by ${
  163. total + extraProfilesToGenerate - jobPostings
  164. } job seekers
  165. * ---
  166. * ${total + extraProfilesToGenerate} Profiles
  167. * ${total} Users
  168. */
  169. `
  170. const write = async () => {
  171. await fs.writeFile(mockOutputPath, '', () => {})
  172. fs.appendFile(
  173. mockOutputPath,
  174. header + 'module.exports = ' + JSON.stringify(outputDataObject),
  175. err => {
  176. if (err) {
  177. console.error(err)
  178. return
  179. }
  180. },
  181. )
  182. }
  183. write()