fill_db.rb 6.3 KB

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