Skip to content
Snippets Groups Projects

Introduce the Insights controllers

21 files
+ 428
14
Compare changes
  • Side-by-side
  • Inline
Files
21
# frozen_string_literal: true
module InsightsActions
extend ActiveSupport::Concern
def show
respond_to do |format|
format.html do
@insights_config = config_data
if @insights_config
first_chart_hash = @insights_config.first.last
@insights_json = insights_json(
chart_type: first_chart_hash[:type],
query: first_chart_hash[:query])
end
end
format.json do
render json: config_data
end
end
end
def query
respond_to do |format|
format.json do
render json: insights_json
end
end
end
private
def insights_json(using_params = params)
case using_params[:query][:name]
when 'by_label'
insights = reduce(issuables(using_params[:query]), using_params)
serializer(using_params[:chart_type]).present(insights)
else
raise 'Chart query not supported yet!'
end
end
def issuables(query_params)
@issuables ||= Gitlab::Insights::Finders::IssuableFinder
.new(insights_entity, current_user, query_params).find
end
def reduce(issuables, using_params)
case using_params[:chart_type]
when 'stacked_bar', 'line'
Gitlab::Insights::Reducers::LabelCountPerPeriodReducer.reduce(issuables, period: using_params[:query][:group_by], labels: using_params[:query][:collection_labels])
when 'bar'
Gitlab::Insights::Reducers::CountPerPeriodReducer.reduce(issuables, period: using_params[:query][:group_by])
else
raise 'Chart type not supported yet!'
end
end
def serializer(chart_type)
case chart_type
when 'stacked_bar'
Gitlab::Insights::Serializers::Chartjs::MultiSeriesSerializer
when 'bar'
Gitlab::Insights::Serializers::Chartjs::BarSerializer
when 'line'
Gitlab::Insights::Serializers::Chartjs::LineSerializer
else
raise 'Chart type not supported yet!'
end
end
def config_data
insights_entity.insights_config
end
end
Loading