[Fleet] Only display log level in Filter that are present in the logs (#84277)

This commit is contained in:
Nicolas Chaulet 2020-11-24 17:54:26 -05:00 committed by GitHub
parent f80da6cc39
commit 5f53d856c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -32,4 +32,6 @@ export const AGENT_LOG_LEVELS = {
DEBUG: 'debug',
};
export const ORDERED_FILTER_LOG_LEVELS = ['error', 'warning', 'warn', 'notice', 'info', 'debug'];
export const DEFAULT_LOG_LEVEL = AGENT_LOG_LEVELS.INFO;

View file

@ -6,10 +6,19 @@
import React, { memo, useState, useEffect } from 'react';
import { EuiPopover, EuiFilterButton, EuiFilterSelectItem } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { AGENT_LOG_LEVELS, AGENT_LOG_INDEX_PATTERN, LOG_LEVEL_FIELD } from './constants';
import { ORDERED_FILTER_LOG_LEVELS, AGENT_LOG_INDEX_PATTERN, LOG_LEVEL_FIELD } from './constants';
import { useStartServices } from '../../../../../hooks';
const LEVEL_VALUES = Object.values(AGENT_LOG_LEVELS);
function sortLogLevels(levels: string[]): string[] {
return [
...new Set([
// order by severity for known level
...ORDERED_FILTER_LOG_LEVELS.filter((level) => levels.includes(level)),
// Add unknown log level
...levels.sort(),
]),
];
}
export const LogLevelFilter: React.FunctionComponent<{
selectedLevels: string[];
@ -18,7 +27,7 @@ export const LogLevelFilter: React.FunctionComponent<{
const { data } = useStartServices();
const [isOpen, setIsOpen] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [levelValues, setLevelValues] = useState<string[]>(LEVEL_VALUES);
const [levelValues, setLevelValues] = useState<string[]>([]);
useEffect(() => {
const fetchValues = async () => {
@ -32,7 +41,7 @@ export const LogLevelFilter: React.FunctionComponent<{
field: LOG_LEVEL_FIELD,
query: '',
});
setLevelValues([...new Set([...LEVEL_VALUES, ...values.sort()])]);
setLevelValues(sortLogLevels(values));
} catch (e) {
setLevelValues([]);
}