From e6268fc9d71f7c4ad3c0aadd41f9bc5e188d3dde Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 7 Jul 2023 10:24:40 -0400 Subject: [PATCH] vim_performance_state_for_ts - don't find(now) since perf_capture_state does a find for now, not reason to try and find it first have tried to remove this extra query a few times. previous attempts deleted this line. this one is different as we added the unless. there were a few cases that we do want a find here. (luckily there are tests around this) --- app/models/metric/ci_mixin/state_finders.rb | 7 +++++-- spec/models/metric/ci_mixin/state_finders_spec.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/models/metric/ci_mixin/state_finders.rb b/app/models/metric/ci_mixin/state_finders.rb index 8fee49425ba..0f59ff9068a 100644 --- a/app/models/metric/ci_mixin/state_finders.rb +++ b/app/models/metric/ci_mixin/state_finders.rb @@ -32,8 +32,11 @@ def vim_performance_state_for_ts(ts) # look for state from previous perf_capture_state call state ||= @states_by_ts[ts_iso_now] else - state = @states_by_ts[Metric::Helper.nearest_hourly_timestamp(Time.now.utc)] - state ||= vim_performance_states.find_by(:timestamp => ts) + ts_iso_now = Metric::Helper.nearest_hourly_timestamp(Time.now.utc) + state = @states_by_ts[ts_iso_now] + unless ts_iso_now == ts_iso + state ||= vim_performance_states.find_by(:timestamp => ts) + end end state ||= perf_capture_state @states_by_ts[state.timestamp.utc.iso8601] = state diff --git a/spec/models/metric/ci_mixin/state_finders_spec.rb b/spec/models/metric/ci_mixin/state_finders_spec.rb index 089220b879e..9fdecaf7d00 100644 --- a/spec/models/metric/ci_mixin/state_finders_spec.rb +++ b/spec/models/metric/ci_mixin/state_finders_spec.rb @@ -93,6 +93,15 @@ expect(image.vim_performance_state_for_ts(timestamp)).to eq(vps_now) end.not_to make_database_queries end + + it "doesn't search db for now since perf_capture_state will do that" do + expect(image).to receive(:perf_capture_state).once.and_return(vps_now) + + expect do + expect(image.vim_performance_state_for_ts(ts_now)).to eq(vps_now) + end.to make_database_queries(:count => 0) + expect { image.vim_performance_state_for_ts(ts_now) }.not_to make_database_queries + end end # ci_mixin/processing.rb uses this