Няма описание http://j1x-huginn.herokuapp.com

event.rb 1.3KB

1234567891011121314151617181920212223242526272829303132333435
  1. require 'json_serialized_field'
  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. attr_accessible :lat, :lng, :payload, :user_id, :user, :expires_at
  8. acts_as_mappable
  9. json_serialize :payload
  10. belongs_to :user
  11. belongs_to :agent, :counter_cache => true, :touch => :last_event_at
  12. scope :recent, lambda { |timespan = 12.hours.ago|
  13. where("events.created_at > ?", timespan)
  14. }
  15. # Emit this event again, as a new Event.
  16. def reemit!
  17. agent.create_event :payload => payload, :lat => lat, :lng => lng
  18. end
  19. # Look for Events whose `expires_at` is present and in the past. Remove those events and then update affected Agents'
  20. # `events_counts` cache columns. This method is called by bin/schedule.rb periodically.
  21. def self.cleanup_expired!
  22. affected_agents = Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).group("agent_id").pluck(:agent_id)
  23. Event.where("expires_at IS NOT NULL AND expires_at < ?", Time.now).delete_all
  24. Agent.where(:id => affected_agents).update_all "events_count = (select count(*) from events where agent_id = agents.id)"
  25. end
  26. end