From f67675533c8db2e1f3bb053d264f8051511bfea9 Mon Sep 17 00:00:00 2001 From: Kartikey Chauhan Date: Wed, 23 Jul 2025 03:49:23 +0530 Subject: [PATCH] fix: use WakatimeService without filters and consistent time ranges for accurate tracking --- app/controllers/api/v1/stats_controller.rb | 21 +++------------------ lib/wakatime_service.rb | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/app/controllers/api/v1/stats_controller.rb b/app/controllers/api/v1/stats_controller.rb index 528114d..3eb93d3 100644 --- a/app/controllers/api/v1/stats_controller.rb +++ b/app/controllers/api/v1/stats_controller.rb @@ -61,25 +61,10 @@ class Api::V1::StatsController < ApplicationController service_params[:scope] = scope if scope.present? if params[:total_seconds] == "true" - query = @user.heartbeats - .coding_only - .with_valid_timestamps - .where(time: start_date..end_date) + service_params[:boundary_aware] = params[:boundary_aware] == "true" - if params[:filter_by_project].present? - filter_by_project = params[:filter_by_project].split(",") - query = query.where(project: filter_by_project) - end - - # do the boundary thingie if requested - use_boundary_aware = params[:boundary_aware] == "true" - total_seconds = if use_boundary_aware - Heartbeat.duration_seconds_boundary_aware(query, start_date.to_f, end_date.to_f) || 0 - else - query.duration_seconds || 0 - end - - return render json: { total_seconds: total_seconds } + summary = WakatimeService.new(**service_params).generate_summary + return render json: { total_seconds: summary[:total_seconds] } end summary = WakatimeService.new(**service_params).generate_summary diff --git a/lib/wakatime_service.rb b/lib/wakatime_service.rb index 908de9b..928d178 100644 --- a/lib/wakatime_service.rb +++ b/lib/wakatime_service.rb @@ -1,18 +1,24 @@ include ApplicationHelper class WakatimeService - def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil) + def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil, boundary_aware: false) @scope = scope || Heartbeat.all @user = user + @boundary_aware = boundary_aware @start_date = convert_to_unix_timestamp(start_date) @end_date = convert_to_unix_timestamp(end_date) + # apply with_valid_timestamps filter if no custom scope provided-- this is copied from query in stats_controller + if scope.nil? + @scope = @scope.with_valid_timestamps + end + # Default to 1 year ago if no start_date provided or if no data exists @start_date = @start_date || @scope.minimum(:time) || 1.year.ago.to_i @end_date = @end_date || @scope.maximum(:time) || Time.current.to_i - @scope = @scope.where("time >= ? AND time < ?", @start_date, @end_date) + @scope = @scope.where(time: @start_date..@end_date) @limit = limit @limit = nil if @limit&.zero? @@ -41,7 +47,14 @@ class WakatimeService summary[:range] = "all_time" summary[:human_readable_range] = "All Time" - @total_seconds = @scope.duration_seconds || 0 + @total_seconds = if @boundary_aware + result = Heartbeat.duration_seconds_boundary_aware(@scope, @start_date, @end_date) || 0 + result + else + result = @scope.duration_seconds || 0 + result + end + summary[:total_seconds] = @total_seconds @total_days = (@end_time - @start_time) / 86400