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