ticket_reopened.rb 3.1 KB

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