Running timers page with view/stop permissions

- New top-menu page listing running timers: owner, issue, project,
  activity, start time, time on this run and entry total, oldest first;
  refreshes every minute
- Role permissions "View running timers" and "Stop other users' timers",
  checked against each timer's project; users can always stop their own
- A timer can be stopped at an earlier time (when the person left):
  hours logged after it are taken off the entry, and the comment notes
  who stopped it and when
- Timers now record when they were started (session_started_at); timers
  running from before show "unknown" until restarted
- Integration tests for permissions, project scoping and stopping; unit
  tests for Timer#stop_for

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-29 07:06:08 -04:00
1 parent 626db60658
commit 78f53d5d5a
11 files changed
+399 -1

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
module Stopwatch
# Everyone's running timers, for the running timers page.
class RunningTimers
Row = Struct.new(:user, :timer, :time_entry, keyword_init: true) do
def project
time_entry&.project
end
def issue
time_entry&.issue
end
def started_at
timer.session_started_at
end
# Whether viewer may stop this timer: their own, or with "Stop running timers" in its project
def stoppable_by?(viewer)
return true if user == viewer
return viewer.admin? if project.nil?
viewer.allowed_to?(:stop_running_timers, project)
end
end
# Running timers the viewer may see: their own, plus timers on entries in projects where they have
# "View running timers". Oldest first, so forgotten timers are at the top.
def self.visible_to(viewer)
all.select do |row|
row.user == viewer ||
(row.project ? viewer.allowed_to?(:view_running_timers, row.project) : viewer.admin?)
end
end
def self.all
# Timers live in each user's preferences; only rows mentioning a timer are loaded
UserPreference.where('others LIKE ?', '%current_timer%').includes(:user).filter_map do |pref|
user = pref.user
next unless user
timer = Timer.new(user)
next unless timer.running?
Row.new(user: user, timer: timer, time_entry: timer.time_entry)
end.sort_by { |row| [row.started_at ? 0 : 1, row.started_at.to_i, row.user.name] }
end
end
end
+50
View File
@@ -7,6 +7,7 @@ module Stopwatch
# time_entry_id - the time entry the timer adds to
# session_hours - hours already added since the timer was started, for the maximum timer length
# tick_token - identifies this start's chain of server-side saves (StopwatchTimerTickJob)
# session_started_at - when the timer was started (started_at moves forward with every save)
class Timer
class HourFormatter
include ApplicationHelper
@@ -44,6 +45,7 @@ module Stopwatch
data[:session_hours] = 0.0
data[:time_entry_id] = time_entry&.id
data[:tick_token] = token
data[:session_started_at] = data[:started_at]
save
end
# Keep saving the time on the server every minute while it runs
@@ -54,6 +56,53 @@ module Stopwatch
update stop: true
end
# Stops the timer for someone else (e.g. a manager on the running timers page).
# at: when the timer should have stopped, e.g. when the person actually left. Hours logged after
# that are taken back off the entry. Must be between the start and now; nil means now.
# by: the user stopping it; noted in the entry's comment.
# Returns the hours this timer session ends up with, or nil if the timer wasn't running.
def stop_for(at: nil, by: nil)
locked do
return nil unless running?
started = session_started_at
total = session_hours # before saving, so a timer at its limit is trimmed correctly too
apply_elapsed_time(stop: true)
save
entry = time_entry # fresh copy, after the elapsed time was saved
keep = total
stopped_at = Time.now
if entry && at && started
stopped_at = Time.at(at.to_i.clamp(started.to_i, Time.now.to_i))
keep = [(stopped_at - started).to_f / 1.hour, total].min
excess = total - keep
entry.update_columns(hours: [entry.read_attribute(:hours).to_f - excess, 0].max) if excess.positive?
end
if entry && by && by != user
zone = user.time_zone || Time.zone
note = I18n.t(:text_stopwatch_timer_stopped_by, name: by.name,
time: stopped_at.in_time_zone(zone).strftime('%Y-%m-%d %H:%M'))
entry.update_columns(comments: [entry.comments.to_s.presence, note].compact.join(' ').truncate(1024))
end
Rails.logger.info "[Stopwatch] Timer for user ##{user.id} stopped by #{by ? "user ##{by.id}" : 'system'}, #{keep.round(2)}h kept"
keep
end
end
# When this timer was started, or nil for timers started before the start time was recorded
def session_started_at
ts = data[:session_started_at]
ts.present? ? Time.at(ts.to_i) : nil
end
# Hours this timer has run since it was started (saved plus unsaved, never beyond the limit)
def session_hours
data[:session_hours].to_f + unsaved_hours
end
# Saves the time elapsed since the last save into the time entry, then stops the timer
# (stop: true) or keeps it running from now. The timer record is locked meanwhile, so two requests
# at the same moment (e.g. two open tabs) can't add the same time twice. Once a timer reaches the
@@ -149,6 +198,7 @@ module Stopwatch
Rails.logger.info "[Stopwatch] Timer for user ##{user.id} stopped at the #{Stopwatch.max_timer_hours}h limit" if capped
data[:started_at] = nil
data[:session_hours] = 0.0
data[:session_started_at] = nil
else
data[:started_at] = Time.now.to_i
data[:session_hours] = data[:session_hours].to_f + hours