Commit 20531aa0 authored by Rajendra Kadam's avatar Rajendra Kadam Committed by Alper Akgun

Add API get handler for usage data queries

Add specs for the API calls
parent 4ee871cb
---
name: usage_data_queries_api
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/57016
rollout_issue_url:
milestone: '13.11'
type: ops
group: group::product intelligence
default_enabled: false
......@@ -293,6 +293,7 @@ module API
mount ::API::Triggers
mount ::API::Unleash
mount ::API::UsageData
mount ::API::UsageDataQueries
mount ::API::UserCounts
mount ::API::Users
mount ::API::Variables
......
# frozen_string_literal: true
module API
class UsageDataQueries < ::API::Base
before { authenticated_as_admin! }
feature_category :usage_ping
namespace 'usage_data' do
before do
not_found! unless Feature.enabled?(:usage_data_queries_api, default_enabled: false, type: :ops)
end
desc 'Get raw SQL queries for usage data SQL metrics' do
detail 'This feature was introduced in GitLab 13.11.'
end
get 'queries' do
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/325534')
queries = Gitlab::UsageDataQueries.uncached_data
present queries
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe API::UsageDataQueries do
include UsageDataHelpers
let_it_be(:admin) { create(:user, admin: true) }
let_it_be(:user) { create(:user) }
before do
stub_usage_data_connections
end
describe 'GET /usage_data/usage_data_queries' do
let(:endpoint) { '/usage_data/queries' }
context 'with authentication' do
before do
stub_feature_flags(usage_data_queries_api: true)
end
it 'returns queries if user is admin' do
get api(endpoint, admin)
expect(response).to have_gitlab_http_status(:ok)
expect(json_response['active_user_count']).to start_with('SELECT COUNT("users"."id") FROM "users"')
end
it 'returns forbidden if user is not admin' do
get api(endpoint, user)
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'without authentication' do
before do
stub_feature_flags(usage_data_queries_api: true)
end
it 'returns unauthorized' do
get api(endpoint)
expect(response).to have_gitlab_http_status(:unauthorized)
end
end
context 'when feature_flag is disabled' do
before do
stub_feature_flags(usage_data_queries_api: false)
end
it 'returns not_found for admin' do
get api(endpoint, admin)
expect(response).to have_gitlab_http_status(:not_found)
end
it 'returns forbidden for non-admin' do
get api(endpoint, user)
expect(response).to have_gitlab_http_status(:forbidden)
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