ticket_overview_list.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
  2. def self.reset(user_id)
  3. key = "TicketOverviewPull::#{user_id}"
  4. Cache.write(key, { needed: true })
  5. end
  6. def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8)
  7. @user = user
  8. @client = client
  9. @client_id = client_id
  10. @ttl = ttl
  11. @asset_lookup = asset_lookup
  12. @last_change = nil
  13. @last_overview = {}
  14. @last_overview_change = nil
  15. @last_ticket_change = nil
  16. @time_now = Time.zone.now.to_i
  17. end
  18. def load
  19. # get whole collection
  20. index_and_lists = Ticket::Overviews.index(@user)
  21. # no data exists
  22. return if index_and_lists.blank?
  23. # no change exists
  24. return if @last_change == index_and_lists
  25. # remember last state
  26. @last_change = index_and_lists
  27. index_and_lists
  28. end
  29. def client_key
  30. "as::load::#{self.class}::#{@user.id}::#{@client_id}"
  31. end
  32. def work_needed?
  33. key = "TicketOverviewPull::#{@user.id}"
  34. if Cache.get(key)
  35. Cache.delete(key)
  36. return true
  37. end
  38. return false if Sessions::CacheIn.get(client_key)
  39. true
  40. end
  41. def push
  42. return if !work_needed?
  43. # reset check interval
  44. Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
  45. # check if min one ticket or overview has changed
  46. last_overview_change = Overview.latest_change
  47. last_ticket_change = Ticket.latest_change
  48. return if last_ticket_change == @last_ticket_change && last_overview_change == @last_overview_change
  49. @last_overview_change = last_overview_change
  50. @last_ticket_change = last_ticket_change
  51. # load current data
  52. index_and_lists = load
  53. return if !index_and_lists
  54. # push overview index
  55. indexes = []
  56. index_and_lists.each do |index|
  57. assets = {}
  58. overview = Overview.lookup(id: index[:overview][:id])
  59. meta = {
  60. name: overview.name,
  61. prio: overview.prio,
  62. link: overview.link,
  63. count: index[:count],
  64. }
  65. indexes.push meta
  66. end
  67. if @client
  68. @client.log "push overview_index for user #{@user.id}"
  69. @client.send(
  70. event: 'ticket_overview_index',
  71. data: indexes,
  72. )
  73. end
  74. @time_now = Time.zone.now.to_i
  75. # push overviews
  76. results = []
  77. index_and_lists.each do |data|
  78. # do not deliver unchanged lists
  79. next if @last_overview[data[:overview][:id]] == data
  80. @last_overview[data[:overview][:id]] = data
  81. assets = {}
  82. overview = Overview.lookup(id: data[:overview][:id])
  83. if asset_needed?(overview)
  84. assets = asset_push(overview, assets)
  85. end
  86. data[:tickets].each do |ticket_meta|
  87. next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
  88. ticket = Ticket.lookup(id: ticket_meta[:id])
  89. next if !ticket
  90. assets = asset_push(ticket, assets)
  91. end
  92. data[:assets] = assets
  93. if !@client
  94. result = {
  95. event: 'ticket_overview_list',
  96. data: data,
  97. }
  98. results.push result
  99. else
  100. @client.log "push overview_list #{overview.link} for user #{@user.id}"
  101. # send update to browser
  102. @client.send(
  103. event: 'ticket_overview_list',
  104. data: data,
  105. )
  106. end
  107. end
  108. return results if !@client
  109. nil
  110. end
  111. end