This commit is contained in:
Echo
2025-07-01 00:24:36 -04:00
parent 8aff60f04c
commit 158e065df3
6 changed files with 65 additions and 153 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1,5 +0,0 @@
class AddTimezoneUtcOffsetToLeaderboards < ActiveRecord::Migration[8.0]
def change
add_column :leaderboards, :timezone_utc_offset, :integer
end
end

3
db/schema.rb generated
View File

@@ -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