lib/models/log.rb in logman-0.0.2 vs lib/models/log.rb in logman-0.1.0.alpha
- old
+ new
@@ -1,41 +1,55 @@
-module Logman
+class Logman
class Log
- include MongoMapper::Document
- belongs_to :bucket
- attr_accessible :log_type, :data_type, :message, :data, :datetime,:sources
+ include Mongoid::Document
+ include Mongoid::Timestamps::Created
- key :log_type, Integer, :required=>true #1-error, 2-success, 3-warning, 4-info
- key :data_type, String #application that generated log like syslog,
+ store_in collection: lambda { Thread.current[:bucket_key] }
- key :message, String, :required=>true #short string message
- key :data, Hash
+ validates_presence_of :log_type, :message
- key :datetime, Time
+ field :log_type, type: Integer #1-error, 2-success, 3-warning, 4-info
+ field :data_type, type: String #application that generated log like syslog,
+ field :message, type: String #short string message
+ field :data, type: Hash
+ field :datetime, type: Time
- many :sources, :class_name=>'Logman::Source'
+ embeds_many :sources, :class_name=>'Logman::Source'
+
+
+
def self.count_on_date(date)
- begin
+ # begin
# raise date.to_s
start_time = Time.new(date.year, date.month, date.day, 0,0,0).utc
- end_time = Time.new(date.year, date.month, date.day+1, 0,0,0).utc
+ end_time = start_time + 1.day
- Log.where(:created_at => { :$gte => start_time }).where(:created_at => { :$lt => end_time }).count
- rescue
- 0
- end
+ count = 0
+
+ Bucket.all.each do |b|
+ count += b.logs.between(created_at: start_time..end_time).count
+ end
+ count
+#
+ # rescue
+ # 0
+ # end
end
- timestamps!
+ #to json
+ def serializable_hash(options={})
+ options[:methods] ||= [:id]
+ super(options)
+ end
end
class Source
- include MongoMapper::EmbeddedDocument
- attr_accessible :name, :ip_address, :hop
+ include Mongoid::Document
+ embedded_in :log, :class_name=>'Logman::Log'
- key :name, String
- key :ip_address, String
- key :hop, Integer, :default=>0
+ field :name, type: String
+ field :ip_address, type: String
+ field :hop, type: Integer
end
end
\ No newline at end of file