Commit a9d904ab authored by Lin Jen-Shin's avatar Lin Jen-Shin

Consolidate query into the same object

This way we don't have to update frontend every time
we want to add more options to Insights.
parent e4ff86e0
......@@ -30,11 +30,7 @@ export const receiveChartDataError = ({ commit }, { chart, error }) =>
export const fetchChartData = ({ dispatch }, { endpoint, chart }) =>
axios
.post(endpoint, {
query: chart.query,
chart_type: chart.type,
projects: chart.projects,
})
.post(endpoint, chart)
.then(({ data }) => dispatch('receiveChartDataSuccess', { chart, data }))
.catch(error => {
let message = `${__('There was an error gathering the chart data')}`;
......
......@@ -39,8 +39,8 @@ module InsightsActions
Gitlab::Insights::Validators::ParamsValidator.new(params).validate!
end
def chart_type_param
@chart_type_param ||= params[:chart_type]
def type_param
@type_param ||= params[:type]
end
def query_param
......@@ -66,7 +66,7 @@ module InsightsActions
end
def reduce(issuables:, period_limit: nil)
case chart_type_param
case type_param
when 'stacked-bar', 'line'
Gitlab::Insights::Reducers::LabelCountPerPeriodReducer.reduce(issuables, period: period_param, period_limit: period_limit, labels: collection_labels_param)
when 'bar', 'pie'
......@@ -86,7 +86,7 @@ module InsightsActions
end
def serializer
case chart_type_param
case type_param
when 'stacked-bar'
Gitlab::Insights::Serializers::Chartjs::MultiSeriesSerializer
when 'bar', 'pie'
......
......@@ -5,17 +5,17 @@ module Gitlab
module Validators
class ParamsValidator
ParamsValidatorError = Class.new(StandardError)
InvalidChartTypeError = Class.new(ParamsValidatorError)
InvalidTypeError = Class.new(ParamsValidatorError)
SUPPORTER_CHART_TYPES = %w[bar line stacked-bar pie].freeze
SUPPORTER_TYPES = %w[bar line stacked-bar pie].freeze
def initialize(params)
@params = params
end
def validate!
unless SUPPORTER_CHART_TYPES.include?(params[:chart_type])
raise InvalidChartTypeError, "Invalid `:chart_type`: `#{params[:chart_type]}`. Allowed values are #{SUPPORTER_CHART_TYPES}!"
unless SUPPORTER_TYPES.include?(params[:type])
raise InvalidTypeError, "Invalid `:type`: `#{params[:type]}`. Allowed values are #{SUPPORTER_TYPES}!"
end
end
......
......@@ -8,7 +8,7 @@ describe Groups::InsightsController do
set(:project) { create(:project, :private) }
set(:insight) { create(:insight, group: parent_group, project: project) }
set(:user) { create(:user) }
let(:query_params) { { chart_type: 'bar', query: { issuable_type: 'issue', collection_labels: ['bug'] } } }
let(:query_params) { { type: 'bar', query: { issuable_type: 'issue', collection_labels: ['bug'] } } }
before do
stub_licensed_features(insights: true)
......
......@@ -174,12 +174,7 @@ describe('Insights store actions', () => {
describe('successful request', () => {
beforeEach(() => {
mock
.onPost(`${gl.TEST_HOST}/query`, {
query: chart.query,
chart_type: chart.type,
})
.reply(200, chartData);
mock.onPost(`${gl.TEST_HOST}/query`, chart).reply(200, chartData);
});
it('calls receiveChartDataSuccess with chart data', done => {
......@@ -202,12 +197,7 @@ describe('Insights store actions', () => {
describe('failed request', () => {
beforeEach(() => {
mock
.onPost(`${gl.TEST_HOST}/query`, {
query: chart.query,
chart_type: chart.type,
})
.reply(500);
mock.onPost(`${gl.TEST_HOST}/query`, chart).reply(500);
});
it('calls receiveChartDataError with error message', done => {
......
......@@ -5,11 +5,11 @@ require 'spec_helper'
RSpec.describe Gitlab::Insights::Validators::ParamsValidator do
subject { described_class.new(params).validate! }
describe ':chart_type' do
described_class::SUPPORTER_CHART_TYPES.each do |chart_type|
context "with chart_type: '#{chart_type}'" do
describe ':type' do
described_class::SUPPORTER_TYPES.each do |type|
context "with type: '#{type}'" do
let(:params) do
{ chart_type: chart_type }
{ type: type }
end
it 'does not raise an error' do
......@@ -18,13 +18,13 @@ RSpec.describe Gitlab::Insights::Validators::ParamsValidator do
end
end
context 'with an invalid :chart_type' do
context 'with an invalid :type' do
let(:params) do
{ chart_type: 'unknown' }
{ type: 'unknown' }
end
it 'raises an error' do
expect { subject }.to raise_error(described_class::InvalidChartTypeError, "Invalid `:chart_type`: `unknown`. Allowed values are #{described_class::SUPPORTER_CHART_TYPES}!")
expect { subject }.to raise_error(described_class::InvalidTypeError, "Invalid `:type`: `unknown`. Allowed values are #{described_class::SUPPORTER_TYPES}!")
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