Skip to content

Commit

Permalink
Feat/dat: Updates for the Data availability tool (#47)
Browse files Browse the repository at this point in the history
Handle nested aggregation for number fields
Add nested aggregation to GuppyWrapper
  • Loading branch information
paulineribeyre authored Aug 2, 2019
1 parent c4a7654 commit fbf5dc4
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gen3/guppy",
"version": "0.1.2",
"version": "0.3.0",
"description": "Server that support GraphQL queries on data from elasticsearch",
"main": "src/server/server.js",
"directories": {
Expand Down
4 changes: 3 additions & 1 deletion src/components/ConnectedFilter/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class ConnectedFilter extends React.Component {
}

setFilter(filter) {
this.filterGroupRef.current.resetFilter();
if (this.filterGroupRef.current) {
this.filterGroupRef.current.resetFilter();
}
this.handleFilterChange(filter);
}

Expand Down
40 changes: 39 additions & 1 deletion src/components/GuppyWrapper/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
askGuppyForTotalCounts,
getAllFieldsFromGuppy,
getAccessibleResources,
askGuppyForNestedAggregationData,
} from '../Utils/queries';
import { ENUM_ACCESSIBILITY } from '../Utils/const';

Expand Down Expand Up @@ -98,6 +99,39 @@ class GuppyWrapper extends React.Component {
if (!fields || fields.length === 0) {
return Promise.resolve({ data: [], totalCount: 0 });
}

// nested aggregation
if (this.props.guppyConfig.mainField) {
const numericAggregation = this.props.guppyConfig.mainFieldIsNumeric;
// use missedNestedFields instead of termsNestedFields for performance
return askGuppyForNestedAggregationData(
this.props.guppyConfig.path,
this.props.guppyConfig.type,
this.props.guppyConfig.mainField,
numericAggregation,
[],
this.props.guppyConfig.aggFields,
this.filter,
this.state.accessibility,
).then((res) => {
if (!res || !res.data) {
throw new Error(`Error getting raw ${this.props.guppyConfig.type} data from Guppy server ${this.props.guppyConfig.path}.`);
}
const data = res.data._aggregation[this.props.guppyConfig.type];
const field = numericAggregation ? 'asTextHistogram' : 'histogram';
const parsedData = data[this.props.guppyConfig.mainField][field];
if (updateDataWhenReceive) {
this.setState({
rawData: parsedData,
});
}
return {
data: res.data,
};
});
}

// non-nested aggregation
return askGuppyForRawData(
this.props.guppyConfig.path,
this.props.guppyConfig.type,
Expand Down Expand Up @@ -274,6 +308,9 @@ GuppyWrapper.propTypes = {
guppyConfig: PropTypes.shape({
path: PropTypes.string,
type: PropTypes.string,
mainField: PropTypes.string,
mainFieldIsNumeric: PropTypes.bool,
aggFields: PropTypes.array,
}).isRequired,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
Expand All @@ -285,7 +322,7 @@ GuppyWrapper.propTypes = {
fields: PropTypes.arrayOf(PropTypes.string),
})),
}).isRequired,
rawDataFields: PropTypes.arrayOf(PropTypes.string).isRequired,
rawDataFields: PropTypes.arrayOf(PropTypes.string),
onReceiveNewAggsData: PropTypes.func,
onFilterChange: PropTypes.func,
accessibleFieldCheckList: PropTypes.arrayOf(PropTypes.string),
Expand All @@ -294,6 +331,7 @@ GuppyWrapper.propTypes = {
GuppyWrapper.defaultProps = {
onReceiveNewAggsData: () => {},
onFilterChange: () => {},
rawDataFields: [],
accessibleFieldCheckList: undefined,
};

Expand Down
21 changes: 15 additions & 6 deletions src/components/Utils/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const queryGuppyForAggs = (path, type, fields, gqlFilter, acc) => {
}).then(response => response.json());
};

const nestedHistogramQueryStrForEachField = mainField => (`
const nestedHistogramQueryStrForEachField = (mainField, numericAggAsText) => (`
${mainField} {
histogram {
${numericAggAsText ? 'asTextHistogram' : 'histogram'} {
key
count
missingFields {
Expand All @@ -68,6 +68,7 @@ const queryGuppyForNestedAgg = (
path,
type,
mainField,
numericAggAsText = false,
termsFields,
missingFields,
gqlFilter,
Expand All @@ -89,7 +90,7 @@ const queryGuppyForNestedAgg = (
const query = `query ($nestedAggFields: JSON) {
_aggregation {
${type} (nestedAggFields: $nestedAggFields, accessibility: ${accessibility}) {
${nestedHistogramQueryStrForEachField(mainField)}
${nestedHistogramQueryStrForEachField(mainField, numericAggAsText)}
}
}
}`;
Expand All @@ -99,7 +100,7 @@ const queryGuppyForNestedAgg = (
const queryWithFilter = `query ($filter: JSON, $nestedAggFields: JSON) {
_aggregation {
${type} (filter: $filter, filterSelf: false, nestedAggFields: $nestedAggFields, accessibility: ${accessibility}) {
${nestedHistogramQueryStrForEachField(mainField)}
${nestedHistogramQueryStrForEachField(mainField, numericAggAsText)}
}
}
}`;
Expand All @@ -112,7 +113,10 @@ const queryGuppyForNestedAgg = (
'Content-Type': 'application/json',
},
body: JSON.stringify(queryBody),
}).then(response => response.json());
}).then(response => response.json())
.catch((err) => {
throw new Error(`Error during queryGuppyForNestedAgg ${err}`);
});
};

const queryGuppyForRawDataAndTotalCounts = (
Expand Down Expand Up @@ -157,7 +161,10 @@ const queryGuppyForRawDataAndTotalCounts = (
'Content-Type': 'application/json',
},
body: JSON.stringify(queryBody),
}).then(response => response.json());
}).then(response => response.json())
.catch((err) => {
throw new Error(`Error during queryGuppyForRawDataAndTotalCounts ${err}`);
});
};

export const askGuppyAboutAllFieldsAndOptions = (
Expand Down Expand Up @@ -200,6 +207,7 @@ export const askGuppyForNestedAggregationData = (
path,
type,
mainField,
numericAggAsText,
termsNestedFields,
missedNestedFields,
filter,
Expand All @@ -210,6 +218,7 @@ export const askGuppyForNestedAggregationData = (
path,
type,
mainField,
numericAggAsText,
termsNestedFields,
missedNestedFields,
gqlFilter,
Expand Down
3 changes: 0 additions & 3 deletions src/server/__mocks__/mockESData/mockNestedAggs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const mockNestedAggs = () => {
project: {
terms: {
field: 'project',
missing: 'no data',
},
},
},
Expand Down Expand Up @@ -82,7 +81,6 @@ const mockNestedAggs = () => {
project: {
terms: {
field: 'project',
missing: 'no data',
},
},
},
Expand Down Expand Up @@ -181,7 +179,6 @@ const mockNestedAggs = () => {
project: {
terms: {
field: 'project',
missing: 'no data',
},
},
},
Expand Down
Loading

0 comments on commit fbf5dc4

Please sign in to comment.