<commit_before>module AppMonit
  class Query
    class << self
      %w(count count_unique list_unique minimum maximum average sum funnel).each do |method_name|
        define_method method_name do |collection_name, params|
          query(method_name, collection_name, params)
        end
      end

      def query(method_name, collection_name, params)
        require 'cgi'
        path = "/v1/queries/#{method_name}"

        params[:event_collection] = collection_name

        response = Http.get("#{path}?query=#{CGI.escape(params.to_json)}")

        case response.code.to_i
          when 200
            JSON.parse(response.body)
          else
            nil
        end
      end<commit_msg>allow query with api key and environment in param<commit_after>module AppMonit
  class Query
    class << self
      %w(count count_unique list_unique minimum maximum average sum funnel).each do |method_name|
        define_method method_name do |collection_name, params|
          query(method_name, collection_name, params)
        end
      end

      def query(method_name, collection_name, params)
        require 'cgi'
        path = "/v1/queries/#{method_name}"

        params[:event_collection] = collection_name

        query_string_parts = []

        query_string_parts << "api_key=#{params.delete(:api_key)}" if params[:api_key]
        query_string_parts << "environment=#{params.delete(:environment)}" if params[:environment]
        query_string_parts << "query=#{CGI.escape(params.to_json)}"

        query_string = query_string_parts.join('&')

        response = Http.get("#{path}?#{query_string}")

        case response.code.to_i
          when 200
            JSON.parse(response.body)
          else
            nil
        end
      end
{'fraction_non_alphanumeric': 0.09575070821529745, 'fraction_numerical': 0.004532577903682719, 'mean_word_length': 2.532, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '2721447', 'n_tokens_mistral': 511, 'n_tokens_neox': 489, 'n_words': 114}