From eb60d1d82ccdd1b3e60c714a2bb538401d50c9a2 Mon Sep 17 00:00:00 2001 From: Echo Date: Tue, 22 Jul 2025 20:55:26 -0400 Subject: [PATCH] =?UTF-8?q?Revert=20"fix:=20use=20WakatimeService=20withou?= =?UTF-8?q?t=20filters=20and=20consistent=20time=20ranges=20f=E2=80=A6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/api/v1/stats_controller.rb | 21 ++++++++++++++++++--- lib/wakatime_service.rb | 19 +++---------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/stats_controller.rb b/app/controllers/api/v1/stats_controller.rb index fd36846..6b9d5f8 100644 --- a/app/controllers/api/v1/stats_controller.rb +++ b/app/controllers/api/v1/stats_controller.rb @@ -61,10 +61,25 @@ class Api::V1::StatsController < ApplicationController service_params[:scope] = scope if scope.present? if params[:total_seconds] == "true" - service_params[:boundary_aware] = params[:boundary_aware] == "true" + query = @user.heartbeats + .coding_only + .with_valid_timestamps + .where(time: start_date..end_date) - summary = WakatimeService.new(**service_params).generate_summary - return render json: { total_seconds: summary[:total_seconds] } + 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 } end summary = WakatimeService.new(**service_params).generate_summary diff --git a/lib/wakatime_service.rb b/lib/wakatime_service.rb index 928d178..908de9b 100644 --- a/lib/wakatime_service.rb +++ b/lib/wakatime_service.rb @@ -1,24 +1,18 @@ include ApplicationHelper class WakatimeService - def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil, boundary_aware: false) + def initialize(user: nil, specific_filters: [], allow_cache: true, limit: 10, start_date: nil, end_date: nil, scope: nil) @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: @start_date..@end_date) + @scope = @scope.where("time >= ? AND time < ?", @start_date, @end_date) @limit = limit @limit = nil if @limit&.zero? @@ -47,14 +41,7 @@ class WakatimeService summary[:range] = "all_time" summary[:human_readable_range] = "All Time" - @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 - + @total_seconds = @scope.duration_seconds || 0 summary[:total_seconds] = @total_seconds @total_days = (@end_time - @start_time) / 86400