forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_db.rb
212 lines (191 loc) · 6.3 KB
/
fill_db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# rubocop:disable Rails/Output
module FillDB
=begin
fill your database with demo records
FillDB.load(
agents: 50,
customers: 1000,
groups: 20,
organizations: 40,
overviews: 5,
tickets: 100,
)
or if you only want to create 100 tickets
FillDB.load(tickets: 100)
FillDB.load(agents: 20)
FillDB.load(overviews: 20)
FillDB.load(tickets: 10000)
=end
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
overviews = params[:overviews] || 0
tickets = params[:tickets] || 0
puts 'load db with:'
puts " agents:#{agents}"
puts " customers:#{customers}"
puts " groups:#{groups}"
puts " organizations:#{organizations}"
puts " overviews:#{overviews}"
puts " tickets:#{tickets}"
# set current user
UserInfo.current_user_id = 1
# organizations
organization_pool = []
if organizations.zero?
organization_pool = Organization.where(active: true)
puts " take #{organization_pool.length} organizations"
else
(1..organizations).each do
ActiveRecord::Base.transaction do
organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
organization_pool.push organization
end
end
end
# create agents
agent_pool = []
if agents.zero?
agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
puts " take #{agent_pool.length} agents"
else
roles = Role.where(name: [ 'Agent'])
groups_all = Group.all
(1..agents).each do
ActiveRecord::Base.transaction do
suffix = rand(99_999).to_s
user = User.create_or_update(
login: "filldb-agent-#{suffix}",
firstname: "agent #{suffix}",
lastname: "agent #{suffix}",
email: "filldb-agent-#{suffix}@example.com",
password: 'agentpw',
active: true,
roles: roles,
groups: groups_all,
)
sleep nice
agent_pool.push user
end
end
end
# create customer
customer_pool = []
if customers.zero?
customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
puts " take #{customer_pool.length} customers"
else
roles = Role.where(name: [ 'Customer'])
groups_all = Group.all
(1..customers).each do
ActiveRecord::Base.transaction do
suffix = rand(99_999).to_s
organization = nil
if organization_pool.present? && rand(2) == 1
organization = organization_pool[ organization_pool.length - 1 ]
end
user = User.create_or_update(
login: "filldb-customer-#{suffix}",
firstname: "customer #{suffix}",
lastname: "customer #{suffix}",
email: "filldb-customer-#{suffix}@example.com",
password: 'customerpw',
active: true,
organization: organization,
roles: roles,
)
sleep nice
customer_pool.push user
end
end
end
# create groups
group_pool = []
if groups.zero?
group_pool = Group.where(active: true)
puts " take #{group_pool.length} groups"
else
(1..groups).each do
ActiveRecord::Base.transaction do
group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
group_pool.push group
Role.where(name: 'Agent').first.users.where(active: true).each do |user|
user_groups = user.groups
user_groups.push group
user.groups = user_groups
user.save!
end
sleep nice
end
end
end
# create overviews
if !overviews.zero?
(1..overviews).each do
ActiveRecord::Base.transaction do
Overview.create!(
name: "Filloverview::#{rand(999_999)}",
role_ids: [Role.find_by(name: 'Agent').id],
condition: {
'ticket.state_id' => {
operator: 'is',
value: Ticket::State.by_category(:work_on_all).pluck(:id),
},
},
order: {
by: 'created_at',
direction: 'ASC',
},
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],
view_mode_default: 's',
},
active: true
)
end
end
end
# create tickets
priority_pool = Ticket::Priority.all
state_pool = Ticket::State.all
return if !tickets || tickets.zero?
(1..tickets).each do
ActiveRecord::Base.transaction do
customer = customer_pool[ rand(customer_pool.length - 1) ]
agent = agent_pool[ rand(agent_pool.length - 1) ]
ticket = Ticket.create!(
title: "some title äöüß#{rand(999_999)}",
group: group_pool[ rand(group_pool.length - 1) ],
customer: customer,
owner: agent,
state: state_pool[ rand(state_pool.length - 1) ],
priority: priority_pool[ rand(priority_pool.length - 1) ],
updated_by_id: agent.id,
created_by_id: agent.id,
)
# create article
Ticket::Article.create!(
ticket_id: ticket.id,
from: customer.email,
to: '[email protected]',
subject: "some subject#{rand(999_999)}",
message_id: "some@id-#{rand(999_999)}",
body: 'some message ...',
internal: false,
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,
)
puts " Ticket #{ticket.number} created"
sleep nice
end
end
end
end
# rubocop:enable Rails/Output