Commit 102485c9 authored by Piotr Skorupa's avatar Piotr Skorupa

Change DatabaseMetric to cache only with cache key passed

parent 649d6cb3
...@@ -46,7 +46,7 @@ module Gitlab ...@@ -46,7 +46,7 @@ module Gitlab
end end
def value def value
start, finish = get_or_cache_batch_ids if batch_query? start, finish = get_or_cache_batch_ids
method(self.class.metric_operation) method(self.class.metric_operation)
.call(relation, .call(relation,
...@@ -69,10 +69,6 @@ module Gitlab ...@@ -69,10 +69,6 @@ module Gitlab
private private
def batch_query?
self.class.metric_start.present? && self.class.metric_finish.present?
end
def relation def relation
self.class.metric_relation.call.where(time_constraints) self.class.metric_relation.call.where(time_constraints)
end end
...@@ -91,11 +87,9 @@ module Gitlab ...@@ -91,11 +87,9 @@ module Gitlab
end end
def get_or_cache_batch_ids def get_or_cache_batch_ids
key_name = if self.class.cache_key.present? return [self.class.start, self.class.finish] unless self.class.cache_key.present?
"metric_instrumentation/#{self.class.cache_key}"
else key_name = "metric_instrumentation/#{self.class.cache_key}"
"metric_instrumentation/#{relation.table_name}"
end
start = Rails.cache.fetch("#{key_name}_minimum_id", expires_in: 1.day) do start = Rails.cache.fetch("#{key_name}_minimum_id", expires_in: 1.day) do
self.class.start self.class.start
......
...@@ -25,11 +25,11 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do ...@@ -25,11 +25,11 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do
expect(subject.value).to eq(3) expect(subject.value).to eq(3)
end end
it 'caches the result of start and finish', :use_clean_rails_redis_caching do it 'does not cache the result of start and finish', :use_clean_rails_redis_caching do
subject.value subject.value
expect(Rails.cache.read('metric_instrumentation/issues_minimum_id')).to eq(314) expect(Rails.cache.read('metric_instrumentation/issues_minimum_id')).to eq(nil)
expect(Rails.cache.read('metric_instrumentation/issues_maximum_id')).to eq(949) expect(Rails.cache.read('metric_instrumentation/issues_maximum_id')).to eq(nil)
end end
context 'with start and finish not called' do context 'with start and finish not called' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment