<commit_before>require 'trollop'
require 'yaml'
require 'pg_inspector/error'
require 'pg_inspector/pg_inspector_operation'
require 'pg_inspector/util'

module PgInspector
  class LockConnectionYAML < PgInspectorOperation
    HELP_MSG_SHORT = "Dump lock friendly connection information to YAML file".freeze
    attr_accessor :locks

    def parse_options(args)
      self.options = Trollop.options(args) do
        opt(:locks, "Lock file",
            :type => :string, :short => "l", :default => DEFAULT_OUTPUT_PATH + "locks.yml")
        opt(:connections, "Human readable active connections file",
            :type => :string, :short => "c", :default => DEFAULT_OUTPUT_PATH + "human.yml")
        opt(:output, "Output file",
            :type => :string, :short => "o", :default => DEFAULT_OUTPUT_PATH + "locks_output.yml")
      end
    end

    def run
      load_lock_file
      process_lock_file
      Util.dump_to_yml_file(
        merge_lock_and_connection(
          YAML.load_file(options[:connections])
        ), "Lock friendly connection info", options[:output]
      )
    end

    private

    def merge_lock_and_connection(connections)
      some_connection_blocked = false
      connections["connections"].each do |conn|
        conn["blocked_by"] = find_lock_blocking_spid(conn["spid"])
        unless conn["blocked_by"].empty?
          some_connection_blocked = true
          puts "Connection #{conn["spid"]} is blocked by #{conn["blocked_by"]}."
        end
      end
      unless some_connection_blocked
        puts "Every connection is OK and not blocked. No need to generate lock graph."
      end
      connections
    end

    def load_lock_file
      self.locks = YAML.load_file(options[:locks])
    end

    def process_lock_file
      locks.each do |lock|
        lock["spid"] = lock["pid"].to_i<commit_msg>input file doesn't exist rescue in step 4<commit_after>require 'trollop'
require 'yaml'
require 'pg_inspector/error'
require 'pg_inspector/pg_inspector_operation'
require 'pg_inspector/util'

module PgInspector
  class LockConnectionYAML < PgInspectorOperation
    HELP_MSG_SHORT = "Dump lock friendly connection information to YAML file".freeze
    attr_accessor :locks, :connections

    def parse_options(args)
      self.options = Trollop.options(args) do
        opt(:locks, "Lock file",
            :type => :string, :short => "l", :default => DEFAULT_OUTPUT_PATH + "locks.yml")
        opt(:connections, "Human readable active connections file",
            :type => :string, :short => "c", :default => DEFAULT_OUTPUT_PATH + "human.yml")
        opt(:output, "Output file",
            :type => :string, :short => "o", :default => DEFAULT_OUTPUT_PATH + "locks_output.yml")
      end
    end

    def run
      load_connection_file
      load_lock_file
      process_lock_file
      Util.dump_to_yml_file(
        merge_lock_and_connection,
        "Lock friendly connection info",
        options[:output]
      )
    end

    private

    def merge_lock_and_connection(connections)
      some_connection_blocked = false
      self.connections["connections"].each do |conn|
        conn["blocked_by"] = find_lock_blocking_spid(conn["spid"])
        unless conn["blocked_by"].empty?
          some_connection_blocked = true
          puts "Connection #{conn["spid"]} is blocked by #{conn["blocked_by"]}."
        end
      end
      unless some_connection_blocked
        puts "Every connection is OK and not blocked. No need to generate lock graph."
      end
      self.connections
    end

    def load_connection_file
      self.connections = YAML.load_file(options[:connections])
    rescue => e
      Util.error_exit(e)
    end

    def load_lock_file
      self.locks = YAML.load_file(options[:locks])
    rescue => e
      Util.error_exit(e)
    end

    def process_lock_file
      locks.each do |lock|
        lock["spid"] = lock["pid"].to_i
{'fraction_non_alphanumeric': 0.1024390243902439, 'fraction_numerical': 0.00025673940949935817, 'mean_word_length': 2.9433198380566803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '4611734', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1148, 'n_words': 284}