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

[Asset management] Osquery app bug squashing #102406

Merged
merged 8 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const StyledEuiCard = styled(EuiCard)`

interface ActionResultsSummaryProps {
actionId: string;
expirationDate: Date;
agentIds?: string[];
isLive?: boolean;
}
Expand All @@ -48,6 +49,7 @@ const renderErrorMessage = (error: string) => (

const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
actionId,
expirationDate,
agentIds,
isLive,
}) => {
Expand All @@ -56,6 +58,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
const [pageIndex, setPageIndex] = useState(0);
// @ts-expect-error update types
const [pageSize, setPageSize] = useState(50);
const expired = useMemo(() => expirationDate < new Date(), [expirationDate]);
const {
// @ts-expect-error update types
data: { aggregations, edges },
Expand All @@ -66,7 +69,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
limit: pageSize,
direction: Direction.asc,
sortField: '@timestamp',
isLive,
isLive: !expired && isLive,
});

const { data: logsResults } = useAllResults({
Expand All @@ -79,7 +82,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
direction: Direction.asc,
},
],
isLive,
isLive: !expired && isLive,
});

const notRespondedCount = useMemo(() => {
Expand Down Expand Up @@ -108,9 +111,13 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
description: aggregations.successful,
},
{
title: i18n.translate('xpack.osquery.liveQueryActionResults.summary.pendingLabelText', {
defaultMessage: 'Not yet responded',
}),
title: expired
? i18n.translate('xpack.osquery.liveQueryActionResults.summary.expiredLabelText', {
defaultMessage: 'Expired',
})
: i18n.translate('xpack.osquery.liveQueryActionResults.summary.pendingLabelText', {
defaultMessage: 'Not yet responded',
}),
description: notRespondedCount,
},
{
Expand All @@ -124,7 +131,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
),
},
],
[agentIds, aggregations.failed, aggregations.successful, notRespondedCount]
[agentIds, aggregations.failed, aggregations.successful, notRespondedCount, expired]
);

const renderAgentIdColumn = useCallback(
Expand Down Expand Up @@ -158,23 +165,30 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
[logsResults]
);

const renderStatusColumn = useCallback((_, item) => {
if (!item.fields.completed_at) {
return i18n.translate('xpack.osquery.liveQueryActionResults.table.pendingStatusText', {
defaultMessage: 'pending',
});
}
const renderStatusColumn = useCallback(
(_, item) => {
if (!item.fields.completed_at) {
return expired
? i18n.translate('xpack.osquery.liveQueryActionResults.table.expiredStatusText', {
defaultMessage: 'expired',
})
: i18n.translate('xpack.osquery.liveQueryActionResults.table.pendingStatusText', {
defaultMessage: 'pending',
});
}

if (item.fields['error.keyword']) {
return i18n.translate('xpack.osquery.liveQueryActionResults.table.errorStatusText', {
defaultMessage: 'error',
});
}
if (item.fields['error.keyword']) {
return i18n.translate('xpack.osquery.liveQueryActionResults.table.errorStatusText', {
defaultMessage: 'error',
});
}

return i18n.translate('xpack.osquery.liveQueryActionResults.table.successStatusText', {
defaultMessage: 'success',
});
}, []);
return i18n.translate('xpack.osquery.liveQueryActionResults.table.successStatusText', {
defaultMessage: 'success',
});
},
[expired]
);

const columns = useMemo(
() => [
Expand Down Expand Up @@ -227,7 +241,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
<EuiFlexGroup>
<EuiFlexItem grow={false}>
<StyledEuiCard title="" description="" textAlign="left">
{notRespondedCount ? <EuiProgress size="xs" position="absolute" /> : null}
{!expired && notRespondedCount ? <EuiProgress size="xs" position="absolute" /> : null}
<EuiDescriptionList
compressed
textStyle="reverse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ESTermQuery } from '../../common/typed_json';
import { queryClient } from '../query_client';

import { generateTablePaginationOptions, getInspectResponse, InspectResponse } from './helpers';
import { useErrorToast } from '../common/hooks/use_error_toast';

export interface ResultsArgs {
results: ResultEdges;
Expand Down Expand Up @@ -56,10 +57,8 @@ export const useActionResults = ({
skip = false,
isLive = false,
}: UseActionResults) => {
const {
data,
notifications: { toasts },
} = useKibana().services;
const { data } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery(
['actionResults', { actionId }],
Expand Down Expand Up @@ -103,9 +102,9 @@ export const useActionResults = ({
aggregations: {
totalResponded,
// @ts-expect-error update types
successful: aggsBuckets.find((bucket) => bucket.key === 'success')?.doc_count ?? 0,
successful: aggsBuckets?.find((bucket) => bucket.key === 'success')?.doc_count ?? 0,
// @ts-expect-error update types
failed: aggsBuckets.find((bucket) => bucket.key === 'error')?.doc_count ?? 0,
failed: aggsBuckets?.find((bucket) => bucket.key === 'error')?.doc_count ?? 0,
},
inspect: getInspectResponse(responseData, {} as InspectResponse),
};
Expand All @@ -124,8 +123,9 @@ export const useActionResults = ({
refetchInterval: isLive ? 1000 : false,
keepPreviousData: true,
enabled: !skip && !!agentIds?.length,
onSuccess: () => setErrorToast(),
onError: (error: Error) =>
toasts.addError(error, {
setErrorToast(error, {
title: i18n.translate('xpack.osquery.action_results.fetchError', {
defaultMessage: 'Error while fetching action results',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/actions/use_action_details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { ESTermQuery } from '../../common/typed_json';

import { getInspectResponse, InspectResponse } from './helpers';
import { useErrorToast } from '../common/hooks/use_error_toast';

export interface ActionDetailsArgs {
actionDetails: Record<string, string>;
Expand All @@ -33,10 +34,8 @@ interface UseActionDetails {
}

export const useActionDetails = ({ actionId, filterQuery, skip = false }: UseActionDetails) => {
const {
data,
notifications: { toasts },
} = useKibana().services;
const { data } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery(
['actionDetails', { actionId, filterQuery }],
Expand All @@ -61,8 +60,9 @@ export const useActionDetails = ({ actionId, filterQuery, skip = false }: UseAct
},
{
enabled: !skip,
onSuccess: () => setErrorToast(),
onError: (error: Error) =>
toasts.addError(error, {
setErrorToast(error, {
title: i18n.translate('xpack.osquery.action_details.fetchError', {
defaultMessage: 'Error while fetching action details',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/actions/use_all_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { ESTermQuery } from '../../common/typed_json';

import { generateTablePaginationOptions, getInspectResponse, InspectResponse } from './helpers';
import { useErrorToast } from '../common/hooks/use_error_toast';

export interface ActionsArgs {
actions: ActionEdges;
Expand Down Expand Up @@ -48,10 +49,8 @@ export const useAllActions = ({
filterQuery,
skip = false,
}: UseAllActions) => {
const {
data,
notifications: { toasts },
} = useKibana().services;
const { data } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery(
['actions', { activePage, direction, limit, sortField }],
Expand Down Expand Up @@ -82,8 +81,9 @@ export const useAllActions = ({
{
keepPreviousData: true,
enabled: !skip,
onSuccess: () => setErrorToast(),
onError: (error: Error) =>
toasts.addError(error, {
setErrorToast(error, {
title: i18n.translate('xpack.osquery.all_actions.fetchError', {
defaultMessage: 'Error while fetching actions',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import {
GetAgentPoliciesResponse,
GetAgentPoliciesResponseItem,
} from '../../../fleet/common';
import { useErrorToast } from '../common/hooks/use_error_toast';

export const useAgentPolicies = () => {
const {
http,
notifications: { toasts },
} = useKibana().services;
const { http } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery<GetAgentPoliciesResponse, unknown, GetAgentPoliciesResponseItem[]>(
['agentPolicies'],
Expand All @@ -34,8 +33,9 @@ export const useAgentPolicies = () => {
placeholderData: [],
keepPreviousData: true,
select: (response) => response.items,
onSuccess: () => setErrorToast(),
onError: (error) =>
toasts.addError(error as Error, {
setErrorToast(error as Error, {
title: i18n.translate('xpack.osquery.agent_policies.fetchError', {
defaultMessage: 'Error while fetching agent policies',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/agent_policies/use_agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ import { useQuery } from 'react-query';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { agentPolicyRouteService } from '../../../fleet/common';
import { useErrorToast } from '../common/hooks/use_error_toast';

interface UseAgentPolicy {
policyId: string;
skip?: boolean;
}

export const useAgentPolicy = ({ policyId, skip }: UseAgentPolicy) => {
const {
http,
notifications: { toasts },
} = useKibana().services;
const { http } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery(
['agentPolicy', { policyId }],
Expand All @@ -29,8 +28,9 @@ export const useAgentPolicy = ({ policyId, skip }: UseAgentPolicy) => {
enabled: !skip,
keepPreviousData: true,
select: (response) => response.item,
onSuccess: () => setErrorToast(),
onError: (error: Error) =>
toasts.addError(error, {
setErrorToast(error, {
title: i18n.translate('xpack.osquery.agent_policy_details.fetchError', {
defaultMessage: 'Error while fetching agent policy details',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/agents/use_agent_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ import {

import { generateTablePaginationOptions, processAggregations } from './helpers';
import { Overlap, Group } from './types';
import { useErrorToast } from '../common/hooks/use_error_toast';

interface UseAgentGroups {
osqueryPolicies: string[];
osqueryPoliciesLoading: boolean;
}

export const useAgentGroups = ({ osqueryPolicies, osqueryPoliciesLoading }: UseAgentGroups) => {
const {
data,
notifications: { toasts },
} = useKibana().services;
const { data } = useKibana().services;
const setErrorToast = useErrorToast();

const { agentPoliciesLoading, agentPolicyById } = useAgentPolicies(osqueryPolicies);
const [platforms, setPlatforms] = useState<Group[]>([]);
Expand Down Expand Up @@ -100,8 +99,9 @@ export const useAgentGroups = ({ osqueryPolicies, osqueryPoliciesLoading }: UseA
},
{
enabled: !osqueryPoliciesLoading && !agentPoliciesLoading,
onSuccess: () => setErrorToast(),
onError: (error) =>
toasts.addError(error as Error, {
setErrorToast(error as Error, {
title: i18n.translate('xpack.osquery.agent_groups.fetchError', {
defaultMessage: 'Error while fetching agent groups',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/agents/use_agent_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import { useQueries, UseQueryResult } from 'react-query';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { agentPolicyRouteService, GetOneAgentPolicyResponse } from '../../../fleet/common';
import { useErrorToast } from '../common/hooks/use_error_toast';

export const useAgentPolicies = (policyIds: string[] = []) => {
const {
http,
notifications: { toasts },
} = useKibana().services;
const { http } = useKibana().services;
const setErrorToast = useErrorToast();

const agentResponse = useQueries(
policyIds.map((policyId) => ({
queryKey: ['agentPolicy', policyId],
queryFn: () => http.get(agentPolicyRouteService.getInfoPath(policyId)),
enabled: policyIds.length > 0,
onSuccess: () => setErrorToast(),
onError: (error) =>
toasts.addError(error as Error, {
setErrorToast(error as Error, {
title: i18n.translate('xpack.osquery.action_policy_details.fetchError', {
defaultMessage: 'Error while fetching policy details',
}),
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/osquery/public/agents/use_agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n';
import { useQuery } from 'react-query';

import { GetAgentStatusResponse, agentRouteService } from '../../../fleet/common';
import { useErrorToast } from '../common/hooks/use_error_toast';
import { useKibana } from '../common/lib/kibana';

interface UseAgentStatus {
Expand All @@ -17,10 +18,8 @@ interface UseAgentStatus {
}

export const useAgentStatus = ({ policyId, skip }: UseAgentStatus) => {
const {
http,
notifications: { toasts },
} = useKibana().services;
const { http } = useKibana().services;
const setErrorToast = useErrorToast();

return useQuery<GetAgentStatusResponse, unknown, GetAgentStatusResponse['results']>(
['agentStatus', policyId],
Expand All @@ -38,8 +37,9 @@ export const useAgentStatus = ({ policyId, skip }: UseAgentStatus) => {
{
enabled: !skip,
select: (response) => response.results,
onSuccess: () => setErrorToast(),
onError: (error) =>
toasts.addError(error as Error, {
setErrorToast(error as Error, {
title: i18n.translate('xpack.osquery.agent_status.fetchError', {
defaultMessage: 'Error while fetching agent status',
}),
Expand Down
Loading