-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PXD-1514 fix(explorer): use sqon to calculate actual number of selected items #346
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,29 +47,43 @@ const getCategoryColorFrom2Colors = index => colorsForCharts.categorical2Colors[ | |
|
||
const getDataKey = showPercentage => (showPercentage ? 'percentage' : 'value'); | ||
|
||
const transformArrangerDataToChart = (field) => { | ||
const transformArrangerDataToChart = (field, sqonValues) => { | ||
const chartData = []; | ||
field.buckets.map(bucket => | ||
chartData.push({ | ||
name: bucket.key, | ||
value: bucket.doc_count, | ||
}), | ||
); | ||
field.buckets | ||
.filter(bucket => (sqonValues === null || sqonValues.includes(bucket.key))) | ||
.forEach(bucket => | ||
chartData.push({ | ||
name: bucket.key, | ||
value: bucket.doc_count, | ||
}), | ||
); | ||
return chartData; | ||
}; | ||
|
||
const transformArrangerDataToSummary = (field, chartType, title) => ({ | ||
const transformArrangerDataToSummary = (field, chartType, title, sqonValues) => ({ | ||
type: chartType, | ||
title, | ||
data: transformArrangerDataToChart(field), | ||
data: transformArrangerDataToChart(field, sqonValues), | ||
}); | ||
|
||
const transformDataToCount = (field, label) => ({ | ||
const transformDataToCount = (field, label, sqonValues) => ({ | ||
label, | ||
value: field.buckets.length, | ||
value: sqonValues ? Math.min(field.buckets.length, sqonValues.length) : field.buckets.length, | ||
}); | ||
|
||
const getCharts = (data, arrangerConfig) => { | ||
/** | ||
* Return an array of selected values in a given field | ||
* If no value selected, return null | ||
*/ | ||
const getSQONValues = (sqon, field) => { | ||
if (!sqon || !sqon.content) return null; | ||
const sqonItems = sqon.content.filter(item => item.content.field === field); | ||
if (!sqonItems || sqonItems.length !== 1) return null; | ||
const sqonValues = sqonItems[0].content.value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats does grabbing the first index give us? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems arranger use |
||
return sqonValues; | ||
}; | ||
|
||
const getCharts = (data, arrangerConfig, sqon) => { | ||
const countItems = []; | ||
const summaries = []; | ||
const stackedBarCharts = []; | ||
|
@@ -78,26 +92,29 @@ const getCharts = (data, arrangerConfig) => { | |
const fields = data.subject.aggregations; | ||
Object.keys(fields).forEach((field) => { | ||
const fieldConfig = arrangerConfig.charts[field]; | ||
const sqonValues = getSQONValues(sqon, field); | ||
if (fieldConfig) { | ||
switch (fieldConfig.chartType) { | ||
case 'count': | ||
countItems.push(transformDataToCount(fields[field], fieldConfig.title)); | ||
countItems.push(transformDataToCount(fields[field], fieldConfig.title, sqonValues)); | ||
break; | ||
case 'pie': | ||
case 'bar': | ||
summaries.push( | ||
transformArrangerDataToSummary( | ||
fields[field], | ||
fieldConfig.chartType, | ||
fieldConfig.title), | ||
fieldConfig.title, | ||
sqonValues), | ||
); | ||
break; | ||
case 'stackedBar': | ||
stackedBarCharts.push( | ||
transformArrangerDataToSummary( | ||
fields[field], | ||
fieldConfig.chartType, | ||
fieldConfig.title), | ||
fieldConfig.title, | ||
sqonValues), | ||
); | ||
break; | ||
default: | ||
|
@@ -121,4 +138,5 @@ module.exports = { | |
transformArrangerDataToChart, | ||
transformArrangerDataToSummary, | ||
getCharts, | ||
getSQONValues, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,23 @@ describe('helper', () => { | |
expect(helper.getDataKey(false)).toBe('value'); | ||
}); | ||
|
||
const noSelectSqonValues = null; | ||
const selectWhiteSqon = { | ||
op: 'and', | ||
content: [ | ||
{ | ||
op: 'in', | ||
content: { | ||
field: 'ethnicity', | ||
value: [ | ||
'White', | ||
], | ||
}, | ||
}, | ||
], | ||
}; | ||
const selectWhiteSqonValues = ['White']; | ||
|
||
const ethnicityFieldJSON = { | ||
buckets: [ | ||
{ doc_count: 4, key: 'White' }, | ||
|
@@ -110,15 +127,17 @@ describe('helper', () => { | |
}; | ||
|
||
it('returns chart data as expected', () => { | ||
expect(helper.transformArrangerDataToChart(ethnicityFieldJSON)).toEqual(ethnicityChartData); | ||
expect(helper.transformArrangerDataToChart(ethnicityFieldJSON, noSelectSqonValues)) | ||
.toEqual(ethnicityChartData); | ||
}); | ||
|
||
it('returns count data as expected', () => { | ||
expect(helper.transformDataToCount(ethnicityFieldJSON, 'Ethnicity')).toEqual(ethnicityCountData); | ||
expect(helper.transformDataToCount(ethnicityFieldJSON, 'Ethnicity', noSelectSqonValues)) | ||
.toEqual(ethnicityCountData); | ||
}); | ||
|
||
it('returns chart summaries as expected', () => { | ||
expect(helper.transformArrangerDataToSummary(ethnicityFieldJSON, 'pie', 'Ethnicity')).toEqual(summaryData); | ||
expect(helper.transformArrangerDataToSummary(ethnicityFieldJSON, 'pie', 'Ethnicity', noSelectSqonValues)).toEqual(summaryData); | ||
}); | ||
|
||
it('gets charts', () => { | ||
|
@@ -127,4 +146,8 @@ describe('helper', () => { | |
expect(charts.summaries).toEqual([summaryData]); | ||
expect(charts.stackedBarCharts).toEqual([]); | ||
}); | ||
|
||
it('return selecetd values from SQON as expected', () => { | ||
expect(helper.getSQONValues(selectWhiteSqon, 'ethnicity')).toEqual(selectWhiteSqonValues); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need some more tests. what if there is a filter with no values selected? does it return the length of the buckets or the sqon count? things like that. |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could also return an empty array right? that might be easier so you wouldn't have to check for null values, but I'll leave that up to you, just a suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I think returning an empty array will just be similar to returning
null
so I still want to keep it 😛sqonValues === null || sqonValues.includes(bucket.key)....
=>sqonValues.length === 0 ? || sqonValues.includes(bucket.key)...