Commit ed243671 authored by allison.browne's avatar allison.browne

Batch grafana metrics and add check for grafana_url

Batch grafana metrics to avoid query timeout. Also check the projects
grafana url to see if it is in the description to avoid conflicts
with non grafana inline metric embeds.
parent edcd1329
......@@ -65,7 +65,7 @@ class Issue < ApplicationRecord
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 :grafana_embedded, -> { where(Issue.arel_table[:description_html].matches('%data-dashboard-url%')) }
scope :grafana_embedded, -> { where('"issues"."description_html" LIKE \'%data-dashboard-url%\'').where('"issues"."description" ~* "grafana_integrations"."grafana_url"') }
scope :public_only, -> { where(confidential: false) }
scope :confidential_only, -> { where(confidential: true) }
......
......@@ -4,7 +4,12 @@ module Gitlab
class GrafanaEmbedUsageData
class << self
def issue_count
Issue.with_project_grafana_integration.grafana_embedded.count
count = 0
Issue.select(:id).with_project_grafana_integration.grafana_embedded.each_batch do |issue_batch|
count += issue_batch.count
end
count
end
end
end
......
......@@ -9,6 +9,7 @@ describe Gitlab::GrafanaEmbedUsageData do
let(:project) { create(:project) }
let(:description_with_embed) { "Some comment\n\nhttps://grafana.example.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" }
let(:description_with_non_grafana_inline_metric) { "Some comment\n\n#{Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url(*['foo', 'bar', 12])}" }
shared_examples "zero count" do
it "does not count the issue" do
......@@ -23,9 +24,12 @@ describe Gitlab::GrafanaEmbedUsageData do
context 'with valid and invalid embeds' do
before do
# Valid
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project, description: description_with_embed)
# In-Valid
create(:issue, project: project, description: description_with_unintegrated_embed)
create(:issue, project: project, description: description_with_non_grafana_inline_metric)
create(:issue, project: project, description: nil)
create(:issue, project: project, description: '')
create(:issue, project: project)
......
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