<commit_before>module Dendrite
  module Generators
    class Nerve < Base

      def initialize(graph:, service_names:)
        super
        @services = @services.collect { |service| ServiceConfig.new(service)}
      end

      def to_h
        service_list = services.inject({}) do |hash, service|
          hash[service.name] = service.to_h
          hash
        end

        {
          instance_id: Dendrite::Config.instance
        }.merge({services: service_list})
      end

      ServiceConfig = Struct.new(:service) do
        extend Forwardable
        def_delegator :service, :name, :name
        def_delegator :service, :namespace, :namespace
        def_delegator :service, :organization, :organization

        def to_h
          {
            host: Dendrite::Config.public_ip,
            port: service.listening_port,
            labels: {<commit_msg>Add port collission check for nerve<commit_after>module Dendrite
  module Generators
    class Nerve < Base

      def initialize(graph:, service_names:)
        super
        @services.group_by { |service| service.listening_port }.each do |port, services|
          if services.length > 1
            raise PortCollission, "Port collission between #{services.collect(&:name).join(',')}"
          end
        end
        @services = @services.collect { |service| ServiceConfig.new(service)}
      end

      def to_h
        service_list = services.inject({}) do |hash, service|
          hash[service.name] = service.to_h
          hash
        end

        {
          instance_id: Dendrite::Config.instance
        }.merge({services: service_list})
      end

      ServiceConfig = Struct.new(:service) do
        extend Forwardable
        def_delegator :service, :name, :name
        def_delegator :service, :namespace, :namespace
        def_delegator :service, :organization, :organization

        def to_h
          {
            host: Dendrite::Config.public_ip,
            port: service.listening_port,
            labels: {
{'fraction_non_alphanumeric': 0.09172482552342971, 'fraction_numerical': 0.0004985044865403788, 'mean_word_length': 2.14576802507837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '4206200', 'n_tokens_mistral': 560, 'n_tokens_neox': 544, 'n_words': 135}