Commit 18e065a3 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Add metrics dashboard builder controller

To house new dashboard panel builder feautre we need to create new
controller and route.
parent cb4db18b
...@@ -7,6 +7,7 @@ export default () => ({ ...@@ -7,6 +7,7 @@ export default () => ({
deploymentsEndpoint: null, deploymentsEndpoint: null,
dashboardEndpoint: invalidUrl, dashboardEndpoint: invalidUrl,
dashboardsEndpoint: invalidUrl, dashboardsEndpoint: invalidUrl,
panelPreviewEndpoint: invalidUrl,
// Dashboard request parameters // Dashboard request parameters
timeRange: null, timeRange: null,
......
...@@ -24,6 +24,7 @@ export const stateAndPropsFromDataset = (dataset = {}) => { ...@@ -24,6 +24,7 @@ export const stateAndPropsFromDataset = (dataset = {}) => {
deploymentsEndpoint, deploymentsEndpoint,
dashboardEndpoint, dashboardEndpoint,
dashboardsEndpoint, dashboardsEndpoint,
panelPreviewEndpoint,
dashboardTimezone, dashboardTimezone,
canAccessOperationsSettings, canAccessOperationsSettings,
operationsSettingsPath, operationsSettingsPath,
...@@ -45,6 +46,7 @@ export const stateAndPropsFromDataset = (dataset = {}) => { ...@@ -45,6 +46,7 @@ export const stateAndPropsFromDataset = (dataset = {}) => {
deploymentsEndpoint, deploymentsEndpoint,
dashboardEndpoint, dashboardEndpoint,
dashboardsEndpoint, dashboardsEndpoint,
panelPreviewEndpoint,
dashboardTimezone, dashboardTimezone,
canAccessOperationsSettings, canAccessOperationsSettings,
operationsSettingsPath, operationsSettingsPath,
......
# frozen_string_literal: true
module Projects
module Metrics
module Dashboards
class BuilderController < Projects::ApplicationController
before_action :ensure_feature_flags
before_action :authorize_metrics_dashboard!
def panel_preview
respond_to do |format|
format.json { render json: render_panel }
end
end
private
def ensure_feature_flags
render_404 unless Feature.enabled?(:metrics_dashboard_new_panel_page, project)
end
def render_panel
{
"title": "Memory Usage (Total)",
"type": "area-chart",
"y_label": "Total Memory Used (GB)",
"weight": 4,
"metrics": [
{
"id": "system_metrics_kubernetes_container_memory_total",
"query_range": "avg(sum(container_memory_usage_bytes{container_name!=\"POD\",pod_name=~\"^{{ci_environment_slug}}-(.*)\",namespace=\"{{kube_namespace}}\"}) by (job)) without (job) /1024/1024/1024",
"label": "Total (GB)",
"unit": "GB",
"metric_id": 15,
"edit_path": nil,
"prometheus_endpoint_path": "/root/autodevops-deploy/-/environments/29/prometheus/api/v1/query_range?query=avg%28sum%28container_memory_usage_bytes%7Bcontainer_name%21%3D%22POD%22%2Cpod_name%3D~%22%5E%7B%7Bci_environment_slug%7D%7D-%28.%2A%29%22%2Cnamespace%3D%22%7B%7Bkube_namespace%7D%7D%22%7D%29+by+%28job%29%29+without+%28job%29++%2F1024%2F1024%2F1024"
}
],
"id": "4570deed516d0bf93fb42879004117009ab456ced27393ec8dce5b6960438132"
}
end
end
end
end
end
...@@ -98,7 +98,8 @@ module EnvironmentsHelper ...@@ -98,7 +98,8 @@ module EnvironmentsHelper
'deployments-endpoint' => project_environment_deployments_path(project, environment, format: :json), 'deployments-endpoint' => project_environment_deployments_path(project, environment, format: :json),
'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json), 'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
'operations-settings-path' => project_settings_operations_path(project), 'operations-settings-path' => project_settings_operations_path(project),
'can-access-operations-settings' => can?(current_user, :admin_operations, project).to_s 'can-access-operations-settings' => can?(current_user, :admin_operations, project).to_s,
'panel-preview-endpoint' => project_metrics_dashboards_builder_path(project, format: :json)
} }
end end
......
...@@ -28,6 +28,12 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -28,6 +28,12 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
get 'metrics(/:dashboard_path)(/:page)', constraints: { dashboard_path: /.+\.yml/, page: 'panel/new' }, get 'metrics(/:dashboard_path)(/:page)', constraints: { dashboard_path: /.+\.yml/, page: 'panel/new' },
to: 'metrics_dashboard#show', as: :metrics_dashboard, format: false to: 'metrics_dashboard#show', as: :metrics_dashboard, format: false
namespace :metrics, module: :metrics do
namespace :dashboards do
post :builder, to: 'builder#panel_preview'
end
end
resources :artifacts, only: [:index, :destroy] resources :artifacts, only: [:index, :destroy]
resources :packages, only: [:index, :show, :destroy], module: :packages resources :packages, only: [:index, :show, :destroy], module: :packages
......
...@@ -44,7 +44,8 @@ RSpec.describe EnvironmentsHelper do ...@@ -44,7 +44,8 @@ RSpec.describe EnvironmentsHelper do
'prometheus-alerts-available' => 'true', 'prometheus-alerts-available' => 'true',
'custom-dashboard-base-path' => Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT, 'custom-dashboard-base-path' => Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT,
'operations-settings-path' => project_settings_operations_path(project), 'operations-settings-path' => project_settings_operations_path(project),
'can-access-operations-settings' => 'true' 'can-access-operations-settings' => 'true',
'panel-preview-endpoint' => project_metrics_dashboards_builder_path(project, format: :json)
) )
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Projects::Metrics::Dashboards::BuilderController' do
let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:user) { create(:user) }
def send_request(params = {})
post namespace_project_metrics_dashboards_builder_path(namespace_id: project.namespace, project_id: project, format: :json, **params)
end
describe 'POST /:namespace/:project/-/metrics/dashboards/builder' do
context 'as anonymous user' do
before do
stub_feature_flags(metrics_dashboard_new_panel_page: true)
end
it 'redirects to sign in' do
send_request
expect(response).to redirect_to(new_user_session_path)
end
end
context 'as user with reporter access' do
before do
stub_feature_flags(metrics_dashboard_new_panel_page: true)
project.add_guest(user)
login_as(user)
end
it 'returns not found' do
send_request
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'as logged in user' do
before do
project.add_developer(user)
login_as(user)
end
context 'metrics_dashboard_new_panel_page is enabled' do
before do
stub_feature_flags(metrics_dashboard_new_panel_page: true)
end
it 'returns success' do
send_request
expect(response).to have_gitlab_http_status(:ok)
end
end
context 'metrics_dashboard_new_panel_page is disabled' do
before do
stub_feature_flags(metrics_dashboard_new_panel_page: false)
end
it 'returns not found' do
send_request
expect(response).to have_gitlab_http_status(:not_found)
end
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