3scale/porta

View on GitHub
app/lib/stats/views/top.rb

Summary

Maintainability
A
0 mins
Test Coverage

module Stats
  module Views
    module Top
      private


      def top(name, options)
        options = options.to_h.symbolize_keys
        options.assert_valid_keys(:period, :since, :metric, :metric_name, :limit, :timezone, :skip_change)
        raise InvalidParameterError, "Missing parameter :period" unless options.key?(:period)

        period = options[:period]
        since = extract_since(options)
        metric = extract_metric(options)

        singular_name = name.to_s.singularize

        data = storage.ordered_hash(since, period,
          :from  => [:stats, source, name],
          :by    => [:stats, source, {singular_name => :*}, metric ],
          :limit => options[:limit] || 10,
          :order => :desc)


        if block_given?
          data.reject!{|id, value| value.blank?}
          data.map { |id, value| yield(id, value) }.compact
        else
          data
        end
      end

    end
  end
end