<commit_before>require 'rubygems'
require 'clockwork'
require 'keen'

include Clockwork

require 'mail'
Mail.defaults do
  delivery_method :smtp, { :address   => "smtp.sendgrid.net",
                           :port      => 587,
                           :domain    => ENV['SENDGRID_DOMAIN'],
                           :user_name => ENV['SENDGRID_USERNAME'],
                           :password  => ENV['<PASSWORD>'],
                           :authentication => 'plain',
                           :enable_starttls_auto => true }
end

def send_mail(_subject, _html_body, _text_body=nil)
  Mail.deliver do
    to '<NAME> <<EMAIL>>'
    from 'keen-cron <<EMAIL>>'
    subject _subject
    text_part do
      body _html_body
    end
    html_part do
      content_type 'text/html; charset=UTF-8'
      body _text_body || _html_body
    end
  end
end

handler do |job|
  puts job
end

every(1.minute, 'count.failure') do
  result = Keen.count('checks',
                   timeframe: 'last_1_minute',
                   filters: [{
                     property_name: "response.status",
                     property_value: 400,
                     operator: "gte"
                   }])

  puts "count.failure: #{result}"

  if result != 0
    send_mail "count.failure is non-zero!", "#{result}"
  end
end

every(1.minute, 'count.timeout') do
  result = Keen.count('checks',
                   timeframe: 'last_1_minute',
                   filters: [{
                     property_name: "response.status",
                     property_value: 0,
                     operator: "eq"
                   }])

  puts "count.timeout: #{result}"<commit_msg>Turn off alerting for utility checks<commit_after>require 'rubygems'
require 'clockwork'
require 'keen'

include Clockwork

require 'mail'
Mail.defaults do
  delivery_method :smtp, { :address   => "smtp.sendgrid.net",
                           :port      => 587,
                           :domain    => ENV['SENDGRID_DOMAIN'],
                           :user_name => ENV['SENDGRID_USERNAME'],
                           :password  => ENV['<PASSWORD>GRID_PASSWORD'],
                           :authentication => 'plain',
                           :enable_starttls_auto => true }
end

def send_mail(_subject, _html_body, _text_body=nil)
  Mail.deliver do
    to '<NAME> <<EMAIL>>'
    from 'keen-cron <<EMAIL>>'
    subject _subject
    text_part do
      body _html_body
    end
    html_part do
      content_type 'text/html; charset=UTF-8'
      body _text_body || _html_body
    end
  end
end

handler do |job|
  puts job
end

every(1.minute, 'count.failure') do
  result = Keen.count('checks',
                   timeframe: 'last_1_minute',
                   filters: [{
                     property_name: "response.status",
                     property_value: 400,
                     operator: "gte"
                   },{
                     property_name: "check.utility",
                     property_value: false,
                     operator: "exists"
                   }])

  puts "count.failure: #{result}"

  if result != 0
    send_mail "count.failure is non-zero!", "#{result}"
  end
end

every(1.minute, 'count.timeout') do
  result = Keen.count('checks',
                   timeframe: 'last_1_minute',
                   filters: [{
                     property_name: "response.status",
                     property_value: 0,
                     operator: "eq"
                   },{
                     property_name: "check.utility",
                     property_value: false,
                     operator: "exists"
                   }])

  puts "count.timeout: #{result}"
{'fraction_non_alphanumeric': 0.10911074740861974, 'fraction_numerical': 0.0070921985815602835, 'mean_word_length': 1.5150891632373114, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, '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': '2379420', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1033, 'n_words': 234}