<commit_before>namespace :best_boy do
  desc "Creates consistent DayReport and MonthReport structure for given events"
  task :recover_report_history  => :environment do 
    unreported = BestBoyEvent.where(reported: false)

    puts "" 
    puts "=================================================================="
    puts "= Creating missing reports for #{unreported.count} events in steps of 1000 records ="
    puts "=================================================================="
    puts ""

    unreported.find_in_batches do |batch|
      batch.each do |event|
        event_month_scope = BestBoy::MonthReport.where(eventable_type: event.owner_type, event_type: event.event)
        event_day_scope = BestBoy::DayReport.where(eventable_type: event.owner_type, event_type: event.event)

        # care for a MonthReport WITHOUT source
        #
        # 

        month_report = event_month_scope.where(event_source: nil).month(event.created_at.month, event.created_at.year).first
        if month_report.nil?
          month_report = event_month_scope.new.tap { |r| r.created_at = event.created_at; r.save! }
        end 
        month_report.increment!(:occurences)
        
        # care for a DayReport WITHOUT source
        #
        #
         
        day_report = event_day_scope.where(event_source: nil).created_on(event.created_at).first  
        if day_report.nil?
          day_report = event_day_scope.new(month_report_id: month_report.id).tap { |r| r.created_at = event.created_at; r.save! }       
        end  
        day_report.increment!(:occurences)
        
        if event.event_source?
          
          # care for a MonthReport WITH source
          #
          #  

          month_report_with_source = event_month_scope.where(event_source: event.event_source).month(event.created_at.month, event.created_at.year).first
          if month_report_with_source.nil?
            month_report_with_source = event_month_scope.new(event_source: event.event_source).tap { |r| r.created_at = event.created_at; r.save! }
          end
          month_report_with_source.increment!(:occurences)
          <commit_msg>use new scopes in rake task<commit_after>name.space :best_boy do
  desc "Creates consistent DayReport and MonthReport structure for given events"
  task :recover_report_history  => :environment do 
    unreported = BestBoyEvent.where(reported: false)

    puts "" 
    puts "=================================================================="
    puts "= Creating missing reports for #{unreported.count} events in steps of 1000 records ="
    puts "=================================================================="
    puts ""

    unreported.find_in_batches do |batch|
      batch.each do |event|
        event_month_scope = BestBoy::MonthReport.where(eventable_type: event.owner_type, event_type: event.event)
        event_day_scope = BestBoy::DayReport.where(eventable_type: event.owner_type, event_type: event.event)

        # care for a MonthReport WITHOUT source
        #
        # 

        month_report = event_month_scope.where(event_source: nil).between(event.created_at.beginning_of_month, event.created_at.end_of_month).first
        if month_report.nil?
          month_report = event_month_scope.new.tap { |r| r.created_at = event.created_at; r.save! }
        end 
        month_report.increment!(:occurences)
        
        # care for a DayReport WITHOUT source
        #
        #
         
        day_report = event_day_scope.where(event_source: nil).created_on(event.created_at).first  
        if day_report.nil?
          day_report = event_day_scope.new(month_report_id: month_report.id).tap { |r| r.created_at = event.created_at; r.save! }       
        end  
        day_report.increment!(:occurences)
        
        if event.event_source?
          
          # care for a MonthReport WITH source
          #
          #  

          month_report_with_source = event_month_scope.where(event_source: event.event_source).between(event.created_at.beginning_of_month, event.created_at.end_of_month).first
          if month_report_with_source.nil?
            month_report_with_source = event_month_scope.new(event_source: event.event_source).tap { |r| r.created_at = event.created_at; r.save! }
          end
          month_report_with_source.increment!(:occurences)
          
{'fraction_non_alphanumeric': 0.1419399220362302, 'fraction_numerical': 0.0018344416418252694, 'mean_word_length': 2.940379403794038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2217042', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1253, 'n_words': 235}