Add streaks (#114)

* Add heartbeatable daily streak helper

* Add streaks to the navbar

* Add streak count to leaderboard entries
This commit is contained in:
Max Wofford 2025-03-24 16:39:20 -04:00 committed by GitHub
parent 7cd2aa3de6
commit a7574956df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 53 additions and 1 deletions

View file

@ -19,6 +19,36 @@ module Heartbeatable
end
end
def streak_days(start_date: 8.days.ago)
scope = coding_only.with_valid_timestamps
days = scope.daily_durations(start_date: start_date, end_date: Time.current)
.sort_by { |date, _| date }
.reverse
streak = 0
days.each do |date, duration|
if duration >= 15 * 60
streak += 1
else
break
end
end
streak
end
def streak_days_formatted(start_date: 8.days.ago)
result = streak_days(start_date: start_date)
if result > 7
"7+"
elsif result < 1
nil
else
result.to_s
end
end
def duration_formatted(scope = all)
seconds = duration_seconds(scope)
hours = seconds / 3600

View file

@ -23,6 +23,8 @@ class User < ApplicationRecord
has_many :api_keys
delegate :streak_days, :streak_days_formatted, to: :heartbeats
enum :hackatime_extension_text_type, {
simple_text: 0,
clock_emoji: 1,

View file

@ -51,6 +51,15 @@
working on <%= link_to @active_projects[entry.user_id].project_name, @active_projects[entry.user_id].repo_url, target: "_blank" %>
</span>
<% end %>
<% if entry.streak_count > 7 %>
<span class="super" title="7+ daily streak">
🔥 7+
</span>
<% elsif entry.streak_count > 0 %>
<span class="super" title="<%= entry.streak_count %> day streak">
🔥 <%= entry.streak_count %>
</span>
<% end %>
</span>
<span class="time"><%= short_time_detailed entry.total_seconds %></span>
</div>

View file

@ -10,6 +10,11 @@
<% if current_user %>
<li>
<%= render "shared/user_mention", user: current_user %>
<% if current_user.streak_days_formatted %>
<p title="🔥 <%= current_user.streak_days_formatted %> day streak">
<%= current_user.streak_days_formatted %> 🔥
</p>
<% end %>
<% if current_user.active_project && current_user.active_project_duration > 60 %>
<p>
Working on: <%= current_user.active_project %>

View file

@ -0,0 +1,5 @@
class AddStreakCountToLeaderboardEntries < ActiveRecord::Migration[8.0]
def change
add_column :leaderboard_entries, :streak_count, :integer, default: 0
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_03_19_193636) do
ActiveRecord::Schema[8.0].define(version: 2025_03_24_203539) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"
@ -162,6 +162,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_19_193636) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", null: false
t.integer "streak_count", default: 0
t.index ["leaderboard_id", "user_id"], name: "idx_leaderboard_entries_on_leaderboard_and_user", unique: true
t.index ["leaderboard_id"], name: "index_leaderboard_entries_on_leaderboard_id"
end