<commit_before>class Notification < ApplicationRecord
  belongs_to :reading
  belongs_to :sensor
  belongs_to :notification_trigger

  after_create :notify_responsible_users

  delegate :responsible_users, to: :sensor

  scope :for_sensor, -> (sensor) { where(sensor: sensor, is_acknowledged: false) }

  def notify_responsible_users
    UserMailer.sensor_alert(responsible_users, self).deliver_now
  end

  def acknowledge!
    self.update_attribute(:is_acknowledged, true)
  end

  def to_message
    sensor_name = self.sensor.name
    room_name = self.sensor.room.name
    trigger = self.notification_trigger
    sensor_type = trigger.sensor_type.name<commit_msg>Add notifications to search index<commit_after>class Notification < ApplicationRecord
  belongs_to :reading
  belongs_to :sensor
  belongs_to :notification_trigger

  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  after_create :notify_responsible_users

  delegate :responsible_users, to: :sensor

  scope :for_sensor, -> (sensor) { where(sensor: sensor, is_acknowledged: false) }

  def notify_responsible_users
    UserMailer.sensor_alert(responsible_users, self).deliver_now
  end

  def as_indexed_json(options={})
    room = sensor.room
    location = room.location
    floor = room.floor
    organization = location.organization

    {
      sensor_name: sensor.name,
      room_name: room.name,
      location_name: location.name,
      organization_name: organization.name,
      notification_trigger: notification_trigger.trigger_when,
      sensor_value: notification_trigger.sensor_value,
      floor: floor.name,
      created_at: created_at
    }
  end

  def acknowledge!
    self.update_attribute(:is_acknowledged, true)
  end

  def to_message
    sensor_name = self.sensor.name
    room_name = self.sensor.room.name
    trigger = self.notification_trigger
    sensor_type = trigger.sensor_type.name
{'fraction_non_alphanumeric': 0.07285115303983228, 'fraction_numerical': 0.0, 'mean_word_length': 4.273480662983426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5687556', 'n_tokens_mistral': 578, 'n_tokens_neox': 548, 'n_words': 118}