|
1 | 1 | include ApplicationHelper
|
2 | 2 |
|
3 | 3 | 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) |
5 | 5 | @scope = scope || Heartbeat.all
|
6 | 6 | @user = user
|
| 7 | + @boundary_aware = boundary_aware |
7 | 8 |
|
8 | 9 | @start_date = convert_to_unix_timestamp(start_date)
|
9 | 10 | @end_date = convert_to_unix_timestamp(end_date)
|
10 | 11 |
|
| 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 | + |
11 | 17 | # Default to 1 year ago if no start_date provided or if no data exists
|
12 | 18 | @start_date = @start_date || @scope.minimum(:time) || 1.year.ago.to_i
|
13 | 19 | @end_date = @end_date || @scope.maximum(:time) || Time.current.to_i
|
14 | 20 |
|
15 |
| - @scope = @scope.where("time >= ? AND time < ?", @start_date, @end_date) |
| 21 | + @scope = @scope.where(time: @start_date..@end_date) |
16 | 22 |
|
17 | 23 | @limit = limit
|
18 | 24 | @limit = nil if @limit&.zero?
|
@@ -41,7 +47,14 @@ def generate_summary
|
41 | 47 | summary[:range] = "all_time"
|
42 | 48 | summary[:human_readable_range] = "All Time"
|
43 | 49 |
|
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 | + |
45 | 58 | summary[:total_seconds] = @total_seconds
|
46 | 59 |
|
47 | 60 | @total_days = (@end_time - @start_time) / 86400
|
|
0 commit comments