<commit_before>require 'timers'

require 'spf/common/extensions/fixnum'
require 'spf/common/logger'

require 'spf/gateway/service'
require 'spf/gateway/pipeline'
require 'spf/gateway/processing-strategies/audio'
require 'spf/gateway/processing-strategies/audio_recognition_processing_strategy'
require 'spf/gateway/processing-strategies/face_recognition_processing_strategy'
require 'spf/gateway/processing-strategies/object_count_processing_strategy'
require 'spf/gateway/processing-strategies/ocr_processing_strategy'
# require 'spf/gateway/processing-strategies/openocr_processing_strategy'
require 'spf/gateway/service-strategies/audio_info_service_strategy'
require 'spf/gateway/service-strategies/basic_service_strategy'
require 'spf/gateway/service-strategies/find_text_service_strategy'


module SPF
  module Gateway

    class ServiceManager

      include SPF::Logging

      @@PROCESSING_STRATEGY_FACTORY = {
        :ocr => SPF::Gateway::OCRProcessingStrategy,
        :object_count => SPF::Gateway::ObjectCountProcessingStrategy,
        :identify_song => SPF::Gateway::AudioRecognitionProcessingStrategy,
        :face_recognition => SPF::Gateway::FaceRecognitionProcessingStrategy
      }

      @@SERVICE_STRATEGY_FACTORY = {
        :basic => SPF::Gateway::BasicServiceStrategy,
        :find_text => SPF::Gateway::FindTextServiceStrategy,
        :audio_info => SPF::Gateway::AudioInfoServiceStrategy
      }

      # Initializes the service manager.
      def initialize
        @services = {}
        @services_lock = Concurrent::ReadWriteLock.new
        @active_pipelines = {}
        @active_pipelines_lock = Concurrent::ReadWriteLock.new
        @timers = Timers::Group.new
      end

      # Instantiates (creates and activates) a service.
      #
      # @param service_name [Symbol] Name of the service to instantiate.
      # @param service_conf [Hash] Configuration of the service to instantiate.
      # @param application [SPF::Gateway::Application] The application the service to instantiate belongs to.
      def instantiate_service(service_name, service_conf, application)
        svc = nil
        @services_lock.with_write_lock do
          # retrieve service location in @services
          app_name = application.name.to_sym
          @services[app_name] ||= {}
          @services[app_name][service_name] ||= [nil, nil]
          svc = @services[app_name][service_name][0]

          # create service if it does not exist...
          unless svc
            svc_strategy = self.class.service_strategy_factory(service_name, service_conf)
            svc = Service.new(service_name, service_conf, application, svc_strategy)
            logger.info "*** PIG: Created new service #{service_name.to_s} ***"
            # add service to the set of services of corresponing application
            # TODO: we operate under the assumption that the (application_name,
            # service_name) couple is unique for each service. Make sure the
            # assumption holds, so that the following statement does not overwrite
            # anything!!!
            @services[app_name][service_name] = [svc, nil]  # [service, timer]
          end
        end

        # ...and activate it!
        # TODO: or postopone activation until the first request arrives?
        activate_service(svc) if svc
      end

      # Instantiates a service if and only if it already exists.
      #
      # @param service_name [SPF::Gateway::Service] Instance of the service to reactivate.
      def restart_service(svc)
        # do nothing if the service was not configured before
        @services_lock.with_read_lock do
          return if @services[svc.application.name.to_sym].nil? ||
            @services[svc.application.name.to_sym][svc.name].nil?
        end

        # reactivate the service
        activate_service(svc)
      end

      # Atomically finds the service from the pair
      # application_name:service_name provided in the
      # parameters and resets any timer associated
      # to that service.
      #
      # @param application_name [Symbol] Name of the application.
      # @param service_name [Symbol] Name of the service to find.
      def get_service_by_name(application_name, service_name)
        # TODO: we operate under the assumption that the (application_name,
        # service_name) couple is unique for each service. Make sure the
        # assumption holds, so that the following statement returns just one service.
        @services_lock.with_read_lock do
          puts "service_name: #{service_name}"<commit_msg>removed put in service manager<commit_after>require 'timers'

require 'spf/common/extensions/fixnum'
require 'spf/common/logger'

