Skip to content

Commit 6d85937

Browse files
authored
Merge pull request #430 from cskartikey/lag_fix
fix: use WakatimeService without filters and consistent time ranges f…
2 parents 92b5846 + f676755 commit 6d85937

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

app/controllers/api/v1/stats_controller.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,10 @@ def user_stats
6161
service_params[:scope] = scope if scope.present?
6262

6363
if params[:total_seconds] == "true"
64-
query = @user.heartbeats
65-
.coding_only
66-
.with_valid_timestamps
67-
.where(time: start_date..end_date)
68-
69-
if params[:filter_by_project].present?
70-
filter_by_project = params[:filter_by_project].split(",")
71-
query = query.where(project: filter_by_project)
72-
end
73-
74-
# do the boundary thingie if requested
75-
use_boundary_aware = params[:boundary_aware] == "true"
76-
total_seconds = if use_boundary_aware
77-
Heartbeat.duration_seconds_boundary_aware(query, start_date.to_f, end_date.to_f) || 0
78-
else
79-
query.duration_seconds || 0
80-
end
64+
service_params[:boundary_aware] = params[:boundary_aware] == "true"
8165

82-
return render json: { total_seconds: total_seconds }
66+
summary = WakatimeService.new(**service_params).generate_summary
67+
return render json: { total_seconds: summary[:total_seconds] }
8368
end
8469

8570
summary = WakatimeService.new(**service_params).generate_summary

lib/wakatime_service.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
include ApplicationHelper
22

33
class WakatimeService
4-
def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil)
4+
def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil, boundary_aware: false)
55
@scope = scope || Heartbeat.all
66
@user = user
7+
@boundary_aware = boundary_aware
78

89
@start_date = convert_to_unix_timestamp(start_date)
910
@end_date = convert_to_unix_timestamp(end_date)
1011

12+
# apply with_valid_timestamps filter if no custom scope provided-- this is copied from query in stats_controller
13+
if scope.nil?
14+
@scope = @scope.with_valid_timestamps
15+
end
16+
1117
# Default to 1 year ago if no start_date provided or if no data exists
1218
@start_date = @start_date || @scope.minimum(:time) || 1.year.ago.to_i
1319
@end_date = @end_date || @scope.maximum(:time) || Time.current.to_i
1420

15-
@scope = @scope.where("time >= ? AND time < ?", @start_date, @end_date)
21+
@scope = @scope.where(time: @start_date..@end_date)
1622

1723
@limit = limit
1824
@limit = nil if @limit&.zero?
@@ -41,7 +47,14 @@ def generate_summary
4147
summary[:range] = "all_time"
4248
summary[:human_readable_range] = "All Time"
4349

44-
@total_seconds = @scope.duration_seconds || 0
50+
@total_seconds = if @boundary_aware
51+
result = Heartbeat.duration_seconds_boundary_aware(@scope, @start_date, @end_date) || 0
52+
result
53+
else
54+
result = @scope.duration_seconds || 0
55+
result
56+
end
57+
4558
summary[:total_seconds] = @total_seconds
4659

4760
@total_days = (@end_time - @start_time) / 86400

0 commit comments

Comments
 (0)