Skip to content

Commit

Permalink
Show previous to offline status in details
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Sep 14, 2022
1 parent 960a161 commit a28866e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
26 changes: 26 additions & 0 deletions x-pack/plugins/fleet/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ export function getAgentStatus(agent: Agent | FleetServerAgent): AgentStatus {
return 'online';
}

export function getPreviousAgentStatusForOfflineAgents(
agent: Agent | FleetServerAgent
): AgentStatus | undefined {
if (agent.unenrollment_started_at && !agent.unenrolled_at) {
return 'unenrolling';
}

if (agent.last_checkin_status === 'error') {
return 'error';
}
if (agent.last_checkin_status === 'degraded') {
return 'degraded';
}

const policyRevision =
'policy_revision' in agent
? agent.policy_revision
: 'policy_revision_idx' in agent
? agent.policy_revision_idx
: undefined;

if (!policyRevision || (agent.upgrade_started_at && agent.upgrade_status !== 'completed')) {
return 'updating';
}
}

export function buildKueryForEnrollingAgents(path: string = ''): string {
return `not (${path}last_checkin:*)`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const AgentDetailsOverviewSection: React.FunctionComponent<{
title: i18n.translate('xpack.fleet.agentDetails.statusLabel', {
defaultMessage: 'Status',
}),
description: <AgentHealth agent={agent} />,
description: <AgentHealth agent={agent} showOfflinePreviousStatus={true} />,
},
{
title: i18n.translate('xpack.fleet.agentDetails.lastActivityLabel', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
* 2.0.
*/

import React from 'react';
import React, { useMemo } from 'react';
import { FormattedMessage, FormattedRelative } from '@kbn/i18n-react';
import { EuiBadge, EuiToolTip } from '@elastic/eui';

import { euiLightVars as euiVars } from '@kbn/ui-theme';

import type { Agent } from '../../../types';
import { getPreviousAgentStatusForOfflineAgents } from '@kbn/fleet-plugin/common/services/agent_status';

interface Props {
agent: Agent;
showOfflinePreviousStatus?: boolean;
}

const Status = {
Expand Down Expand Up @@ -48,8 +50,8 @@ const Status = {
),
};

function getStatusComponent(agent: Agent): React.ReactElement {
switch (agent.status) {
function getStatusComponent(status: Agent['status']): React.ReactElement {
switch (status) {
case 'warning':
case 'error':
case 'degraded':
Expand All @@ -67,10 +69,21 @@ function getStatusComponent(agent: Agent): React.ReactElement {
}
}

export const AgentHealth: React.FunctionComponent<Props> = ({ agent }) => {
export const AgentHealth: React.FunctionComponent<Props> = ({
agent,
showOfflinePreviousStatus,
}) => {
const { last_checkin: lastCheckIn } = agent;
const msLastCheckIn = new Date(lastCheckIn || 0).getTime();

const previousToOfflineStatus = useMemo(() => {
if (!showOfflinePreviousStatus || agent.status !== 'offline') {
return;
}

return getPreviousAgentStatusForOfflineAgents(agent);
}, [showOfflinePreviousStatus]);

return (
<EuiToolTip
position="top"
Expand All @@ -93,7 +106,10 @@ export const AgentHealth: React.FunctionComponent<Props> = ({ agent }) => {
)
}
>
{getStatusComponent(agent)}
<>
{getStatusComponent(agent.status)}
{previousToOfflineStatus ? getStatusComponent(previousToOfflineStatus) : null}
</>
</EuiToolTip>
);
};

0 comments on commit a28866e

Please sign in to comment.