ticket_stats.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module TicketStats
  3. extend ActiveSupport::Concern
  4. private
  5. def ticket_ids_and_assets(condition, current_user, limit, assets)
  6. tickets = Ticket.search(
  7. limit: limit,
  8. condition: condition,
  9. current_user: current_user,
  10. sort_by: 'created_at',
  11. order_by: 'desc',
  12. )
  13. assets_of_tickets(tickets, assets)
  14. end
  15. def ticket_stats_last_year(condition)
  16. volume_by_year = []
  17. now = Time.zone.now
  18. (0..11).each do |month_back| # rubocop:disable Style/EachForSimpleLoop
  19. date_to_check = now - month_back.month
  20. date_start = "#{date_to_check.year}-#{date_to_check.month}-01 00:00:00"
  21. date_end = "#{date_to_check.year}-#{date_to_check.month}-#{date_to_check.end_of_month.day} 00:00:00"
  22. # created
  23. created = TicketPolicy::ReadScope.new(current_user).resolve
  24. .where(created_at: (date_start..date_end))
  25. .where(condition)
  26. .count
  27. # closed
  28. closed = TicketPolicy::ReadScope.new(current_user).resolve
  29. .where(close_at: (date_start..date_end))
  30. .where(condition)
  31. .count
  32. data = {
  33. month: date_to_check.month,
  34. year: date_to_check.year,
  35. text: Date::MONTHNAMES[date_to_check.month],
  36. created: created,
  37. closed: closed,
  38. }
  39. volume_by_year.push data
  40. end
  41. volume_by_year
  42. end
  43. def assets_of_tickets(tickets, assets)
  44. ticket_ids = []
  45. tickets.each do |ticket|
  46. ticket_ids.push ticket.id
  47. assets = ticket.assets(assets)
  48. end
  49. ticket_ids
  50. end
  51. end