app/models/project_stat.rb
# Stamp (fill in) the current values into a ProjectStat. Uses database.
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/BlockLength
Stamp (fill in) the current values into a ProjectStat. Uses database.
Stamp ( fill in ) the current values into a ProjectStat . Uses database .
def stamp
    Project.transaction do
      STAT_VALUES.each do |completion|
        public_send "percent_ge_#{completion}=", Project.gteq(completion).count
        next if completion.to_i.zero? 
        public_send "percent_1_ge_#{completion}=",
                    Project.where(
                      'badge_percentage_1 >= ?',
                      completion.to_i
                    ).count
        public_send "percent_2_ge_#{completion}=",
                    Project.where(
                      'badge_percentage_2 >= ?',
                      completion.to_i
                    ).count
      end
      self.projects_edited = Project.where('created_at < updated_at').count
      self.created_since_yesterday = Project.created_since(1.day.ago).count
      self.updated_since_yesterday = Project.updated_since(1.day.ago).count -
                                     created_since_yesterday
      self.reminders_sent =
        Project.where('last_reminder_at > ?', 1.day.ago).count
      self.reactivated_after_reminder =
        Project.where('last_reminder_at > ?', REACTIVATION_PERIOD.day.ago)
               .where('updated_at > ?', REACTIVATION_PERIOD.day.ago).count
      self.active_projects = Project.updated_since(ACTIVE_PERIOD.day.ago).count
      self.active_edited_projects =
        Project.updated_since(ACTIVE_PERIOD.day.ago)
               .where('created_at < updated_at').count
      self.active_in_progress =
        Project.updated_since(ACTIVE_PERIOD.day.ago)
               .where('badge_percentage_0 < 100').count
      self.active_edited_in_progress =
        Project.updated_since(ACTIVE_PERIOD.day.ago)
               .where('created_at < updated_at')
               .where('badge_percentage_0 < 100').count
      User.transaction do
        self.users = User.count
        self.github_users = User.where(provider: 'github').count
        self.local_users = User.where(provider: 'local').count
        self.users_created_since_yesterday = User.created_since(1.day.ago).count
        self.users_updated_since_yesterday = User.updated_since(1.day.ago).count
        self.users_with_projects = Project.select(:user_id).distinct.count
        self.users_without_projects = users - users_with_projects
        self.users_with_multiple_projects =
          Project.unscoped.group(:user_id).having('count(*) > 1').count.length
        self.users_with_passing_projects =
          Project.select(:user_id)
                 .where('badge_percentage_0 >= 100')
                 .distinct.count
        self.users_with_silver_projects =
          Project.select(:user_id)
                 .where('badge_percentage_1 >= 100')
                 .distinct.count
        self.users_with_gold_projects =
          Project.select(:user_id)
                 .where('badge_percentage_2 >= 100')
                 .distinct.count
      end
      AdditionalRight.transaction do
        self.additional_rights_entries = AdditionalRight.count
        self.projects_with_additional_rights =
          AdditionalRight.select(:project_id).distinct.count
        self.users_with_additional_rights =
          AdditionalRight.select(:user_id).distinct.count
      end
    end
    self 
  end
def stamp Project . transaction do STAT_VALUES . each do | completion | public_send "percent_ge_#{completion}=" , Project . gteq ( completion ) . count next if completion . to_i . zero? public_send "percent_1_ge_#{completion}=" , Project . where ( 'badge_percentage_1 >= ?' , completion . to_i ) . count public_send "percent_2_ge_#{completion}=" , Project . where ( 'badge_percentage_2 >= ?' , completion . to_i ) . count end self . projects_edited = Project . where ( 'created_at < updated_at' ) . count self . created_since_yesterday = Project . created_since ( 1 . day . ago ) . count self . updated_since_yesterday = Project . updated_since ( 1 . day . ago ) . count - created_since_yesterday self . reminders_sent = Project . where ( 'last_reminder_at > ?' , 1 . day . ago ) . count self . reactivated_after_reminder = Project . where ( 'last_reminder_at > ?' , REACTIVATION_PERIOD . day . ago ) . where ( 'updated_at > ?' , REACTIVATION_PERIOD . day . ago ) . count self . active_projects = Project . updated_since ( ACTIVE_PERIOD . day . ago ) . count self . active_edited_projects = Project . updated_since ( ACTIVE_PERIOD . day . ago ) . where ( 'created_at < updated_at' ) . count self . active_in_progress = Project . updated_since ( ACTIVE_PERIOD . day . ago ) . where ( 'badge_percentage_0 < 100' ) . count self . active_edited_in_progress = Project . updated_since ( ACTIVE_PERIOD . day . ago ) . where ( 'created_at < updated_at' ) . where ( 'badge_percentage_0 < 100' ) . count User . transaction do self . users = User . count self . github_users = User . where ( provider : 'github' ) . count self . local_users = User . where ( provider : 'local' ) . count self . users_created_since_yesterday = User . created_since ( 1 . day . ago ) . count self . users_updated_since_yesterday = User . updated_since ( 1 . day . ago ) . count self . users_with_projects = Project . select ( :user_id ) . distinct . count self . users_without_projects = users - users_with_projects self . users_with_multiple_projects = Project . unscoped . group ( :user_id ) . having ( 'count(*) > 1' ) . count . length self . users_with_passing_projects = Project . select ( :user_id ) . where ( 'badge_percentage_0 >= 100' ) . distinct . count self . users_with_silver_projects = Project . select ( :user_id ) . where ( 'badge_percentage_1 >= 100' ) . distinct . count self . users_with_gold_projects = Project . select ( :user_id ) . where ( 'badge_percentage_2 >= 100' ) . distinct . count end AdditionalRight . transaction do self . additional_rights_entries = AdditionalRight . count self . projects_with_additional_rights = AdditionalRight . select ( :project_id ) . distinct . count self . users_with_additional_rights = AdditionalRight . select ( :user_id ) . distinct . count end end self end
Stamp (fill in) the current values into a ProjectStat.
Stamp ( fill in ) the current values into a ProjectStat .
# Use a transaction to get values from a single consistent point in time. # Count projects at different levels of completion # Don't record percentage "0" > level 0 # These use 1.day.ago, so a create or updates in 24 hours + fractional # seconds won't be counted today, and *might* not be counted previously. # This isn't important enough to solve. # Exclude newly-created records from updated_since count # Record the number of reminders sent within the day. # If an inactive project becomes active within REACTIVATION_PERIOD # number of days after a reminder, the reminder is likely to be the # cause.  Record the number of such projects. # Record the number of active project badge entries # (the number of project badge entries updated # within ACTIVE_PERIOD number of days) # The following nested transaction is defensive coding. # We don't need to use a nested transaction today, because Project and # User are implemented in a single database.  However, we ever # implemented Project and User in multiple class-specific databases # in the future, and did not nest them, we would silently be # outside a transaction - and that bug would be hard to detect. See: # http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ # ClassMethods.html # Some of these values can be calculated from others, but it's # convenient to provide them separately. # Return self to support method chaining