You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3KB

  1. class Analysis
  2. include Mongoid::Document
  3. include Mongoid::Timestamps
  4. field :type, type: Symbol
  5. field :host, type: String
  6. field :port, type: Numeric
  7. field :pending, type: Boolean
  8. field :date, type: Time
  9. field :result, type: Array
  10. validates_presence_of :type
  11. validates_presence_of :host
  12. validates_presence_of :port
  13. validates_uniqueness_of :type, scope: %i[host port]
  14. index type: 1
  15. index({ type: 1, host: 1, port: 1 }, { unique: true })
  16. def self.[](type, host, port)
  17. key = self.key type, host, port
  18. self.where(key).first
  19. end
  20. def self.pending(type, host, port)
  21. analysis = self[type, host, port]
  22. if analysis
  23. analysis.remove_attribute :result
  24. analysis.update_attributes pending: true, date: Time.now
  25. analysis
  26. else
  27. self.create! type: type, host: host, port: port, pending: true, date: Time.now
  28. end
  29. end
  30. def self.result(type, host, port, result)
  31. analysis = self[type, host, port]
  32. if analysis
  33. analysis.remove_attribute :pending
  34. analysis.update_attributes result: result, date: Time.now
  35. analysis
  36. else
  37. self.create! type: type, host: host, port: port, result: result, date: Time.now
  38. end
  39. end
  40. def publish(result)
  41. self.remove_attribute :pending
  42. self.update_attribute :result, result
  43. end
  44. private
  45. def self.key(type, host, port)
  46. { type: type, host: host, port: port }
  47. end
  48. end