Skip to content

Commit

Permalink
to minimyze bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Jul 18, 2022
1 parent 3442f30 commit 57018ae
Show file tree
Hide file tree
Showing 14 changed files with 470 additions and 151 deletions.
Expand Up @@ -19,16 +19,16 @@ const MOCK_AGGS = {
};

jest.mock('../lib/rule_api', () => ({
loadRuleAggregations: jest.fn(),
loadRuleAggregationsWithKueryFilter: jest.fn(),
}));

const { loadRuleAggregations } = jest.requireMock('../lib/rule_api');
const { loadRuleAggregationsWithKueryFilter } = jest.requireMock('../lib/rule_api');

const onError = jest.fn();

describe('useLoadRuleAggregations', () => {
beforeEach(() => {
loadRuleAggregations.mockResolvedValue(MOCK_AGGS);
loadRuleAggregationsWithKueryFilter.mockResolvedValue(MOCK_AGGS);
jest.clearAllMocks();
});

Expand All @@ -54,7 +54,7 @@ describe('useLoadRuleAggregations', () => {
await waitForNextUpdate();
});

expect(loadRuleAggregations).toBeCalledWith(expect.objectContaining(params));
expect(loadRuleAggregationsWithKueryFilter).toBeCalledWith(expect.objectContaining(params));
expect(result.current.rulesStatusesTotal).toEqual(MOCK_AGGS.ruleExecutionStatus);
});

Expand All @@ -80,12 +80,12 @@ describe('useLoadRuleAggregations', () => {
await waitForNextUpdate();
});

expect(loadRuleAggregations).toBeCalledWith(expect.objectContaining(params));
expect(loadRuleAggregationsWithKueryFilter).toBeCalledWith(expect.objectContaining(params));
expect(result.current.rulesStatusesTotal).toEqual(MOCK_AGGS.ruleExecutionStatus);
});

