Skip to content

Commit

Permalink
fix(graphCardSelectors): cache update for product id
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera committed Nov 19, 2019
1 parent 4ee6a8d commit 60a1a5c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/redux/actions/rhelActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getGraphReportsRhel = (id = null, query = {}) => dispatch =>
type: rhelTypes.GET_GRAPH_REPORT_RHEL,
payload: rhelServices.getGraphReportsRhel(id, query),
meta: {
productId: id,
data: { id },
query,
notifications: {
rejected: {
Expand All @@ -23,7 +23,7 @@ const getGraphCapacityRhel = (id = null, query = {}) => dispatch =>
type: rhelTypes.GET_GRAPH_CAPACITY_RHEL,
payload: rhelServices.getGraphCapacityRhel(id, query),
meta: {
productId: id,
data: { id },
query,
notifications: {
rejected: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GraphCardSelectors should error on a RHEL product ID without a product ID provided: rhelGraphCard: no product id error 1`] = `
Object {
"error": true,
"fulfilled": false,
"graphData": Object {
"hypervisor": Array [],
"sockets": Array [],
"threshold": Array [],
},
"initialLoad": true,
"pending": false,
}
`;

exports[`GraphCardSelectors should error on a RHEL product ID without granularity provided: rhelGraphCard: no granularity error 1`] = `
Object {
"error": true,
Expand Down
62 changes: 62 additions & 0 deletions src/redux/selectors/__tests__/graphCardSelectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ describe('GraphCardSelectors', () => {
component: {},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {},
data: { [rhelApiTypes.RHSM_API_RESPONSE_CAPACITY_DATA]: [] }
},
report: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {},
data: { [rhelApiTypes.RHSM_API_RESPONSE_PRODUCTS_DATA]: [] }
}
Expand All @@ -32,19 +38,51 @@ describe('GraphCardSelectors', () => {
expect(graphCardSelectors.rhelGraphCard(state)).toMatchSnapshot('rhelGraphCard: no granularity error');
});

it('should error on a RHEL product ID without a product ID provided', () => {
const state = {
rhelGraph: {
component: {},
capacity: {
fulfilled: true,
metaData: {},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
data: { [rhelApiTypes.RHSM_API_RESPONSE_CAPACITY_DATA]: [] }
},
report: {
fulfilled: true,
metaData: {},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
data: { [rhelApiTypes.RHSM_API_RESPONSE_PRODUCTS_DATA]: [] }
}
}
};

expect(graphCardSelectors.rhelGraphCard(state)).toMatchSnapshot('rhelGraphCard: no product id error');
});

it('should handle pending state on a RHEL product ID', () => {
const state = {
rhelGraph: {
component: {},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
data: { [rhelApiTypes.RHSM_API_RESPONSE_CAPACITY_DATA]: [] }
},
report: {
pending: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand All @@ -62,6 +100,9 @@ describe('GraphCardSelectors', () => {
component: {},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.MONTHLY
},
Expand All @@ -78,6 +119,9 @@ describe('GraphCardSelectors', () => {
},
report: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down Expand Up @@ -123,13 +167,19 @@ describe('GraphCardSelectors', () => {
},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
data: { [rhelApiTypes.RHSM_API_RESPONSE_CAPACITY_DATA]: [] }
},
report: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down Expand Up @@ -161,6 +211,9 @@ describe('GraphCardSelectors', () => {
},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down Expand Up @@ -189,6 +242,9 @@ describe('GraphCardSelectors', () => {
},
report: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down Expand Up @@ -229,6 +285,9 @@ describe('GraphCardSelectors', () => {
},
capacity: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down Expand Up @@ -257,6 +316,9 @@ describe('GraphCardSelectors', () => {
},
report: {
fulfilled: true,
metaData: {
id: 'Lorem Ipsum'
},
metaQuery: {
[rhelApiTypes.RHSM_API_QUERY_GRANULARITY]: rhelApiTypes.RHSM_API_QUERY_GRANULARITY_TYPES.DAILY
},
Expand Down
16 changes: 12 additions & 4 deletions src/redux/selectors/graphCardSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const rhelGraphCardSelector = createSelector(
const graphGranularity = component.graphGranularity || null;
const reportGranularity = _get(report, ['metaQuery', rhelApiTypes.RHSM_API_QUERY_GRANULARITY], null);
const capacityGranularity = _get(capacity, ['metaQuery', rhelApiTypes.RHSM_API_QUERY_GRANULARITY], null);
const reportProductId = _get(report, ['metaData', 'id'], null);
const capacityProductId = _get(capacity, ['metaData', 'id'], null);

let productId = null;
let granularity = null;

if (
Expand All @@ -24,7 +28,11 @@ const rhelGraphCardSelector = createSelector(
granularity = reportGranularity;
}

const cachedGranularity = (granularity && rhelGraphCardCache[granularity]) || {};
if (reportProductId === capacityProductId) {
productId = reportProductId;
}

const cachedGranularity = (granularity && productId && rhelGraphCardCache[`${productId}_${granularity}`]) || {};
const initialLoad = typeof cachedGranularity.initialLoad === 'boolean' ? cachedGranularity.initialLoad : true;

const updatedData = {
Expand All @@ -41,7 +49,7 @@ const rhelGraphCardSelector = createSelector(
...component
};

if (granularity === null) {
if (granularity === null || productId === null) {
updatedData.error = true;
return updatedData;
}
Expand All @@ -51,7 +59,7 @@ const rhelGraphCardSelector = createSelector(
updatedData.pending = report.pending || capacity.pending || false;
}

if (capacity.fulfilled && report.fulfilled && granularity) {
if (capacity.fulfilled && report.fulfilled && granularity && productId) {
const productsData = _get(report, ['data', rhelApiTypes.RHSM_API_RESPONSE_PRODUCTS_DATA], []);
const thresholdData = _get(capacity, ['data', rhelApiTypes.RHSM_API_RESPONSE_CAPACITY_DATA], []);

Expand Down Expand Up @@ -104,7 +112,7 @@ const rhelGraphCardSelector = createSelector(

updatedData.initialLoad = false;
updatedData.fulfilled = true;
rhelGraphCardCache[granularity] = { ...updatedData };
rhelGraphCardCache[`${productId}_${granularity}`] = { ...updatedData };
}

return updatedData;
Expand Down

0 comments on commit 60a1a5c

Please sign in to comment.