Commit b63dee17 authored by Andreas Brandl's avatar Andreas Brandl

Merge branch '31385-usage-ping-for-embedded-grafana-charts-2' into 'master'

Usage Ping for embedded Grafana charts Part 2

See merge request gitlab-org/gitlab!19648
parents eea5a539 92de19ba
class AddIndexToGrafanaIntegrations < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :grafana_integrations, :enabled, where: 'enabled IS TRUE'
end
def down
remove_concurrent_index :grafana_integrations, :enabled
end
end
......@@ -1888,6 +1888,7 @@ ActiveRecord::Schema.define(version: 2019_11_25_140458) do
t.string "encrypted_token_iv", limit: 255, null: false
t.string "grafana_url", limit: 1024, null: false
t.boolean "enabled", default: false, null: false
t.index ["enabled"], name: "index_grafana_integrations_on_enabled", where: "(enabled IS TRUE)"
t.index ["project_id"], name: "index_grafana_integrations_on_project_id"
end
......
# 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
......@@ -84,6 +84,7 @@ module Gitlab
issues: count(Issue),
issues_with_associated_zoom_link: count(ZoomMeeting.added_to_issue),
issues_using_zoom_quick_actions: count(ZoomMeeting.select(:issue_id).distinct),
issues_with_embedded_grafana_charts_approx: ::Gitlab::GrafanaEmbedUsageData.issue_count,
keys: count(Key),
label_lists: count(List.label),
lfs_objects: count(LfsObject),
......
# 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
......@@ -53,6 +53,8 @@ describe Gitlab::UsageData do
create(:grafana_integration, project: projects[1], enabled: true)
create(:grafana_integration, project: projects[2], enabled: false)
allow(Gitlab::GrafanaEmbedUsageData).to receive(:issue_count).and_return(2)
ProjectFeature.first.update_attribute('repository_access_level', 0)
end
......@@ -152,6 +154,7 @@ describe Gitlab::UsageData do
issues
issues_with_associated_zoom_link
issues_using_zoom_quick_actions
issues_with_embedded_grafana_charts_approx
keys
label_lists
labels
......@@ -211,6 +214,7 @@ describe Gitlab::UsageData do
expect(count_data[:projects_with_error_tracking_enabled]).to eq(1)
expect(count_data[:issues_with_associated_zoom_link]).to eq(2)
expect(count_data[:issues_using_zoom_quick_actions]).to eq(3)
expect(count_data[:issues_with_embedded_grafana_charts_approx]).to eq(2)
expect(count_data[:clusters_enabled]).to eq(4)
expect(count_data[:project_clusters_enabled]).to eq(3)
......
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