require 'spf/gateway/service'
require 'spf/gateway/pipeline'
require 'spf/gateway/processing-strategies/audio'
require 'spf/gateway/processing-strategies/audio_recognition_processing_strategy'
require 'spf/gateway/processing-strategies/face_recognition_processing_strategy'
require 'spf/gateway/processing-strategies/object_count_processing_strategy'
require 'spf/gateway/processing-strategies/ocr_processing_strategy'
# require 'spf/gateway/processing-strategies/openocr_processing_strategy'
require 'spf/gateway/service-strategies/audio_info_service_strategy'
require 'spf/gateway/service-strategies/basic_service_strategy'
require 'spf/gateway/service-strategies/find_text_service_strategy'


module SPF
  module Gateway

    class ServiceManager

      include SPF::Logging

      @@PROCESSING_STRATEGY_FACTORY = {
        :ocr => SPF::Gateway::OCRProcessingStrategy,
        :object_count => SPF::Gateway::ObjectCountProcessingStrategy,
        :identify_song => SPF::Gateway::AudioRecognitionProcessingStrategy,
        :face_recognition => SPF::Gateway::FaceRecognitionProcessingStrategy
      }

      @@SERVICE_STRATEGY_FACTORY = {
        :basic => SPF::Gateway::BasicServiceStrategy,
        :find_text => SPF::Gateway::FindTextServiceStrategy,
        :audio_info => SPF::Gateway::AudioInfoServiceStrategy
      }

      # Initializes the service manager.
      def initialize
        @services = {}
        @services_lock = Concurrent::ReadWriteLock.new
        @active_pipelines = {}
        @active_pipelines_lock = Concurrent::ReadWriteLock.new
        @timers = Timers::Group.new
      end

      # Instantiates (creates and activates) a service.
      #
      # @param service_name [Symbol] Name of the service to instantiate.
      # @param service_conf [Hash] Configuration of the service to instantiate.
      # @param application [SPF::Gateway::Application] The application the service to instantiate belongs to.
      def instantiate_service(service_name, service_conf, application)
        svc = nil
        @services_lock.with_write_lock do
          # retrieve service location in @services
          app_name = application.name.to_sym
          @services[app_name] ||= {}
          @services[app_name][service_name] ||= [nil, nil]
          svc = @services[app_name][service_name][0]

          # create service if it does not exist...
          unless svc
            svc_strategy = self.class.service_strategy_factory(service_name, service_conf)
            svc = Service.new(service_name, service_conf, application, svc_strategy)
            logger.info "*** PIG: Created new service #{service_name.to_s} ***"
            # add service to the set of services of corresponing application
            # TODO: we operate under the assumption that the (application_name,
            # service_name) couple is unique for each service. Make sure the
            # assumption holds, so that the following statement does not overwrite
            # anything!!!
            @services[app_name][service_name] = [svc, nil]  # [service, timer]
          end
        end

        # ...and activate it!
        # TODO: or postopone activation until the first request arrives?
        activate_service(svc) if svc
      end

      # Instantiates a service if and only if it already exists.
      #
      # @param service_name [SPF::Gateway::Service] Instance of the service to reactivate.
      def restart_service(svc)
        # do nothing if the service was not configured before
        @services_lock.with_read_lock do
          return if @services[svc.application.name.to_sym].nil? ||
            @services[svc.application.name.to_sym][svc.name].nil?
        end

        # reactivate the service
        activate_service(svc)
      end

      # Atomically finds the service from the pair
      # application_name:service_name provided in the
      # parameters and resets any timer associated
      # to that service.
      #
      # @param application_name [Symbol] Name of the application.
      # @param service_name [Symbol] Name of the service to find.
      def get_service_by_name(application_name, service_name)
        # TODO: we operate under the assumption that the (application_name,
        # service_name) couple is unique for each service. Make sure the
        # assumption holds, so that the following statement returns just one service.
        @services_lock.with_read_lock do
      
{'fraction_non_alphanumeric': 0.0821557673348669, 'fraction_numerical': 0.00021908204622631176, 'mean_word_length': 3.151887221464302, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4769262', 'n_tokens_mistral': 2560, 'n_tokens_neox': 2531, 'n_words': 650}