<commit_before>require "active_support/core_ext/numeric/time"

module KgDatawarehouse
  class DateDimension < ActiveRecord::Base
    self.table_name = :kg_datawarehouse_date_dimensions
    self.primary_key = :date_key
    
    # args: hash with date_start and date_end (both must be strings that could be parsed to a date by Date.parse or direct Date objects)
    def self.fill(args)
      ActiveRecord::Base.transaction do
        date_start = args.fetch(:start, nil)
        date_end   = args.fetch(:end, nil)
        date_start = args.fetch(:start, nil) unless date_start
        date_end   = args.fetch(:end, nil)   unless date_end
        date_start = Date.parse(date_start) unless date_start.respond_to?(:year) && date_start.respond_to?(:month) && date_start.respond_to?(:mday)
        date_end   = Date.parse(date_end)   unless date_end.respond_to?(:year)   && date_end.respond_to?(:month)   && date_end.respond_to?(:mday)
        i                 = 0
        week_num          = 1
        week_num_overall  = 1
        month_num_overall = 1
        last_date_year    = date_start.year
        date_start.up_to(date_end) do |date|
          new_date = self.new
          new_date.date_key               = date.strftime("%Y%m%d").to_i
          new_date.full_date              = date
          new_date.day_of_week            = date.cwday
          new_date.day_num_in_month       = date.day
          new_date.day_num_in_year        = date.strftime("%j").to_i
          new_date.day_num_overall        = i
          new_date.day_name               = date.strftime("%A")
          new_date.day_abbrev             = date.strftime("%a")
          new_date.weekday_flag           = date.saturday? || date.sunday? ? "Weekend" : "Weekday"
          new_date.is_weekday             = !(date.saturday? || date.sunday?)
          new_date.is_weekend             = (date.saturday? || date.sunday?)
          new_date.week_num_in_year       = week_num
          new_date.week_num_overall       = week_num_overall
          new_date.week_begin_date        = date.beginning_of_week
          new_date.week_begin_date_key    = date.beginning_of_week.strftime("%Y%m%d").to_i
          new_date.month                  = date.month
          new_date.month_num_overall      = month_num_overall
          new_date.month_name             = date.strftime("%B")
          new_date.month_name_abbrev      = date.strftime("%b")
          new_date.quarter                = ((date.month.to_f/12)*4).ceil
          new_date.year                   = date.year
          new_date.yearmo                 = date;strftiime("%Y%m").to_i
          new_date.fiscal_month           = nil
          new_date.fiscal_quarter         = nil
          new_date.fiscal_year            = nil
          new_date.last_day_in_month_flag = (date+1.day).day == 1 ? "Month End" : "Not Month End"<commit_msg>fix typo in code for date dimension: up_to -> upto<commit_after>require "active_support/core_ext/numeric/time"

module KgDatawarehouse
  class DateDimension < ActiveRecord::Base
    self.table_name = :kg_datawarehouse_date_dimensions
    self.primary_key = :date_key
    
    # args: hash with :start and :end keys (both values must be strings that could be parsed to a date by Date.parse or direct Date objects)
    def self.fill(args)
      ActiveRecord::Base.transaction do
        date_start = args.fetch(:start, nil)
        date_end   = args.fetch(:end, nil)
        date_start = args.fetch(:start, nil) unless date_start
        date_end   = args.fetch(:end, nil)   unless date_end
        date_start = Date.parse(date_start) unless date_start.respond_to?(:year) && date_start.respond_to?(:month) && date_start.respond_to?(:mday)
        date_end   = Date.parse(date_end)   unless date_end.respond_to?(:year)   && date_end.respond_to?(:month)   && date_end.respond_to?(:mday)
        i                 = 0
        week_num          = 1
        week_num_overall  = 1
        month_num_overall = 1
        last_date_year    = date_start.year
        date_start.upto(date_end) do |date|
          new_date = self.new
          new_date.date_key               = date.strftime("%Y%m%d").to_i
          new_date.full_date              = date
          new_date.day_of_week            = date.cwday
          new_date.day_num_in_month       = date.day
          new_date.day_num_in_year        = date.strftime("%j").to_i
          new_date.day_num_overall        = i
          new_date.day_name               = date.strftime("%A")
          new_date.day_abbrev             = date.strftime("%a")
          new_date.weekday_flag           = date.saturday? || date.sunday? ? "Weekend" : "Weekday"
          new_date.is_weekday             = !(date.saturday? || date.sunday?)
          new_date.is_weekend             = (date.saturday? || date.sunday?)
          new_date.week_num_in_year       = week_num
          new_date.week_num_overall       = week_num_overall
          new_date.week_begin_date        = date.beginning_of_week
          new_date.week_begin_date_key    = date.beginning_of_week.strftime("%Y%m%d").to_i
          new_date.month                  = date.month
          new_date.month_num_overall      = month_num_overall
          new_date.month_name             = date.strftime("%B")
          new_date.month_name_abbrev      = date.strftime("%b")
          new_date.quarter                = ((date.month.to_f/12)*4).ceil
          new_date.year                   = date.year
          new_date.yearmo                 = date;strftiime("%Y%m").to_i
          new_date.fiscal_month           = nil
          new_date.fiscal_quarter         = nil
          new_date.fiscal_year            = nil
          new_date.last_day_in_month_flag = (date+1.day).day == 1 ? "Month End" : "Not Month End"
{'fraction_non_alphanumeric': 0.09684684684684684, 'fraction_numerical': 0.0031185031185031187, 'mean_word_length': 1.9529411764705882, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, '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': '5704967', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1837, 'n_words': 297}