debian-mirror-gitlab/lib/api/metrics/dashboard/annotations.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.8 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module API
module Metrics
module Dashboard
2021-01-03 14:25:43 +05:30
class Annotations < ::API::Base
2021-01-29 00:20:46 +05:30
feature_category :metrics
2022-07-23 23:45:48 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2023-01-13 00:05:48 +05:30
desc 'Create a new annotation' do
detail 'Creates a new monitoring dashboard annotation'
2020-04-22 19:07:51 +05:30
success Entities::Metrics::Dashboard::Annotation
2023-01-13 00:05:48 +05:30
failure [
{ code: 400, message: 'Bad Request' },
{ code: 401, message: 'Unauthorized' },
{ code: 404, message: 'Not Found' }
]
tags %w[dashboard_annotations]
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
ANNOTATIONS_SOURCES = [
{ class: ::Environment, resource: :environments, create_service_param_key: :environment },
2022-06-21 17:19:12 +05:30
{ class: ::Clusters::Cluster, resource: :clusters, create_service_param_key: :cluster }
2020-05-24 23:13:21 +05:30
].freeze
ANNOTATIONS_SOURCES.each do |annotations_source|
resource annotations_source[:resource] do
params do
requires :starting_at, type: DateTime,
2023-01-13 00:05:48 +05:30
desc: 'Date time string, ISO 8601 formatted, such as 2016-03-11T03:45:40Z.'\
'Timestamp marking start point of annotation.'
2020-05-24 23:13:21 +05:30
optional :ending_at, type: DateTime,
2023-01-13 00:05:48 +05:30
desc: 'Date time string, ISO 8601 formatted, such as 2016-03-11T03:45:40Z.'\
'Timestamp marking end point of annotation.'\
'When not supplied, an annotation displays as a single event at the start point.'
2020-05-24 23:13:21 +05:30
requires :dashboard_path, type: String, coerce_with: -> (val) { CGI.unescape(val) },
2023-01-13 00:05:48 +05:30
desc: 'ID of the dashboard which needs to be annotated.'\
'Treated as a CGI-escaped path, and automatically un-escaped.'
requires :description, type: String, desc: 'Description of the annotation.'
2020-05-24 23:13:21 +05:30
end
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
post ':id/metrics_dashboard/annotations' do
2023-07-09 08:55:56 +05:30
not_found! if Feature.enabled?(:remove_monitor_metrics)
2020-05-24 23:13:21 +05:30
annotations_source_object = annotations_source[:class].find(params[:id])
2020-04-22 19:07:51 +05:30
2023-05-27 22:25:52 +05:30
forbidden! unless can?(current_user, :admin_metrics_dashboard_annotation, annotations_source_object)
2020-04-22 19:07:51 +05:30
2023-01-13 00:05:48 +05:30
create_service_params = declared(params).merge(
annotations_source[:create_service_param_key] => annotations_source_object
)
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
result = ::Metrics::Dashboard::Annotations::CreateService.new(current_user, create_service_params).execute
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
if result[:status] == :success
present result[:annotation], with: Entities::Metrics::Dashboard::Annotation
else
error!(result, 400)
end
2020-04-22 19:07:51 +05:30
end
end
end
end
end
end
end