ticket_overview_list.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
  2. def self.reset(user_id)
  3. Cache.write("TicketOverviewPull::#{user_id}", { needed: true })
  4. end
  5. def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 7)
  6. @user = user
  7. @client = client
  8. @client_id = client_id
  9. @ttl = ttl
  10. @asset_lookup = asset_lookup
  11. @last_index_lists = nil
  12. @last_overview = {}
  13. @last_overview_change = nil
  14. @last_ticket_change = nil
  15. @last_full_fetch = nil
  16. end
  17. def self.overview_history_append(overview, user_id)
  18. key = "TicketOverviewHistory::#{user_id}"
  19. history = Cache.get(key) || []
  20. history.prepend overview
  21. history.uniq!
  22. if history.count > 4
  23. history.pop
  24. end
  25. Cache.write(key, history)
  26. end
  27. def self.overview_history_get(user_id)
  28. Cache.get("TicketOverviewHistory::#{user_id}")
  29. end
  30. def load
  31. # get whole collection
  32. index_and_lists = nil
  33. local_overview_changed = overview_changed?
  34. if !@last_index_lists || !@last_full_fetch || @last_full_fetch < (Time.zone.now.to_i - 60) || local_overview_changed
  35. # check if min one ticket has changed
  36. return if !ticket_changed?(true) && !local_overview_changed
  37. index_and_lists = Ticket::Overviews.index(@user)
  38. @last_full_fetch = Time.zone.now.to_i
  39. else
  40. # check if min one ticket has changed
  41. return if !ticket_changed? && !local_overview_changed
  42. index_and_lists_local = Ticket::Overviews.index(@user, Sessions::Backend::TicketOverviewList.overview_history_get(@user.id))
  43. # compare index_and_lists_local to index_and_lists_local
  44. # return if no changes
  45. index_and_lists = []
  46. @last_index_lists.each do |last_index|
  47. found_in_particular_index = false
  48. index_and_lists_local.each do |local_index|
  49. next if local_index[:overview][:id] != last_index[:overview][:id]
  50. index_and_lists.push local_index
  51. found_in_particular_index = true
  52. break
  53. end
  54. next if found_in_particular_index == true
  55. index_and_lists.push last_index
  56. end
  57. end
  58. # no data exists
  59. return if index_and_lists.blank?
  60. # no change exists
  61. return if @last_index_lists == index_and_lists
  62. # remember last state
  63. @last_index_lists = index_and_lists
  64. index_and_lists
  65. end
  66. def local_to_run?
  67. return false if !@time_now
  68. return true if pull_overview?
  69. false
  70. end
  71. def pull_overview?
  72. result = Cache.get("TicketOverviewPull::#{@user.id}")
  73. Cache.delete("TicketOverviewPull::#{@user.id}") if result
  74. return true if result
  75. false
  76. end
  77. def push
  78. return if !to_run? && !local_to_run?
  79. @time_now = Time.zone.now.to_i
  80. # load current data
  81. index_and_lists = load
  82. return if !index_and_lists
  83. # push overview index
  84. indexes = []
  85. index_and_lists.each do |index|
  86. overview = Overview.lookup(id: index[:overview][:id])
  87. next if !overview
  88. meta = {
  89. name: overview.name,
  90. prio: overview.prio,
  91. link: overview.link,
  92. count: index[:count],
  93. }
  94. indexes.push meta
  95. end
  96. if @client
  97. @client.log "push overview_index for user #{@user.id}"
  98. @client.send(
  99. event: 'ticket_overview_index',
  100. data: indexes,
  101. )
  102. end
  103. @time_now = Time.zone.now.to_i
  104. # push overviews
  105. results = []
  106. assets = AssetsSet.new
  107. index_and_lists.each do |data|
  108. # do not deliver unchanged lists
  109. next if @last_overview[data[:overview][:id]] == [data[:tickets], data[:overview]]
  110. @last_overview[data[:overview][:id]] = [data[:tickets], data[:overview]]
  111. overview = Overview.lookup(id: data[:overview][:id])
  112. next if !overview
  113. if asset_needed?(overview)
  114. assets = asset_push(overview, assets)
  115. end
  116. data[:tickets].each do |ticket_meta|
  117. next if !asset_needed_by_updated_at?('Ticket', ticket_meta[:id], ticket_meta[:updated_at])
  118. ticket = Ticket.lookup(id: ticket_meta[:id])
  119. next if !ticket
  120. assets = asset_push(ticket, assets)
  121. end
  122. data[:assets] = assets.to_h
  123. if !@client
  124. result = {
  125. event: 'ticket_overview_list',
  126. data: data,
  127. }
  128. results.push result
  129. else
  130. @client.log "push overview_list #{overview.link} for user #{@user.id}"
  131. # send update to browser
  132. @client.send(
  133. event: 'ticket_overview_list',
  134. data: data,
  135. )
  136. end
  137. assets.flush
  138. end
  139. return results if !@client
  140. nil
  141. end
  142. def overview_changed?
  143. # check if min one overview has changed
  144. last_overview_change = Overview.latest_change
  145. return false if last_overview_change == @last_overview_change
  146. @last_overview_change = last_overview_change
  147. true
  148. end
  149. def ticket_changed?(reset = false)
  150. # check if min one ticket has changed
  151. last_ticket_change = Ticket.latest_change
  152. return false if last_ticket_change == @last_ticket_change
  153. @last_ticket_change = last_ticket_change if reset
  154. true
  155. end
  156. end