it('should call onError if API fails', async () => {
loadRuleAggregations.mockRejectedValue('');
loadRuleAggregationsWithKueryFilter.mockRejectedValue('');
const params = {
searchText: '',
typesFilter: [],
Expand Down
Expand Up @@ -8,7 +8,7 @@
import { i18n } from '@kbn/i18n';
import { useState, useCallback, useMemo } from 'react';
import { RuleExecutionStatusValues } from '@kbn/alerting-plugin/common';
import { loadRuleAggregations, LoadRuleAggregationsProps } from '../lib/rule_api';
import { loadRuleAggregationsWithKueryFilter, LoadRuleAggregationsProps } from '../lib/rule_api';
import { useKibana } from '../../common/lib/kibana';

type UseLoadRuleAggregationsProps = Omit<LoadRuleAggregationsProps, 'http'> & {
Expand Down Expand Up @@ -38,7 +38,7 @@ export function useLoadRuleAggregations({

const internalLoadRuleAggregations = useCallback(async () => {
try {
const rulesAggs = await loadRuleAggregations({
const rulesAggs = await loadRuleAggregationsWithKueryFilter({
http,
searchText,
typesFilter,
Expand Down
Expand Up @@ -14,10 +14,10 @@ import {
import { RuleStatus } from '../../types';

jest.mock('../lib/rule_api', () => ({
loadRules: jest.fn(),
loadRulesWithKueryFilter: jest.fn(),
}));

const { loadRules } = jest.requireMock('../lib/rule_api');
const { loadRulesWithKueryFilter } = jest.requireMock('../lib/rule_api');

const onError = jest.fn();
const onPage = jest.fn();
Expand Down Expand Up @@ -233,7 +233,7 @@ const MOCK_RULE_DATA = {

describe('useLoadRules', () => {
beforeEach(() => {
loadRules.mockResolvedValue(MOCK_RULE_DATA);
loadRulesWithKueryFilter.mockResolvedValue(MOCK_RULE_DATA);
jest.clearAllMocks();
});

Expand Down Expand Up @@ -273,7 +273,7 @@ describe('useLoadRules', () => {
expect(result.current.rulesState.isLoading).toBeFalsy();

expect(onPage).toBeCalledTimes(0);
expect(loadRules).toBeCalledWith(expect.objectContaining(params));
expect(loadRulesWithKueryFilter).toBeCalledWith(expect.objectContaining(params));
expect(result.current.rulesState.data).toEqual(expect.arrayContaining(MOCK_RULE_DATA.data));
expect(result.current.rulesState.totalItemCount).toEqual(MOCK_RULE_DATA.total);
});
Expand Down Expand Up @@ -305,11 +305,11 @@ describe('useLoadRules', () => {
await waitForNextUpdate();
});

expect(loadRules).toBeCalledWith(expect.objectContaining(params));
expect(loadRulesWithKueryFilter).toBeCalledWith(expect.objectContaining(params));
});

it('should reset the page if the data is fetched while paged', async () => {
loadRules.mockResolvedValue({
loadRulesWithKueryFilter.mockResolvedValue({
...MOCK_RULE_DATA,
data: [],
});
Expand Down Expand Up @@ -347,7 +347,7 @@ describe('useLoadRules', () => {
});

it('should call onError if API fails', async () => {
loadRules.mockRejectedValue('');
loadRulesWithKueryFilter.mockRejectedValue('');
const params = {
page: {
index: 0,
Expand Down Expand Up @@ -378,7 +378,7 @@ describe('useLoadRules', () => {

describe('No data', () => {
it('noData should be true, if there is no Filter and no rules', async () => {
loadRules.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
loadRulesWithKueryFilter.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
const params = {
page: {
index: 0,
Expand Down Expand Up @@ -411,7 +411,7 @@ describe('useLoadRules', () => {
});

it('noData should be false, if there is rule types filter and no rules', async () => {
loadRules.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
loadRulesWithKueryFilter.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
const params = {
page: {
index: 0,
Expand Down Expand Up @@ -444,7 +444,7 @@ describe('useLoadRules', () => {
});

it('noData should be true, if there is rule types filter and no rules with hasDefaultRuleTypesFiltersOn = true', async () => {
loadRules.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
loadRulesWithKueryFilter.mockResolvedValue({ ...MOCK_RULE_DATA, data: [] });
const params = {
page: {
index: 0,
Expand Down
Expand Up @@ -8,7 +8,7 @@ import { useMemo, useCallback, useReducer } from 'react';
import { i18n } from '@kbn/i18n';
import { isEmpty } from 'lodash';
import { Rule, Pagination } from '../../types';
import { loadRules, LoadRulesProps } from '../lib/rule_api';
import { loadRulesWithKueryFilter, LoadRulesProps } from '../lib/rule_api';
import { useKibana } from '../../common/lib/kibana';

interface RuleState {
Expand Down Expand Up @@ -112,7 +112,7 @@ export function useLoadRules({
dispatch({ type: ActionTypes.SET_LOADING, payload: true });

try {
const rulesResponse = await loadRules({
const rulesResponse = await loadRulesWithKueryFilter({
http,
page,
searchText,
Expand Down
Expand Up @@ -5,36 +5,16 @@
* 2.0.
*/
import { HttpSetup } from '@kbn/core/public';
import { AsApiContract, RewriteRequestCase } from '@kbn/actions-plugin/common';
import { RuleAggregations, RuleStatus } from '../../../types';
import { AsApiContract } from '@kbn/actions-plugin/common';
import { RuleAggregations } from '../../../types';
import { INTERNAL_BASE_ALERTING_API_PATH } from '../../constants';
import { mapFilterToKueryNode } from './map_filters_to_kql';

export interface RuleTagsAggregations {
ruleTags: string[];
}

const rewriteBodyRes: RewriteRequestCase<RuleAggregations> = ({
rule_execution_status: ruleExecutionStatus,
rule_enabled_status: ruleEnabledStatus,
rule_muted_status: ruleMutedStatus,
rule_snoozed_status: ruleSnoozedStatus,
rule_tags: ruleTags,
...rest
}: any) => ({
...rest,
ruleExecutionStatus,
ruleEnabledStatus,
ruleMutedStatus,
ruleSnoozedStatus,
ruleTags,
});

const rewriteTagsBodyRes: RewriteRequestCase<RuleTagsAggregations> = ({
rule_tags: ruleTags,
}: any) => ({
ruleTags,
});
import { mapFiltersToKql } from './map_filters_to_kql';
import {
LoadRuleAggregationsProps,
rewriteBodyRes,
rewriteTagsBodyRes,
RuleTagsAggregations,
} from './aggregate_helpers';

// TODO: https://github.com/elastic/kibana/issues/131682
export async function loadRuleTags({ http }: { http: HttpSetup }): Promise<RuleTagsAggregations> {
Expand All @@ -44,16 +24,6 @@ export async function loadRuleTags({ http }: { http: HttpSetup }): Promise<RuleT
return rewriteTagsBodyRes(res);
}

export interface LoadRuleAggregationsProps {
http: HttpSetup;
searchText?: string;
typesFilter?: string[];
actionTypesFilter?: string[];
ruleExecutionStatusesFilter?: string[];
ruleStatusesFilter?: RuleStatus[];
tagsFilter?: string[];
}

export async function loadRuleAggregations({
http,
searchText,
Expand All @@ -63,20 +33,20 @@ export async function loadRuleAggregations({
ruleStatusesFilter,
tagsFilter,
}: LoadRuleAggregationsProps): Promise<RuleAggregations> {
const filtersKueryNode = mapFilterToKueryNode({
const filters = mapFiltersToKql({
typesFilter,
actionTypesFilter,
tagsFilter,
ruleExecutionStatusesFilter,
ruleStatusesFilter,
searchText,
tagsFilter,
});

const res = await http.get<AsApiContract<RuleAggregations>>(
`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
{
query: {
...(filtersKueryNode ? { filter: JSON.stringify(filtersKueryNode) } : {}),
search_fields: searchText ? JSON.stringify(['name', 'tags']) : undefined,
search: searchText,
filter: filters.length ? filters.join(' and ') : undefined,
default_search_operator: 'AND',
},
}
Expand Down
@@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { HttpSetup } from '@kbn/core/public';
import { RewriteRequestCase } from '@kbn/actions-plugin/common';
import { RuleAggregations, RuleStatus } from '../../../types';

export interface RuleTagsAggregations {
ruleTags: string[];
}

export const rewriteBodyRes: RewriteRequestCase<RuleAggregations> = ({
rule_execution_status: ruleExecutionStatus,
rule_enabled_status: ruleEnabledStatus,
rule_muted_status: ruleMutedStatus,
rule_snoozed_status: ruleSnoozedStatus,
rule_tags: ruleTags,
...rest
}: any) => ({
...rest,
ruleExecutionStatus,
ruleEnabledStatus,
ruleMutedStatus,
ruleSnoozedStatus,
ruleTags,
});

export const rewriteTagsBodyRes: RewriteRequestCase<RuleTagsAggregations> = ({
rule_tags: ruleTags,
}: any) => ({
ruleTags,
});

export interface LoadRuleAggregationsProps {
http: HttpSetup;
searchText?: string;
typesFilter?: string[];
actionTypesFilter?: string[];
ruleExecutionStatusesFilter?: string[];
ruleStatusesFilter?: RuleStatus[];
tagsFilter?: string[];
}
@@ -0,0 +1,41 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { AsApiContract } from '@kbn/actions-plugin/common';
import { RuleAggregations } from '../../../types';
import { INTERNAL_BASE_ALERTING_API_PATH } from '../../constants';
import { LoadRuleAggregationsProps, rewriteBodyRes } from './aggregate_helpers';
import { mapFilterToKueryNode } from './map_filters_to_kql';

export async function loadRuleAggregationsWithKueryFilter({
http,
searchText,
typesFilter,
actionTypesFilter,
ruleExecutionStatusesFilter,
ruleStatusesFilter,
tagsFilter,
}: LoadRuleAggregationsProps): Promise<RuleAggregations> {
const filtersKueryNode = mapFilterToKueryNode({
typesFilter,
actionTypesFilter,
tagsFilter,
ruleExecutionStatusesFilter,
ruleStatusesFilter,
searchText,
});

const res = await http.get<AsApiContract<RuleAggregations>>(
`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
{
query: {
...(filtersKueryNode ? { filter: JSON.stringify(filtersKueryNode) } : {}),
default_search_operator: 'AND',
},
}
);
return rewriteBodyRes(res);
}
Expand Up @@ -7,8 +7,9 @@

export { alertingFrameworkHealth } from './health';
export { mapFilterToKueryNode } from './map_filters_to_kql';
export type { LoadRuleAggregationsProps } from './aggregate';
export type { LoadRuleAggregationsProps } from './aggregate_helpers';
export { loadRuleAggregations, loadRuleTags } from './aggregate';
export { loadRuleAggregationsWithKueryFilter } from './aggregate_kuery_filter';
export { createRule } from './create';
export { deleteRules } from './delete';
export { disableRule, disableRules } from './disable';
Expand All @@ -18,8 +19,9 @@ export { loadRuleSummary } from './rule_summary';
export { muteAlertInstance } from './mute_alert';
export { muteRule, muteRules } from './mute';
export { loadRuleTypes } from './rule_types';
export type { LoadRulesProps } from './rules';
export type { LoadRulesProps } from './rules_helpers';
export { loadRules } from './rules';
export { loadRulesWithKueryFilter } from './rules_kuery_filter';
export { loadRuleState } from './state';
export type { LoadExecutionLogAggregationsProps } from './load_execution_log_aggregations';
export { loadExecutionLogAggregations } from './load_execution_log_aggregations';
Expand Down

0 comments on commit 57018ae

Please sign in to comment.