Skip to content
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 @@ -11,6 +11,7 @@ import {
EuiCallOut,
EuiFlexGroup,
EuiFlexItem,
EuiLoadingSpinner,
EuiPopover,
EuiPopoverTitle,
EuiSpacer,
Expand All @@ -31,7 +32,7 @@ export const StatusRuleViz = ({
}: {
ruleParams: StatusRuleParamsProps['ruleParams'];
}) => {
const { data } = useSelector(selectInspectStatusRule);
const { data, loading } = useSelector(selectInspectStatusRule);
const dispatch = useDispatch();
const {
services: { inspector },
Expand All @@ -57,7 +58,7 @@ export const StatusRuleViz = ({

return (
<EuiCallOut iconType="search" size="s">
<EuiFlexGroup alignItems="center" gutterSize="none">
<EuiFlexGroup alignItems="center" gutterSize="s">
<EuiFlexItem grow={false}>
{i18n.translate('xpack.synthetics.statusRuleViz.ruleAppliesToFlexItemLabel', {
defaultMessage: 'Rule applies to ',
Expand All @@ -68,17 +69,19 @@ export const StatusRuleViz = ({
isOpen={isPopoverOpen}
closePopover={() => setIsPopoverOpen(false)}
button={
<EuiButtonEmpty
data-test-subj="syntheticsStatusRuleVizMonitorQueryIDsButton"
size="xs"
onClick={() => setIsPopoverOpen(!isPopoverOpen)}
>
{i18n.translate('xpack.synthetics.statusRuleViz.monitorQueryIdsPopoverButton', {
defaultMessage:
'{total} existing {total, plural, one {monitor} other {monitors}}',
values: { total: data?.monitors.length },
})}
</EuiButtonEmpty>
loading ? undefined : (
<EuiButtonEmpty
data-test-subj="syntheticsStatusRuleVizMonitorQueryIDsButton"
size="xs"
onClick={() => setIsPopoverOpen(!isPopoverOpen)}
>
{i18n.translate('xpack.synthetics.statusRuleViz.monitorQueryIdsPopoverButton', {
defaultMessage:
'{total} existing {total, plural, one {monitor} other {monitors}}',
values: { total: data?.monitors.length },
})}
</EuiButtonEmpty>
)
}
>
<EuiPopoverTitle>
Expand All @@ -93,6 +96,11 @@ export const StatusRuleViz = ({
<RuleMonitorsTable />
</EuiPopover>
</EuiFlexItem>
{loading && (
<EuiFlexItem grow={false}>
<EuiLoadingSpinner size="s" />
</EuiFlexItem>
)}
{/* to push detail button to end*/}
<EuiFlexItem />
<EuiFlexItem grow={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
getUngroupedReasonMessage,
} from './message_utils';
import { queryMonitorStatusAlert } from './queries/query_monitor_status_alert';
import { parseArrayFilters } from '../../routes/common';
import { parseArrayFilters, parseLocationFilter } from '../../routes/common';
import { SyntheticsServerSetup } from '../../types';
import { SyntheticsEsClient } from '../../lib';
import {
Expand Down Expand Up @@ -115,22 +115,35 @@ export class StatusRuleExecutor {
return processMonitors([]);
}

const locationIds = await parseLocationFilter(
{
savedObjectsClient: this.soClient,
server: this.server,
syntheticsMonitorClient: this.syntheticsMonitorClient,
},
this.params.locations
);

const { filtersStr } = parseArrayFilters({
configIds,
filter: baseFilter,
tags: this.params?.tags,
locations: this.params?.locations,
monitorTypes: this.params?.monitorTypes,
monitorQueryIds: this.params?.monitorIds,
projects: this.params?.projects,
tags: this.params.tags,
locations: locationIds,
monitorTypes: this.params.monitorTypes,
monitorQueryIds: this.params.monitorIds,
projects: this.params.projects,
});

this.monitors = await getAllMonitors({
soClient: this.soClient,
filter: filtersStr,
});

this.debug(`Found ${this.monitors.length} monitors for params ${JSON.stringify(this.params)}`);
this.debug(
`Found ${this.monitors.length} monitors for params ${JSON.stringify(
this.params
)} | parsed location filter is ${JSON.stringify(locationIds)} `
);
return processMonitors(this.monitors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('common utils', () => {
schedules: ['schedule1', 'schedule2'],
});
expect(filters.filtersStr).toMatchInlineSnapshot(
`"synthetics-monitor.attributes.tags:(\\"tag1\\" OR \\"tag2\\") AND synthetics-monitor.attributes.project_id:(\\"project1\\" OR \\"project2\\") AND synthetics-monitor.attributes.type:(\\"type1\\" OR \\"type2\\") AND synthetics-monitor.attributes.schedule.number:(\\"schedule1\\" OR \\"schedule2\\") AND synthetics-monitor.attributes.id:(\\"query1\\" OR \\"query2\\") AND synthetics-monitor.attributes.config_id:(\\"1\\" OR \\"2\\")"`
`"synthetics-monitor.attributes.tags:(\\"tag1\\" OR \\"tag2\\") AND synthetics-monitor.attributes.project_id:(\\"project1\\" OR \\"project2\\") AND synthetics-monitor.attributes.type:(\\"type1\\" OR \\"type2\\") AND synthetics-monitor.attributes.locations.id:(\\"loc1\\" OR \\"loc2\\") AND synthetics-monitor.attributes.schedule.number:(\\"schedule1\\" OR \\"schedule2\\") AND synthetics-monitor.attributes.id:(\\"query1\\" OR \\"query2\\") AND synthetics-monitor.attributes.config_id:(\\"1\\" OR \\"2\\")"`
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,11 @@ export const getMonitors = async (
sortField,
sortOrder,
query,
tags,
monitorTypes,
locations,
filter = '',
searchAfter,
projects,
schedules,
monitorQueryIds,
showFromAllSpaces,
} = context.request.query;

const { filtersStr } = await getMonitorFilters({
filter,
monitorTypes,
tags,
locations,
projects,
schedules,
monitorQueryIds,
context,
});
const { filtersStr } = await getMonitorFilters(context);

return context.savedObjectsClient.find({
type: syntheticsMonitorType,
Expand All @@ -127,19 +111,29 @@ interface Filters {
projects?: string | string[];
schedules?: string | string[];
monitorQueryIds?: string | string[];
configIds?: string | string[];
}

export const getMonitorFilters = async (
data: {
context: RouteContext;
} & Filters
) => {
const { context, locations } = data;
const locationFilter = await parseLocationFilter(context, locations);
export const getMonitorFilters = async (context: RouteContext) => {
const {
tags,
monitorTypes,
filter = '',
projects,
schedules,
monitorQueryIds,
locations: queryLocations,
} = context.request.query;
const locations = await parseLocationFilter(context, queryLocations);

return parseArrayFilters({
...data,
locationFilter,
filter,
tags,
monitorTypes,
projects,
schedules,
monitorQueryIds,
locations,
});
};

Expand All @@ -151,25 +145,22 @@ export const parseArrayFilters = ({
monitorTypes,
schedules,
monitorQueryIds,
locationFilter,
}: Filters & {
locationFilter?: string | string[];
configIds?: string[];
}) => {
locations,
}: Filters) => {
const filtersStr = [
filter,
getSavedObjectKqlFilter({ field: 'tags', values: tags }),
getSavedObjectKqlFilter({ field: 'project_id', values: projects }),
getSavedObjectKqlFilter({ field: 'type', values: monitorTypes }),
getSavedObjectKqlFilter({ field: 'locations.id', values: locationFilter }),
getSavedObjectKqlFilter({ field: 'locations.id', values: locations }),
getSavedObjectKqlFilter({ field: 'schedule.number', values: schedules }),
getSavedObjectKqlFilter({ field: 'id', values: monitorQueryIds }),
getSavedObjectKqlFilter({ field: 'config_id', values: configIds }),
]
.filter((f) => !!f)
.join(' AND ');

return { filtersStr, locationFilter };
return { filtersStr, locationIds: locations };
};

export const getSavedObjectKqlFilter = ({
Expand Down Expand Up @@ -206,12 +197,24 @@ export const getSavedObjectKqlFilter = ({
return `${fieldKey}:"${escapeQuotes(values)}"`;
};

const parseLocationFilter = async (context: RouteContext, locations?: string | string[]) => {
export const parseLocationFilter = async (
{
syntheticsMonitorClient,
savedObjectsClient,
server,
}: Pick<RouteContext, 'syntheticsMonitorClient' | 'savedObjectsClient' | 'server'>,
locations?: string | string[]
) => {
if (!locations || locations?.length === 0) {
return;
}

const { allLocations } = await getAllLocations(context);
const { allLocations } = await getAllLocations({
syntheticsMonitorClient,
savedObjectsClient,
server,
excludeAgentPolicies: true,
});

if (Array.isArray(locations)) {
return locations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('current status route', () => {
})
);
const routeContext: any = {
request: {},
request: { query: {} },
syntheticsEsClient,
};

Expand Down Expand Up @@ -316,7 +316,7 @@ describe('current status route', () => {
);

const routeContext: any = {
request: {},
request: { query: {} },
syntheticsEsClient,
};

Expand Down Expand Up @@ -420,7 +420,7 @@ describe('current status route', () => {
})
);
const routeContext: any = {
request: {},
request: { query: {} },
syntheticsEsClient,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,15 @@ export const SUMMARIES_PAGE_SIZE = 5000;

export class OverviewStatusService {
filterData: {
locationFilter?: string[] | string;
locationIds?: string[] | string;
filtersStr?: string;
} = {};
constructor(
private readonly routeContext: RouteContext<Record<string, any>, OverviewStatusQuery>
) {}

async getOverviewStatus() {
const { request } = this.routeContext;
const queryParams = request.query as OverviewStatusQuery;

this.filterData = await getMonitorFilters({
...queryParams,
context: this.routeContext,
});
this.filterData = await getMonitorFilters(this.routeContext);

const [allConfigs, statusResult] = await Promise.all([
this.getMonitorConfigs(),
Expand All @@ -70,7 +64,7 @@ export class OverviewStatusService {
disabledCount,
disabledMonitorsCount,
projectMonitorsCount,
} = processMonitors(allConfigs, this.filterData?.locationFilter);
} = processMonitors(allConfigs, this.filterData?.locationIds);

return {
allIds,
Expand Down Expand Up @@ -100,7 +94,7 @@ export class OverviewStatusService {
projects,
showFromAllSpaces,
} = params;
const { locationFilter } = this.filterData;
const { locationIds } = this.filterData;
const getTermFilter = (field: string, value: string | string[] | undefined) => {
if (!value || isEmpty(value)) {
return [];
Expand Down Expand Up @@ -129,10 +123,10 @@ export class OverviewStatusService {
...getTermFilter('monitor.project.id', projects),
];

if (scopeStatusByLocation && !isEmpty(locationFilter) && locationFilter) {
if (scopeStatusByLocation && !isEmpty(locationIds) && locationIds) {
filters.push({
terms: {
'observer.name': locationFilter,
'observer.name': locationIds,
},
});
}
Expand Down Expand Up @@ -242,7 +236,7 @@ export class OverviewStatusService {
const enabledMonitors = monitors.filter((monitor) => monitor.attributes[ConfigKey.ENABLED]);
const disabledMonitors = monitors.filter((monitor) => !monitor.attributes[ConfigKey.ENABLED]);

const queryLocIds = this.filterData?.locationFilter;
const queryLocIds = this.filterData?.locationIds;

disabledMonitors.forEach((monitor) => {
const monitorQueryId = monitor.attributes[ConfigKey.MONITOR_QUERY_ID];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ export const getSyntheticsSuggestionsRoute: SyntheticsRestApiRouteFactory<
savedObjectsClient,
server: { logger },
} = route;
const { tags, locations, projects, monitorQueryIds, query } = route.request.query;
const { query } = route.request.query;

const { filtersStr } = await getMonitorFilters({
tags,
locations,
projects,
monitorQueryIds,
context: route,
});
const { filtersStr } = await getMonitorFilters(route);
const { allLocations = [] } = await getAllLocations(route);
try {
const data = await savedObjectsClient.find<EncryptedSyntheticsMonitorAttributes>({
Expand Down