Skip to content

Commit

Permalink
Refactor saved query request to not set page size of 10000 (#124187) (#…
Browse files Browse the repository at this point in the history
…124901)

* Update saved query service to fix expensive query

* Fix types

(cherry picked from commit a8aa3bf)

Co-authored-by: Lukas Olson <olson.lukas@gmail.com>
  • Loading branch information
kibanamachine and lukasolson authored Feb 8, 2022
1 parent 4a1056b commit 869749a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ describe('saved query service', () => {
});
const result = await getAllSavedQueries();
expect(http.post).toBeCalled();
expect(http.post).toHaveBeenCalledWith('/api/saved_query/_find', {
body: '{"perPage":10000}',
});
expect(http.post).toHaveBeenCalledWith('/api/saved_query/_all');
expect(result).toEqual([{ attributes: savedQueryAttributes }]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export const createSavedQueryService = (http: HttpStart) => {
// we have to tell the saved objects client how many to fetch, otherwise it defaults to fetching 20 per page
const getAllSavedQueries = async (): Promise<SavedQuery[]> => {
const { savedQueries } = await http.post<{ savedQueries: SavedQuery[] }>(
'/api/saved_query/_find',
{ body: JSON.stringify({ perPage: 10000 }) }
'/api/saved_query/_all'
);
return savedQueries;
};
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/data/server/query/route_handler_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ export function registerSavedQueryRouteHandlerContext(context: RequestHandlerCon
return { total, savedQueries };
};

const getAllSavedQueries = async () => {
const finder = context.core.savedObjects.client.createPointInTimeFinder<SavedQueryAttributes>({
type: 'query',
perPage: 100,
});

const savedObjects: Array<SavedObject<SavedQueryAttributes>> = [];
for await (const response of finder.find()) {
savedObjects.push(...(response.saved_objects ?? []));
}
await finder.close();

const savedQueries = savedObjects.map(injectReferences);
return { total: savedQueries.length, savedQueries };
};

const deleteSavedQuery = (id: string) => {
return context.core.savedObjects.client.delete('query', id);
};
Expand All @@ -146,6 +162,7 @@ export function registerSavedQueryRouteHandlerContext(context: RequestHandlerCon
get: getSavedQuery,
count: getSavedQueriesCount,
find: findSavedQueries,
getAll: getAllSavedQueries,
delete: deleteSavedQuery,
};
}
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/data/server/query/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ export function registerSavedQueryRoutes({ http }: CoreSetup): void {
}
);

router.post(
{
path: `${SAVED_QUERY_PATH}/_all`,
validate: {},
},
async (context, request, response) => {
try {
const body = await context.savedQuery.getAll();
return response.ok({ body });
} catch (e) {
// TODO: Handle properly
return response.customError(e);
}
}
);

router.delete(
{
path: `${SAVED_QUERY_PATH}/{id}`,
Expand Down

0 comments on commit 869749a

Please sign in to comment.