<commit_before>require 'chef/knife/tidy_base'

class Chef
  class Knife
    class TidyServerReport < Knife

      include Knife::TidyBase

      deps do
        require 'ffi_yajl'
      end

      banner "knife tidy server report (options)"

      option :node_threshold,
        :long => '--node-threshold NUM_DAYS',
        :default => 30,
        :description => 'Maximum number of days since last checkin before node is considered stale (default: 30)'

      def run
        ensure_reports_dir!
        FileUtils.rm_f(server_warnings_file_path)

        ui.stdout.puts(ui.color("Writing to #{tidy.reports_dir} directory", :magenta))
        delete_existing_reports

        orgs = if config[:org_list]
                 config[:org_list].split(',')
               else
                 all_orgs
               end

        stale_orgs = []
        node_threshold = config[:node_threshold].to_i

        orgs.each do |org|
          pre_12_3_nodes = []
          unconverged_recent_nodes = []
          ui.info "  Organization: #{org}"
          cb_list = cookbook_list(org)
          version_count = cookbook_count(cb_list).sort_by(&:last).reverse.to_h
          used_cookbooks = {}
          nodes = nodes_list(org)
          db_nodes = rest.get("/organizations/#{org}/nodes")
          unless nodes.length == db_nodes.length
            ood_message = "Search index is out of date! No cleanup action will be taken for #{org}."
            ui.error(ood_message)
            action_needed(ood_message, server_warnings_file_path)
            next
          end

          nodes.each do |node|
            # If the node hasn't checked in.
            if !node['chef_packages']
              # If the node is under an hour old.
              if (Time.now.to_i - node['ohai_time'].to_i) < 3600
                unconverged_recent_nodes << node['name']
              end
              next
            end
            chef_version = Gem::Version.new(node['chef_packages']['chef']['version'])
            # If the node has checked in within the node_threshold with a client older than 12.3
            if chef_version < Gem::Version.new("12.3") && (Time.now.to_i - node['ohai_time'].to_i) <= node_threshold * 86400
              pre_12_3_nodes << node['name']
            end
          end

          nodes.select{|node| !node['cookbooks'].nil?}.each do |node|
            node['cookbooks'].each do |name, version_hash|
              version = Gem::Version.new(version_hash['version']).to_s
              if used_cookbooks[name] && !used_cookbooks[name].include?(version)
                used_cookbooks[name].push(version)
              else
                used_cookbooks[name] = [version]
              end
            end
          end

          pins = environment_constraints(org)
          used_cookbooks = check_environment_pins(used_cookbooks, pins, cb_list)

          stale_nodes = []
          nodes.each do |n|<commit_msg>Fix conditional logic in building used_cookbook list<commit_after>require 'chef/knife/tidy_base'

class Chef
  class Knife
    class TidyServerReport < Knife

      include Knife::TidyBase

      deps do
        require 'ffi_yajl'
      end

      banner "knife tidy server report (options)"

      option :node_threshold,
        :long => '--node-threshold NUM_DAYS',
        :default => 30,
        :description => 'Maximum number of days since last checkin before node is considered stale (default: 30)'

      def run
        ensure_reports_dir!
        FileUtils.rm_f(server_warnings_file_path)

        ui.stdout.puts(ui.color("Writing to #{tidy.reports_dir} directory", :magenta))
        delete_existing_reports

        orgs = if config[:org_list]
                 config[:org_list].split(',')
               else
                 all_orgs
               end

        stale_orgs = []
        node_threshold = config[:node_threshold].to_i

        orgs.each do |org|
          pre_12_3_nodes = []
          unconverged_recent_nodes = []
          ui.info "  Organization: #{org}"
          cb_list = cookbook_list(org)
          version_count = cookbook_count(cb_list).sort_by(&:last).reverse.to_h
          used_cookbooks = {}
          nodes = nodes_list(org)
          db_nodes = rest.get("/organizations/#{org}/nodes")
          unless nodes.length == db_nodes.length
            ood_message = "Search index is out of date! No cleanup action will be taken for #{org}."
            ui.error(ood_message)
            action_needed(ood_message, server_warnings_file_path)
            next
          end

          nodes.each do |node|
            # If the node hasn't checked in.
            if !node['chef_packages']
              # If the node is under an hour old.
              if (Time.now.to_i - node['ohai_time'].to_i) < 3600
                unconverged_recent_nodes << node['name']
              end
              next
            end
            chef_version = Gem::Version.new(node['chef_packages']['chef']['version'])
            # If the node has checked in within the node_threshold with a client older than 12.3
            if chef_version < Gem::Version.new("12.3") && (Time.now.to_i - node['ohai_time'].to_i) <= node_threshold * 86400
              pre_12_3_nodes << node['name']
            end
          end

          nodes.select{|node| !node['cookbooks'].nil?}.each do |node|
            node['cookbooks'].each do |name, version_hash|
              version = Gem::Version.new(version_hash['version']).to_s
              if used_cookbooks[name]
                used_cookbooks[name].push(version) unless used_cookbooks[name].include?(version)
              else
                used_cookbooks[name] = [version]
              end
            end
          end

          pins = environment_constraints(org)
          used_cookbooks = check_environment_pins(used_cookbooks, pins, cb_list)

          stale_nodes = []
          nodes.each do |n|
{'fraction_non_alphanumeric': 0.09884706680230587, 'fraction_numerical': 0.0084774499830451, 'mean_word_length': 1.9217434373452205, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501791', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1675, 'n_words': 386}