Commit 8e8c3acb authored by Alper Akgun's avatar Alper Akgun

Refactor issues with embedded grafana charts

Refactor issues_with_embedded_grafana_charts_approx to use the batch
counting with failover fallback
parent 98e42f80
---
title: Optimize issues with embedded grafana charts usage counter
merge_request: 28936
author:
type: performance
# frozen_string_literal: true
module Gitlab
class GrafanaEmbedUsageData
class << self
def issue_count
# rubocop:disable CodeReuse/ActiveRecord
Issue.joins('JOIN grafana_integrations USING (project_id)')
.where("issues.description LIKE '%' || grafana_integrations.grafana_url || '%'")
.where(grafana_integrations: { enabled: true })
.count
# rubocop:enable CodeReuse/ActiveRecord
end
end
end
end
...@@ -90,7 +90,7 @@ module Gitlab ...@@ -90,7 +90,7 @@ module Gitlab
issues_created_from_gitlab_error_tracking_ui: count(SentryIssue), issues_created_from_gitlab_error_tracking_ui: count(SentryIssue),
issues_with_associated_zoom_link: count(ZoomMeeting.added_to_issue), issues_with_associated_zoom_link: count(ZoomMeeting.added_to_issue),
issues_using_zoom_quick_actions: distinct_count(ZoomMeeting, :issue_id), issues_using_zoom_quick_actions: distinct_count(ZoomMeeting, :issue_id),
issues_with_embedded_grafana_charts_approx: ::Gitlab::GrafanaEmbedUsageData.issue_count, issues_with_embedded_grafana_charts_approx: grafana_embed_usage_data,
incident_issues: count(::Issue.authored(::User.alert_bot)), incident_issues: count(::Issue.authored(::User.alert_bot)),
keys: count(Key), keys: count(Key),
label_lists: count(List.label), label_lists: count(List.label),
...@@ -133,6 +133,14 @@ module Gitlab ...@@ -133,6 +133,14 @@ module Gitlab
{ avg_cycle_analytics: {} } { avg_cycle_analytics: {} }
end end
# rubocop:disable CodeReuse/ActiveRecord
def grafana_embed_usage_data
count(Issue.joins('JOIN grafana_integrations USING (project_id)')
.where("issues.description LIKE '%' || grafana_integrations.grafana_url || '%'")
.where(grafana_integrations: { enabled: true }))
end
# rubocop: enable CodeReuse/ActiveRecord
def features_usage_data def features_usage_data
features_usage_data_ce features_usage_data_ce
end end
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::GrafanaEmbedUsageData do
describe '#issue_count' do
subject { described_class.issue_count }
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
expect(subject).to eq(0)
end
end
context 'with project grafana integration enabled' do
before do
create(:grafana_integration, project: project, enabled: true)
end
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)
end
it 'counts only the issues with embeds' do
expect(subject).to eq(2)
end
end
end
context 'with project grafana integration disabled' do
before do
create(:grafana_integration, project: project, enabled: false)
end
context 'with one issue having a grafana link in the description and one without' do
before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end
it_behaves_like('zero count')
end
end
context 'with an un-integrated project' do
context 'with one issue having a grafana link in the description and one without' do
before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end
it_behaves_like('zero count')
end
end
end
end
...@@ -14,7 +14,7 @@ describe Gitlab::UsageData, :aggregate_failures do ...@@ -14,7 +14,7 @@ describe Gitlab::UsageData, :aggregate_failures do
let!(:ud) { build(:usage_data) } let!(:ud) { build(:usage_data) }
before do before do
allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count).and_return(2) allow(described_class).to receive(:grafana_embed_usage_data).and_return(2)
end end
subject { described_class.data } subject { described_class.data }
...@@ -220,6 +220,71 @@ describe Gitlab::UsageData, :aggregate_failures do ...@@ -220,6 +220,71 @@ describe Gitlab::UsageData, :aggregate_failures do
end end
end end
describe '#grafana_embed_usage_data' do
subject { described_class.grafana_embed_usage_data }
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
expect(subject).to eq(0)
end
end
context 'with project grafana integration enabled' do
before do
create(:grafana_integration, project: project, enabled: true)
end
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)
end
it 'counts only the issues with embeds' do
expect(subject).to eq(2)
end
end
end
context 'with project grafana integration disabled' do
before do
create(:grafana_integration, project: project, enabled: false)
end
context 'with one issue having a grafana link in the description and one without' do
before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end
it_behaves_like('zero count')
end
end
context 'with an un-integrated project' do
context 'with one issue having a grafana link in the description and one without' do
before do
create(:issue, project: project, description: description_with_embed)
create(:issue, project: project)
end
it_behaves_like('zero count')
end
end
end
describe '#count' do describe '#count' do
let(:relation) { double(:relation) } let(:relation) { double(:relation) }
......
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