<commit_before>module Nexaas
  module Async
    module Collector
      class AsyncResourceJob

        include Sidekiq::Worker
        sidekiq_options queue: Nexaas::Async::Collector.queue_name

        def perform(collect_id, user_id, klass_name, klass_method, args=[])
          instrument_start(collect_id, user_id, klass_name, klass_method)
          content = call_for_method(klass_name, klass_method, args)
          Persist.save(user_id, collect_id, content)
          instrument_finish(collect_id, user_id, klass_name, klass_method)
        end

        private

        def call_for_method(klass_name, klass_method, args=[])
          if args.any?
            klass_name.to_s.constantize.send(klass_method, *args)
          else
            klass_name.to_s.constantize.send(klass_method)
          end
        end

        def instrument_start(collect_id, user_id, klass_name, klass_method)
          @start = Time.current.to_i
          ActiveSupport::Notifications.instrument("#{instrumentation_context}.start", {
            collect_id: collect_id, user_id: user_id,
            class_name: klass_name, class_method: klass_method,
            start: @start
          })
        end

        def instrument_finish(collect_id, user_id, klass_name, klass_method)
          duration = (Time.current.to_i - @start)
          ActiveSupport::Notifications.instrument("#{instrumentation_context}.finish", {
            collect_id: collect_id, user_id: user_id,
            class_name: klass_name, class_method: klass_method,
            duration: duration
          })
        end

        def instrumentation_context
          Nexaas::Async::Collector.instrumentation_context
        end
      end
    end
  end
end<commit_msg>Add finish parameter to instrumentation<commit_after>module Nexaas
  module Async
    module Collector
      class AsyncResourceJob

        include Sidekiq::Worker
        sidekiq_options queue: Nexaas::Async::Collector.queue_name

        def perform(collect_id, user_id, klass_name, klass_method, args=[])
          instrument_start(collect_id, user_id, klass_name, klass_method)
          content = call_for_method(klass_name, klass_method, args)
          Persist.save(user_id, collect_id, content)
          instrument_finish(collect_id, user_id, klass_name, klass_method)
        end

        private

        def call_for_method(klass_name, klass_method, args=[])
          if args.any?
            klass_name.to_s.constantize.send(klass_method, *args)
          else
            klass_name.to_s.constantize.send(klass_method)
          end
        end

        def instrument_start(collect_id, user_id, klass_name, klass_method)
          @start = Time.current.to_i
          ActiveSupport::Notifications.instrument("#{instrumentation_context}.start", {
            collect_id: collect_id, user_id: user_id,
            class_name: klass_name, class_method: klass_method,
            start: @start
          })
        end

        def instrument_finish(collect_id, user_id, klass_name, klass_method)
          finish = Time.current.to_i
          duration = (finish - @start)
          ActiveSupport::Notifications.instrument("#{instrumentation_context}.finish", {
            collect_id: collect_id, user_id: user_id,
            class_name: klass_name, class_method: klass_method,
            finish: finish, duration: duration
          })
        end

        def instrumentation_context
          Nexaas::Async::Collector.instrumentation_context
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07672706681766704, 'fraction_numerical': 0.0, 'mean_word_length': 2.518924302788845, 'pattern_counts': {'":': 0, '<': 3, '<?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': '252931', 'n_tokens_mistral': 1019, 'n_tokens_neox': 1045, 'n_words': 197}