Commit 364d921a authored by allison.browne's avatar allison.browne

Changes to grafana usage data based on CR

Use find_each instead of each_batch. Simplify specs.
Cleanup and consistency changes. Query only for issues
with a grafana integration enabled
parent 86d8e294
...@@ -62,6 +62,7 @@ class Issue < ApplicationRecord ...@@ -62,6 +62,7 @@ class Issue < ApplicationRecord
scope :order_relative_position_asc, -> { reorder(::Gitlab::Database.nulls_last_order('relative_position', 'ASC')) } scope :order_relative_position_asc, -> { reorder(::Gitlab::Database.nulls_last_order('relative_position', 'ASC')) }
scope :preload_associated_models, -> { preload(:labels, project: :namespace) } scope :preload_associated_models, -> { preload(:labels, project: :namespace) }
scope :with_project_grafana_integration, -> { includes(project: :grafana_integration).where(projects: { grafana_integrations: { enabled: true } } ) }
scope :with_api_entity_associations, -> { preload(:timelogs, :assignees, :author, :notes, :labels, project: [:route, { namespace: :route }] ) } scope :with_api_entity_associations, -> { preload(:timelogs, :assignees, :author, :notes, :labels, project: [:route, { namespace: :route }] ) }
scope :public_only, -> { where(confidential: false) } scope :public_only, -> { where(confidential: false) }
......
...@@ -4,26 +4,17 @@ module Gitlab ...@@ -4,26 +4,17 @@ module Gitlab
class GrafanaEmbedUsageData class GrafanaEmbedUsageData
class << self class << self
def issue_count def issue_count
get_embed_count
end
private
def get_embed_count
Issue.class_eval { include EachBatch } unless Issue < EachBatch
count = 0 count = 0
Issue.eager_load(project: :grafana_integration).each_batch do |issue_batch| Issue.with_project_grafana_integration.find_each do |issue|
embed_count_per_batch = issue_batch.map do |issue| count += has_grafana_url?(issue) ? 1 : 0
has_grafana_url?(issue)
end.count(&:itself)
count += embed_count_per_batch
end end
count count
end end
private
def has_grafana_url?(issue) def has_grafana_url?(issue)
html = Banzai.render(issue.description, project: issue.project) html = Banzai.render(issue.description, project: issue.project)
result = Banzai::Filter::InlineGrafanaMetricsFilter.new( result = Banzai::Filter::InlineGrafanaMetricsFilter.new(
......
...@@ -13,53 +13,43 @@ describe Gitlab::GrafanaEmbedUsageData do ...@@ -13,53 +13,43 @@ describe Gitlab::GrafanaEmbedUsageData do
let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" } let(:description_with_unintegrated_embed) { "Some comment\n\nhttps://grafana.exp.com/d/xvAk4q0Wk/go-processes?orgId=1&from=1573238522762&to=1573240322762&var-job=prometheus&var-interval=10m&panelId=1&fullscreen" }
shared_examples "zero count" do shared_examples "zero count" do
context "without grafana embeds" do it "does not count the issue" do
it "does not count the issue" do expect(subject).to eq(0)
expect(subject).to eq(0)
end
end end
end end
context 'with grafana integrated project' do context 'with project grafana integration enabled' do
before do before do
create(:grafana_integration, project: project) create(:grafana_integration, project: project)
end end
it 'counts multiple issues with grafana links' do context 'with valid and invalid embeds' do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
expect(subject).to eq(2)
end
context 'issue description has no grafana link' do
before do before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project, description: description_with_unintegrated_embed)
create(:issue, project: project, description: nil)
create(:issue, project: project, description: '')
create(:issue, project: project) create(:issue, project: project)
end end
it_behaves_like('zero count') it 'counts only the issues with embeds' do
end expect(subject).to eq(2)
context 'issue description is nil' do
before do
create(:issue, project: project, description: nil)
end end
it_behaves_like('zero count')
end end
end
context 'issue description is empty' do context 'with project grafana integration disabled' do
before do before do
create(:issue, project: project, description: '') create(:grafana_integration, project: project, enabled: false)
end
it_behaves_like('zero count')
end end
context 'issue description has grafana link for un-integrated grafana instance' do context 'with one issue having a grafana link in the description and one without' do
before do before do
create(:issue, project: project, description: description_with_unintegrated_embed) create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end end
it_behaves_like('zero count') it_behaves_like('zero count')
end end
end end
...@@ -73,22 +63,6 @@ describe Gitlab::GrafanaEmbedUsageData do ...@@ -73,22 +63,6 @@ describe Gitlab::GrafanaEmbedUsageData do
it_behaves_like('zero count') it_behaves_like('zero count')
end end
context 'with issue description nil' do
before do
create(:issue, project: project, description: nil)
end
it_behaves_like('zero count')
end
context 'with issue description empty' do
before do
create(:issue, project: project, description: '')
end
it_behaves_like('zero count')
end
end end
end end
end end
...@@ -53,7 +53,7 @@ describe Gitlab::UsageData do ...@@ -53,7 +53,7 @@ describe Gitlab::UsageData do
create(:grafana_integration, project: projects[1], enabled: true) create(:grafana_integration, project: projects[1], enabled: true)
create(:grafana_integration, project: projects[2], enabled: false) create(:grafana_integration, project: projects[2], enabled: false)
allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count) { 2 } allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count).and_return(2)
ProjectFeature.first.update_attribute('repository_access_level', 0) ProjectFeature.first.update_attribute('repository_access_level', 0)
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