<commit_before># frozen_string_literal: true
module TaskVault
  class Server < BBLib::LazyClass
    attr_valid_dir :path, allow_nil: true, default: Dir.pwd, serialize: true, always: true
    attr_hash :components, serialize: true, always: true

    def path=(path)
      @path = path
      @components.each do |_n, c|
        c.path = path if c.respond_to?(:path=)
      end
    end

    def start(delay = 0.1)
      @components.all? do |_n, c|
        c.start
        sleep(delay)
      end
    end

    def stop(delay = 0.1, complete: false)
      @components.reverse.all? do |n, c|
        next if !complete && n == :overseer
        c.stop
        sleep(delay)
      end
    end

    def restart
      @components.all? { |_n, c| c.restart }
    end

    def set_handlers(*handlers)
      @components.map { |n, c| [n, c.handlers = handlers] }.to_h
    end

    def running?
      @components.any? { |_n, c| c.running? }
    end

    def healthy?
      @components.all? { |_n, c| c.running? }
    end

    def add(components)
      components.each do |name, component|
        raise ArgumentError, "Values must be descendants of TaskVault::Component not #{component.class}" unless component.is_a?(TaskVault::Component)
        component.parent = self
        component.name = name
        @components[name.to_sym] = component
      end
    end

    def remove(*components)
      components.each { |name| @components.delete(name.to_sym) }
    end

    def health
      if healthy?
        :green
      elsif running?
        :yellow
      else
        :red
      end
    end

    def status
      {
        health:      health,
        version:     TaskVault::VERSION,
        ip_address:  ip_address,
        config_path: path,
        working_dir: Dir.pwd,
        time:        server_time,
        running:     running?,
        components:  @components.map { |n, c| [n, { running: c.running?, uptime: c.uptime, class: c.class }] }.to_h
        # handlers:    courier.message_handlers.map { |n, h| [n, { running: h.running?, uptime: h.uptime }] }.to_h
      }
    end

    def ip_address(version = 4)<commit_msg>Added hostname and pid to the status return.<commit_after># frozen_string_literal: true
module TaskVault
  class Server < BBLib::LazyClass
    attr_valid_dir :path, allow_nil: true, default: Dir.pwd, serialize: true, always: true
    attr_hash :components, serialize: true, always: true

    def path=(path)
      @path = path
      @components.each do |_n, c|
        c.path = path if c.respond_to?(:path=)
      end
    end

    def start(delay = 0.1)
      @components.all? do |_n, c|
        c.start
        sleep(delay)
      end
    end

    def stop(delay = 0.1, complete: false)
      @components.reverse.all? do |n, c|
        next if !complete && n == :overseer
        c.stop
        sleep(delay)
      end
    end

    def restart
      @components.all? { |_n, c| c.restart }
    end

    def set_handlers(*handlers)
      @components.map { |n, c| [n, c.handlers = handlers] }.to_h
    end

    def running?
      @components.any? { |_n, c| c.running? }
    end

    def healthy?
      @components.all? { |_n, c| c.running? }
    end

    def add(components)
      components.each do |name, component|
        raise ArgumentError, "Values must be descendants of TaskVault::Component not #{component.class}" unless component.is_a?(TaskVault::Component)
        component.parent = self
        component.name = name
        @components[name.to_sym] = component
      end
    end

    def remove(*components)
      components.each { |name| @components.delete(name.to_sym) }
    end

    def health
      if healthy?
        :green
      elsif running?
        :yellow
      else
        :red
      end
    end

    def status
      {
        health:      health,
        version:     TaskVault::VERSION,
        hostname:    (`hostname`.chop rescue nil),
        pid:         Process.pid,
        ip_address:  ip_address,
        config_path: path,
        working_dir: Dir.pwd,
        time:        server_time,
        running:     running?,
        components:  @components.map { |n, c| [n, { running: c.running?, uptime: c.uptime, class: c.class }] }.to_h
        # handlers:    courier.message_handlers.map { |n, h| [n, { running: h.running?, uptime: h.uptime }] }.to_h
      }
    end

    def ip_address(version = 4)
{'fraction_non_alphanumeric': 0.11200551977920883, 'fraction_numerical': 0.0022999080036798527, 'mean_word_length': 2.062676056338028, 'pattern_counts': {'":': 0, '<': 5, '<?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': '193853', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1403, 'n_words': 380}