2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2015-05-04 18:58:28 +00:00
|
|
|
# rubocop:disable Rails/Output
|
2021-06-23 11:35:27 +00:00
|
|
|
module FillDb
|
2015-08-07 09:24:47 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
fill your database with demo records
|
|
|
|
|
2021-06-23 11:35:27 +00:00
|
|
|
FillDb.load(
|
2017-10-15 06:19:57 +00:00
|
|
|
agents: 50,
|
|
|
|
customers: 1000,
|
|
|
|
groups: 20,
|
|
|
|
organizations: 40,
|
2017-11-27 10:50:57 +00:00
|
|
|
overviews: 5,
|
|
|
|
tickets: 100,
|
2017-10-15 06:19:57 +00:00
|
|
|
)
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
or if you only want to create 100 tickets
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2021-06-23 11:35:27 +00:00
|
|
|
FillDb.load(tickets: 100)
|
|
|
|
FillDb.load(agents: 20)
|
|
|
|
FillDb.load(overviews: 20)
|
|
|
|
FillDb.load(tickets: 10000)
|
2015-08-07 09:24:47 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
def self.load(params)
|
|
|
|
nice = params[:nice] || 0.5
|
|
|
|
agents = params[:agents] || 0
|
|
|
|
customers = params[:customers] || 0
|
|
|
|
groups = params[:groups] || 0
|
|
|
|
organizations = params[:organizations] || 0
|
2017-11-27 10:50:57 +00:00
|
|
|
overviews = params[:overviews] || 0
|
2017-10-15 06:19:57 +00:00
|
|
|
tickets = params[:tickets] || 0
|
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'load db with:'
|
2014-07-23 22:20:39 +00:00
|
|
|
puts " agents:#{agents}"
|
|
|
|
puts " customers:#{customers}"
|
|
|
|
puts " groups:#{groups}"
|
|
|
|
puts " organizations:#{organizations}"
|
2017-11-27 10:50:57 +00:00
|
|
|
puts " overviews:#{overviews}"
|
2014-07-23 22:20:39 +00:00
|
|
|
puts " tickets:#{tickets}"
|
|
|
|
|
|
|
|
# set current user
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
|
|
|
|
# organizations
|
|
|
|
organization_pool = []
|
2020-11-05 16:31:00 +00:00
|
|
|
if organizations.zero?
|
|
|
|
organization_pool = Organization.where(active: true)
|
|
|
|
puts " take #{organization_pool.length} organizations"
|
|
|
|
else
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..organizations).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-10-05 12:00:33 +00:00
|
|
|
organization = Organization.create!(name: "FillOrganization::#{counter}", active: true)
|
2015-08-07 09:24:47 +00:00
|
|
|
organization_pool.push organization
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create agents
|
|
|
|
agent_pool = []
|
2020-11-05 16:31:00 +00:00
|
|
|
if agents.zero?
|
|
|
|
agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
|
|
|
|
puts " take #{agent_pool.length} agents"
|
|
|
|
else
|
2017-10-15 06:19:57 +00:00
|
|
|
roles = Role.where(name: [ 'Agent'])
|
2014-07-23 22:20:39 +00:00
|
|
|
groups_all = Group.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..agents).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-10-05 12:00:33 +00:00
|
|
|
suffix = counter.to_s
|
2015-08-07 09:24:47 +00:00
|
|
|
user = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: "filldb-agent-#{suffix}",
|
2015-08-07 09:24:47 +00:00
|
|
|
firstname: "agent #{suffix}",
|
2018-12-19 17:31:51 +00:00
|
|
|
lastname: "agent #{suffix}",
|
|
|
|
email: "filldb-agent-#{suffix}@example.com",
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups_all,
|
2015-08-07 09:24:47 +00:00
|
|
|
)
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
agent_pool.push user
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create customer
|
|
|
|
customer_pool = []
|
2020-11-05 16:31:00 +00:00
|
|
|
if customers.zero?
|
|
|
|
customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
|
|
|
|
puts " take #{customer_pool.length} customers"
|
|
|
|
else
|
2017-10-15 06:19:57 +00:00
|
|
|
roles = Role.where(name: [ 'Customer'])
|
2014-07-23 22:20:39 +00:00
|
|
|
groups_all = Group.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2021-09-20 10:47:05 +00:00
|
|
|
true_or_false = [true, false]
|
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..customers).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-10-05 12:00:33 +00:00
|
|
|
suffix = counter.to_s
|
2015-08-07 09:24:47 +00:00
|
|
|
organization = nil
|
2021-09-20 10:47:05 +00:00
|
|
|
if organization_pool.present? && true_or_false.sample
|
|
|
|
organization = organization_pool.sample
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
|
|
|
user = User.create_or_update(
|
2018-12-19 17:31:51 +00:00
|
|
|
login: "filldb-customer-#{suffix}",
|
|
|
|
firstname: "customer #{suffix}",
|
|
|
|
lastname: "customer #{suffix}",
|
|
|
|
email: "filldb-customer-#{suffix}@example.com",
|
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
2015-08-07 09:24:47 +00:00
|
|
|
organization: organization,
|
2018-12-19 17:31:51 +00:00
|
|
|
roles: roles,
|
2015-08-07 09:24:47 +00:00
|
|
|
)
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
customer_pool.push user
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create groups
|
|
|
|
group_pool = []
|
2020-11-05 16:31:00 +00:00
|
|
|
if groups.zero?
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2020-11-05 16:31:00 +00:00
|
|
|
group_pool = Group.where(active: true)
|
|
|
|
puts " take #{group_pool.length} groups"
|
|
|
|
else
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..groups).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-10-05 12:00:33 +00:00
|
|
|
group = Group.create!(name: "FillGroup::#{counter}", active: true)
|
2015-08-07 09:24:47 +00:00
|
|
|
group_pool.push group
|
2017-10-01 12:25:52 +00:00
|
|
|
Role.where(name: 'Agent').first.users.where(active: true).each do |user|
|
2015-08-07 09:24:47 +00:00
|
|
|
user_groups = user.groups
|
|
|
|
user_groups.push group
|
|
|
|
user.groups = user_groups
|
2017-10-15 06:19:57 +00:00
|
|
|
user.save!
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
2017-11-27 10:50:57 +00:00
|
|
|
# create overviews
|
|
|
|
if !overviews.zero?
|
|
|
|
(1..overviews).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2019-06-28 11:38:49 +00:00
|
|
|
Overview.create!(
|
2021-10-05 12:00:33 +00:00
|
|
|
name: "Filloverview::#{counter}",
|
2018-12-19 17:31:51 +00:00
|
|
|
role_ids: [Role.find_by(name: 'Agent').id],
|
2017-11-27 10:50:57 +00:00
|
|
|
condition: {
|
|
|
|
'ticket.state_id' => {
|
|
|
|
operator: 'is',
|
2018-12-19 17:31:51 +00:00
|
|
|
value: Ticket::State.by_category(:work_on_all).pluck(:id),
|
2017-11-27 10:50:57 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
order: {
|
|
|
|
by: 'created_at',
|
2017-11-27 10:50:57 +00:00
|
|
|
direction: 'ASC',
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
view: {
|
|
|
|
d: %w[title customer group state owner created_at],
|
|
|
|
s: %w[title customer group state owner created_at],
|
|
|
|
m: %w[number title customer group state owner created_at],
|
2017-11-27 10:50:57 +00:00
|
|
|
view_mode_default: 's',
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
active: true
|
2017-11-27 10:50:57 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-23 22:20:39 +00:00
|
|
|
# create tickets
|
|
|
|
priority_pool = Ticket::Priority.all
|
|
|
|
state_pool = Ticket::State.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
return if !tickets || tickets.zero?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
(1..tickets).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-09-20 10:47:05 +00:00
|
|
|
customer = customer_pool.sample
|
|
|
|
agent = agent_pool.sample
|
2017-11-23 08:09:44 +00:00
|
|
|
ticket = Ticket.create!(
|
2021-10-05 12:00:33 +00:00
|
|
|
title: "some title äöüß#{counter}",
|
2021-09-20 10:47:05 +00:00
|
|
|
group: group_pool.sample,
|
2018-12-19 17:31:51 +00:00
|
|
|
customer: customer,
|
|
|
|
owner: agent,
|
2021-09-20 10:47:05 +00:00
|
|
|
state: state_pool.sample,
|
|
|
|
priority: priority_pool.sample,
|
2017-11-23 08:09:44 +00:00
|
|
|
updated_by_id: agent.id,
|
|
|
|
created_by_id: agent.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# create article
|
2019-06-28 11:38:49 +00:00
|
|
|
Ticket::Article.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
ticket_id: ticket.id,
|
|
|
|
from: customer.email,
|
|
|
|
to: 'some_recipient@example.com',
|
2021-10-05 12:00:33 +00:00
|
|
|
subject: "some subject#{counter}",
|
|
|
|
message_id: "some@id-#{counter}",
|
2018-12-19 17:31:51 +00:00
|
|
|
body: 'some message ...',
|
|
|
|
internal: false,
|
|
|
|
sender: Ticket::Article::Sender.where(name: 'Customer').first,
|
|
|
|
type: Ticket::Article::Type.where(name: 'phone').first,
|
2017-11-23 08:09:44 +00:00
|
|
|
updated_by_id: agent.id,
|
|
|
|
created_by_id: agent.id,
|
|
|
|
)
|
|
|
|
puts " Ticket #{ticket.number} created"
|
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
end
|
2021-10-05 12:00:33 +00:00
|
|
|
|
|
|
|
def self.counter
|
|
|
|
@counter ||= SecureRandom.random_number(1_000_000)
|
|
|
|
@counter += 1
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|
2018-04-12 14:57:37 +00:00
|
|
|
# rubocop:enable Rails/Output
|