Class: Cullender::Rule

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cullender/rule.rb

Constant Summary collapse

OPERATORS =

basic operators [“term”, “terms”, “string”, “range”, “prefix”, “fuzzy”]

{
  "text_field" => {
    I18n.t("cullender.operators.term") => "term",
    I18n.t("cullender.operators.prefix") => "prefix"
  },
  "number_field" => {
    I18n.t("cullender.operators.gt") => "gt",
    I18n.t("cullender.operators.gte") => "gte",
    I18n.t("cullender.operators.lt") => "lt",
    I18n.t("cullender.operators.lte") => "lte",
    I18n.t("cullender.operators.eq") => "term"
  },
  "range_field" => {
    I18n.t("cullender.operators.gt") => "gt",
    I18n.t("cullender.operators.gte") => "gte",
    I18n.t("cullender.operators.lt") => "lt",
    I18n.t("cullender.operators.lte") => "lte",
    I18n.t("cullender.operators.eq") => "term"
  },
  "date" => {
    I18n.t("cullender.operators.from") => "from",
    I18n.t("cullender.operators.to") => "to"
  },
  "datetime" => {
    I18n.t("cullender.operators.from") => "from",
    I18n.t("cullender.operators.to") => "to"
  },
  "select" => {
    I18n.t("cullender.operators.is") => "term",
    I18n.t("cullender.operators.is_not") => "term"
  },
  "check_box" => {
    I18n.t("cullender.operators.eq") => "is"
  }
}
FILTER_MAP =
{
  "term" => "==",
  "prefix" => "starts with",
  "gt" => ">",
  "gte" => ">=",
  "lt" => "<",
  "lte" => "<=",
  "from" => ">=",
  "to" => "<="
}
FIELD_TYPE_MAP =
{
  'string' => 'text_field',
  'integer' => 'number_field',
  'long' => 'number_field',
  'double' => 'number_field',
  'float' => 'number_field',
  'date' => 'date',
  'boolean' => 'check_box'
}

Instance Method Summary collapse

Instance Method Details

#convert_to_boolean(query, params = self.triggers) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/cullender/rule.rb', line 107

def convert_to_boolean(query, params = self.triggers)
  query.boolean do |bool|
    params.each do |key, values|
      if key.to_i > 0
        bool.should do |q|
          convert_to_boolean(q, values)
        end
      else
        bool.must do |q|
          convert_to_basic_query(q, key, values)
        end
      end
    end
  end
  query
end

#fire(event) ⇒ Object

This method searches elastic search for events that match this specific



76
77
78
79
# File 'app/models/cullender/rule.rb', line 76

def fire(event)
  event.add_labels(self.labels) unless self.labels.empty?
  # send any notifications
end

#to_queryObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/cullender/rule.rb', line 88

def to_query
  query = ::Tire::Search::Query.new
  if triggers.present?
    
    # check if there is only one field
    count = number_of_fields
    if count > 1
      # complex operators ["boolean", "filtered", "dis_max", "nested"]
      convert_to_boolean(query)
    elsif count == 1
      query = convert_to_basic_query(query, triggers.first[0], triggers.first[1])
    else

    end
  end
  query
end

#to_query_procObject

converts the current rule to a proc accepting an an elastic search query object



82
83
84
85
86
# File 'app/models/cullender/rule.rb', line 82

def to_query_proc
  Proc.new do
    self.to_query
  end
end