diff --git a/app/controllers/leaderboards_controller.rb b/app/controllers/leaderboards_controller.rb index b3a58b1..67a8cbc 100644 --- a/app/controllers/leaderboards_controller.rb +++ b/app/controllers/leaderboards_controller.rb @@ -78,16 +78,7 @@ class LeaderboardsController < ApplicationController def generate_regional_leaderboard return nil unless current_user&.timezone_utc_offset - leaderboard = Leaderboard.where.not(finished_generating_at: nil) - .find_by( - start_date: start_date, - period_type: @period_type, - timezone_utc_offset: current_user.timezone_utc_offset, - deleted_at: nil - ) - - # fuck - leaderboard || LeaderboardGenerator.generate_timezone_offset_leaderboard( + LeaderboardGenerator.generate_timezone_offset_leaderboard( start_date, current_user.timezone_utc_offset, @period_type ) end @@ -105,7 +96,6 @@ class LeaderboardsController < ApplicationController leaderboard = Rails.cache.fetch(cache_key, expires_in: 1.minute) do Leaderboard.where.not(finished_generating_at: nil) - .global .find_by(start_date: start_date, period_type: @period_type, deleted_at: nil) end diff --git a/app/jobs/warm_mini_leaderboard_cache_job.rb b/app/jobs/warm_mini_leaderboard_cache_job.rb index 23dd8c1..28e5b0c 100644 --- a/app/jobs/warm_mini_leaderboard_cache_job.rb +++ b/app/jobs/warm_mini_leaderboard_cache_job.rb @@ -6,79 +6,15 @@ class WarmMiniLeaderboardCacheJob < ApplicationJob offsets.each do |offset| begin - # for some fucnking reason this shit broke now im pissed - [:daily, :weekly, :last_7_days].each do |period_type| - generate_timezone_leaderboard(offset, period_type) - end - - Rails.logger.info "made leaderboard offset #{offset >= 0 ? '+' : ''}#{offset}" + LeaderboardGenerator.generate_timezone_offset_leaderboard( + Date.current, + offset, + :daily + ) + Rails.logger.info "Warmed mini leaderboard cache for UTC#{offset >= 0 ? '+' : ''}#{offset}" rescue => e - Rails.logger.error "didnt make leaderboard offset #{offset >= 0 ? '+' : ''}#{offset}: #{e.message}" + Rails.logger.error "Failed to warm cache for UTC#{offset >= 0 ? '+' : ''}#{offset}: #{e.message}" end end end - - private - - def generate_timezone_leaderboard(offset, period_type) - date = case period_type - when :weekly then Date.current.beginning_of_week - when :last_7_days then Date.current - 6.days - else Date.current - end - - leaderboard = Leaderboard.create!( - start_date: date, - period_type: period_type, - timezone_utc_offset: offset - ) - - users = User.users_in_timezone_offset(offset).not_convicted - user_ids = users.pluck(:id) - - return leaderboard if user_ids.empty? - - date_range = case period_type - when :weekly - date.beginning_of_day...(date + 7.days).beginning_of_day - when :last_7_days - (date - 6.days).beginning_of_day...date.end_of_day - else - date.all_day - end - - ActiveRecord::Base.transaction do - entries_data = Heartbeat.where(user_id: user_ids, time: date_range) - .coding_only - .with_valid_timestamps - .joins(:user) - .where.not(users: { github_uid: nil }) - .group(:user_id) - .duration_seconds - - entries_data = entries_data.filter { |_, total_seconds| total_seconds > 60 } - - streaks = Heartbeat.daily_streaks_for_users(entries_data.keys) if entries_data.any? - - entries_to_create = entries_data.map do |user_id, total_seconds| - { - leaderboard_id: leaderboard.id, - user_id: user_id, - total_seconds: total_seconds, - streak_count: streaks[user_id] || 0 - } - end - - LeaderboardEntry.insert_all!(entries_to_create) if entries_to_create.any? - end - - leaderboard.update!(finished_generating_at: Time.current) - - Leaderboard.where.not(id: leaderboard.id) - .where(start_date: date, period_type: period_type, timezone_utc_offset: offset) - .where(deleted_at: nil) - .update_all(deleted_at: Time.current) - - leaderboard - end end diff --git a/app/models/leaderboard.rb b/app/models/leaderboard.rb index e16a34a..eca396b 100644 --- a/app/models/leaderboard.rb +++ b/app/models/leaderboard.rb @@ -10,13 +10,9 @@ class Leaderboard < ApplicationRecord enum :period_type, { daily: 0, weekly: 1, - last_7_days: 2, - daily_timezone_normalized: 3 + last_7_days: 2 } - scope :for_timezone_offset, ->(offset) { where(timezone_utc_offset: offset) } - scope :global, -> { where(timezone_utc_offset: nil) } - def finished_generating? finished_generating_at.present? end diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb index 045cef3..d7210a0 100644 --- a/config/initializers/good_job.rb +++ b/config/initializers/good_job.rb @@ -23,29 +23,25 @@ Rails.application.configure do GoodJob.active_record_parent_class = "ApplicationDirectRecord" config.good_job.cron = { - update_slack_status: { - cron: "*/5 * * * *", - class: "UserSlackStatusUpdateJob" - }, - daily_leaderboard_update: { - cron: "* * * * *", - class: "LeaderboardUpdateJob", - args: [ :daily ] - }, - weekly_leaderboard_update: { - cron: "*/2 * * * *", - class: "LeaderboardUpdateJob", - args: [ :weekly ] - }, - last_7_days_leaderboard_update: { - cron: "*/7 * * * *", - class: "LeaderboardUpdateJob", - args: [ :last_7_days ] - }, - timezone_leaderboard_refresh: { - cron: "*/5 * * * *", - class: "WarmMiniLeaderboardCacheJob" - }, + # update_slack_status: { + # cron: "*/5 * * * *", + # class: "UserSlackStatusUpdateJob" + # }, + # daily_leaderboard_update: { + # cron: "* * * * *", + # class: "LeaderboardUpdateJob", + # args: [ :daily ] + # }, + # weekly_leaderboard_update: { + # cron: "*/2 * * * *", + # class: "LeaderboardUpdateJob", + # args: [ :weekly ] + # }, + # last_7_days_leaderboard_update: { + # cron: "*/7 * * * *", + # class: "LeaderboardUpdateJob", + # args: [ :last_7_days ] + # }, # sailors_log_poll: { # cron: "*/2 * * * *", # class: "SailorsLogPollForChangesJob" @@ -58,28 +54,28 @@ Rails.application.configure do # cron: "0 12 * * *", # class: "UpdateSlackNeighborhoodChannelsJob" # }, - slack_username_update: { - cron: "0 0 * * *", - class: "SlackUsernameUpdateJob" - }, - scan_github_repos: { - cron: "0 10 * * *", - class: "ScanGithubReposJob" - }, - sync_all_user_repo_events: { - cron: "0 */6 * * *", # Every 6 hours (at minute 0 of 0, 6, 12, 18 hours) - class: "SyncAllUserRepoEventsJob", - description: "Periodically syncs repository events for all eligible users." - }, - scan_repo_events_for_commits: { - cron: "0 */3 * * *", # Every 3 hours at minute 0 - class: "ScanRepoEventsForCommitsJob", - description: "Scans repository host events (PushEvents) and enqueues jobs to process new commits." - }, - cleanup_expired_email_verification_requests: { - cron: "* * * * *", - class: "CleanupExpiredEmailVerificationRequestsJob" - }, + # slack_username_update: { + # cron: "0 0 * * *", + # class: "SlackUsernameUpdateJob" + # }, + # scan_github_repos: { + # cron: "0 10 * * *", + # class: "ScanGithubReposJob" + # }, + # sync_all_user_repo_events: { + # cron: "0 */6 * * *", # Every 6 hours (at minute 0 of 0, 6, 12, 18 hours) + # class: "SyncAllUserRepoEventsJob", + # description: "Periodically syncs repository events for all eligible users." + # }, + # scan_repo_events_for_commits: { + # cron: "0 */3 * * *", # Every 3 hours at minute 0 + # class: "ScanRepoEventsForCommitsJob", + # description: "Scans repository host events (PushEvents) and enqueues jobs to process new commits." + # }, + # cleanup_expired_email_verification_requests: { + # cron: "* * * * *", + # class: "CleanupExpiredEmailVerificationRequestsJob" + # }, # update_airtable_user_data: { # cron: "0 13 * * *", # class: "UpdateAirtableUserDataJob" @@ -139,24 +135,24 @@ Rails.application.configure do trigger_time_update: { cron: "*/15 * * * *", class: "Neighborhood::TriggerTimeUpdateJob" - }, - geocode_users_without_country: { - cron: "7 * * * *", - class: "GeocodeUsersWithoutCountryJob" - }, - cleanup_successful_jobs: { - cron: "0 0 * * *", - class: "CleanupSuccessfulJobsJob" - }, + } + # geocode_users_without_country: { + # cron: "7 * * * *", + # class: "GeocodeUsersWithoutCountryJob" + # }, + # cleanup_successful_jobs: { + # cron: "0 0 * * *", + # class: "CleanupSuccessfulJobsJob" + # }, # sync_stale_repo_metadata: { # cron: "0 4 * * *", # Daily at 4 AM # class: "SyncStaleRepoMetadataJob", # description: "Refreshes repository metadata (stars, commit counts, etc.) for repositories with stale data." # }, - cleanup_old_leaderboards: { - cron: "0 3 * * *", # daily at 3 - class: "CleanupOldLeaderboardsJob", - description: "Remove leaderboards older than 2 days" - } + # cleanup_old_leaderboards: { + # cron: "0 3 * * *", # daily at 3 + # class: "CleanupOldLeaderboardsJob", + # description: "Remove leaderboards older than 2 days" + # } } end diff --git a/db/migrate/20250701035652_add_timezone_utc_offset_to_leaderboards.rb b/db/migrate/20250701035652_add_timezone_utc_offset_to_leaderboards.rb deleted file mode 100644 index 8782f4e..0000000 --- a/db/migrate/20250701035652_add_timezone_utc_offset_to_leaderboards.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddTimezoneUtcOffsetToLeaderboards < ActiveRecord::Migration[8.0] - def change - add_column :leaderboards, :timezone_utc_offset, :integer - end -end diff --git a/db/schema.rb b/db/schema.rb index a606cfb..f9b3bcf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_07_01_035652) do +ActiveRecord::Schema[8.0].define(version: 2025_06_30_000002) do create_schema "pganalyze" # These are extensions that must be enabled in order to support this database @@ -272,7 +272,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_07_01_035652) do t.datetime "finished_generating_at" t.datetime "deleted_at" t.integer "period_type", default: 0, null: false - t.integer "timezone_utc_offset" t.index ["start_date"], name: "index_leaderboards_on_start_date", where: "(deleted_at IS NULL)" end