ticket_reopened.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Report::TicketReopened < Report::Base
  3. =begin
  4. result = Report::TicketReopened.aggs(
  5. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  6. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  7. interval: 'month', # quarter, month, week, day, hour, minute, second
  8. selector: selector, # ticket selector to get only a collection of tickets
  9. timezone: 'Europe/Berlin',
  10. )
  11. returns
  12. [4,5,1,5,0,51,5,56,7,4]
  13. =end
  14. def self.aggs(params_origin)
  15. params = params_origin.dup
  16. ticket_state_ids = ticket_ids
  17. result = []
  18. case params[:interval]
  19. when 'month'
  20. stop_interval = 12
  21. when 'week'
  22. stop_interval = 7
  23. when 'day'
  24. stop_interval = 31
  25. when 'hour'
  26. stop_interval = 24
  27. when 'minute'
  28. stop_interval = 60
  29. end
  30. (1..stop_interval).each do |_counter|
  31. case params[:interval]
  32. when 'month'
  33. params[:range_end] = params[:range_start].next_month
  34. when 'week', 'day'
  35. params[:range_end] = params[:range_start].next_day
  36. when 'hour'
  37. params[:range_end] = params[:range_start] + 1.hour
  38. when 'minute'
  39. params[:range_end] = params[:range_start] + 1.minute
  40. end
  41. without_merged_tickets = {
  42. 'ticket_state.name' => {
  43. 'operator' => 'is not',
  44. 'value' => 'merged'
  45. }
  46. }
  47. params[:selector].merge!(without_merged_tickets) # do not show merged tickets in reports
  48. count = history_count(
  49. object: 'Ticket',
  50. type: 'updated',
  51. attribute: 'state',
  52. id_from: ticket_state_ids,
  53. id_not_to: ticket_state_ids,
  54. start: params[:range_start],
  55. end: params[:range_end],
  56. selector: params[:selector]
  57. )
  58. result.push count
  59. params[:range_start] = params[:range_end]
  60. end
  61. result
  62. end
  63. =begin
  64. result = Report::TicketReopened.items(
  65. range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  66. range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  67. selector: selector, # ticket selector to get only a collection of tickets
  68. timezone: 'Europe/Berlin',
  69. )
  70. returns
  71. {
  72. count: 123,
  73. ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  74. assets: assets,
  75. }
  76. =end
  77. def self.items(params)
  78. ticket_state_ids = ticket_ids
  79. result = history(
  80. object: 'Ticket',
  81. type: 'updated',
  82. attribute: 'state',
  83. id_from: ticket_state_ids,
  84. id_not_to: ticket_state_ids,
  85. start: params[:range_start],
  86. end: params[:range_end],
  87. selector: params[:selector]
  88. )
  89. return result if params[:sheet].present?
  90. assets = {}
  91. result[:ticket_ids].each do |ticket_id|
  92. ticket_full = Ticket.find(ticket_id)
  93. assets = ticket_full.assets(assets)
  94. end
  95. result[:assets] = assets
  96. result
  97. end
  98. def self.ticket_ids
  99. key = 'Report::TicketReopened::StateList'
  100. ticket_state_ids = Rails.cache.read(key)
  101. return ticket_state_ids if ticket_state_ids
  102. ticket_state_types = Ticket::StateType.where(name: %w[closed merged removed])
  103. ticket_state_ids = []
  104. ticket_state_types.each do |ticket_state_type|
  105. ticket_state_type.states.each do |ticket_state|
  106. ticket_state_ids.push ticket_state.id
  107. end
  108. end
  109. Rails.cache.write(key, ticket_state_ids, { expires_in: 2.days })
  110. ticket_state_ids
  111. end
  112. end