fill_db.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. tickets: 100
  11. )
  12. or if you only want to create 100 tickets
  13. FillDB.load(tickets: 100)
  14. =end
  15. def self.load(params)
  16. nice = params[:nice] || 0.5
  17. agents = params[:agents] || 0
  18. customers = params[:customers] || 0
  19. groups = params[:groups] || 0
  20. organizations = params[:organizations] || 0
  21. tickets = params[:tickets] || 0
  22. puts 'load db with:'
  23. puts " agents:#{agents}"
  24. puts " customers:#{customers}"
  25. puts " groups:#{groups}"
  26. puts " organizations:#{organizations}"
  27. puts " tickets:#{tickets}"
  28. # set current user
  29. UserInfo.current_user_id = 1
  30. # organizations
  31. organization_pool = []
  32. if organizations && !organizations.zero?
  33. (1..organizations).each do
  34. ActiveRecord::Base.transaction do
  35. organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
  36. organization_pool.push organization
  37. end
  38. end
  39. else
  40. organization_pool = Organization.where(active: true)
  41. puts " take #{organization_pool.length} organizations"
  42. end
  43. # create agents
  44. agent_pool = []
  45. if agents && !agents.zero?
  46. roles = Role.where(name: [ 'Agent'])
  47. groups_all = Group.all
  48. (1..agents).each do
  49. ActiveRecord::Base.transaction do
  50. suffix = rand(99_999).to_s
  51. user = User.create_or_update(
  52. login: "filldb-agent-#{suffix}",
  53. firstname: "agent #{suffix}",
  54. lastname: "agent #{suffix}",
  55. email: "filldb-agent-#{suffix}@example.com",
  56. password: 'agentpw',
  57. active: true,
  58. roles: roles,
  59. groups: groups_all,
  60. )
  61. sleep nice
  62. agent_pool.push user
  63. end
  64. end
  65. else
  66. agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
  67. puts " take #{agent_pool.length} agents"
  68. end
  69. # create customer
  70. customer_pool = []
  71. if customers && !customers.zero?
  72. roles = Role.where(name: [ 'Customer'])
  73. groups_all = Group.all
  74. (1..customers).each do
  75. ActiveRecord::Base.transaction do
  76. suffix = rand(99_999).to_s
  77. organization = nil
  78. if !organization_pool.empty? && rand(2) == 1
  79. organization = organization_pool[ organization_pool.length - 1 ]
  80. end
  81. user = User.create_or_update(
  82. login: "filldb-customer-#{suffix}",
  83. firstname: "customer #{suffix}",
  84. lastname: "customer #{suffix}",
  85. email: "filldb-customer-#{suffix}@example.com",
  86. password: 'customerpw',
  87. active: true,
  88. organization: organization,
  89. roles: roles,
  90. )
  91. sleep nice
  92. customer_pool.push user
  93. end
  94. end
  95. else
  96. customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
  97. puts " take #{customer_pool.length} customers"
  98. end
  99. # create groups
  100. group_pool = []
  101. if groups && !groups.zero?
  102. (1..groups).each do
  103. ActiveRecord::Base.transaction do
  104. group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
  105. group_pool.push group
  106. Role.where(name: 'Agent').first.users.where(active: true).each do |user|
  107. user_groups = user.groups
  108. user_groups.push group
  109. user.groups = user_groups
  110. user.save!
  111. end
  112. sleep nice
  113. end
  114. end
  115. else
  116. group_pool = Group.where(active: true)
  117. puts " take #{group_pool.length} groups"
  118. end
  119. # create tickets
  120. priority_pool = Ticket::Priority.all
  121. state_pool = Ticket::State.all
  122. if tickets && !tickets.zero?
  123. (1..tickets).each do
  124. ActiveRecord::Base.transaction do
  125. customer = customer_pool[ rand(customer_pool.length - 1) ]
  126. agent = agent_pool[ rand(agent_pool.length - 1) ]
  127. ticket = Ticket.create!(
  128. title: "some title äöüß#{rand(999_999)}",
  129. group: group_pool[ rand(group_pool.length - 1) ],
  130. customer: customer,
  131. owner: agent,
  132. state: state_pool[ rand(state_pool.length - 1) ],
  133. priority: priority_pool[ rand(priority_pool.length - 1) ],
  134. updated_by_id: agent.id,
  135. created_by_id: agent.id,
  136. )
  137. # create article
  138. article = Ticket::Article.create!(
  139. ticket_id: ticket.id,
  140. from: customer.email,
  141. to: 'some_recipient@example.com',
  142. subject: "some subject#{rand(999_999)}",
  143. message_id: "some@id-#{rand(999_999)}",
  144. body: 'some message ...',
  145. internal: false,
  146. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  147. type: Ticket::Article::Type.where(name: 'phone').first,
  148. updated_by_id: agent.id,
  149. created_by_id: agent.id,
  150. )
  151. puts " Ticket #{ticket.number} created"
  152. sleep nice
  153. end
  154. end
  155. end
  156. end
  157. end