Skip to content

Commit

Permalink
vim_performance_state_for_ts - don't find(now)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
kbrock committed Jul 11, 2023
1 parent 0a7830e commit 9877c05
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/metric/ci_mixin/state_finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/models/metric/ci_mixin/state_finders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,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
Expand Down

0 comments on commit 9877c05

Please sign in to comment.