Skip to content

Commit

Permalink
Pass on and use global search/filters to embeddable.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Oct 11, 2024
1 parent 6bb2d66 commit 2c24dbd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 React, { useMemo, type FC } from 'react';

import datemath from '@elastic/datemath';

import type { TimeRange } from '@kbn/es-query';

import { createMergedEsQuery } from '../../application/utils/search_utils';
import { useAiopsAppContext } from '../../hooks/use_aiops_app_context';
import { useFilterQueryUpdates } from '../../hooks/use_filters_query';
import { useSearch } from '../../hooks/use_search';
import { useDataSource } from '../../hooks/use_data_source';
import { getDefaultLogRateAnalysisAppState } from '../../application/url_state/log_rate_analysis';

import { LogRateAnalysisDocumentCountChartData } from './log_rate_analysis_content/log_rate_analysis_document_count_chart_data';
import { LogRateAnalysisContent } from './log_rate_analysis_content/log_rate_analysis_content';

export interface LogRateAnalysisForEmbeddableProps {
timeRange: TimeRange;
}

export const LogRateAnalysisForEmbeddable: FC<LogRateAnalysisForEmbeddableProps> = ({
timeRange,
}) => {
const { uiSettings } = useAiopsAppContext();
const { dataView } = useDataSource();
const { filters, query } = useFilterQueryUpdates();
const appState = getDefaultLogRateAnalysisAppState({
searchQuery: createMergedEsQuery(query, filters, dataView, uiSettings),
filters,
});
const { searchQuery } = useSearch({ dataView, savedSearch: null }, appState, true);

const timeRangeParsed = useMemo(() => {
if (timeRange) {
const min = datemath.parse(timeRange.from);
const max = datemath.parse(timeRange.to);
if (min && max) {
return { min, max };
}
}
}, [timeRange]);

return (
<>
<LogRateAnalysisDocumentCountChartData
timeRange={timeRangeParsed}
esSearchQuery={searchQuery}
/>
<LogRateAnalysisContent esSearchQuery={searchQuery} />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import type { Observable } from 'rxjs';
import { BehaviorSubject, combineLatest, distinctUntilChanged, map } from 'rxjs';
import { createBrowserHistory } from 'history';

import datemath from '@elastic/datemath';

import { UrlStateProvider } from '@kbn/ml-url-state';
import { Router } from '@kbn/shared-ux-router';
import { AIOPS_EMBEDDABLE_ORIGIN } from '@kbn/aiops-common/constants';
Expand All @@ -27,10 +25,10 @@ import type { SignificantItem } from '@kbn/ml-agg-utils';
import { AiopsAppContext, type AiopsAppContextValue } from '../hooks/use_aiops_app_context';
import { DataSourceContextProvider } from '../hooks/use_data_source';
import { ReloadContextProvider } from '../hooks/use_reload';
import { FilterQueryContextProvider } from '../hooks/use_filters_query';
import type { AiopsPluginStartDeps } from '../types';

import { LogRateAnalysisDocumentCountChartData } from '../components/log_rate_analysis/log_rate_analysis_content/log_rate_analysis_document_count_chart_data';
import { LogRateAnalysisContent } from '../components/log_rate_analysis/log_rate_analysis_content/log_rate_analysis_content';
import { LogRateAnalysisForEmbeddable } from '../components/log_rate_analysis/log_rate_analysis_for_embeddable';

/**
* Only used to initialize internally
Expand Down Expand Up @@ -124,16 +122,6 @@ const LogRateAnalysisEmbeddableWrapperWithDeps: FC<LogRateAnalysisPropsWithDeps>

const history = createBrowserHistory();

const timeRangeParsed = useMemo(() => {
if (timeRange) {
const min = datemath.parse(timeRange.from);
const max = datemath.parse(timeRange.to);
if (min && max) {
return { min, max };
}
}
}, [timeRange]);

// We use the following pattern to track changes of dataViewId, and if there's
// a change, we unmount and remount the complete inner component. This makes
// sure the component is reinitialized correctly when the options of the
Expand Down Expand Up @@ -165,19 +153,20 @@ const LogRateAnalysisEmbeddableWrapperWithDeps: FC<LogRateAnalysisPropsWithDeps>
<Router history={history}>
<ReloadContextProvider reload$={resultObservable$}>
<AiopsAppContext.Provider value={aiopsAppContextValue}>
<UrlStateProvider>
<DataSourceContextProvider
dataViews={pluginStart.data.dataViews}
dataViewId={dataViewId}
>
<LogRateAnalysisReduxProvider>
<DatePickerContextProvider {...datePickerDeps}>
<LogRateAnalysisDocumentCountChartData timeRange={timeRangeParsed} />
<LogRateAnalysisContent />
</DatePickerContextProvider>
</LogRateAnalysisReduxProvider>
</DataSourceContextProvider>
</UrlStateProvider>
<DatePickerContextProvider {...datePickerDeps}>
<UrlStateProvider>
<DataSourceContextProvider
dataViews={pluginStart.data.dataViews}
dataViewId={dataViewId}
>
<FilterQueryContextProvider timeRange={timeRange}>
<LogRateAnalysisReduxProvider>
<LogRateAnalysisForEmbeddable timeRange={timeRange} />
</LogRateAnalysisReduxProvider>
</FilterQueryContextProvider>
</DataSourceContextProvider>
</UrlStateProvider>
</DatePickerContextProvider>
</AiopsAppContext.Provider>
</ReloadContextProvider>
</Router>
Expand Down

0 comments on commit 2c24dbd

Please sign in to comment.