Commit 649d6cb3 authored by Piotr Skorupa's avatar Piotr Skorupa

Add cache_start_and_finish_as and update specs

parent ba4b88e1
......@@ -38,7 +38,11 @@ module Gitlab
@column = column
end
attr_reader :metric_operation, :metric_relation, :metric_start, :metric_finish, :column
def cache_start_and_finish_as(cache_key)
@cache_key = cache_key
end
attr_reader :metric_operation, :metric_relation, :metric_start, :metric_finish, :column, :cache_key
end
def value
......@@ -87,7 +91,11 @@ module Gitlab
end
def get_or_cache_batch_ids
key_name = "metric_instrumentation/#{relation.table_name}"
key_name = if self.class.cache_key.present?
"metric_instrumentation/#{self.class.cache_key}"
else
"metric_instrumentation/#{relation.table_name}"
end
start = Rails.cache.fetch("#{key_name}_minimum_id", expires_in: 1.day) do
self.class.start
......
......@@ -31,5 +31,37 @@ RSpec.describe Gitlab::Usage::Metrics::Instrumentations::DatabaseMetric do
expect(Rails.cache.read('metric_instrumentation/issues_minimum_id')).to eq(314)
expect(Rails.cache.read('metric_instrumentation/issues_maximum_id')).to eq(949)
end
context 'with start and finish not called' do
subject do
described_class.tap do |m|
m.relation { Issue }
m.operation :count
end.new(time_frame: 'all')
end
it 'calculates a correct result' do
expect(subject.value).to eq(3)
end
end
context 'with cache_start_and_finish_as called' do
subject do
described_class.tap do |m|
m.relation { Issue }
m.operation :count
m.start { m.relation.minimum(:id) }
m.finish { m.relation.maximum(:id) }
m.cache_start_and_finish_as :special_issue_count
end.new(time_frame: 'all')
end
it 'caches using the key name passed', :use_clean_rails_redis_caching do
subject.value
expect(Rails.cache.read('metric_instrumentation/special_issue_count_minimum_id')).to eq(314)
expect(Rails.cache.read('metric_instrumentation/special_issue_count_maximum_id')).to eq(949)
end
end
end
end
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