ticket_overview_list.rb 3.2 KB

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