@@ -16,6 +16,18 @@ class Message
16
16
key :line , Integer
17
17
18
18
LIMIT = 100
19
+ scope :not_deleted , :deleted => [ false , nil ]
20
+ scope :by_blacklisted_terms , lambda { |terms |
21
+ where ( :message . nin => terms . collect { |term | /#{ Regexp . escape term } / } )
22
+ }
23
+ scope :by_blacklist , lambda { |blacklist | by_blacklisted_terms ( blacklist . all_terms ) }
24
+ #scope :by_blacklist, lambda {|blacklist|
25
+ # where(:message.nin => blacklist.all_terms.collect { |term|
26
+ # /#{Regexp.escape term}/
27
+ # })
28
+ #}
29
+ scope :page , lambda { |number | skip ( self . get_offset ( number ) ) }
30
+ scope :default_scope , fields ( :full_message => 0 ) . order ( "_id DESC" ) . not_deleted . limit ( LIMIT )
19
31
20
32
def self . get_conditions_from_date ( timeframe )
21
33
conditions = { }
@@ -37,34 +49,30 @@ def self.get_conditions_from_date(timeframe)
37
49
def self . all_of_blacklist id , page = 1
38
50
page = 1 if page . blank?
39
51
40
- conditions = Hash . new
41
-
42
- ( blacklist = BlacklistedTerm . get_all_as_condition_hash ( false , id ) ) . blank? ? nil : conditions [ :message ] = blacklist ;
43
-
44
- conditions [ :deleted ] = [ false , nil ]
45
-
46
- return self . all :limit => LIMIT , :order => "_id DESC" , :conditions => conditions , :offset => self . get_offset ( page ) , :fields => { :full_message => 0 }
52
+ b = Blacklist . find ( id )
53
+ #return by_blacklist(b).not_deleted.limit(LIMIT).order("_id DESC").offset(self.get_offset(page)).fields(:full_message => 0).all
54
+ return by_blacklist ( b ) . default_scope . page ( page ) . all
47
55
end
48
56
49
57
def self . count_of_blacklist id
50
- conditions = Hash . new
58
+ b = Blacklist . find ( id )
51
59
52
- ( blacklist = BlacklistedTerm . get_all_as_condition_hash ( false , id ) ) . blank? ? nil : conditions [ :message ] = blacklist ;
53
- conditions [ :deleted ] = [ false , nil ]
54
-
55
- return self . count :conditions => conditions
60
+ return by_blacklist ( b ) . count
56
61
end
57
62
58
63
def self . all_with_blacklist page = 1 , limit = LIMIT
59
64
page = 1 if page . blank?
60
65
61
- conditions = Hash . new
66
+ # conditions = Hash.new
62
67
63
- ( blacklist = BlacklistedTerm . get_all_as_condition_hash ) . blank? ? nil : conditions [ :message ] = blacklist ;
68
+ # (blacklist = BlacklistedTerm.get_all_as_condition_hash).blank? ? nil : conditions[:message] = blacklist;
64
69
65
- conditions [ :deleted ] = [ false , nil ]
70
+ #conditions[:deleted] = [false, nil]
71
+
72
+ #return self.all :limit => limit, :order => "_id DESC", :conditions => conditions, :offset => self.get_offset(page), :fields => { :full_message => 0 }
66
73
67
- return self . all :limit => limit , :order => "_id DESC" , :conditions => conditions , :offset => self . get_offset ( page ) , :fields => { :full_message => 0 }
74
+ terms = Blacklist . all_terms
75
+ Message . by_blacklisted_terms ( terms ) . default_scope . page ( page )
68
76
end
69
77
70
78
def self . all_by_quickfilter filters , page = 1 , limit = LIMIT , conditions_only = false
@@ -74,10 +82,12 @@ def self.all_by_quickfilter filters, page = 1, limit = LIMIT, conditions_only =
74
82
75
83
unless filters . blank?
76
84
# Message
77
- conditions = ( filters [ :message ] . blank? ? conditions : conditions . where ( :message => /#{ Regexp . escape ( filters [ :message ] . strip ) } / ) )
85
+ conditions = conditions . where ( :message => /#{ Regexp . escape ( filters [ :message ] . strip ) } / ) unless filters [ :message ] . blank?
86
+ #conditions = (filters[:message].blank? ? conditions : conditions.where(:message => /#{Regexp.escape(filters[:message].strip)}/))
78
87
79
88
# Time Frame
80
- conditions = ( filters [ :date ] . blank? ? conditions : conditions . where ( :created_at => get_conditions_from_date ( filters [ :date ] ) ) )
89
+ conditions = conditions . where ( :created_at => get_conditions_from_date ( filters [ :date ] ) ) unless filters [ :date ] . blank?
90
+ #conditions = (filters[:date].blank? ? conditions : conditions.where(:created_at => get_conditions_from_date(filters[:date])))
81
91
82
92
#unless filters[:date_from].blank?
83
93
# from = Chronic::parse(filters[:date_from]).to_i
@@ -90,20 +100,23 @@ def self.all_by_quickfilter filters, page = 1, limit = LIMIT, conditions_only =
90
100
#end
91
101
92
102
# Facility
93
- conditions = ( filters [ :facility ] . blank? ? conditions : conditions . where ( :facility => filters [ :facility ] . to_i ) )
103
+ conditions = conditions . where ( :facility => filters [ :facility ] . to_i ) unless filters [ :facility ] . blank?
104
+ #conditions = (filters[:facility].blank? ? conditions : conditions.where(:facility => filters[:facility].to_i))
94
105
95
106
# Severity
96
- conditions = ( filters [ :severity ] . blank? ? conditions : conditions . where ( :level => filters [ :severity ] . to_i ) )
107
+ conditions = conditions . where ( :level => filters [ :severity ] . to_i ) unless filters [ :severity ] . blank?
108
+ #conditions = (filters[:severity].blank? ? conditions : conditions.where(:level => filters[:severity].to_i))
97
109
98
110
# Host
99
- conditions = ( filters [ :host ] . blank? ? conditions : conditions . where ( :host => filters [ :host ] ) )
111
+ conditions = conditions . where ( :host => filters [ :host ] ) unless filters [ :host ] . blank?
112
+ #conditions = (filters[:host].blank? ? conditions : conditions.where(:host => filters[:host]))
100
113
end
101
-
102
- conditions = conditions . where ( :deleted => [ false , nil ] )
114
+
115
+ Rails . logger . warn ( "CONDITIONS FOR MONGO: #{ conditions . inspect } " )
103
116
104
117
return ( conditions_only ?
105
118
conditions :
106
- conditions . limit ( limit ) . offset ( self . get_offset ( page ) ) . order ( "_id DESC" ) . fields ( :full_message => 0 )
119
+ conditions . default_scope . page ( page )
107
120
)
108
121
end
109
122
0 commit comments