stats_store.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class StatsStore < ApplicationModel
  3. store :data
  4. =begin
  5. count = StatsStore.count_by_search(
  6. object: 'User',
  7. o_id: ticket.owner_id,
  8. key: 'ticket:reopen',
  9. start: Time.zone.now - 7.days,
  10. end: Time.zone.now,
  11. )
  12. =end
  13. def self.count_by_search(data)
  14. # lookups
  15. if data[:object]
  16. object_id = ObjectLookup.by_name( data[:object] )
  17. end
  18. StatsStore.where(stats_store_object_id: object_id, o_id: data[:o_id], key: data[:key])
  19. .where('created_at > ? AND created_at < ?', data[:start], data[:end]).count
  20. end
  21. =begin
  22. item = StatsStore.search(
  23. object: 'User',
  24. o_id: current_user.id,
  25. key: 'dashboard',
  26. )
  27. =end
  28. def self.search(data)
  29. # lookups
  30. if data[:object]
  31. data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
  32. data.delete(:object)
  33. end
  34. find_by(data)
  35. end
  36. =begin
  37. item = StatsStore.sync(
  38. object: 'User',
  39. o_id: current_user.id,
  40. key: 'dashboard',
  41. data: {some data},
  42. )
  43. =end
  44. def self.sync(params)
  45. data = params[:data]
  46. params.delete(:data)
  47. item = search(params)
  48. if item
  49. item.data = data
  50. item.save
  51. return item
  52. end
  53. # lookups
  54. if data[:object]
  55. data[:stats_store_object_id] = ObjectLookup.by_name( data[:object] )
  56. data.delete(:object)
  57. end
  58. params[:data] = data
  59. params[:created_by_id] = 1
  60. create(params)
  61. end
  62. =begin
  63. StatsStore.add(
  64. object: 'User',
  65. o_id: ticket.owner_id,
  66. key: 'ticket:reopen',
  67. data: { ticket_id: ticket.id },
  68. created_at: Time.zone.now,
  69. )
  70. =end
  71. def self.add(data)
  72. # lookups
  73. if data[:object]
  74. object_id = ObjectLookup.by_name( data[:object] )
  75. end
  76. # create history
  77. record = {
  78. stats_store_object_id: object_id,
  79. o_id: data[:o_id],
  80. key: data[:key],
  81. data: data[:data],
  82. created_at: data[:created_at],
  83. created_by_id: data[:created_by_id],
  84. }
  85. StatsStore.create(record)
  86. end
  87. =begin
  88. cleanup old stats store
  89. StatsStore.cleanup
  90. optional you can put the max oldest stats store entries as argument
  91. StatsStore.cleanup(3.months)
  92. =end
  93. def self.cleanup(diff = 3.months)
  94. StatsStore.where('updated_at < ?', Time.zone.now - diff).delete_all
  95. true
  96. end
  97. end