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

[Backport 2.18] Refactor: refactor contextProvider get to reduce re-fetch #367

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
26 changes: 15 additions & 11 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const GeneratePopoverBody: React.FC<{
}> = ({ incontextInsight, httpSetup, usageCollection, closePopover, getStartServices }) => {
const [summary, setSummary] = useState('');
const [insight, setInsight] = useState('');
const [contextObject, setContextObject] = useState<ContextObj | undefined>(undefined);
const [insightAvailable, setInsightAvailable] = useState(false);
const [showInsight, setShowInsight] = useState(false);
const [discoverLoading, setDiscoverLoading] = useState(false);
Expand All @@ -54,9 +55,9 @@ export const GeneratePopoverBody: React.FC<{

useEffect(() => {
const getMonitorType = async () => {
const context = await incontextInsight.contextProvider?.();
const monitorType = context?.additionalInfo?.monitorType;
const dsl = context?.additionalInfo?.dsl;
if (!contextObject) return;
const monitorType = contextObject?.additionalInfo?.monitorType;
const dsl = contextObject?.additionalInfo?.dsl;
// Only this two types from alerting contain DSL and index.
const isSupportedMonitorType =
monitorType === 'query_level_monitor' || monitorType === 'bucket_level_monitor';
Expand All @@ -77,7 +78,7 @@ export const GeneratePopoverBody: React.FC<{
setDisplayDiscoverButton(isSupportedMonitorType && hasTimeRangeFilter);
};
getMonitorType();
}, [incontextInsight, setDisplayDiscoverButton]);
}, [contextObject, setDisplayDiscoverButton]);

useEffectOnce(() => {
onGenerateSummary(
Expand All @@ -99,9 +100,11 @@ export const GeneratePopoverBody: React.FC<{
defaultMessage: 'Generate summary error',
})
);
// closePopover();
closePopover();
return;
}
// onGenerateSummary will get contextObj when mounted, use this returned value to set state to avoid re-fetch.
setContextObject(contextObj);
const contextContent = contextObj?.context || '';
const dataSourceId = contextObj?.dataSourceId;
const dataSourceQuery = dataSourceId ? { dataSourceId } : {};
Expand Down Expand Up @@ -153,12 +156,13 @@ export const GeneratePopoverBody: React.FC<{
reportMetric(usageCollection, metricAppName, 'generated', METRIC_TYPE.COUNT);
})
.catch((error) => {
console.error('Error generate summary:', error);
toasts.addDanger(
i18n.translate('assistantDashboards.incontextInsight.generateSummaryError', {
defaultMessage: 'Generate summary error',
})
);
// closePopover();
closePopover();
});
};

Expand Down Expand Up @@ -205,9 +209,9 @@ export const GeneratePopoverBody: React.FC<{
const handleNavigateToDiscover = async () => {
try {
setDiscoverLoading(true);
const context = await incontextInsight?.contextProvider?.();
const dsl = context?.additionalInfo?.dsl;
const indexName = context?.additionalInfo?.index;
if (!contextObject) return;
const dsl = contextObject?.additionalInfo?.dsl;
const indexName = contextObject?.additionalInfo?.index;
if (!dsl || !indexName) return;
const dslObject = JSON.parse(dsl);
const filters = dslObject?.query?.bool?.filter;
Expand All @@ -229,7 +233,7 @@ export const GeneratePopoverBody: React.FC<{
startDeps.data,
indexName,
timeFieldName,
context?.dataSourceId
contextObject?.dataSourceId
);
if (!indexPattern) return;
const query = await buildUrlQuery(
Expand All @@ -238,7 +242,7 @@ export const GeneratePopoverBody: React.FC<{
indexPattern,
dslObject,
timeRangeDSL,
context?.dataSourceId
contextObject?.dataSourceId
);
// Navigate to new discover with query built to populate, use new window to avoid discover search failed.
const discoverUrl = `data-explorer/discover#?${query}`;
Expand Down
Loading