Skip to content

Commit

Permalink
Fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Nov 23, 2020
1 parent cb79895 commit 493381e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ export const DEFAULT_DATE_RANGE = {
start: 'now-1d',
end: 'now',
};

export const AGENT_LOG_LEVELS = {
ERROR: 'error',
WARNING: 'warning',
INFO: 'info',
DEBUG: 'debug',
};

export const DEFAULT_LOG_LEVEL = AGENT_LOG_LEVELS.INFO;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
EuiButtonEmpty,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n/react';
import semverGte from 'semver/functions/gte';
import semverCoerce from 'semver/functions/coerce';
import { RedirectAppLinks } from '../../../../../../../../../../../src/plugins/kibana_react/public';
import { TimeRange, esKuery } from '../../../../../../../../../../../src/plugins/data/public';
import { LogStream } from '../../../../../../../../../infra/public';
Expand Down Expand Up @@ -138,6 +140,18 @@ export const AgentLogs: React.FunctionComponent<{ agent: Agent }> = memo(({ agen
[logStreamQuery, dateRange.endExpression, dateRange.startExpression, http.basePath]
);

const agentVersion = agent.local_metadata?.elastic?.agent?.version;
const isLogLevelSelectionAvailable = useMemo(() => {
if (!agentVersion) {
return false;
}
const agentVersionWithPrerelease = semverCoerce(agentVersion)?.version;
if (!agentVersionWithPrerelease) {
return false;
}
return semverGte(agentVersionWithPrerelease, '7.11.0');
}, [agentVersion]);

return (
<WrapperFlexGroup direction="column" gutterSize="m">
<EuiFlexItem grow={false}>
Expand Down Expand Up @@ -214,9 +228,11 @@ export const AgentLogs: React.FunctionComponent<{ agent: Agent }> = memo(({ agen
/>
</EuiPanel>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SelectLogLevel agent={agent} />
</EuiFlexItem>
{isLogLevelSelectionAvailable && (
<EuiFlexItem grow={false}>
<SelectLogLevel agent={agent} />
</EuiFlexItem>
)}
</WrapperFlexGroup>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,56 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { memo, useState, useMemo } from 'react';
import React, { memo, useState, useCallback } from 'react';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import semverGte from 'semver/functions/gte';
import { EuiSelect, EuiFormLabel, EuiButtonEmpty, EuiFlexItem, EuiFlexGroup } from '@elastic/eui';
import { Agent } from '../../../../../types';
import { sendPostAgentAction, useStartServices } from '../../../../../hooks';
import { AGENT_LOG_LEVELS, DEFAULT_LOG_LEVEL } from './constants';

export const SelectLogLevel: React.FC<{ agent: Agent }> = memo(({ agent }) => {
const agentVersion = agent.local_metadata?.elastic?.agent?.version;
const isAvailable = useMemo(() => {
return semverGte(agentVersion, '7.11.0');
}, [agentVersion]);

const { notifications } = useStartServices();
const [isLoading, setIsLoading] = useState(false);
const [agentLogLevel, setAgentLogLevel] = useState(
agent.local_metadata?.elastic?.agent?.log_level ?? 'info'
agent.local_metadata?.elastic?.agent?.log_level ?? DEFAULT_LOG_LEVEL
);
const [selectedLogLevel, setSelectedLogLevel] = useState(agentLogLevel);

if (!isAvailable) {
return null;
}
const onClickApply = useCallback(() => {
setIsLoading(true);
async function send() {
try {
const res = await sendPostAgentAction(agent.id, {
action: {
type: 'SETTINGS',
data: {
log_level: selectedLogLevel,
},
},
});
if (res.error) {
throw res.error;
}
setAgentLogLevel(selectedLogLevel);
notifications.toasts.addSuccess(
i18n.translate('xpack.fleet.agentLogs.selectLogLevel.successText', {
defaultMessage: 'Changed agent logging level to "{logLevel}".',
values: {
logLevel: selectedLogLevel,
},
})
);
} catch (error) {
notifications.toasts.addError(error, {
title: 'Error',
});
}
setIsLoading(false);
}

send();
}, [notifications, selectedLogLevel, agent.id]);

return (
<EuiFlexGroup gutterSize="m" alignItems="center">
Expand All @@ -42,60 +68,29 @@ export const SelectLogLevel: React.FC<{ agent: Agent }> = memo(({ agent }) => {
<EuiFlexItem grow={false}>
<EuiSelect
disabled={isLoading}
compressed={true}
id="selectAgentLogLevel"
value={selectedLogLevel}
onChange={(event) => {
setSelectedLogLevel(event.target.value);
}}
options={[
{ text: 'error', value: 'error' },
{ text: 'warning', value: 'warning' },
{ text: 'info', value: 'info' },
{ text: 'debug', value: 'debug' },
{ text: AGENT_LOG_LEVELS.ERROR, value: AGENT_LOG_LEVELS.ERROR },
{ text: AGENT_LOG_LEVELS.WARNING, value: AGENT_LOG_LEVELS.WARNING },
{ text: AGENT_LOG_LEVELS.INFO, value: AGENT_LOG_LEVELS.INFO },
{ text: AGENT_LOG_LEVELS.DEBUG, value: AGENT_LOG_LEVELS.DEBUG },
]}
/>
</EuiFlexItem>
{agentLogLevel !== selectedLogLevel && (
<EuiFlexItem grow={false}>
<EuiButtonEmpty
flush="left"
size="xs"
isLoading={isLoading}
disabled={agentLogLevel === selectedLogLevel}
iconType="refresh"
onClick={() => {
setIsLoading(true);
async function send() {
try {
const res = await sendPostAgentAction(agent.id, {
action: {
type: 'SETTINGS',
data: {
log_level: selectedLogLevel,
},
},
});
if (res.error) {
throw res.error;
}
setAgentLogLevel(selectedLogLevel);
notifications.toasts.addSuccess(
i18n.translate('xpack.fleet.agentLogs.selectLogLevel.successText', {
defaultMessage: 'Changed agent logging level to "{logLevel}".',
values: {
logLevel: selectedLogLevel,
},
})
);
} catch (error) {
notifications.toasts.addError(error, {
title: 'Error',
});
}
setIsLoading(false);
}

send();
}}
onClick={onClickApply}
>
{isLoading ? (
<FormattedMessage
Expand Down

0 comments on commit 493381e

Please sign in to comment.