<commit_before>require File.expand_path('../../../vendor/gems/gchart/lib/gchart', __FILE__)

class ApplicationController < ActionController::Base
  include RefurlHelper
  before_filter :initialize_site_settings
  before_filter :authenticate_user!, :except => [:payload]
  protect_from_forgery
  layout 'application'
  helper_method :redirect_to_ref_url, :admin?, :external_hours_chart_url
  rescue_from 'Acl9::AccessDenied', :with => :access_denied

  def build_week_hash_for(date, hash={})
    until date.saturday?
      day = date.strftime("%A")
      hash[day] = date
      date = date.tomorrow
    end
    return hash
  end

  def external_hours_chart_url(users, options = {})
    users                 = Array(users)
    width                 = options.fetch(:width, "450x120")
    date                  = options.fetch(:date, Time.zone.now)
    title                 = options.fetch(:title, "")
    start_date, end_date  = date.beginning_of_week.to_date, date.end_of_week.to_date

    hours = WorkUnit.for_users(users).scheduled_between(start_date,end_date).all
    internal_hours, external_hours, max_hours = determine_daily_hours(hours, start_date, end_date)

    GChart.bar(:title => title,
                         :orientation => :vertical,
                         :axis => [["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], [0, max_hours]],
                         :colors => ['ff0000', '00ff00'],
                         :size => width,
                         :data => [internal_hours,external_hours],
                         :legend => ["Int","Ext"],
                         :extras => {"chf" => "bg,s,00000000"} # Makes fill transparent
                         ).to_url
  end

  private

  def determine_daily_hours hours, start_date, end_date
    internal_hours, external_hours = [],[]
    max_hours = 0
    (start_date..end_date).each do |i_date|
      _beg, _end = i_date.beginning_of_day, i_date.end_of_day
      hours = hours.select {|wu| wu.scheduled_at.to_date == _beg.to_date }
      internal_hours << sum_hours(:internal?, hours)
      external_hours << sum_hours(:external?, hours)
      max_hours = [max_hours, hours.map(&:hours).max.to_i].max
    end
    return [internal_hours, external_hours, max_hours]
  end

  def sum_hours(method, hours)
    hours.select{|wu| wu.send(method) }.sum(&:hours)
  end

  def redirect_unless_monday(path_prefix, date)
    @start_date = date ? Date.parse(date) : Date.current
    unless @start_date.monday?
      redirect_to(path_prefix + @start_date.beginning_of_week.strftime("%F"))
    end
  end<commit_msg>refactor'd<commit_after>require File.expand_path('../../../vendor/gems/gchart/lib/gchart', __FILE__)

class ApplicationController < ActionController::Base
  include RefurlHelper
  before_filter :initialize_site_settings
  before_filter :authenticate_user!, :except => [:payload]
  protect_from_forgery
  layout 'application'
  helper_method :redirect_to_ref_url, :admin?, :external_hours_chart_url
  rescue_from 'Acl9::AccessDenied', :with => :access_denied

  def build_week_hash_for(date, hash={})
    until date.saturday?
      day = date.strftime("%A")
      hash[day] = date
      date = date.tomorrow
    end
    return hash
  end

  def external_hours_chart_url(users, options = {})
    users                 = Array(users)
    width                 = options.fetch(:width, "450x120")
    date                  = options.fetch(:date, Time.zone.now)
    title                 = options.fetch(:title, "")
    start_date, end_date  = date.beginning_of_week.to_date, date.end_of_week.to_date

    hours = WorkUnit.for_users(users).scheduled_between(start_date,end_date).all
    internal_hours, external_hours, max_hours = determine_daily_hours(hours, start_date, end_date)

    GChart.bar(:title => title,
                         :orientation => :vertical,
                         :axis => [["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], [0, max_hours]],
                         :colors => ['ff0000', '00ff00'],
                         :size => width,
                         :data => [internal_hours,external_hours],
                         :legend => ["Int","Ext"],
                         :extras => {"chf" => "bg,s,00000000"} # Makes fill transparent
                         ).to_url
  end

  private

  def determine_daily_hours hours, start_date, end_date
    internal_hours, external_hours = [],[]
    max_hours = 0
    (start_date..end_date).each do |i_date|
      _beg, _end = i_date.beginning_of_day, i_date.end_of_day
      hours = hours.select {|wu| wu.scheduled_at.to_date == _beg.to_date }
      internal_hours << sum_hours(:internal?, hours)
      external_hours << sum_hours(:external?, hours)
      max_hours = [max_hours, max_hours(hours)].max
    end
    return [internal_hours, external_hours, max_hours]
  end

  def sum_hours(method, hours)
    hours.select{|wu| wu.send(method) }.sum(&:hours)
  end

  def max_hours hours
    hours.map(&:hours).max.to_i
  end

  def redirect_unless_monday(path_prefix, date)
    @start_date = date ? Date.parse(date) : Date.current
    unless @start_date.monday?
      redirect_to(path_prefix + @start_date.beginning_of_week.strftime("%F"))
    end
  end
{'fraction_non_alphanumeric': 0.1250720461095101, 'fraction_numerical': 0.009606147934678195, 'mean_word_length': 2.7372577171572146, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '399851', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1613, 'n_words': 310}