Skip to content

Commit

Permalink
Add reducer to store pending actions in store
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Jun 10, 2021
1 parent 6aca956 commit e8619d3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export const initialEndpointPageState = (): Immutable<EndpointState> => {
policyVersionInfo: undefined,
hostStatus: undefined,
isolationRequestState: createUninitialisedResourceState(),
/**
* Getting pending endpoint actions is "supplemental" data, so there is no need to show other Async
* states other than Loaded
*/
endpointPendingActions: createLoadedResourceState(new Map()),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import {
getIsIsolationRequestPending,
getCurrentIsolationRequestState,
getActivityLogData,
detailsData,
} from './selectors';
import { EndpointState, PolicyIds } from '../types';
import { AgentIdsPendingActions, EndpointState, PolicyIds } from '../types';
import {
sendGetEndpointSpecificPackagePolicies,
sendGetEndpointSecurityPackage,
Expand All @@ -57,6 +58,7 @@ import { isolateHost, unIsolateHost } from '../../../../common/lib/endpoint_isol
import { AppAction } from '../../../../common/store/actions';
import { resolvePathVariables } from '../../../../common/utils/resolve_path_variables';
import { ServerReturnedEndpointPackageInfo } from './action';
import { fetchPendingActionsByAgentId } from '../../../../common/lib/endpoint_pending_actions';

type EndpointPageStore = ImmutableMiddlewareAPI<EndpointState, AppAction>;

Expand Down Expand Up @@ -111,6 +113,8 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
payload: endpointResponse,
});

loadEndpointsPendingActions(store);

try {
const endpointsTotalCount = await endpointsTotal(coreStart.http);
dispatch({
Expand Down Expand Up @@ -331,6 +335,8 @@ export const endpointMiddlewareFactory: ImmutableMiddlewareFactory<EndpointState
});
}

loadEndpointsPendingActions(store);

// call the activity log api
dispatch({
type: 'endpointDetailsActivityLogChanged',
Expand Down Expand Up @@ -529,3 +535,48 @@ async function getEndpointPackageInfo(
logError(error);
}
}

/**
* retrieves the Endpoint pending actions for all of the existing endpoints being displayed on the list
* or the details tab.
*
* @param store
*/
const loadEndpointsPendingActions = async ({
getState,
dispatch,
}: EndpointPageStore): Promise<void> => {
const state = getState();
const detailsEndpoint = detailsData(state);
const listEndpoints = listData(state);
const agentsIds = new Set<string>();

// get all agent ids for the endpoints in the list
if (detailsEndpoint) {
agentsIds.add(detailsEndpoint.elastic.agent.id);
}

for (const endpointInfo of listEndpoints) {
agentsIds.add(endpointInfo.metadata.elastic.agent.id);
}

if (agentsIds.size === 0) {
return;
}

try {
const { data: pendingActions } = await fetchPendingActionsByAgentId(Array.from(agentsIds));
const agentIdToPendingActions: AgentIdsPendingActions = new Map();

for (const pendingAction of pendingActions) {
agentIdToPendingActions.set(pendingAction.agent_id, pendingAction.pending_actions);
}

dispatch({
type: 'endpointPendingActionsStateChanged',
payload: createLoadedResourceState(agentIdToPendingActions),
});
} catch (error) {
logError(error);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { EndpointDetailsActivityLogChanged } from './action';
import { EndpointDetailsActivityLogChanged, EndpointPendingActionsStateChanged } from './action';
import {
isOnEndpointPage,
hasSelectedEndpoint,
Expand Down Expand Up @@ -38,6 +38,19 @@ const handleEndpointDetailsActivityLogChanged: CaseReducer<EndpointDetailsActivi
};
};

const handleEndpointPendingActionsStateChanged: CaseReducer<EndpointPendingActionsStateChanged> = (
state,
action
) => {
if (isOnEndpointPage(state)) {
return {
...state,
endpointPendingActions: action.payload,
};
}
return state;
};

/* eslint-disable-next-line complexity */
export const endpointListReducer: StateReducer = (state = initialEndpointPageState(), action) => {
if (action.type === 'serverReturnedEndpointList') {
Expand Down Expand Up @@ -123,6 +136,8 @@ export const endpointListReducer: StateReducer = (state = initialEndpointPageSta
};
} else if (action.type === 'endpointDetailsActivityLogChanged') {
return handleEndpointDetailsActivityLogChanged(state, action);
} else if (action.type === 'endpointPendingActionsStateChanged') {
return handleEndpointPendingActionsStateChanged(state, action);
} else if (action.type === 'serverReturnedPoliciesForOnboarding') {
return {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ export const getEndpointHostIsolationStatusPropsCallback: (
const endpointPendingActions = pendingActionsState.data.get(endpoint.elastic.agent.id);

if (endpointPendingActions) {
pendingIsolate = endpointPendingActions.pending_actions?.isolate ?? 0;
pendingUnIsolate = endpointPendingActions.pending_actions?.unisolate ?? 0;
pendingIsolate = endpointPendingActions?.isolate ?? 0;
pendingUnIsolate = endpointPendingActions?.unisolate ?? 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ export interface EndpointState {
hostStatus?: HostStatus;
/** Host isolation request state for a single endpoint */
isolationRequestState: AsyncResourceState<HostIsolationResponse>;
/** Holds a map of `agentId` to `EndpointPendingActions` that is used by both the list and details view. */
endpointPendingActions: AsyncResourceState<Map<string, EndpointPendingActions>>;
/**
* Holds a map of `agentId` to `EndpointPendingActions` that is used by both the list and details view
* Getting pending endpoint actions is "supplemental" data, so there is no need to show other Async
* states other than Loaded
*/
endpointPendingActions: AsyncResourceState<AgentIdsPendingActions>;
}

export type AgentIdsPendingActions = Map<string, EndpointPendingActions['pending_actions']>;

/**
* packagePolicy contains a list of Package Policy IDs (received via Endpoint metadata policy response) mapped to a boolean whether they exist or not.
* agentPolicy contains a list of existing Package Policy Ids mapped to an associated Fleet parent Agent Config.
Expand Down

0 comments on commit e8619d3

Please sign in to comment.