fill_db.rb 24 KB

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