<commit_before>require "metrix/base"

module Metrix
  class ProcessMetric < Base
    attr_reader :time
    set_known_metrics %w(minflt cminflt majflt cmajflt utime stime cutime sctime num_threads
      vsize rss
    )
    set_prefix "system.process"

    class << self
      def all
        Dir.glob("/proc/*").select do |path|
          File.directory?(path) && File.basename(path)[/^\d+$/]
        end.map do |path|
          Metrix::ProcessMetric.new(File.read(path + "/stat"))
        end
      end
    end

    def name
      comm.gsub(/^\(/, "").gsub(/\)$/, "")
    end

    def chunks
      @chunks ||= @raw.split(" ").map { |c| cast_int(c) }
    end

    def tags
      {
        name:   name,
        pid:    pid,
        ppid:   ppid,
      }
    end

    def unfiltered_metrics
      {
        minflt: minflt,
        cminflt: cminflt,
        majflt: majflt,
        cmajflt: cmajflt,
        utime: utime,
        stime: stime,
        cutime: cutime,
        sctime: sctime,
        num_threads: num_threads,
        vsize: vsize,
        rss: rss,
      }
    end

    def cast_int(str)
      Integer(str) rescue str
    end

    {
      :pid => 0,
      :comm => 1,
      :state => 2,
      :ppid => 3,
      :pgrp => 4,
      :session => 5,
      :tty_nr => 6,
      :tpgid => 7,
      :flags => 8,
      :minflt => 9,
      :cminflt => 10,
      :majflt => 11,
      :cmajflt => 12,
      :utime => 13,
      :stime => 14,
      :cutime => 15,
      :sctime => 16,
      :priority => 17,
      :nice => 18,
      :num_threads => 19,
      :itrealvalue => 20,
      :starttime => 21,
      :vsize => 22,
      :rss => 23,
      :rsslim => 24,
      :startcode => 25,
      :endcode => 26,
      :startstac => 27,
      :guest_time => 42,
      :cguest_time => 43,
    }.each do |k, v|
      define_method(k) do
        chunks.at(v)
      end
    end
  end
end
<commit_msg>use find for gathering processes<commit_after>require "metrix/base"

module Metrix
  class ProcessMetric < Base
    attr_reader :time
    set_known_metrics %w(minflt cminflt majflt cmajflt utime stime cutime sctime num_threads
      vsize rss
    )
    set_prefix "system.process"

    class << self
      def all
        IO.popen("find /proc -maxdepth 2 -mindepth 2 -name stat") do |io|
          io.map do |path|
            Metrix::ProcessMetric.new(File.read(path.strip))
          end
        end
      end
    end

    def name
      comm.gsub(/^\(/, "").gsub(/\)$/, "")
    end

    def chunks
      @chunks ||= @raw.split(" ").map { |c| cast_int(c) }
    end

    def tags
      {
        name:   name,
        pid:    pid,
        ppid:   ppid,
      }
    end

    def unfiltered_metrics
      {
        minflt: minflt,
        cminflt: cminflt,
        majflt: majflt,
        cmajflt: cmajflt,
        utime: utime,
        stime: stime,
        cutime: cutime,
        sctime: sctime,
        num_threads: num_threads,
        vsize: vsize,
        rss: rss,
      }
    end

    def cast_int(str)
      Integer(str) rescue str
    end

    {
      :pid => 0,
      :comm => 1,
      :state => 2,
      :ppid => 3,
      :pgrp => 4,
      :session => 5,
      :tty_nr => 6,
      :tpgid => 7,
      :flags => 8,
      :minflt => 9,
      :cminflt => 10,
      :majflt => 11,
      :cmajflt => 12,
      :utime => 13,
      :stime => 14,
      :cutime => 15,
      :sctime => 16,
      :priority => 17,
      :nice => 18,
      :num_threads => 19,
      :itrealvalue => 20,
      :starttime => 21,
      :vsize => 22,
      :rss => 23,
      :rsslim => 24,
      :startcode => 25,
      :endcode => 26,
      :startstac => 27,
      :guest_time => 42,
      :cguest_time => 43,
    }.each do |k, v|
      define_method(k) do
        chunks.at(v)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.13509933774834437, 'fraction_numerical': 0.027019867549668876, 'mean_word_length': 1.5173333333333334, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 63, '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': '6060001', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1406, 'n_words': 337}