Sha256: bf1713788c55cb6ac1f05220d6f538d40f3caf0ef1289a4fdab34fd43a390c53

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module GraphQLMetrics
  BASE_CLASS = if defined?(GraphQL::Batch::Executor)
    GraphQL::Batch::Executor
  else
    class NoExecutor
      class << self
        def resolve(_loader)
          super
        end

        def around_promise_callbacks
          super
        end
      end
    end

    NoExecutor
  end

  class TimedBatchExecutor < BASE_CLASS
    TIMINGS = {}
    private_constant :TIMINGS

    class << self
      def timings
        TIMINGS
      end

      def clear_timings
        TIMINGS.clear
      end

      def serialize_loader_key(loader_key)
        identifiers = []

        serialized = loader_key.map do |group_arg|
          if [Class, Symbol, String].include?(group_arg.class)
            group_arg
          elsif group_arg.is_a?(Numeric)
            identifiers << group_arg
            '_'
          elsif group_arg.respond_to?(:id)
            identifiers << group_arg.id
            "#{group_arg.class}/_"
          else
            '?'
          end
        end

        [serialized.map(&:to_s).join('/'), identifiers.map(&:to_s)]
      end
    end

    def resolve(loader)
      @resolve_meta = {
        start_time: Instrumentation.current_time,
        current_loader: loader,
        perform_queue_sizes: loader.send(:queue).size
      }

      super
    end

    def around_promise_callbacks
      return super unless @resolve_meta

      end_time = Instrumentation.current_time

      TIMINGS[@resolve_meta[:current_loader].loader_key] ||= { times: [], perform_queue_sizes: [] }
      TIMINGS[@resolve_meta[:current_loader].loader_key][:times] << end_time - @resolve_meta[:start_time]
      TIMINGS[@resolve_meta[:current_loader].loader_key][:perform_queue_sizes] << @resolve_meta[:perform_queue_sizes]

      @resolve_meta = nil

      super
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
graphql-metrics-2.0.1 lib/graphql_metrics/timed_batch_executor.rb
graphql-metrics-2.0.0 lib/graphql_metrics/timed_batch_executor.rb