ticket_reopened.rb 3.5 KB

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