<commit_before># == Schema Information
#
# Table name: metrics
#
#  id          :integer          not null, primary key
#  check_id    :integer          not null
#  name        :string(255)
#  key         :string(255)      not null
#  tag_hash    :string(32)       not null
#  status      :integer
#  last_value  :decimal(20, 2)
#  last_status :integer
#  created_at  :datetime         not null
#  updated_at  :datetime
#


class Metric < ActiveRecord::Base

  if not const_defined? :METADATA_SOURCE then
    METADATA_SOURCE = 2

    module Status
      UNKNOWN  = 0
      OK       = 1
      WARNING  = 2
      CRITICAL = 3
      TIMEOUT  = 4
    end

    Bixby::Util.create_const_map(Status)
  end

  belongs_to :check
  has_many :tags, -> { where("object_type = #{Metadata::Type::METRIC}") }, :class_name => :Metadata, :foreign_key => :object_fk_id

  multi_tenant :via => :check

  attr_accessor :data, :metadata, :query

  # Shortcut accessor for this Metric's Org
  #
  # @return [Org]
  def org
    self.check.org
  end

  # Find an existing Metric or return a new instance
  #
  # @param [Check] check
  # @param [String] key
  # @param [Hash] metadata
  # @return [Metric]
  def self.for(check, key, metadata = {})

    hash = hash_metadata(metadata)
    m = Metric.where(:check_id => check.id, :key => key, :tag_hash => hash).first
    if not m.blank? then
      return m
    end

    m = Metric.new
    m.check = check
    m.key = key
    m.tag_hash = hash
    m.tags = []
    m.tags += metadata.map { |k,v| Metadata.for(k, v, METADATA_SOURCE) } if metadata

    return m
  end

  def self.metrics_for_host(host, time_start=nil, time_end=nil, tags = {}, agg = "sum", downsample = nil)

    # set defaults
    time_start = Time.new - 86400 if time_start.nil?
    time_end = Time.new if time_end.nil?
    tags ||= {}
    agg ||= "sum"
    downsample ||= "1h-avg"
<commit_msg>cleanup constants; use add_tag helper<commit_after># == Schema Information
#
# Table name: metrics
#
#  id          :integer          not null, primary key
#  check_id    :integer          not null
#  name        :string(255)
#  key         :string(255)      not null
#  tag_hash    :string(32)       not null
#  status      :integer
#  last_value  :decimal(20, 2)
#  last_status :integer
#  created_at  :datetime         not null
#  updated_at  :datetime
#


class Metric < ActiveRecord::Base

  module Status
    UNKNOWN  = 0
    OK       = 1
    WARNING  = 2
    CRITICAL = 3
    TIMEOUT  = 4
  end
  Bixby::Util.create_const_map(Status)

  belongs_to :check
  has_many :tags, -> { where("object_type = #{Metadata::Type::METRIC}") }, :class_name => :Metadata, :foreign_key => :object_fk_id

  multi_tenant :via => :check

  attr_accessor :data, :metadata, :query

  # Shortcut accessor for this Metric's Org
  #
  # @return [Org]
  def org
    self.check.org
  end

  # Add new tag to this metric
  #
  # @param [String] key
  # @param [String] value
  # @param [Fixnum] source           (optional, default: METRIC)
  def add_tag(key, value, source=Metadata::Source::METRIC)
    tags << Metadata.new(:key => key, :value => value, :source => source,
                             :object_type => Metadata::Type::METRIC, :object_fk_id => id)
    nil
  end

  # Find an existing Metric or return a new instance
  #
  # @param [Check] check
  # @param [String] key
  # @param [Hash] metadata          key/value pairs
  # @return [Metric]
  def self.for(check, key, metadata = {})

    hash = hash_metadata(metadata)
    m = Metric.where(:check_id => check.id, :key => key, :tag_hash => hash).first
    if not m.blank? then
      return m
    end

    m = Metric.new
    m.check = check
    m.key = key
    m.tag_hash = hash
    m.tags = []
    metadata.each{ |k,v| m.add_tag(k, v) } if metadata

    return m
  end

  def self.metrics_for_host(host, time_start=nil, time_end=nil, tags = {}, agg = "sum", downsample = nil)

    # set defaults
    time_start = Time.new - 86400 if time_start.nil?
    time_end = Time.new if time_end.nil?
    tags ||= {}
    agg ||= "sum"
    downsample ||= "1h-avg"

{'fraction_non_alphanumeric': 0.12061779848001962, 'fraction_numerical': 0.011032115714635941, 'mean_word_length': 2.493150684931507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, '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': '1263276', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1415, 'n_words': 387}