Skip to content
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

Host Checks selection saga #1618

Merged
merged 15 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/js/components/ChecksSelection/ChecksSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const defaultSelectedChecks = [];
function ChecksSelection({
className,
targetID,
targetName,
catalog,
selected = defaultSelectedChecks,
loading = false,
Expand Down Expand Up @@ -119,7 +120,7 @@ function ChecksSelection({
<button
className="flex justify-center items-center bg-jungle-green-500 hover:opacity-75 text-white font-bold py-2 px-4 rounded"
disabled={saving}
onClick={() => onSave(selectedChecks, targetID)}
onClick={() => onSave(selectedChecks, targetID, targetName)}
type="button"
data-testid="save-selection-button"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('ChecksSelection component', () => {
const onClear = jest.fn();
const user = userEvent.setup();
const targetID = faker.datatype.uuid();
const targetName = faker.lorem.word();

const group = faker.animal.cat();
const catalog = catalogCheckFactory.buildList(2, { group });
Expand All @@ -132,6 +133,7 @@ describe('ChecksSelection component', () => {
<ChecksSelection
catalog={catalog}
targetID={targetID}
targetName={targetName}
onSave={onSave}
onUpdateCatalog={onUpdateCatalog}
onClear={onClear}
Expand All @@ -143,7 +145,7 @@ describe('ChecksSelection component', () => {
await user.click(switches[0]);
await user.click(screen.getByText('Save Check Selection'));

expect(onSave).toBeCalledWith([checkID1, checkID2], targetID);
expect(onSave).toBeCalledWith([checkID1, checkID2], targetID, targetName);
expect(onUpdateCatalog).toBeCalled();
expect(onClear).toBeCalled();
});
Expand Down
14 changes: 10 additions & 4 deletions assets/js/components/ClusterDetails/ChecksSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EOS_LOADING_ANIMATED } from 'eos-icons-react';
import { remove, uniq, toggle, groupBy } from '@lib/lists';
import { getCatalog } from '@state/selectors/catalog';
import { updateCatalog } from '@state/actions/catalog';
import { checksSelected } from '@state/actions/cluster';
import { checksSelected } from '@state/clusters';
import { executionRequested } from '@state/actions/lastExecutions';

import CatalogContainer from '@components/ChecksCatalog/CatalogContainer';
Expand Down Expand Up @@ -101,6 +101,14 @@ function ChecksSelection({ clusterId, cluster }) {
}
}, [loading]);

const saveSelection = () =>
dispatch(
checksSelected({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to pass selectedChecks and clusterId as the function arguments?
Using global args looks a bit unsafe.
But talking from my ignorance... XD

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would need to have different names tho, otherwise it will clash.
And at the end we would be still passing the same variables, with a different name.

I am open to suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a suggestion...
Anyway, now that i checked a bit, we should maybe wrap this functions in useCallback, otherwise the function is redefined in every render. Maybe a cheap operation, but with the useCallback it is avoided.
Maybe @dottorblaster has stronger opinions on this

checks: selectedChecks,
clusterID: clusterId,
})
);

return (
<div className="bg-white rounded p-3">
<CatalogContainer
Expand Down Expand Up @@ -148,9 +156,7 @@ function ChecksSelection({ clusterId, cluster }) {
<div className="place-items-end flex">
<button
className="flex justify-center items-center bg-jungle-green-500 hover:opacity-75 text-white font-bold py-2 px-4 rounded"
onClick={() =>
dispatch(checksSelected(selectedChecks, clusterId))
}
onClick={saveSelection}
type="button"
data-testid="save-selection-button"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('ClusterDetails ChecksSelection component', () => {
},
},
{
type: 'CHECKS_SELECTED',
type: 'CLUSTER_CHECKS_SELECTED',
payload: {
checks: selectedChecks,
clusterID: cluster.id,
Expand Down
13 changes: 7 additions & 6 deletions assets/js/components/HostDetails/HostChecksSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function HostChecksSelection({
catalogError,
catalogLoading,
onUpdateCatalog,
onSaveSelection,
isSavingSelection,
}) {
return (
<div className="w-full px-2 sm:px-0">
Expand All @@ -25,20 +27,19 @@ function HostChecksSelection({
<HostInfoBox provider={provider} agentVersion={agentVersion} />
<ChecksSelection
targetID={hostID}
targetName={hostName}
catalog={catalog}
catalogError={catalogError}
loading={catalogLoading}
selected={selectedChecks}
onSave={(_selectedChecks, _hostID) => {
// TODO: dispatch check selection for a host
}}
onSave={(selection, targetID, targetName) =>
onSaveSelection(selection, targetID, targetName)
}
onUpdateCatalog={() => onUpdateCatalog()}
onClear={() => {
// TODO
}}
saving={false}
error={null}
success={false}
saving={isSavingSelection}
/>
</div>
);
Expand Down
33 changes: 25 additions & 8 deletions assets/js/components/HostDetails/HostSettingsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { useParams } from 'react-router-dom';

import LoadingBox from '@components/LoadingBox';

import { checksSelected } from '@state/hostChecksSelection';
import { updateCatalog } from '@state/actions/catalog';
import { getCatalog } from '@state/selectors/catalog';
import { getHost } from '@state/selectors';
import { getCheckSelection } from '@state/selectors/checksSelection';
import HostChecksSelection from './HostChecksSelection';

function HostSettingsPage() {
Expand All @@ -21,16 +23,36 @@ function HostSettingsPage() {
loading: catalogLoading,
} = useSelector(getCatalog());

const { saving } = useSelector(getCheckSelection());

if (!host) {
return <LoadingBox text="Loading..." />;
}

const {
hostname: hostName,
provider,
agent_version: agentVersion,
selected_checks: selectedChecks,
} = host;

const refreshCatalog = () =>
dispatch(
updateCatalog({
provider: host.provider,
target_type: 'host',
})
);

const saveSelection = (selection, targetID, targetName) =>
dispatch(
checksSelected({
hostID: targetID,
hostName: targetName,
checks: selection,
})
);

return (
<HostChecksSelection
hostID={hostID}
Expand All @@ -41,14 +63,9 @@ function HostSettingsPage() {
catalog={catalog}
catalogError={catalogError}
catalogLoading={catalogLoading}
onUpdateCatalog={() =>
dispatch(
updateCatalog({
provider: host.provider,
target_type: 'host',
})
)
}
onUpdateCatalog={refreshCatalog}
isSavingSelection={saving}
onSaveSelection={saveSelection}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions assets/js/lib/test-utils/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const defaultInitialState = {
),
},
clusterChecksSelection: {},
checksSelection: {},
catalog: { loading: false, data: [], error: null },
};

Expand Down
6 changes: 0 additions & 6 deletions assets/js/state/actions/cluster.js

This file was deleted.

5 changes: 4 additions & 1 deletion assets/js/state/clusters.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSlice } from '@reduxjs/toolkit';
import { createAction, createSlice } from '@reduxjs/toolkit';

const initialState = {
loading: false,
Expand Down Expand Up @@ -103,6 +103,9 @@ export const clustersListSlice = createSlice({

export const CLUSTER_DEREGISTERED = 'CLUSTER_DEREGISTERED';

export const CLUSTER_CHECKS_SELECTED = 'CLUSTER_CHECKS_SELECTED';
export const checksSelected = createAction(CLUSTER_CHECKS_SELECTED);

export const {
setClusters,
appendCluster,
Expand Down
26 changes: 26 additions & 0 deletions assets/js/state/hostChecksSelection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createAction, createSlice } from '@reduxjs/toolkit';

const initialState = {
saving: false,
};

export const checksSelectionSlice = createSlice({
name: 'checksSelection',
initialState,
reducers: {
startSavingChecksSelection: (state) => {
state.saving = true;
},
stopSavingChecksSelection: (state) => {
state.saving = false;
},
},
});

export const HOST_CHECKS_SELECTED = 'HOST_CHECKS_SELECTED';
export const checksSelected = createAction(HOST_CHECKS_SELECTED);

export const { startSavingChecksSelection, stopSavingChecksSelection } =
checksSelectionSlice.actions;

export default checksSelectionSlice.reducer;
34 changes: 34 additions & 0 deletions assets/js/state/hostChecksSelection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import checksSelectionReducer, {
startSavingChecksSelection,
stopSavingChecksSelection,
} from '@state/hostChecksSelection';

describe('Checks Selection reducer', () => {
it('should mark a check selection as saving', () => {
const initialState = {
saving: false,
};

const action = startSavingChecksSelection();

const expectedState = {
saving: true,
};

expect(checksSelectionReducer(initialState, action)).toEqual(expectedState);
});

it('should mark a check selection as completed', () => {
const initialState = {
saving: true,
};

const action = stopSavingChecksSelection();

const expectedState = {
saving: false,
};

expect(checksSelectionReducer(initialState, action)).toEqual(expectedState);
});
});
9 changes: 9 additions & 0 deletions assets/js/state/hosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export const hostsListSlice = createSlice({
return host;
});
},
updateSelectedChecks: (state, action) => {
state.hosts = state.hosts.map((host) => {
if (host.id === action.payload.hostID) {
host.selected_checks = action.payload.checks;
}
return host;
});
},
setHeartbeatPassing: (state, action) => {
state.hosts = state.hosts.map((host) => {
if (host.id === action.payload.id) {
Expand Down Expand Up @@ -127,6 +135,7 @@ export const {
updateHost,
addTagToHost,
removeTagFromHost,
updateSelectedChecks,
setHeartbeatPassing,
setHeartbeatCritical,
setHostListDeregisterable,
Expand Down
25 changes: 25 additions & 0 deletions assets/js/state/hosts.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { faker } from '@faker-js/faker';
import hostsReducer, {
removeHost,
setHostListDeregisterable,
setHostNotDeregisterable,
setHostDeregistering,
setHostNotDeregistering,
updateSelectedChecks,
} from '@state/hosts';
import { hostFactory } from '@lib/test-utils/factories';

Expand Down Expand Up @@ -83,4 +85,27 @@ describe('Hosts reducer', () => {

expect(hostsReducer(initialState, action)).toEqual(expectedState);
});

it('should update the check selection for a host', () => {
const initialCheckSelection = [
faker.datatype.uuid(),
faker.datatype.uuid(),
];
const host1 = hostFactory.build({ selected_checks: initialCheckSelection });
const host2 = hostFactory.build();
const initialState = { hosts: [host1, host2] };

const newChecksSelection = [faker.datatype.uuid(), faker.datatype.uuid()];

const action = updateSelectedChecks({
hostID: host1.id,
checks: newChecksSelection,
});

const expectedState = {
hosts: [{ ...host1, selected_checks: newChecksSelection }, host2],
};

expect(hostsReducer(initialState, action)).toEqual(expectedState);
});
});
2 changes: 2 additions & 0 deletions assets/js/state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sapSystemsHealthSummaryReducer from './healthSummary';
import hostsListReducer from './hosts';
import clustersListReducer from './clusters';
import clusterChecksSelectionReducer from './clusterChecksSelection';
import hostChecksSelectionReducer from './hostChecksSelection';
import checksResultsFiltersReducer from './checksResultsFilters';
import sapSystemListReducer from './sapSystems';
import databasesListReducer from './databases';
Expand All @@ -23,6 +24,7 @@ export const store = configureStore({
hostsList: hostsListReducer,
clustersList: clustersListReducer,
clusterChecksSelection: clusterChecksSelectionReducer,
checksSelection: hostChecksSelectionReducer,
nelsonkopliku marked this conversation as resolved.
Show resolved Hide resolved
checksResultsFilters: checksResultsFiltersReducer,
sapSystemsList: sapSystemListReducer,
databasesList: databasesListReducer,
Expand Down
Loading