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. organization_pool = Organization.where(active: true)
  40. puts " take #{organization_pool.length} organizations"
  41. else
  42. (1..organizations).each do
  43. ActiveRecord::Base.transaction do
  44. organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
  45. organization_pool.push organization
  46. end
  47. end
  48. end
  49. # create agents
  50. agent_pool = []
  51. if agents.zero?
  52. agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
  53. puts " take #{agent_pool.length} agents"
  54. else
  55. roles = Role.where(name: [ 'Agent'])
  56. groups_all = Group.all
  57. (1..agents).each do
  58. ActiveRecord::Base.transaction do
  59. suffix = rand(99_999).to_s
  60. user = User.create_or_update(
  61. login: "filldb-agent-#{suffix}",
  62. firstname: "agent #{suffix}",
  63. lastname: "agent #{suffix}",
  64. email: "filldb-agent-#{suffix}@example.com",
  65. password: 'agentpw',
  66. active: true,
  67. roles: roles,
  68. groups: groups_all,
  69. )
  70. sleep nice
  71. agent_pool.push user
  72. end
  73. end
  74. end
  75. # create customer
  76. customer_pool = []
  77. if customers.zero?
  78. customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
  79. puts " take #{customer_pool.length} customers"
  80. else
  81. roles = Role.where(name: [ 'Customer'])
  82. groups_all = Group.all
  83. (1..customers).each do
  84. ActiveRecord::Base.transaction do
  85. suffix = rand(99_999).to_s
  86. organization = nil
  87. if organization_pool.present? && rand(2) == 1
  88. organization = organization_pool[ organization_pool.length - 1 ]
  89. end
  90. user = User.create_or_update(
  91. login: "filldb-customer-#{suffix}",
  92. firstname: "customer #{suffix}",
  93. lastname: "customer #{suffix}",
  94. email: "filldb-customer-#{suffix}@example.com",
  95. password: 'customerpw',
  96. active: true,
  97. organization: organization,
  98. roles: roles,
  99. )
  100. sleep nice
  101. customer_pool.push user
  102. end
  103. end
  104. end
  105. # create groups
  106. group_pool = []
  107. if groups.zero?
  108. group_pool = Group.where(active: true)
  109. puts " take #{group_pool.length} groups"
  110. else
  111. (1..groups).each do
  112. ActiveRecord::Base.transaction do
  113. group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
  114. group_pool.push group
  115. Role.where(name: 'Agent').first.users.where(active: true).each do |user|
  116. user_groups = user.groups
  117. user_groups.push group
  118. user.groups = user_groups
  119. user.save!
  120. end
  121. sleep nice
  122. end
  123. end
  124. end
  125. # create overviews
  126. if !overviews.zero?
  127. (1..overviews).each do
  128. ActiveRecord::Base.transaction do
  129. 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. 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