fill_db.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://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::#{rand(999_999)}", 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 = rand(99_999).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. (1..customers).each do
  85. ActiveRecord::Base.transaction do
  86. suffix = rand(99_999).to_s
  87. organization = nil
  88. if organization_pool.present? && rand(2) == 1
  89. organization = organization_pool[ organization_pool.length - 1 ]
  90. end
  91. user = User.create_or_update(
  92. login: "filldb-customer-#{suffix}",
  93. firstname: "customer #{suffix}",
  94. lastname: "customer #{suffix}",
  95. email: "filldb-customer-#{suffix}@example.com",
  96. password: 'customerpw',
  97. active: true,
  98. organization: organization,
  99. roles: roles,
  100. )
  101. sleep nice
  102. customer_pool.push user
  103. end
  104. end
  105. end
  106. # create groups
  107. group_pool = []
  108. if groups.zero?
  109. group_pool = Group.where(active: true)
  110. puts " take #{group_pool.length} groups"
  111. else
  112. (1..groups).each do
  113. ActiveRecord::Base.transaction do
  114. group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
  115. group_pool.push group
  116. Role.where(name: 'Agent').first.users.where(active: true).each do |user|
  117. user_groups = user.groups
  118. user_groups.push group
  119. user.groups = user_groups
  120. user.save!
  121. end
  122. sleep nice
  123. end
  124. end
  125. end
  126. # create overviews
  127. if !overviews.zero?
  128. (1..overviews).each do
  129. ActiveRecord::Base.transaction do
  130. Overview.create!(
  131. name: "Filloverview::#{rand(999_999)}",
  132. role_ids: [Role.find_by(name: 'Agent').id],
  133. condition: {
  134. 'ticket.state_id' => {
  135. operator: 'is',
  136. value: Ticket::State.by_category(:work_on_all).pluck(:id),
  137. },
  138. },
  139. order: {
  140. by: 'created_at',
  141. direction: 'ASC',
  142. },
  143. view: {
  144. d: %w[title customer group state owner created_at],
  145. s: %w[title customer group state owner created_at],
  146. m: %w[number title customer group state owner created_at],
  147. view_mode_default: 's',
  148. },
  149. active: true
  150. )
  151. end
  152. end
  153. end
  154. # create tickets
  155. priority_pool = Ticket::Priority.all
  156. state_pool = Ticket::State.all
  157. return if !tickets || tickets.zero?
  158. (1..tickets).each do
  159. ActiveRecord::Base.transaction do
  160. customer = customer_pool[ rand(customer_pool.length - 1) ]
  161. agent = agent_pool[ rand(agent_pool.length - 1) ]
  162. ticket = Ticket.create!(
  163. title: "some title äöüß#{rand(999_999)}",
  164. group: group_pool[ rand(group_pool.length - 1) ],
  165. customer: customer,
  166. owner: agent,
  167. state: state_pool[ rand(state_pool.length - 1) ],
  168. priority: priority_pool[ rand(priority_pool.length - 1) ],
  169. updated_by_id: agent.id,
  170. created_by_id: agent.id,
  171. )
  172. # create article
  173. Ticket::Article.create!(
  174. ticket_id: ticket.id,
  175. from: customer.email,
  176. to: 'some_recipient@example.com',
  177. subject: "some subject#{rand(999_999)}",
  178. message_id: "some@id-#{rand(999_999)}",
  179. body: 'some message ...',
  180. internal: false,
  181. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  182. type: Ticket::Article::Type.where(name: 'phone').first,
  183. updated_by_id: agent.id,
  184. created_by_id: agent.id,
  185. )
  186. puts " Ticket #{ticket.number} created"
  187. sleep nice
  188. end
  189. end
  190. end
  191. end
  192. # rubocop:enable Rails/Output