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