event.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. require 'location'
  2. # Events are how Huginn Agents communicate and log information about the world. Events can be emitted and received by
  3. # Agents. They contain a serialized `payload` of arbitrary JSON data, as well as optional `lat`, `lng`, and `expires_at`
  4. # fields.
  5. class Event < ActiveRecord::Base
  6. include JSONSerializedField
  7. include LiquidDroppable
  8. attr_accessible :lat, :lng, :location, :payload, :user_id, :user, :expires_at
  9. acts_as_mappable
  10. json_serialize :payload
  11. belongs_to :user
  12. belongs_to :agent, :counter_cache => true
  13. has_many :agent_logs_as_inbound_event, :class_name => "AgentLog", :foreign_key => :inbound_event_id, :dependent => :nullify
  14. has_many :agent_logs_as_outbound_event, :class_name => "AgentLog", :foreign_key => :outbound_event_id, :dependent => :nullify
  15. scope :recent, lambda { |timespan = 12.hours.ago|
  16. where("events.created_at > ?", timespan)
  17. }
  18. after_create :update_agent_last_event_at
  19. after_create :possibly_propagate
  20. scope :expired, lambda {
  21. where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
  22. }
  23. scope :with_location, -> {
  24. where.not(lat: nil).where.not(lng: nil)
  25. }
  26. def location
  27. @location ||= Location.new(
  28. # lat and lng are BigDecimal, but converted to Float by the Location class
  29. lat: lat,
  30. lng: lng,
  31. radius:
  32. begin
  33. h = payload[:horizontal_accuracy].presence
  34. v = payload[:vertical_accuracy].presence
  35. if h && v
  36. (h.to_f + v.to_f) / 2
  37. else
  38. (h || v || payload[:accuracy]).to_f
  39. end
  40. end,
  41. course: payload[:course],
  42. speed: payload[:speed].presence)
  43. end
  44. def location=(location)
  45. case location
  46. when nil
  47. self.lat = self.lng = nil
  48. return
  49. when Location
  50. else
  51. location = Location.new(location)
  52. end
  53. self.lat, self.lng = location.lat, location.lng
  54. location
  55. end
  56. # Emit this event again, as a new Event.
  57. def reemit!
  58. agent.create_event :payload => payload, :lat => lat, :lng => lng
  59. end
  60. # Look for Events whose `expires_at` is present and in the past. Remove those events and then update affected Agents'
  61. # `events_counts` cache columns. This method is called by bin/schedule.rb periodically.
  62. def self.cleanup_expired!
  63. affected_agents = Event.expired.group("agent_id").pluck(:agent_id)
  64. Event.expired.delete_all
  65. Agent.where(:id => affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)"
  66. end
  67. protected
  68. def update_agent_last_event_at
  69. agent.touch :last_event_at
  70. end
  71. def possibly_propagate
  72. #immediately schedule agents that want immediate updates
  73. propagate_ids = agent.receivers.where(:propagate_immediately => true).pluck(:id)
  74. Agent.receive!(:only_receivers => propagate_ids) unless propagate_ids.empty?
  75. end
  76. end
  77. class EventDrop
  78. def initialize(object)
  79. @payload = object.payload
  80. super
  81. end
  82. def before_method(key)
  83. @payload[key]
  84. end
  85. def each(&block)
  86. @payload.each(&block)
  87. end
  88. def agent
  89. @payload.fetch(__method__) {
  90. @object.agent
  91. }
  92. end
  93. def created_at
  94. @payload.fetch(__method__) {
  95. @object.created_at
  96. }
  97. end
  98. def _location_
  99. @object.location
  100. end
  101. end