fill_db.rb 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # rubocop:disable Rails/Output
  3. module FillDb
  4. =begin
  5. fill your database with demo records
  6. FillDb.load(
  7. object_manager_attributes: {
  8. user: {
  9. 'input': 1,
  10. 'multiselect': 1,
  11. },
  12. ticket: {
  13. 'textarea': 1,
  14. 'multiselect': 1,
  15. },
  16. },
  17. agents: 50,
  18. customers: 1000,
  19. groups: 20,
  20. organizations: 40,
  21. overviews: 5,
  22. tickets: 100,
  23. knowledge_base_answers: 100,
  24. knowledge_base_categories: 20,
  25. public_links: 2,
  26. nice: 0,
  27. )
  28. or if you only want to create 100 tickets
  29. FillDb.load(tickets: 100, nice: 0)
  30. FillDb.load(tickets: 100, nice: 1, log: true)
  31. FillDb.load(agents: 20, nice: 0)
  32. FillDb.load(overviews: 20, nice: 0)
  33. FillDb.load(tickets: 10000, nice: 0)
  34. FillDb.load(knowledge_base_answers: 100, nice: 0)
  35. FillDb.load(knowledge_base_categories: 20, nice: 0)
  36. FillDb.load(public_links: 2, nice: 0)
  37. =end
  38. def self.load(params)
  39. params[:log] = params[:log] || false
  40. return load_data(params) if params[:log]
  41. Rails.logger.silence { load_data(params) }
  42. end
  43. def self.load_data(params)
  44. nice = params[:nice] || 0.5
  45. object_manager_attributes = params[:object_manager_attributes]
  46. agents = params[:agents] || 0
  47. customers = params[:customers] || 0
  48. groups = params[:groups] || 0
  49. organizations = params[:organizations] || 0
  50. overviews = params[:overviews] || 0
  51. tickets = params[:tickets] || 0
  52. knowledge_base_answers = params[:knowledge_base_answers] || 0
  53. knowledge_base_categories = params[:knowledge_base_categories] || 0
  54. public_links = params[:public_links] || 0
  55. puts 'load db with:'
  56. puts " object_manager_attributes: #{object_manager_attributes}"
  57. puts " agents: #{agents}"
  58. puts " customers: #{customers}"
  59. puts " groups: #{groups}"
  60. puts " organizations: #{organizations}"
  61. puts " overviews: #{overviews}"
  62. puts " tickets: #{tickets}"
  63. puts " knowledge_base_answers: #{knowledge_base_answers}"
  64. puts " knowledge_base_categories: #{knowledge_base_categories}"
  65. puts " public_links: #{public_links}"
  66. # set current user
  67. UserInfo.current_user_id = 1
  68. # create object attributes
  69. object_manager_attributes_value_lookup = {}
  70. if object_manager_attributes.present?
  71. ActiveRecord::Base.transaction do
  72. object_manager_attributes.each do |object, attribute_types|
  73. attribute_types.each do |attribute_type, amount|
  74. next if amount.zero?
  75. object_manager_attributes_value_lookup[object] ||= {}
  76. amount.times do |index|
  77. name = "#{attribute_type}_#{counter}"
  78. object_attribute_creation = public_send("create_object_attribute_type_#{attribute_type}",
  79. object: object,
  80. name: name,
  81. display: name,
  82. editable: true,
  83. active: true,
  84. screens: {
  85. create_middle: {
  86. '-all-' => {
  87. shown: true,
  88. required: false,
  89. },
  90. },
  91. create: {
  92. '-all-' => {
  93. shown: true,
  94. required: false,
  95. },
  96. },
  97. edit: {
  98. '-all-' => {
  99. shown: true,
  100. required: false,
  101. },
  102. },
  103. },
  104. to_migrate: true,
  105. to_delete: false,
  106. to_config: false,
  107. position: index + 1000)
  108. ObjectManager::Attribute.add(object_attribute_creation[:attribute_params])
  109. object_manager_attributes_value_lookup[object][name] = object_attribute_creation[:value]
  110. end
  111. end
  112. end
  113. ObjectManager::Attribute.migration_execute(false)
  114. end
  115. end
  116. # organizations
  117. organization_pool = []
  118. if organizations.zero?
  119. organization_pool = Organization.where(active: true)
  120. puts " take #{organization_pool.length} organizations"
  121. else
  122. (1..organizations).each do
  123. ActiveRecord::Base.transaction do
  124. create_params = {
  125. name: "FillOrganization::#{counter}",
  126. active: true
  127. }
  128. if object_manager_attributes_value_lookup[:organization].present?
  129. create_params = create_params.merge(object_manager_attributes_value_lookup[:organization])
  130. end
  131. organization = Organization.create!(create_params)
  132. organization_pool.push organization
  133. end
  134. end
  135. end
  136. # create agents
  137. agent_pool = []
  138. if agents.zero?
  139. agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
  140. puts " take #{agent_pool.length} agents"
  141. else
  142. roles = Role.where(name: [ 'Agent'])
  143. groups_all = Group.all
  144. (1..agents).each do
  145. ActiveRecord::Base.transaction do
  146. suffix = counter.to_s
  147. create_params = {
  148. login: "filldb-agent-#{suffix}",
  149. firstname: "agent #{suffix}",
  150. lastname: "agent #{suffix}",
  151. email: "filldb-agent-#{suffix}@example.com",
  152. password: 'agentpw',
  153. active: true,
  154. roles: roles,
  155. groups: groups_all,
  156. }
  157. if object_manager_attributes_value_lookup[:user].present?
  158. create_params = create_params.merge(object_manager_attributes_value_lookup[:user])
  159. end
  160. user = User.create_or_update(create_params)
  161. sleep nice
  162. agent_pool.push user
  163. end
  164. end
  165. end
  166. # create customer
  167. customer_pool = []
  168. if customers.zero?
  169. customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
  170. puts " take #{customer_pool.length} customers"
  171. else
  172. roles = Role.where(name: [ 'Customer'])
  173. groups_all = Group.all
  174. true_or_false = [true, false]
  175. (1..customers).each do
  176. ActiveRecord::Base.transaction do
  177. suffix = counter.to_s
  178. organization = nil
  179. if organization_pool.present? && true_or_false.sample
  180. organization = organization_pool.sample
  181. end
  182. create_params = {
  183. login: "filldb-customer-#{suffix}",
  184. firstname: "customer #{suffix}",
  185. lastname: "customer #{suffix}",
  186. email: "filldb-customer-#{suffix}@example.com",
  187. password: 'customerpw',
  188. active: true,
  189. organization: organization,
  190. roles: roles,
  191. }
  192. if object_manager_attributes_value_lookup[:user].present?
  193. create_params = create_params.merge(object_manager_attributes_value_lookup[:user])
  194. end
  195. user = User.create_or_update(create_params)
  196. sleep nice
  197. customer_pool.push user
  198. end
  199. end
  200. end
  201. # create groups
  202. group_pool = []
  203. if groups.zero?
  204. group_pool = Group.where(active: true)
  205. puts " take #{group_pool.length} groups"
  206. else
  207. (1..groups).each do
  208. ActiveRecord::Base.transaction do
  209. create_params = {
  210. name: "FillGroup::#{counter}",
  211. active: true,
  212. }
  213. if object_manager_attributes_value_lookup[:group].present?
  214. create_params = create_params.merge(object_manager_attributes_value_lookup[:group])
  215. end
  216. group = Group.create!(create_params)
  217. group_pool.push group
  218. Role.where(name: 'Agent').first.users.where(active: true).each do |user|
  219. user_groups = user.groups
  220. user_groups.push group
  221. user.groups = user_groups
  222. user.save!
  223. end
  224. sleep nice
  225. end
  226. end
  227. end
  228. # create overviews
  229. if !overviews.zero?
  230. (1..overviews).each do
  231. ActiveRecord::Base.transaction do
  232. Overview.create!(
  233. name: "Filloverview::#{counter}",
  234. role_ids: [Role.find_by(name: 'Agent').id],
  235. condition: {
  236. 'ticket.state_id' => {
  237. operator: 'is',
  238. value: Ticket::State.by_category(:work_on_all).pluck(:id),
  239. },
  240. },
  241. order: {
  242. by: 'created_at',
  243. direction: 'ASC',
  244. },
  245. view: {
  246. d: %w[title customer group state owner created_at],
  247. s: %w[title customer group state owner created_at],
  248. m: %w[number title customer group state owner created_at],
  249. view_mode_default: 's',
  250. },
  251. active: true
  252. )
  253. end
  254. end
  255. end
  256. # create tickets
  257. if tickets.positive?
  258. priority_pool = Ticket::Priority.all
  259. state_pool = Ticket::State.all
  260. tickets.times do
  261. ActiveRecord::Base.transaction do
  262. customer = customer_pool.sample
  263. agent = agent_pool.sample
  264. create_params = {
  265. title: "some title äöüß#{counter}",
  266. group: group_pool.sample,
  267. customer: customer,
  268. owner: agent,
  269. state: state_pool.sample,
  270. priority: priority_pool.sample,
  271. updated_by_id: agent.id,
  272. created_by_id: agent.id,
  273. }
  274. if object_manager_attributes_value_lookup[:ticket].present?
  275. create_params = create_params.merge(object_manager_attributes_value_lookup[:ticket])
  276. end
  277. ticket = Ticket.create!(create_params)
  278. # create article
  279. Ticket::Article.create!(
  280. ticket_id: ticket.id,
  281. from: customer.email,
  282. to: 'some_recipient@example.com',
  283. subject: "some subject#{counter}",
  284. message_id: "some@id-#{counter}",
  285. body: 'some message ...',
  286. internal: false,
  287. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  288. type: Ticket::Article::Type.where(name: 'phone').first,
  289. updated_by_id: agent.id,
  290. created_by_id: agent.id,
  291. )
  292. puts " Ticket #{ticket.number} created"
  293. sleep nice
  294. end
  295. end
  296. end
  297. knowledge_base = nil
  298. knowledge_base_categories_created = nil
  299. if knowledge_base_categories.positive?
  300. ActiveRecord::Base.transaction do
  301. knowledge_base = create_knowledge_base
  302. knowledge_base_categories_created = create_knowledge_base_categories(
  303. amount: knowledge_base_categories,
  304. knowledge_base_id: knowledge_base.id,
  305. locale_id: knowledge_base.kb_locales.first.id,
  306. sleep_time: nice,
  307. )
  308. end
  309. end
  310. if knowledge_base_answers.positive?
  311. ActiveRecord::Base.transaction do
  312. create_knowledge_base_answers(
  313. amount: knowledge_base_answers,
  314. categories_amount: knowledge_base_categories,
  315. categories: knowledge_base_categories_created,
  316. knowledge_base: knowledge_base,
  317. agents: agent_pool,
  318. sleep_time: nice,
  319. )
  320. end
  321. end
  322. return if public_links.zero?
  323. ActiveRecord::Base.transaction do
  324. create_public_links(
  325. amount: public_links,
  326. sleep_time: nice,
  327. )
  328. end
  329. end
  330. def self.counter
  331. @counter ||= SecureRandom.random_number(1_000_000)
  332. @counter += 1
  333. end
  334. def self.create_knowledge_base
  335. return KnowledgeBase.first if KnowledgeBase.count.positive?
  336. params = {
  337. iconset: 'FontAwesome',
  338. color_highlight: '#38ae6a',
  339. color_header: '#f9fafb',
  340. color_header_link: 'hsl(206,8%,50%)',
  341. homepage_layout: 'grid',
  342. category_layout: 'grid',
  343. active: true,
  344. kb_locales_attributes: [
  345. {
  346. system_locale_id: Locale.first.id,
  347. primary: true,
  348. },
  349. ],
  350. }
  351. clean_params = KnowledgeBase.association_name_to_id_convert(params)
  352. clean_params = KnowledgeBase.param_cleanup(clean_params, true)
  353. knowledge_base = KnowledgeBase.new(clean_params)
  354. knowledge_base.associations_from_param(params)
  355. knowledge_base.save!
  356. puts " KnowledgeBase #{knowledge_base.id} created"
  357. knowledge_base
  358. end
  359. def self.create_knowledge_base_categories(params)
  360. amount = params[:amount]
  361. knowledge_base_id = params[:knowledge_base_id]
  362. locale_id = params[:locale_id]
  363. sleep_time = params[:sleep_time]
  364. category_icons = %w[f1eb f143 f17c f109 f011 f275 f26c f0eb f2a3 f299 f0d0 f14e f26b f249 f108 f17a f09b f2a0 f20e f233]
  365. category_pool = []
  366. amount.times do |index|
  367. category = KnowledgeBase::Category.create!(
  368. knowledge_base_id: knowledge_base_id,
  369. category_icon: category_icons.sample,
  370. position: index
  371. )
  372. puts " KnowledgeBase::Category #{category.id} created"
  373. category_pool.push category
  374. category_translation = KnowledgeBase::Category::Translation.create!(
  375. title: "some title#{counter}",
  376. kb_locale_id: locale_id,
  377. category_id: category.id,
  378. )
  379. puts " KnowledgeBase::Category::Translation #{category_translation.title} created"
  380. sleep sleep_time
  381. end
  382. category_pool
  383. end
  384. def self.create_knowledge_base_answers(params)
  385. answers_amount = params[:amount]
  386. categories_amount = params[:categories_amount]
  387. categories = params[:categories]
  388. knowledge_base = params[:knowledge_base]
  389. agents = params[:agents]
  390. sleep_time = params[:sleep_time]
  391. if knowledge_base.blank?
  392. knowledge_base = create_knowledge_base
  393. end
  394. locale = knowledge_base.kb_locales.first
  395. category_pool = categories.presence || create_knowledge_base_categories(categories_amount, knowledge_base.id, locale.id, sleep_time)
  396. if category_pool.blank?
  397. puts " Found #{category_pool.count} categories, aborting!"
  398. return
  399. end
  400. answers_amount.times do |index|
  401. answer = KnowledgeBase::Answer.create!(
  402. category_id: category_pool.sample.id,
  403. promoted: false,
  404. position: index,
  405. )
  406. content = KnowledgeBase::Answer::Translation::Content.create!(
  407. body: '<div style="color:rgb(63, 63, 63);">
  408. <p>some content...</p>
  409. </div>'
  410. )
  411. agent = agents.sample
  412. KnowledgeBase::Answer::Translation.create!(
  413. title: "some title#{counter}",
  414. kb_locale_id: locale.id,
  415. answer_id: answer.id,
  416. content_id: content.id,
  417. created_by_id: agent.id,
  418. updated_by_id: agent.id,
  419. )
  420. puts " KnowledgeBase::Answer #{answer.id} created"
  421. sleep sleep_time
  422. end
  423. end
  424. def self.create_public_links(params)
  425. public_links_amount = params[:amount]
  426. sleep_time = params[:sleep_time]
  427. public_links_amount.times do |index|
  428. public_link = PublicLink.create!(
  429. title: "Example#{counter}",
  430. screen: %w[login signup],
  431. link: "https://zammad#{counter}.com",
  432. new_tab: true,
  433. prio: index,
  434. updated_by_id: 1,
  435. created_by_id: 1,
  436. )
  437. puts " PublicLink #{public_link.id} created"
  438. sleep sleep_time
  439. end
  440. end
  441. def self.create_object_attribute_type_input(params)
  442. {
  443. attribute_params: params.merge(
  444. data_type: 'input',
  445. data_option: {
  446. type: 'text',
  447. maxlength: 200,
  448. null: true,
  449. translate: false,
  450. }
  451. ),
  452. value: 'example value',
  453. }
  454. end
  455. def self.create_object_attribute_type_textarea(params)
  456. {
  457. attribute_params: params.merge(
  458. data_type: 'textarea',
  459. data_option: {
  460. type: 'textarea',
  461. maxlength: 200,
  462. rows: 4,
  463. null: true,
  464. translate: false,
  465. }
  466. ),
  467. value: "example value\nwith line break",
  468. }
  469. end
  470. def self.create_object_attribute_type_integer(params)
  471. {
  472. attribute_params: params.merge(
  473. data_type: 'integer',
  474. data_option: {
  475. default: 0,
  476. null: true,
  477. min: 0,
  478. max: 9999,
  479. }
  480. ),
  481. value: 99,
  482. }
  483. end
  484. def self.create_object_attribute_type_boolean(params)
  485. {
  486. attribute_params: params.merge(
  487. data_type: 'boolean',
  488. data_option: {
  489. default: false,
  490. null: true,
  491. options: {
  492. true => 'yes',
  493. false => 'no',
  494. }
  495. }
  496. ),
  497. value: true,
  498. }
  499. end
  500. def self.create_object_attribute_type_date(params)
  501. {
  502. attribute_params: params.merge(
  503. data_type: 'date',
  504. data_option: {
  505. diff: 24,
  506. null: true,
  507. }
  508. ),
  509. value: '2022-12-01',
  510. }
  511. end
  512. def self.create_object_attribute_type_datettime(params)
  513. {
  514. attribute_params: params.merge(
  515. data_type: 'datetime',
  516. data_option: {
  517. diff: 24,
  518. future: true,
  519. past: true,
  520. null: true,
  521. }
  522. ),
  523. value: '2022-10-01 12:00:00',
  524. }
  525. end
  526. def self.create_object_attribute_type_select(params)
  527. multiple = params[:data_type] == 'multiselect'
  528. {
  529. attribute_params: params.merge(
  530. data_type: params[:data_type] || 'select',
  531. data_option: {
  532. default: multiple ? [] : '',
  533. options: {
  534. 'key_1' => 'value_1',
  535. 'key_2' => 'value_2',
  536. 'key_3' => 'value_3',
  537. 'key_4' => 'value_4',
  538. },
  539. multiple: multiple,
  540. translate: true,
  541. nulloption: true,
  542. null: true,
  543. }
  544. ),
  545. value: multiple ? %w[key_1 key_3] : 'key_3',
  546. }
  547. end
  548. def self.create_object_attribute_type_multiselect(params)
  549. create_object_attribute_type_select(
  550. params.merge(
  551. data_type: 'multiselect',
  552. )
  553. )
  554. end
  555. def self.create_object_attribute_type_tree_select(params)
  556. multiple = params[:data_type] == 'multi_tree_select'
  557. {
  558. attribute_params: params.merge(
  559. data_type: params[:data_type] || 'tree_select',
  560. data_option: {
  561. default: multiple ? [] : '',
  562. options: [
  563. {
  564. 'name' => 'Incident',
  565. 'value' => 'Incident',
  566. 'children' => [
  567. {
  568. 'name' => 'Hardware',
  569. 'value' => 'Incident::Hardware',
  570. 'children' => [
  571. {
  572. 'name' => 'Monitor',
  573. 'value' => 'Incident::Hardware::Monitor'
  574. },
  575. {
  576. 'name' => 'Mouse',
  577. 'value' => 'Incident::Hardware::Mouse'
  578. },
  579. {
  580. 'name' => 'Keyboard',
  581. 'value' => 'Incident::Hardware::Keyboard'
  582. }
  583. ]
  584. },
  585. {
  586. 'name' => 'Softwareproblem',
  587. 'value' => 'Incident::Softwareproblem',
  588. 'children' => [
  589. {
  590. 'name' => 'CRM',
  591. 'value' => 'Incident::Softwareproblem::CRM'
  592. },
  593. {
  594. 'name' => 'EDI',
  595. 'value' => 'Incident::Softwareproblem::EDI'
  596. },
  597. {
  598. 'name' => 'SAP',
  599. 'value' => 'Incident::Softwareproblem::SAP',
  600. 'children' => [
  601. {
  602. 'name' => 'Authentication',
  603. 'value' => 'Incident::Softwareproblem::SAP::Authentication'
  604. },
  605. {
  606. 'name' => 'Not reachable',
  607. 'value' => 'Incident::Softwareproblem::SAP::Not reachable'
  608. }
  609. ]
  610. },
  611. {
  612. 'name' => 'MS Office',
  613. 'value' => 'Incident::Softwareproblem::MS Office',
  614. 'children' => [
  615. {
  616. 'name' => 'Excel',
  617. 'value' => 'Incident::Softwareproblem::MS Office::Excel'
  618. },
  619. {
  620. 'name' => 'PowerPoint',
  621. 'value' => 'Incident::Softwareproblem::MS Office::PowerPoint'
  622. },
  623. {
  624. 'name' => 'Word',
  625. 'value' => 'Incident::Softwareproblem::MS Office::Word'
  626. },
  627. {
  628. 'name' => 'Outlook',
  629. 'value' => 'Incident::Softwareproblem::MS Office::Outlook'
  630. }
  631. ]
  632. }
  633. ]
  634. }
  635. ]
  636. },
  637. {
  638. 'name' => 'Service request',
  639. 'value' => 'Service request',
  640. 'children' => [
  641. {
  642. 'name' => 'New software requirement',
  643. 'value' => 'Service request::New software requirement'
  644. },
  645. {
  646. 'name' => 'New hardware',
  647. 'value' => 'Service request::New hardware'
  648. },
  649. {
  650. 'name' => 'Consulting',
  651. 'value' => 'Service request::Consulting'
  652. }
  653. ]
  654. },
  655. {
  656. 'name' => 'Change request',
  657. 'value' => 'Change request'
  658. }
  659. ],
  660. multiple: multiple,
  661. translate: true,
  662. nulloption: true,
  663. null: true,
  664. }
  665. ),
  666. value: multiple ? ['Change request', 'Incident::Hardware::Monitor', 'Incident::Softwareproblem::MS Office::Word'] : 'Incident::Hardware::Monitor',
  667. }
  668. end
  669. def self.create_object_attribute_type_multi_tree_select(params)
  670. create_object_attribute_type_tree_select(
  671. params.merge(
  672. data_type: 'multi_tree_select',
  673. )
  674. )
  675. end
  676. end
  677. # rubocop:enable Rails/Output