event.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. acts_as_mappable
  9. json_serialize :payload
  10. belongs_to :user, optional: true
  11. belongs_to :agent, :counter_cache => true
  12. has_many :agent_logs_as_inbound_event, :class_name => "AgentLog", :foreign_key => :inbound_event_id, :dependent => :nullify
  13. has_many :agent_logs_as_outbound_event, :class_name => "AgentLog", :foreign_key => :outbound_event_id, :dependent => :nullify
  14. scope :recent, lambda { |timespan = 12.hours.ago|
  15. where("events.created_at > ?", timespan)
  16. }
  17. after_create :update_agent_last_event_at
  18. after_create :possibly_propagate
  19. scope :expired, lambda {
  20. where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
  21. }
  22. case ActiveRecord::Base.connection.adapter_name
  23. when /\Amysql/i
  24. # Protect the Event table from InnoDB's AUTO_INCREMENT Counter
  25. # Initialization by always keeping the latest event.
  26. scope :to_expire, -> { expired.where.not(id: maximum(:id)) }
  27. else
  28. scope :to_expire, -> { expired }
  29. end
  30. scope :with_location, -> {
  31. where.not(lat: nil).where.not(lng: nil)
  32. }
  33. def location
  34. @location ||= Location.new(
  35. # lat and lng are BigDecimal, but converted to Float by the Location class
  36. lat: lat,
  37. lng: lng,
  38. radius:
  39. begin
  40. h = payload[:horizontal_accuracy].presence
  41. v = payload[:vertical_accuracy].presence
  42. if h && v
  43. (h.to_f + v.to_f) / 2
  44. else
  45. (h || v || payload[:accuracy]).to_f
  46. end
  47. end,
  48. course: payload[:course],
  49. speed: payload[:speed].presence)
  50. end
  51. def location=(location)
  52. case location
  53. when nil
  54. self.lat = self.lng = nil
  55. return
  56. when Location
  57. else
  58. location = Location.new(location)
  59. end
  60. self.lat, self.lng = location.lat, location.lng
  61. location
  62. end
  63. # Emit this event again, as a new Event.
  64. def reemit!
  65. agent.create_event :payload => payload, :lat => lat, :lng => lng
  66. end
  67. # Look for Events whose `expires_at` is present and in the past. Remove those events and then update affected Agents'
  68. # `events_counts` cache columns. This method is called by bin/schedule.rb periodically.
  69. def self.cleanup_expired!
  70. transaction do
  71. affected_agents = Event.expired.group("agent_id").pluck(:agent_id)
  72. Event.to_expire.delete_all
  73. Agent.where(id: affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)"
  74. end
  75. end
  76. protected
  77. def update_agent_last_event_at
  78. agent.touch :last_event_at
  79. end
  80. def possibly_propagate
  81. #immediately schedule agents that want immediate updates
  82. propagate_ids = agent.receivers.where(:propagate_immediately => true).pluck(:id)
  83. Agent.receive!(:only_receivers => propagate_ids) unless propagate_ids.empty?
  84. end
  85. end
  86. class EventDrop
  87. def initialize(object)
  88. @payload = object.payload
  89. super
  90. end
  91. def liquid_method_missing(key)
  92. @payload[key]
  93. end
  94. def each(&block)
  95. @payload.each(&block)
  96. end
  97. def agent
  98. @payload.fetch(__method__) {
  99. @object.agent
  100. }
  101. end
  102. def created_at
  103. @payload.fetch(__method__) {
  104. @object.created_at
  105. }
  106. end
  107. def _location_
  108. @object.location
  109. end
  110. def as_json
  111. {location: _location_.as_json, agent: @object.agent.to_liquid.as_json, payload: @payload.as_json, created_at: created_at.as_json}
  112. end
  113. end