Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Obs Alerting] Save/use active group state from url !! #197771

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion packages/kbn-alerts-grouping/src/components/alerts_grouping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ const AlertsGroupingInternal = <T extends BaseAlertsGroupAggregations>(
renderGroupPanel,
getGroupStats,
children,
initialGroupings,
onGroupingsChange,
} = props;
const { dataViews, notifications, http } = services;
const { grouping, updateGrouping } = useAlertsGroupingState(groupingId);
const { grouping, updateGrouping } = useAlertsGroupingState(groupingId, initialGroupings);

const { dataView } = useAlertsDataView({
featureIds,
Expand Down Expand Up @@ -118,6 +120,18 @@ const AlertsGroupingInternal = <T extends BaseAlertsGroupAggregations>(
onOptionsChange,
});

useEffect(() => {
Copy link
Member

@umbopepato umbopepato Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving the initialization logic to the grouping context provider?

export const AlertsGroupingContextProvider = ({
  initialState = {},
  children,
}: PropsWithChildren<{ initialState?: AlertsGroupingState }>) => {
  const [groupingState, setGroupingState] = useState<AlertsGroupingState>(initialState);
  return (
    <AlertsGroupingContext.Provider
      value={useMemo(
        () => ({ groupingState, setGroupingState }),
        [groupingState, setGroupingState]
      )}
    >
      {children}
    </AlertsGroupingContext.Provider>
  );
};

This way we could avoid the initialization effect.

In case, this is how AlertsGrouping could forward the initialState to the provider:

export const AlertsGrouping = typedMemo(
  <T extends BaseAlertsGroupAggregations>(props: AlertsGroupingProps<T>) => {
    return (
      <AlertsGroupingContextProvider
        initialState={
          props.initialGroupings
            ? { [props.groupingId]: { activeGroups: props.initialGroupings } }
            : undefined
        }
      >
        <AlertsGroupingInternal {...props} />
      </AlertsGroupingContextProvider>
    );
  }
);

if (initialGroupings) {
updateGrouping(initialGroupings);
}
}, [initialGroupings, updateGrouping]);

useEffect(() => {
onGroupingsChange?.({
activeGroups: selectedGroups,
});
}, [selectedGroups, onGroupingsChange]);

useEffect(() => {
// The `none` grouping is managed from the internal selector state
if (isNoneGroup(selectedGroups)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const AlertsGroupingContextProvider = ({ children }: PropsWithChildren<{}
);
};

export const useAlertsGroupingState = (groupingId: string) => {
export const useAlertsGroupingState = (groupingId: string, initialGroupings?: GroupModel) => {
const { groupingState, setGroupingState } = useContext(AlertsGroupingContext);
const updateGrouping = useCallback(
(groupModel: Partial<GroupModel> | null) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const useAlertsGroupingState = (groupingId: string) => {
[groupingState, groupingId]
);
return {
grouping,
grouping: initialGroupings ? { ...grouping, ...initialGroupings } : grouping,
updateGrouping,
};
};
8 changes: 8 additions & 0 deletions packages/kbn-alerts-grouping/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export interface AlertsGroupingProps<
dataViews: DataViewsServicePublic;
http: HttpSetup;
};
/**
* Initial groupings configuration
*/
initialGroupings?: GroupModel;
/**
* Callback for when the groupings change
*/
onGroupingsChange?: (groupings: GroupModel) => void;
}

export interface AlertsGroupAggregationBucket {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ interface AlertSearchBarStateTransitions {
setSavedQueryId: (
state: AlertSearchBarContainerState
) => (savedQueryId?: string) => AlertSearchBarContainerState;
setGroupings: (
state: AlertSearchBarContainerState
) => (groupings: string[]) => AlertSearchBarContainerState;
}

const DEFAULT_STATE: AlertSearchBarContainerState = {
Expand All @@ -41,6 +44,7 @@ const DEFAULT_STATE: AlertSearchBarContainerState = {
kuery: '',
status: ALL_ALERTS.status,
filters: [],
groupings: [],
};

const transitions: AlertSearchBarStateTransitions = {
Expand All @@ -50,6 +54,7 @@ const transitions: AlertSearchBarStateTransitions = {
setStatus: (state) => (status) => ({ ...state, status }),
setFilters: (state) => (filters) => ({ ...state, filters }),
setSavedQueryId: (state) => (savedQueryId) => ({ ...state, savedQueryId }),
setGroupings: (state) => (groupings) => ({ ...state, groupings }),
};

const alertSearchBarStateContainer = createStateContainer(DEFAULT_STATE, transitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const alertSearchBarState = t.partial({
t.literal(ALERT_STATUS_RECOVERED),
t.literal(ALERT_STATUS_ALL),
]),
groupings: t.array(t.string),
});

export function useAlertSearchBarStateContainer(
Expand All @@ -52,10 +53,8 @@ export function useAlertSearchBarStateContainer(

const { setRangeFrom, setRangeTo, setKuery, setStatus, setFilters, setSavedQueryId } =
stateContainer.transitions;
const { rangeFrom, rangeTo, kuery, status, filters, savedQueryId } = useContainerSelector(
stateContainer,
(state) => state
);
const { rangeFrom, rangeTo, kuery, status, filters, savedQueryId, groupings } =
useContainerSelector(stateContainer, (state) => state);

useEffect(() => {
if (!savedQuery) {
Expand Down Expand Up @@ -94,12 +93,14 @@ export function useAlertSearchBarStateContainer(
onRangeToChange: setRangeTo,
onStatusChange: setStatus,
onFiltersChange: setFilters,
onGroupingsChange: stateContainer.transitions.setGroupings,
shahzad31 marked this conversation as resolved.
Show resolved Hide resolved
filters,
rangeFrom,
rangeTo,
status,
savedQuery,
setSavedQuery,
groupings,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface AlertSearchBarContainerState {
status: AlertStatus;
filters?: Filter[];
savedQueryId?: string;
groupings?: string[];
}

interface AlertSearchBarStateTransitions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { BrushEndListener, XYBrushEvent } from '@elastic/charts';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { BoolQuery, Filter } from '@kbn/es-query';
Expand Down Expand Up @@ -132,6 +132,12 @@ function InternalAlertsPage() {
error: 0,
snoozed: 0,
});
const onGroupingsChange = useCallback(
({ activeGroups }: { activeGroups: string[] }) => {
alertSearchBarStateProps.onGroupingsChange(activeGroups);
},
[alertSearchBarStateProps]
);
const [esQuery, setEsQuery] = useState<{ bool: BoolQuery }>();
const timeBuckets = useTimeBuckets();
const bucketSize = useMemo(
Expand Down Expand Up @@ -265,6 +271,14 @@ function InternalAlertsPage() {
globalQuery={{ query: alertSearchBarStateProps.kuery, language: 'kuery' }}
groupingId={ALERTS_PAGE_ALERTS_TABLE_CONFIG_ID}
defaultGroupingOptions={DEFAULT_GROUPING_OPTIONS}
initialGroupings={
alertSearchBarStateProps?.groupings?.length
? {
activeGroups: alertSearchBarStateProps.groupings,
}
: undefined
}
onGroupingsChange={onGroupingsChange}
getAggregationsByGroupingField={getAggregationsByGroupingField}
renderGroupPanel={renderGroupPanel}
getGroupStats={getGroupStats}
Expand Down