fill_db.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # rubocop:disable Rails/Output
  2. module FillDB
  3. =begin
  4. fill your database with demo records
  5. FillDB.load(
  6. agents: 50,
  7. customers: 1000,
  8. groups: 20,
  9. organizations: 40,
  10. overviews: 5,
  11. tickets: 100,
  12. )
  13. or if you only want to create 100 tickets
  14. FillDB.load(tickets: 100)
  15. FillDB.load(agents: 20)
  16. FillDB.load(overviews: 20)
  17. FillDB.load(tickets: 10000)
  18. =end
  19. def self.load(params)
  20. nice = params[:nice] || 0.5
  21. agents = params[:agents] || 0
  22. customers = params[:customers] || 0
  23. groups = params[:groups] || 0
  24. organizations = params[:organizations] || 0
  25. overviews = params[:overviews] || 0
  26. tickets = params[:tickets] || 0
  27. puts 'load db with:'
  28. puts " agents:#{agents}"
  29. puts " customers:#{customers}"
  30. puts " groups:#{groups}"
  31. puts " organizations:#{organizations}"
  32. puts " overviews:#{overviews}"
  33. puts " tickets:#{tickets}"
  34. # set current user
  35. UserInfo.current_user_id = 1
  36. # organizations
  37. organization_pool = []
  38. if !organizations.zero?
  39. (1..organizations).each do
  40. ActiveRecord::Base.transaction do
  41. organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
  42. organization_pool.push organization
  43. end
  44. end
  45. else
  46. organization_pool = Organization.where(active: true)
  47. puts " take #{organization_pool.length} organizations"
  48. end
  49. # create agents
  50. agent_pool = []
  51. if !agents.zero?
  52. roles = Role.where(name: [ 'Agent'])
  53. groups_all = Group.all
  54. (1..agents).each do
  55. ActiveRecord::Base.transaction do
  56. suffix = rand(99_999).to_s
  57. user = User.create_or_update(
  58. login: "filldb-agent-#{suffix}",
  59. firstname: "agent #{suffix}",
  60. lastname: "agent #{suffix}",
  61. email: "filldb-agent-#{suffix}@example.com",
  62. password: 'agentpw',
  63. active: true,
  64. roles: roles,
  65. groups: groups_all,
  66. )
  67. sleep nice
  68. agent_pool.push user
  69. end
  70. end
  71. else
  72. agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
  73. puts " take #{agent_pool.length} agents"
  74. end
  75. # create customer
  76. customer_pool = []
  77. if !customers.zero?
  78. roles = Role.where(name: [ 'Customer'])
  79. groups_all = Group.all
  80. (1..customers).each do
  81. ActiveRecord::Base.transaction do
  82. suffix = rand(99_999).to_s
  83. organization = nil
  84. if organization_pool.present? && rand(2) == 1
  85. organization = organization_pool[ organization_pool.length - 1 ]
  86. end
  87. user = User.create_or_update(
  88. login: "filldb-customer-#{suffix}",
  89. firstname: "customer #{suffix}",
  90. lastname: "customer #{suffix}",
  91. email: "filldb-customer-#{suffix}@example.com",
  92. password: 'customerpw',
  93. active: true,
  94. organization: organization,
  95. roles: roles,
  96. )
  97. sleep nice
  98. customer_pool.push user
  99. end
  100. end
  101. else
  102. customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
  103. puts " take #{customer_pool.length} customers"
  104. end
  105. # create groups
  106. group_pool = []
  107. if !groups.zero?
  108. (1..groups).each do
  109. ActiveRecord::Base.transaction do
  110. group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
  111. group_pool.push group
  112. Role.where(name: 'Agent').first.users.where(active: true).each do |user|
  113. user_groups = user.groups
  114. user_groups.push group
  115. user.groups = user_groups
  116. user.save!
  117. end
  118. sleep nice
  119. end
  120. end
  121. else
  122. group_pool = Group.where(active: true)
  123. puts " take #{group_pool.length} groups"
  124. end
  125. # create overviews
  126. if !overviews.zero?
  127. (1..overviews).each do
  128. ActiveRecord::Base.transaction do
  129. overview = Overview.create!(
  130. name: "Filloverview::#{rand(999_999)}",
  131. role_ids: [Role.find_by(name: 'Agent').id],
  132. condition: {
  133. 'ticket.state_id' => {
  134. operator: 'is',
  135. value: Ticket::State.by_category(:work_on_all).pluck(:id),
  136. },
  137. },
  138. order: {
  139. by: 'created_at',
  140. direction: 'ASC',
  141. },
  142. view: {
  143. d: %w[title customer group state owner created_at],
  144. s: %w[title customer group state owner created_at],
  145. m: %w[number title customer group state owner created_at],
  146. view_mode_default: 's',
  147. },
  148. active: true
  149. )
  150. end
  151. end
  152. end
  153. # create tickets
  154. priority_pool = Ticket::Priority.all
  155. state_pool = Ticket::State.all
  156. return if !tickets || tickets.zero?
  157. (1..tickets).each do
  158. ActiveRecord::Base.transaction do
  159. customer = customer_pool[ rand(customer_pool.length - 1) ]
  160. agent = agent_pool[ rand(agent_pool.length - 1) ]
  161. ticket = Ticket.create!(
  162. title: "some title äöüß#{rand(999_999)}",
  163. group: group_pool[ rand(group_pool.length - 1) ],
  164. customer: customer,
  165. owner: agent,
  166. state: state_pool[ rand(state_pool.length - 1) ],
  167. priority: priority_pool[ rand(priority_pool.length - 1) ],
  168. updated_by_id: agent.id,
  169. created_by_id: agent.id,
  170. )
  171. # create article
  172. article = Ticket::Article.create!(
  173. ticket_id: ticket.id,
  174. from: customer.email,
  175. to: 'some_recipient@example.com',
  176. subject: "some subject#{rand(999_999)}",
  177. message_id: "some@id-#{rand(999_999)}",
  178. body: 'some message ...',
  179. internal: false,
  180. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  181. type: Ticket::Article::Type.where(name: 'phone').first,
  182. updated_by_id: agent.id,
  183. created_by_id: agent.id,
  184. )
  185. puts " Ticket #{ticket.number} created"
  186. sleep nice
  187. end
  188. end
  189. end
  190. end
  191. # rubocop:enable Rails/Output