Skip to content

Commit

Permalink
Fix time filter and query related functionalt test in group 3
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
  • Loading branch information
abbyhu2000 committed Jul 4, 2023
1 parent ef57929 commit 176cddf
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const DashboardEditor = () => {
console.log('isDirty', dashboard.isDirty);

Check failure on line 93 in src/plugins/dashboard/public/application/components/dashboard_editor.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux

Unexpected console statement

Check failure on line 93 in src/plugins/dashboard/public/application/components/dashboard_editor.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Windows

Unexpected console statement
}
console.log('dashboardContainer', dashboardContainer);

Check failure on line 95 in src/plugins/dashboard/public/application/components/dashboard_editor.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux

Unexpected console statement

Check failure on line 95 in src/plugins/dashboard/public/application/components/dashboard_editor.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Windows

Unexpected console statement
console.log('indexPatterns', indexPatterns)
console.log('indexPatterns', indexPatterns);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface DashboardTopNavProps {
dashboard: Dashboard;
currentAppState: DashboardAppState;
isEmbeddableRendered: boolean;
indexPatterns: IndexPattern []
indexPatterns: IndexPattern[];
dashboardContainer?: DashboardContainer;
}

Expand All @@ -41,16 +41,14 @@ const TopNav = ({
currentAppState,
isEmbeddableRendered,
dashboardContainer,
indexPatterns
indexPatterns,
}: DashboardTopNavProps) => {
const [filters, setFilters] = useState<Filter[]>([]);
const [topNavMenu, setTopNavMenu] = useState<any>();
const [isFullScreenMode, setIsFullScreenMode] = useState<any>();

const { services } = useOpenSearchDashboards<DashboardServices>();
const { TopNavMenu } = services.navigation.ui;
const { data, dashboardConfig, setHeaderActionMenu } = services;
const { query: queryService } = data;
const { dashboardConfig, setHeaderActionMenu } = services;

const location = useLocation();
const queryParameters = new URLSearchParams(location.search);
Expand All @@ -75,10 +73,6 @@ const TopNav = ({
const shouldShowNavBarComponent = (forceShow: boolean): boolean =>
(forceShow || isChromeVisible) && !currentAppState?.fullScreenMode;

useEffect(() => {
setFilters(queryService.filterManager.getFilters());
}, [services, queryService]);

useEffect(() => {
if (isEmbeddableRendered) {
const navActions = getNavActions(
Expand Down Expand Up @@ -128,7 +122,6 @@ const TopNav = ({
return (
<TopNavMenu
appName={'dashboard'}
savedQueryId={currentAppState?.savedQuery}
config={showTopNavMenu ? topNavMenu : undefined}
className={isFullScreenMode ? 'osdTopNavMenu-isFullScreen' : undefined}
screenTitle={currentAppState.title}
Expand All @@ -141,7 +134,10 @@ const TopNav = ({
indexPatterns={indexPatterns}
showSaveQuery={services.dashboardCapabilities.saveQuery as boolean}
savedQuery={undefined}
onSavedQueryIdChange={() => {}}
onSavedQueryIdChange={(savedQueryId?: string) => {
stateContainer.transitions.set('savedQuery', savedQueryId);
}}
savedQueryId={currentAppState?.savedQuery}
onQuerySubmit={handleRefresh}
setMenuMountPoint={isEmbeddedExternally ? undefined : setHeaderActionMenu}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { SavedObjectSaveOpts } from 'src/plugins/saved_objects/public';
import { updateSavedDashboard } from './update_saved_dashboard';

import { DashboardAppStateContainer } from '../../types';
import { Dashboard } from '../../dashboard';

/**
* Saves the dashboard.
Expand All @@ -43,11 +44,12 @@ export function saveDashboard(
timeFilter: TimefilterContract,
stateContainer: DashboardAppStateContainer,
savedDashboard: any,
saveOptions: SavedObjectSaveOpts
saveOptions: SavedObjectSaveOpts,
dashboard: Dashboard
): Promise<string> {
const appState = stateContainer.getState();

updateSavedDashboard(savedDashboard, appState, timeFilter);
updateSavedDashboard(savedDashboard, appState, timeFilter, dashboard);

return savedDashboard.save(saveOptions).then((id: string) => {
if (id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,37 @@ import { FilterUtils } from './filter_utils';
import { SavedObjectDashboard } from '../../saved_dashboards';
import { DashboardAppState } from '../../types';
import { opensearchFilters } from '../../../../data/public';
import { Dashboard } from '../../dashboard';

export function updateSavedDashboard(
savedDashboard: SavedObjectDashboard,
appState: DashboardAppState,
timeFilter: TimefilterContract
timeFilter: TimefilterContract,
dashboard: Dashboard
) {
savedDashboard.title = appState.title;
savedDashboard.description = appState.description;
savedDashboard.timeRestore = appState.timeRestore;
savedDashboard.panelsJSON = JSON.stringify(appState.panels);
savedDashboard.optionsJSON = JSON.stringify(appState.options);

savedDashboard.timeFrom = savedDashboard.timeRestore
const timeFrom = savedDashboard.timeRestore
? FilterUtils.convertTimeToUTCString(timeFilter.getTime().from)
: undefined;
savedDashboard.timeTo = savedDashboard.timeRestore
const timeTo = savedDashboard.timeRestore
? FilterUtils.convertTimeToUTCString(timeFilter.getTime().to)
: undefined;

const timeRestoreObj: RefreshInterval = _.pick(timeFilter.getRefreshInterval(), [
'display',
'pause',
'section',
'value',
]) as RefreshInterval;
savedDashboard.refreshInterval = savedDashboard.timeRestore ? timeRestoreObj : undefined;
const refreshInterval = savedDashboard.timeRestore ? timeRestoreObj : undefined;
savedDashboard.timeFrom = timeFrom;
savedDashboard.timeTo = timeTo;
savedDashboard.refreshInterval = refreshInterval;

// save only unpinned filters
const unpinnedFilters = appState.filters.filter(
Expand All @@ -68,4 +74,17 @@ export function updateSavedDashboard(

// save the queries
savedDashboard.searchSource.setField('query', appState.query as Query);

dashboard.setState({
title: appState.title,
description: appState.description,
timeRestore: appState.timeRestore,
panels: appState.panels,
options: appState.options,
timeFrom,
timeTo,
refreshInterval,
query: appState.query as Query,
filters: unpinnedFilters,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,13 @@ export const getNavActions = (
async function save(saveOptions: SavedObjectSaveOpts) {
const timefilter = queryService.timefilter.timefilter;
try {
const id = await saveDashboard(timefilter, stateContainer, savedDashboard, saveOptions);
const id = await saveDashboard(
timefilter,
stateContainer,
savedDashboard,
saveOptions,
dashboard
);

if (id) {
notifications.toasts.addSuccess({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const createDashboardEmbeddable = (
let dashboardContainer: DashboardContainer;
let inputSubscription: Subscription | undefined;
let outputSubscription: Subscription | undefined;
let panelIndexPatterns: IndexPattern[] = [];

const {
embeddable,
Expand Down Expand Up @@ -254,8 +253,8 @@ const createDashboardEmbeddable = (
panelIndexPatterns.push(...embeddableIndexPatterns);
});
panelIndexPatterns = uniqBy(panelIndexPatterns, 'id');
return panelIndexPatterns
}
return panelIndexPatterns;
};

const updateIndexPatternsOperator = pipe(
filter((container: DashboardContainer) => !!container && !isErrorEmbeddable(container)),
Expand All @@ -270,22 +269,19 @@ const createDashboardEmbeddable = (
switchMap((panelIndexPatterns: IndexPattern[]) => {
return new Observable((observer) => {
if (panelIndexPatterns && panelIndexPatterns.length > 0) {

if (observer.closed) return;
setIndexPatterns(panelIndexPatterns)
observer.complete();

if (observer.closed) return;
setIndexPatterns(panelIndexPatterns);
observer.complete();
} else {
data.indexPatterns.getDefault().then((defaultIndexPattern) => {
if (observer.closed) return;
setIndexPatterns([defaultIndexPattern as IndexPattern])
setIndexPatterns([defaultIndexPattern as IndexPattern]);
observer.complete();
})
});
}
})
})
)

});
})
);

if (dashboardFactory) {
return dashboardFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export const useEditorUpdates = (
if (changes) {
dashboardContainer.updateInput(changes);

if (changes.filters || changes.query || changes.timeRange || changes.refreshConfig) {
if (changes.timeRange || changes.refreshConfig) {
if (dashboardInstance.timeRestore) {
dashboard.isDirty = true;
}
}

if (changes.filters || changes.query) {
dashboard.isDirty = true;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/plugins/dashboard/public/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Dashboard<TDashboardParams = DashboardParams> {
this.filters = cloneDeep(dashboardState.filters);
}

async setState(state: PartialDashboardState) {
setState(state: PartialDashboardState) {
if (state.id) {
this.id = state.id;
}
Expand Down Expand Up @@ -109,12 +109,12 @@ export class Dashboard<TDashboardParams = DashboardParams> {
if (state.searchSource) {
this.searchSource = state.searchSource;
}
// if (state.query) {
// this.query = this.getQuery(state.query);
// }
// if (state.filters) {
// this.filters = this.getFilters(state.filters);
// }
if (state.query) {
this.query = state.query;
}
if (state.filters) {
this.filters = state.filters;
}
}

public setIsDirty(value: boolean) {
Expand Down
111 changes: 0 additions & 111 deletions src/plugins/dashboard/public/saved_dashboards/_saved_dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,114 +39,3 @@ export const convertToSerializedDashboard = (
filters: savedDashboard.getFilters(),
};
};

/* export const convertFromSerializedDashboard = (
serializedDashboard: SerializedDashboard
): ISavedDashboard => {
const {
id,
timeRestore,
timeTo,
timeFrom,
refreshInterval,
description,
panels,
options,
uiState,
lastSavedTitle,
searchSource,
query,
filters,
} = serializedDashboard;
return {
id,
timeRestore,
timeTo,
timeFrom,
description,
panelsJSON: JSON.stringify(panels),
optionsJSON: JSON.stringify(options),
uiStateJSON: JSON.stringify(uiState),
lastSavedTitle,
refreshInterval,
searchSource,
getQuery: () => query,
getFilters: () => filters,
};
};
export function createSavedDashboardClass(
services: SavedObjectOpenSearchDashboardsServices
): new (id: string) => SavedObjectDashboard {
const SavedObjectClass = createSavedObjectClass(services);
class SavedDashboard extends SavedObjectClass {
public static type = 'dashboard';
public static mapping: Record<string, any> = {
title: 'text',
hits: 'integer',
description: 'text',
panelsJSON: 'text',
optionsJSON: 'text',
version: 'integer',
timeRestore: 'boolean',
timeTo: 'keyword',
timeFrom: 'keyword',
refreshInterval: {
type: 'object',
properties: {
display: { type: 'keyword' },
pause: { type: 'boolean' },
section: { type: 'integer' },
value: { type: 'integer' },
},
},
};
// Order these fields to the top, the rest are alphabetical
public static fieldOrder = ['title', 'description'];
public static searchSource = true;
public showInRecentlyAccessed = true;
constructor(id: string) {
super({
type: SavedDashboard.type,
mapping: SavedDashboard.mapping,
searchSource: SavedDashboard.searchSource,
extractReferences,
injectReferences,
// if this is null/undefined then the SavedObject will be assigned the defaults
id,
// default values that will get assigned if the doc is new
defaults: {
title: '',
hits: 0,
description: '',
panelsJSON: '[]',
optionsJSON: JSON.stringify({
// for BWC reasons we can't default dashboards that already exist without this setting to true.
useMargins: !id,
hidePanelTitles: false,
}),
version: 1,
timeRestore: false,
timeTo: undefined,
timeFrom: undefined,
refreshInterval: undefined,
},
});
this.getFullPath = () => `/app/dashboardsNew#${createDashboardEditUrl(String(this.id))}`;
}
getQuery() {
return this.searchSource!.getOwnField('query') || { query: '', language: 'kuery' };
}
getFilters() {
return this.searchSource!.getOwnField('filter') || [];
}
}
return (SavedDashboard as unknown) as new (id: string) => SavedObjectDashboard;
}*/

0 comments on commit 176cddf

Please sign in to comment.