Skip to content

Commit

Permalink
Make "show in inventory" filter waffle map
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipb committed Apr 14, 2021
1 parent 67e487b commit ed314b6
Showing 1 changed file with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { AnomalySeverityIndicator } from '../../../../../../../components/loggin
import { useSourceContext } from '../../../../../../../containers/metrics_source';
import { createResultsUrl } from '../flyout_home';
import { useWaffleViewState, WaffleViewState } from '../../../../hooks/use_waffle_view_state';
import { useUiTracker } from '../../../../../../../../../observability/public';
type JobType = 'k8s' | 'hosts';
type SortField = 'anomalyScore' | 'startTime';
interface JobOption {
Expand All @@ -56,15 +57,17 @@ const AnomalyActionMenu = ({
type,
startTime,
closeFlyout,
partitionFieldName,
partitionFieldValue,
influencerField,
influencers,
disableShowInInventory,
}: {
jobId: string;
type: string;
startTime: number;
closeFlyout: () => void;
partitionFieldName?: string;
partitionFieldValue?: string;
influencerField: string;
influencers: string[];
disableShowInInventory?: boolean;
}) => {
const [isOpen, setIsOpen] = useState(false);
const close = useCallback(() => setIsOpen(false), [setIsOpen]);
Expand Down Expand Up @@ -95,31 +98,27 @@ const AnomalyActionMenu = ({
region: '',
autoReload: false,
filterQuery: {
expression:
partitionFieldName && partitionFieldValue
? `${partitionFieldName}: "${partitionFieldValue}"`
: ``,
expression: influencers.reduce((query, i) => {
if (query) {
query = `${query} or `;
}
return `${query} ${influencerField}: "${i}"`;
}, ''),
kind: 'kuery',
},
legend: { palette: 'cool', reverseColors: false, steps: 10 },
time: startTime,
};
onViewChange(anomalyViewParams);
closeFlyout();
}, [jobId, onViewChange, startTime, type, partitionFieldName, partitionFieldValue, closeFlyout]);
}, [jobId, onViewChange, startTime, type, influencers, influencerField, closeFlyout]);

const anomaliesUrl = useLinkProps({
app: 'ml',
pathname: `/explorer?_g=${createResultsUrl([jobId.toString()])}`,
});

const items = [
<EuiContextMenuItem key="showInInventory" icon="search" onClick={showInInventory}>
<FormattedMessage
id="xpack.infra.ml.anomalyFlyout.actions.showInInventory"
defaultMessage="Show in Inventory"
/>
</EuiContextMenuItem>,
<EuiContextMenuItem key="openInAnomalyExplorer" icon="popout" {...anomaliesUrl}>
<FormattedMessage
id="xpack.infra.ml.anomalyFlyout.actions.openInAnomalyExplorer"
Expand All @@ -128,6 +127,17 @@ const AnomalyActionMenu = ({
</EuiContextMenuItem>,
];

if (!disableShowInInventory) {
items.push(
<EuiContextMenuItem key="showInInventory" icon="search" onClick={showInInventory}>
<FormattedMessage
id="xpack.infra.ml.anomalyFlyout.actions.showInInventory"
defaultMessage="Show in Inventory"
/>
</EuiContextMenuItem>
);
}

return (
<>
<EuiPopover
Expand Down Expand Up @@ -180,6 +190,8 @@ export const AnomaliesTable = (props: Props) => {
const [search, setSearch] = useState('');
const [start, setStart] = useState('now-30d');
const [end, setEnd] = useState('now');
const [canShowInInventory, setCanShowInInventory] = useState(false);
const trackMetric = useUiTracker({ app: 'infra_metrics' });
const [timeRange, setTimeRange] = useState<{ start: number; end: number }>({
start: datemathToEpochMillis(start) || 0,
end: datemathToEpochMillis(end, 'up') || 0,
Expand Down Expand Up @@ -311,6 +323,16 @@ export const AnomaliesTable = (props: Props) => {
[hostChangeSort, k8sChangeSort, jobType]
);

useEffect(() => {
if (results) {
results.forEach((r) => {
if (r.influencers.length > 100) {
trackMetric({ metric: 'metrics_ml_anomaly_detection_more_than_100_influencers' });
}
});
}
}, [results, trackMetric]);

const onTableChange = (criteria: Criteria<MetricsHostsAnomaly>) => {
setSorting(criteria.sort);
changeSortOptions({
Expand Down Expand Up @@ -384,8 +406,11 @@ export const AnomaliesTable = (props: Props) => {
<AnomalyActionMenu
jobId={anomaly.jobId}
type={anomaly.type}
partitionFieldName={anomaly.partitionFieldName}
partitionFieldValue={anomaly.partitionFieldValue}
influencerField={
anomaly.type === 'metrics_hosts' ? 'host.name' : 'kubernetes.pod.uid'
}
disableShowInInventory={anomaly.influencers.length > 100}
influencers={anomaly.influencers}
startTime={anomaly.startTime}
closeFlyout={closeFlyout}
/>
Expand Down

0 comments on commit ed314b6

Please sign in to comment.