debian-mirror-gitlab/app/controllers/concerns/cycle_analytics_params.rb

80 lines
2.4 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module CycleAnalyticsParams
extend ActiveSupport::Concern
2019-12-21 20:55:43 +05:30
def cycle_analytics_project_params
return {} unless params[:cycle_analytics].present?
params[:cycle_analytics].permit(:start_date, :created_after, :created_before, :branch_name)
end
def cycle_analytics_group_params
2020-03-13 15:44:24 +05:30
return {} unless params.present?
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
params.permit(:group_id, :start_date, :created_after, :created_before, project_ids: [])
2019-12-21 20:55:43 +05:30
end
2017-08-17 22:00:37 +05:30
def options(params)
2021-10-27 15:23:28 +05:30
@options ||= {}.tap do |opts|
opts[:current_user] = current_user
opts[:projects] = params[:project_ids] if params[:project_ids]
opts[:group] = params[:group_id] if params[:group_id]
opts[:from] = params[:from] || start_date(params)
opts[:to] = params[:to] if params[:to]
opts[:end_event_filter] = params[:end_event_filter] if params[:end_event_filter]
2022-01-26 12:08:38 +05:30
opts[:use_aggregated_data_collector] = params[:use_aggregated_data_collector] if params[:use_aggregated_data_collector]
2021-10-27 15:23:28 +05:30
opts.merge!(params.slice(*::Gitlab::Analytics::CycleAnalytics::RequestParams::FINDER_PARAM_NAMES))
opts.merge!(date_range(params))
end
2017-08-17 22:00:37 +05:30
end
2021-10-27 15:23:28 +05:30
private
2017-08-17 22:00:37 +05:30
def start_date(params)
2018-03-17 18:26:18 +05:30
case params[:start_date]
when '7'
7.days.ago
when '30'
30.days.ago
else
90.days.ago
end
2017-08-17 22:00:37 +05:30
end
2019-12-21 20:55:43 +05:30
def date_range(params)
{}.tap do |date_range_params|
date_range_params[:from] = to_utc_time(params[:created_after]).beginning_of_day if params[:created_after]
date_range_params[:to] = to_utc_time(params[:created_before]).end_of_day if params[:created_before]
end.compact
end
def to_utc_time(field)
2020-04-08 14:13:33 +05:30
date = field.is_a?(Date) || field.is_a?(Time) ? field : Date.parse(field)
2020-01-01 13:55:28 +05:30
date.to_time.utc
2019-12-21 20:55:43 +05:30
end
2021-10-27 15:23:28 +05:30
def permitted_cycle_analytics_params
params.permit(*::Gitlab::Analytics::CycleAnalytics::RequestParams::STRONG_PARAMS_DEFINITION)
end
def all_cycle_analytics_params
permitted_cycle_analytics_params.merge(current_user: current_user)
end
def request_params
@request_params ||= ::Gitlab::Analytics::CycleAnalytics::RequestParams.new(all_cycle_analytics_params)
end
def validate_params
if request_params.invalid?
render(
json: { message: 'Invalid parameters', errors: request_params.errors },
status: :unprocessable_entity
)
end
end
2017-08-17 22:00:37 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
CycleAnalyticsParams.prepend_mod_with('CycleAnalyticsParams')