class DataValidation::Comparison::ComparisonBase

Attributes

category[R]
division_id[R]
logger_name[R]
season[R]
web_url[R]

Public Class Methods

new(web_url) click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 9
def initialize(web_url)
  @web_url = web_url
end

Public Instance Methods

check_item(item, team, re_check) click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 60
def check_item(item, team, re_check)
  error_info = []
  i = 0
  rank_value = nil # the value used to rank

  field_array.each do |field_name|

    if re_check && field_name == related_rank_field
      i += 1
      next
    end

    if field_name.is_a? String
      team_value = team[field_name]
    elsif field_name.is_a? Array
      team_value = team[field_name[0]][field_name[1]]
    else
      i += 1
      next
    end

    if item.length < field_array.length && empty_field && empty_field == field_name ## when the field is empty in the table
      error_info << "team #{item[0]}'s #{field_name} is not empty: #{team_value}" unless (team_value == 0 || team_value.nil?)
      next
    end


    if compare_values(item[i], team_value)

      if field_name == rank_field
        rank_value = team_value
      end
    else
      error_info << "team #{item[0]}'s #{field_name}: #{item[i]} -- #{team_value}"
    end

    i += 1
  end
  [error_info, rank_value]
end
compare() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 13
def compare
  if api_path.nil?
    raise 'Please define api_path'
  end

  if field_array.nil?
    raise 'Please define field_array'
  end

  data_access = DataValidation::DataAccess.new(web_url, DataValidation.api_request_host + api_path, determine_request_body_and_logger_name)

  puts 'requesting web page...'
  if web_url.match /rating[a-z]+.php/
    web_data = data_access.get_regional_rating_table_data
  else
    web_data = data_access.get_main_table_data
  end

  puts 'requesting mobile api data: ' + DataValidation.api_request_host + api_path
  api_data = data_access.get_response_from_mobile_api

  if web_data.length != api_data['results']['results']['teams'].length
    logger.error "The record size between web page and api data is not the same: #{web_data.length} -- #{api_data['results']['results']['teams'].length}"
    return
  end

  web_data.each_with_index do |item, index|
    teams = api_data['results']['results']['teams']
    team = teams[index]

    error_log = []

    error_info, rank_value = check_item(item, team, false)
    error_log << error_info

    if error_info.size > 0 && rank_value
      possible_teams = teams.select { |team| team[rank_field] == rank_value }
      possible_teams.each do |team|
        error, value = check_item(item, team, true)
        error_log << error
      end
    end

    error_log.min_by { |log| log.length }.each { |item| logger.error item }
  end
end
compare_conf_standings(api_data, data_access) click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 107
def compare_conf_standings(api_data, data_access)
  puts 'Start to compare conf ranking tables'
  conf_standing_data = data_access.conf_standing_data

  conf_standing_data.each do |standing|
    full_conf = ''
    standing.each_with_index do |record, i|
      if i == 0
        state = record[0]
        team_class = record[1]
        conf = record[2]
        full_conf = "#{state}, #{team_class}, #{conf}"

        api_data = data_access.get_response_from_mobile_api_with_params(conf_rank_api_url, %Q|{"season": "#{season}", "conference": {"category": "#{category}", "conference": "#{conf}", "divisionId": #{division_id.to_i}, "state": "#{state}", "teamClass": "#{team_class}"}, "currPage": 1, "pageSize": 1000}|)

        if api_data['results']['results']['total'] != standing.length - 1
          logger.error "standing for #{full_conf} has incorrect length: #{standing.length - 1} : #{api_data['results']['results']['total']}"
          break
        end
      else

        team_data = api_data['results']['results']['teams'][i - 1]

        record.each_with_index do |item, index|
          if index == 0 && item.to_i != team_data['rank']
          elsif index == 1 && !compare_floats(team_data['powerRating'], item)
            logger.error "team #{i} powerRating in #{full_conf}: #{item.to_f} -- #{team_data['powerRating']}"
          elsif index == 2 && item.to_i != team_data['conference']['wins']
            logger.error "team #{i} conference wins in #{full_conf}: #{item.to_i} -- #{team_data['conference']['wins']}"
          elsif index == 3 && item.to_i != team_data['conference']['losses']
            logger.error "team #{i} conference losses in #{full_conf}: #{item.to_i} -- #{team_data['conference']['losses']}"
          elsif index == 4 && item.to_i != team_data['conference']['ties']
            logger.error "team #{i} conference ties in #{full_conf}: #{item.to_i} -- #{team_data['conference']['ties']}"
          elsif index == 5 && !compare_floats(team_data['winLossConf'], item)
            logger.error "team #{i} winLossConf in #{full_conf}: #{item.to_f} -- #{team_data['winLossConf']}"
          elsif index == 6 && item.to_i != team_data['total']['wins']
            logger.error "team #{i} total wins in #{full_conf}: #{item.to_i} -- #{team_data['total']['wins']}"
          elsif index == 7 && item.to_i != team_data['total']['losses']
            logger.error "team #{i} total losses in #{full_conf}: #{item.to_i} -- #{team_data['total']['losses']}"
          elsif index == 8 && item.to_i != team_data['total']['ties']
            logger.error "team #{i} total ties in #{full_conf}: #{item.to_i} -- #{team_data['total']['ties']}"
          elsif index == 9 && !compare_floats(team_data['winLossTotal'], item)
            logger.error "team #{i} winLossTotal in #{full_conf}: #{item.to_f} -- #{team_data['winLossTotal']}"
          end
        end
      end

    end
  end
end
compare_values(page_item, team_value) click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 101
def compare_values(page_item, team_value)
  (team_value.is_a?(String) && team_value.include?(page_item.strip)) ||
    (team_value.is_a?(Float) && compare_floats(team_value, page_item)) ||
    (team_value.is_a?(Integer) && team_value == page_item.to_i)
end

Protected Instance Methods

api_path() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 160
def api_path

end
compare_floats(team_value, page_item) click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 202
def compare_floats(team_value, page_item)
  f = page_item.to_f
  (team_value - f).round(decimal_places_num) <= 0.01
end
decimal_places_num() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 207
def decimal_places_num
  2
end
determine_request_body_and_logger_name() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 180
def determine_request_body_and_logger_name
  @category = if web_url.match /bingrl/
                'GIRLS'
              elsif web_url.match /binboy/
                'BOYS'
              elsif web_url.match /binmen/
                'MEN'
              elsif web_url.match /binwom/
                'WOMEN'
              end

  @season = "20#{web_url.match(/update(..)/)[1]}"

  @division_id = web_url.match(/(\d+)\.php/)[1].to_i

  operation = web_url.match(/\/([a-z]+)\d+.php/)[1]

  @logger_name = "log/#{season}/#{category.downcase}/#{operation}/#{operation}_#{season}_#{category}_#{division_id}.log"

  %Q|{"category":"#{category}","season":#{season},"divisionId":#{division_id},"currPage":1,"pageSize":1000}|
end
empty_field() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 168
def empty_field

end
field_array() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 164
def field_array

end
rank_field() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 172
def rank_field

end

Private Instance Methods

logger() click to toggle source
# File lib/data_validation/comparison/comparison_base.rb, line 213
def logger
  @logger ||= begin
    tokens = logger_name.split('/')
    1.upto(tokens.size - 1) do |n|
      dir = tokens[0...n].join('/')
      Dir.mkdir(dir) unless Dir.exist?(dir)
    end
    logger = Logger.new(File.new(logger_name, 'w'))
    logger.info(web_url)
    logger
  end
end