Sha256: 1ffccadb5ed3788c43e32ae7b7dcd2e339448a4c3a6baa970bffb820c29df944

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# helpers for processing params and validating input
module Batsd::Dash
  module ParamsHelper
    ##
    # Parse params and return stats hash
    #
    # @return [Hash] stats hash
    def parse_statistics
      Hash.new { |hash,key| hash[key] = [] }.tap do |stats|
        %w[ counters gauges timers ].each do |datatype|
          list = params[datatype]

          list = [list] unless Array === list
          list.reject! { |m| m.nil? || m.empty? }

          stats[datatype] = list unless list.empty?
        end
      end
    end

    ##
    # Parse time range from params
    #
    # @return [Array] an array with 2 elements (start and stop)
    def parse_time_range
      start, stop = params[:start], params[:stop]

      if start.nil? && stop.nil?
        now = Time.now.to_i

        # 1 hr range
        # TODO make this setting?
        [ now - 1800, now ]

      else
        [start.to_i, stop.to_i].tap do |range|
          if range[0] <= 0 || range[1] <= 0 || range[0] >= range[1]
            return nil
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
batsd-dash-0.5.0 lib/batsd-dash/params.rb