<commit_before>#-------------------------------------------------------------------------------
# AssetAuditor
#
# Generic auditor for auditing an asset inventory
#
#-------------------------------------------------------------------------------
class AssetAuditor < AbstractAuditor

  #-----------------------------------------------------------------------------
  # Takes set of properties and generates a list of assets that will be audited
  #-----------------------------------------------------------------------------
  def audit options={}

    # Loop through each org at a time
    Organization.all.each do |org|
      # Only process operational assets
      write_to_activity_log org, "Performing #{audit.name} on asset inventory"
      Asset.operational.where(:organization => org).order(:asset_subtype_id).pluck(:object_key).each do |obj_key|
        asset = Asset.find_by(object_key: obj_key)
        update_status asset
      end
    end

  end
  #-----------------------------------------------------------------------------
  # Takes an asset (typed or untyped) and checks the compliance and updates the
  # audit table wth the results
  #-----------------------------------------------------------------------------
  def update_status a

    errors = []
    if a.nil?
      Rails.logger.debug "Asset cannot be nil"
      return errors
    end

    # Dont check disposed of assets
    if a.disposed?
      return errors
    end

    # Strongly type the asset but only if we need to
    asset = a.is_typed? ? a : Asset.get_typed_asset(a)

    Rails.logger.debug "Testing asset #{asset.object_key} for compliance. Type is #{asset.class.name}"
    start_date = Chronic.parse('10/1/2015').to_date
    end_date = Chronic.parse('12/31/2015').to_date

    passed = true
    if asset.reported_condition_date.blank? or asset.reported_condition_date < start_date
      passed = false
      errors << "Condition has not been updated during the audit period"
    end

    if asset.service_status_date.blank? or asset.service_status_date < start_date
      passed = false
      errors << "Service Status has not been updated during the audit period"
    end

    if asset.respond_to? :milage_updates
      if asset.reported_mileage_date.blank? or asset.reported_mileage_date < start_date
        passed = false
        errors << "Mileage has not been updated during the audit period"
      end
    end

    audit = AuditResult.find_or_create_by(:organization_id => asset.organization_id, :auditable_id => asset.id, :auditable_type => 'Asset', :audit_id => @audit.id) do |aud|
      aud.class_name = asset.asset_type.name
      aud.audit_result_type_id = (passed == true) ? AuditResultType::AUDIT_RESULT_PASSED : AuditResultType::AUDIT_RESULT_FAILED
      if errors.present?
        aud.notes = errors.compactjoin("\n")
      end
    end
    audit.save

  end

  #-----------------------------------------------------------------------------
  #
  #-----------------------------------------------------------------------------
  def initialize(audit)
    super
  end

  #-----------------------------------------------------------------------------
  protected
  #-----------------------------------------------------------------------------

end<commit_msg>Fix bug<commit_after>#-------------------------------------------------------------------------------
# AssetAuditor
#
# Generic auditor for auditing an asset inventory
#
#-------------------------------------------------------------------------------
class AssetAuditor < AbstractAuditor

  #-----------------------------------------------------------------------------
  # Takes set of properties and generates a list of assets that will be audited
  #-----------------------------------------------------------------------------
  def audit options={}

    # Loop through each org at a time
    Organization.all.each do |org|
      # Only process operational assets
      write_to_activity_log org, "Performing #{audit.name} on asset inventory"
      Asset.operational.where(:organization => org).order(:asset_subtype_id).pluck(:object_key).each do |obj_key|
        asset = Asset.find_by(object_key: obj_key)
        update_status asset
      end
    end

  end
  #-----------------------------------------------------------------------------
  # Takes an asset (typed or untyped) and checks the compliance and updates the
  # audit table wth the results
  #-----------------------------------------------------------------------------
  def update_status a

    errors = []
    if a.nil?
      Rails.logger.debug "Asset cannot be nil"
      return errors
    end

    # Dont check disposed of assets
    if a.disposed?
      return errors
    end

    # Strongly type the asset but only if we need to
    asset = a.is_typed? ? a : Asset.get_typed_asset(a)

    Rails.logger.debug "Testing asset #{asset.object_key} for compliance. Type is #{asset.class.name}"
    start_date = Chronic.parse('10/1/2015').to_date
    end_date = Chronic.parse('12/31/2015').to_date

    passed = true
    if asset.reported_condition_date.blank? or asset.reported_condition_date < start_date
      passed = false
      errors << "Condition has not been updated during the audit period"
    end

    if asset.service_status_date.blank? or asset.service_status_date < start_date
      passed = false
      errors << "Service Status has not been updated during the audit period"
    end

    if asset.respond_to? :milage_updates
      if asset.reported_mileage_date.blank? or asset.reported_mileage_date < start_date
        passed = false
        errors << "Mileage has not been updated during the audit period"
      end
    end

    audit = AuditResult.find_or_create_by(:organization_id => asset.organization_id, :auditable_id => asset.id, :auditable_type => 'Asset', :audit_id => @audit.id) do |aud|
      aud.class_name = asset.asset_type.name
      aud.audit_result_type_id = (passed == true) ? AuditResultType::AUDIT_RESULT_PASSED : AuditResultType::AUDIT_RESULT_FAILED
      if errors.present?
        aud.notes = errors.join("\n")
      end
    end
    audit.save

  end

  #-----------------------------------------------------------------------------
  #
  #-----------------------------------------------------------------------------
  def initialize(audit)
    super
  end

  #-----------------------------------------------------------------------------
  protected
  #-----------------------------------------------------------------------------

end
{'fraction_non_alphanumeric': 0.3010290277991092, 'fraction_numerical': 0.004607587160190447, 'mean_word_length': 1.330708661417323, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, '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': '91935', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1548, 'n_words': 450}