Added doc, improved performance through ActiveRecord::Base.transaction.

This commit is contained in:
Martin Edenhofer 2015-08-07 11:24:47 +02:00
parent 506e2f1e04
commit e1dc0b1707

View file

@ -1,5 +1,18 @@
# rubocop:disable Rails/Output # rubocop:disable Rails/Output
module FillDB module FillDB
=begin
fill your database with demo records
FillDB.load(agents, customers, groups, organizations, tickets)
e. g.
FillDB.load(10, 100, 5, 40, 1000)
=end
def self.load( agents, customers, groups, organizations, tickets ) def self.load( agents, customers, groups, organizations, tickets )
puts 'load db with:' puts 'load db with:'
puts " agents:#{agents}" puts " agents:#{agents}"
@ -14,10 +27,15 @@ module FillDB
# organizations # organizations
organization_pool = [] organization_pool = []
if organizations && !organizations.zero? if organizations && !organizations.zero?
(1..organizations).each {
organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true ) ActiveRecord::Base.transaction do
organization_pool.push organization
} (1..organizations).each {
organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true )
organization_pool.push organization
}
end
else else
organization_pool = Organization.where(active: true) organization_pool = Organization.where(active: true)
end end
@ -27,20 +45,25 @@ module FillDB
if agents && !agents.zero? if agents && !agents.zero?
roles = Role.where( name: [ 'Agent'] ) roles = Role.where( name: [ 'Agent'] )
groups_all = Group.all groups_all = Group.all
(1..agents).each {
suffix = rand(99_999).to_s ActiveRecord::Base.transaction do
user = User.create_or_update(
login: "filldb-agent-#{suffix}", (1..agents).each {
firstname: "agent #{suffix}", suffix = rand(99_999).to_s
lastname: "agent #{suffix}", user = User.create_or_update(
email: "filldb-agent-#{suffix}@example.com", login: "filldb-agent-#{suffix}",
password: 'agentpw', firstname: "agent #{suffix}",
active: true, lastname: "agent #{suffix}",
roles: roles, email: "filldb-agent-#{suffix}@example.com",
groups: groups_all, password: 'agentpw',
) active: true,
agent_pool.push user roles: roles,
} groups: groups_all,
)
agent_pool.push user
}
end
else else
agent_pool = Role.where(name: 'Agent').first.users.where(active: true) agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
puts " take #{agent_pool.length} agents" puts " take #{agent_pool.length} agents"
@ -51,24 +74,29 @@ module FillDB
if customers && !customers.zero? if customers && !customers.zero?
roles = Role.where( name: [ 'Customer'] ) roles = Role.where( name: [ 'Customer'] )
groups_all = Group.all groups_all = Group.all
(1..customers).each {
suffix = rand(99_999).to_s ActiveRecord::Base.transaction do
organization = nil
if !organization_pool.empty? && rand(2) == 1 (1..customers).each {
organization = organization_pool[ organization_pool.length - 1 ] suffix = rand(99_999).to_s
end organization = nil
user = User.create_or_update( if !organization_pool.empty? && rand(2) == 1
login: "filldb-customer-#{suffix}", organization = organization_pool[ organization_pool.length - 1 ]
firstname: "customer #{suffix}", end
lastname: "customer #{suffix}", user = User.create_or_update(
email: "filldb-customer-#{suffix}@example.com", login: "filldb-customer-#{suffix}",
password: 'customerpw', firstname: "customer #{suffix}",
active: true, lastname: "customer #{suffix}",
organization: organization, email: "filldb-customer-#{suffix}@example.com",
roles: roles, password: 'customerpw',
) active: true,
customer_pool.push user organization: organization,
} roles: roles,
)
customer_pool.push user
}
end
else else
customer_pool = Role.where(name: 'Customer').first.users.where(active: true) customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
end end
@ -77,16 +105,21 @@ module FillDB
group_pool = [] group_pool = []
if groups && !groups.zero? if groups && !groups.zero?
puts "1..#{groups}" puts "1..#{groups}"
(1..groups).each {
group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true ) ActiveRecord::Base.transaction do
group_pool.push group
Role.where(name: 'Agent').first.users.where(active: true).each {|user| (1..groups).each {
user_groups = user.groups group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true )
user_groups.push group group_pool.push group
user.groups = user_groups Role.where(name: 'Agent').first.users.where(active: true).each {|user|
user.save user_groups = user.groups
user_groups.push group
user.groups = user_groups
user.save
}
} }
}
end
else else
group_pool = Group.where(active: true) group_pool = Group.where(active: true)
end end
@ -94,35 +127,41 @@ module FillDB
# create tickets # create tickets
priority_pool = Ticket::Priority.all priority_pool = Ticket::Priority.all
state_pool = Ticket::State.all state_pool = Ticket::State.all
if tickets && !tickets.zero? if tickets && !tickets.zero?
(1..tickets).each {
customer = customer_pool[ rand(customer_pool.length - 1) ] ActiveRecord::Base.transaction do
agent = agent_pool[ rand(agent_pool.length - 1) ]
ticket = Ticket.create( (1..tickets).each {
title: 'some title äöüß' + rand(999_999).to_s, customer = customer_pool[ rand(customer_pool.length - 1) ]
group: group_pool[ rand(group_pool.length - 1) ], agent = agent_pool[ rand(agent_pool.length - 1) ]
customer: customer, ticket = Ticket.create(
owner: agent, title: 'some title äöüß' + rand(999_999).to_s,
state: state_pool[ rand(state_pool.length - 1) ], group: group_pool[ rand(group_pool.length - 1) ],
priority: priority_pool[ rand(priority_pool.length - 1) ], customer: customer,
updated_by_id: agent.id, owner: agent,
created_by_id: agent.id, state: state_pool[ rand(state_pool.length - 1) ],
) priority: priority_pool[ rand(priority_pool.length - 1) ],
# create article updated_by_id: agent.id,
article = Ticket::Article.create( created_by_id: agent.id,
ticket_id: ticket.id, )
from: customer.email, # create article
to: 'some_recipient@example.com', article = Ticket::Article.create(
subject: 'some subject' + rand(999_999).to_s, ticket_id: ticket.id,
message_id: 'some@id-' + rand(999_999).to_s, from: customer.email,
body: 'some message ...', to: 'some_recipient@example.com',
internal: false, subject: 'some subject' + rand(999_999).to_s,
sender: Ticket::Article::Sender.where(name: 'Customer').first, message_id: 'some@id-' + rand(999_999).to_s,
type: Ticket::Article::Type.where(name: 'phone').first, body: 'some message ...',
updated_by_id: agent.id, internal: false,
created_by_id: agent.id, sender: Ticket::Article::Sender.where(name: 'Customer').first,
) type: Ticket::Article::Type.where(name: 'phone').first,
} updated_by_id: agent.id,
created_by_id: agent.id,
)
}
end
end end
end end
end end