fill_db.rb 6.0 KB

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