ticket_merged.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Report::TicketMerged < Report::Base
  3. # Returns amount of merged tickets in a given time range
  4. # sliced by given time interval.
  5. #
  6. # @example
  7. #
  8. # result = Report::TicketMerged.aggs(
  9. # range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  10. # range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  11. # interval: 'month', # month, week, day, hour, minute, second
  12. # selector: selector, # ticket selector to get only a collection of tickets
  13. # timezone: 'Europe/Berlin',
  14. # )
  15. # #=> [4,5,1,5,0,51,5,56,7,4]
  16. #
  17. # @return [Array<Integer>]
  18. def self.aggs(params_origin)
  19. params = params_origin.deep_dup
  20. Array.new(interval_length(params)) do |_counter|
  21. case params[:interval]
  22. when 'month'
  23. params[:range_end] = params[:range_start].next_month
  24. when 'week', 'day'
  25. params[:range_end] = params[:range_start].next_day
  26. when 'hour'
  27. params[:range_end] = params[:range_start] + 1.hour
  28. when 'minute'
  29. params[:range_end] = params[:range_start] + 1.minute
  30. end
  31. count = history_count(query_params(params))
  32. params[:range_start] = params[:range_end]
  33. count
  34. end
  35. end
  36. # Returns merged tickets in a given time range matching the selector
  37. #
  38. # @example
  39. #
  40. # result = Report::TicketMerged.items(
  41. # range_start: Time.zone.parse('2015-01-01T00:00:00Z'),
  42. # range_end: Time.zone.parse('2015-12-31T23:59:59Z'),
  43. # selector: selector, # ticket selector to get only a collection of tickets
  44. # )
  45. #
  46. # #=> {
  47. # count: 123,
  48. # ticket_ids: [4,5,1,5,0,51,5,56,7,4],
  49. # assets: assets,
  50. # }
  51. #
  52. # @return [Hash]
  53. def self.items(params)
  54. result = history(query_params(params))
  55. if params[:sheet].blank?
  56. result[:assets] = ApplicationModel::CanAssets
  57. .reduce(Ticket.where(id: result[:ticket_ids]), {})
  58. end
  59. result
  60. end
  61. def self.query_params(params)
  62. {
  63. object: 'Ticket',
  64. type: 'updated',
  65. attribute: 'state',
  66. start: params[:range_start],
  67. end: params[:range_end],
  68. selector: params[:selector],
  69. id_to: Ticket::State.lookup(name: 'merged').id,
  70. }
  71. end
  72. end