Skip to content

Commit

Permalink
use setScreenContext instead of PoC localStorage approach
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed May 8, 2024
1 parent e4dbf4f commit f6279c7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,27 @@ export function DiscoverLayout({ stateContainer }: DiscoverLayoutProps) {
});

// The assistant is getting the state from the url correctly
// expect from the index pattern where we have only the dataview id
// except from the index pattern where we have only the dataview id.
useEffect(() => {
const indexPattern = dataView?.getIndexPattern() || null;
const indexPatternTimeField = dataView?.getTimeField()?.spec.name || null;
const dataViewId = dataView?.id;

let indexPatternText =
'The current Elasticsearch index pattern could not be identified or the current page does not make use of an Elasticsearch index pattern.';

if (indexPattern !== null && indexPatternTimeField === null) {
indexPatternText = `The current Elasticsearch index is '${indexPattern}' and it has no time field specified.`;
} else if (indexPattern !== null && indexPatternTimeField !== null) {
indexPatternText = `The current Elasticsearch index is '${indexPattern}' and its time field is '${indexPatternTimeField}'.`;
}

const dataViewIdText = dataViewId ? ` The Kibana Data View Id is ${dataViewId}.` : '';

return observabilityAIAssistant?.service.setScreenContext({
screenDescription: `The user is looking at the Discover view on the ${
isPlainRecord ? 'ES|QL' : 'dataView'
} mode. The index pattern is the ${dataView.getIndexPattern()}`,
} mode. ${indexPatternText}${dataViewIdText}`,
});
}, [dataView, isPlainRecord, observabilityAIAssistant?.service]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useLocalStorageListener } from '@kbn/ml-local-storage';
import type { AggregateQuery, Query, TimeRange } from '@kbn/es-query';
import { type DataView, DataViewType } from '@kbn/data-views-plugin/public';
import { DataViewPickerProps } from '@kbn/unified-search-plugin/public';
Expand Down Expand Up @@ -79,16 +78,6 @@ export const DiscoverTopNav = ({
const closeFieldEditor = useRef<() => void | undefined>();
const closeDataViewEditor = useRef<() => void | undefined>();

const [lsIndex, saveLsIndex] = useLocalStorageListener('obs-ai-assistant-index', null);
const [lsDataViewId, saveLsDataViewId] = useLocalStorageListener(
'obs-ai-assistant-data-view-id',
null
);
const [lsIndexTimeField, saveLsIndexTimeField] = useLocalStorageListener(
'obs-ai-assistant-index-time-field',
null
);

useEffect(() => {
return () => {
// Make sure to close the editors when unmounting
Expand Down Expand Up @@ -199,19 +188,6 @@ export const DiscoverTopNav = ({
topNavMenu,
]);

useEffect(() => {
saveLsIndex(dataView?.getIndexPattern() || null);
saveLsIndexTimeField(dataView?.getTimeField()?.spec.name || null);
saveLsDataViewId(dataView?.id);

return () => {
saveLsIndex(null);
saveLsIndexTimeField(null);
saveLsDataViewId(null);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataView]);

const savedSearchId = useSavedSearch().id;
const savedSearchHasChanged = useSavedSearchHasChanged();
const dataViewPickerProps: DataViewPickerProps = useMemo(() => {
Expand Down
1 change: 0 additions & 1 deletion x-pack/packages/ml/local_storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
*/

export { StorageContextProvider, useStorage } from './src/storage_context';
export { useLocalStorageListener } from './src/use_local_storage_listener';
63 changes: 0 additions & 63 deletions x-pack/packages/ml/local_storage/src/use_local_storage_listener.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { useEffect, useState } from 'react';
import datemath from '@elastic/datemath';
import moment from 'moment';
import { useLocalStorageListener } from '@kbn/ml-local-storage';
import { useKibana } from './use_kibana';
import { useObservabilityAIAssistantAppService } from './use_observability_ai_assistant_app_service';

Expand All @@ -23,9 +22,6 @@ export function useNavControlScreenContext() {
},
} = useKibana();

const [lsIndex] = useLocalStorageListener('obs-ai-assistant-index', null);
const [lsDataViewId] = useLocalStorageListener('obs-ai-assistant-data-view-id', null);
const [lsIndexTimeField] = useLocalStorageListener('obs-ai-assistant-index-time-field', null);
const { from, to } = data.query.timefilter.timefilter.getTime();

const [href, setHref] = useState(window.location.href);
Expand Down Expand Up @@ -66,13 +62,8 @@ export function useNavControlScreenContext() {
const start = datemath.parse(from)?.toISOString() ?? moment().subtract(1, 'day').toISOString();
const end = datemath.parse(to)?.toISOString() ?? moment().toISOString();

const index =
lsIndex !== null && lsIndexTimeField !== null
? `The current Elasticsearch index is '${lsIndex}' and its time field is '${lsIndexTimeField}'.`
: `The current Elasticsearch index could not be identified or the current page does not make use of an Elasticsearch index.`;

return service.setScreenContext({
screenDescription: `The user is looking at ${href}. The current time range is ${start} - ${end}. ${index} The Kibana Data View Id is ${lsDataViewId}.`,
screenDescription: `The user is looking at ${href}. The current time range is ${start} - ${end}.`,
});
}, [service, from, to, href, lsDataViewId, lsIndex, lsIndexTimeField]);
}, [service, from, to, href]);
}

0 comments on commit f6279c7

Please sign in to comment.