Skip to content

Commit eb60d1d

Browse files
authored
Revert "fix: use WakatimeService without filters and consistent time ranges f…"
1 parent 6d85937 commit eb60d1d

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

app/controllers/api/v1/stats_controller.rb

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

6363
if params[:total_seconds] == "true"
64-
service_params[:boundary_aware] = params[:boundary_aware] == "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
6581

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

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

lib/wakatime_service.rb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
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, boundary_aware: false)
4+
def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil)
55
@scope = scope || Heartbeat.all
66
@user = user
7-
@boundary_aware = boundary_aware
87

98
@start_date = convert_to_unix_timestamp(start_date)
109
@end_date = convert_to_unix_timestamp(end_date)
1110

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-
1711
# Default to 1 year ago if no start_date provided or if no data exists
1812
@start_date = @start_date || @scope.minimum(:time) || 1.year.ago.to_i
1913
@end_date = @end_date || @scope.maximum(:time) || Time.current.to_i
2014

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

2317
@limit = limit
2418
@limit = nil if @limit&.zero?
@@ -47,14 +41,7 @@ def generate_summary
4741
summary[:range] = "all_time"
4842
summary[:human_readable_range] = "All Time"
4943

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-
44+
@total_seconds = @scope.duration_seconds || 0
5845
summary[:total_seconds] = @total_seconds
5946

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

0 commit comments

Comments
 (0)