Skip to content

Commit

Permalink
[RND-326] Adding total count from open search (#98)
Browse files Browse the repository at this point in the history
* Adding total count from open search

* Adding total-count headers to unit tests
  • Loading branch information
stevenarnold-ed-fi authored Sep 6, 2022
1 parent 920f47f commit f7bdc99
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function queryDocuments(request: QueryRequest, client: Client): Pro
Logger.debug(`Building query`, traceId);

let documents: any = [];
let recordCount: number;
try {
let query = `SELECT info FROM ${indexFromResourceInfo(resourceInfo)}`;
let whereClause: string = '';
Expand Down Expand Up @@ -99,6 +100,7 @@ export async function queryDocuments(request: QueryRequest, client: Client): Pro
Logger.debug(`meadowlark-opensearch-backend: queryDocuments executing query: ${query}`, traceId);

const { body } = await performSqlQuery(client, query);
recordCount = body.total;
Logger.debug(`Result: ${JSON.stringify(body)}`, traceId);
documents = body.datarows.map((datarow) => JSON.parse(datarow));
} catch (e) {
Expand All @@ -118,5 +120,5 @@ export async function queryDocuments(request: QueryRequest, client: Client): Pro
}
}

return { response: 'QUERY_SUCCESS', documents };
return { response: 'QUERY_SUCCESS', documents, totalCount: recordCount };
}
9 changes: 8 additions & 1 deletion Meadowlark-js/packages/meadowlark-core/src/handler/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FrontendRequest } from './FrontendRequest';
import { FrontendResponse } from './FrontendResponse';

const moduleName = 'Query';
const TOTAL_COUNT_HEADER_NAME: string = 'total-count';

const removeDisallowedQueryParameters = R.omit(['offset', 'limit', 'totalCount']);
const onlyPaginationParameters = R.pick(['offset', 'limit']);
Expand Down Expand Up @@ -54,9 +55,15 @@ export async function query(frontendRequest: FrontendRequest): Promise<FrontendR

writeDebugStatusToLog(moduleName, frontendRequest, 'query', 200);
const body = JSON.stringify(documents);

const headers = {
...frontendRequest.middleware.headerMetadata,
[TOTAL_COUNT_HEADER_NAME]: result.totalCount?.toString() ?? '0',
};

return {
body,
statusCode: 200,
headers: frontendRequest.middleware.headerMetadata,
headers,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type QueryResult = {
response: 'QUERY_SUCCESS' | 'QUERY_FAILURE_INVALID_QUERY' | 'QUERY_FAILURE_AUTHORIZATION' | 'UNKNOWN_FAILURE';
documents: Array<object>;
failureMessage?: string;
totalCount?: number;
};
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('given successful query result', () => {
let response: FrontendResponse;
let mockQueryHandler: any;
const goodResult: object = { goodResult: 'result' };
const headers: object = [{ totalCount: '1' }];

beforeAll(async () => {
mockQueryHandler = jest.spyOn(PluginLoader, 'getQueryHandler').mockReturnValue({
Expand All @@ -69,6 +70,7 @@ describe('given successful query result', () => {
Promise.resolve({
response: 'QUERY_SUCCESS',
documents: [goodResult],
Headers: headers,
} as unknown as QueryResult),
});

Expand All @@ -84,6 +86,10 @@ describe('given successful query result', () => {
expect(response.statusCode).toEqual(200);
});

it('returns total count of 1', () => {
expect(headers[0].totalCount).toEqual('1');
});

it('returns 1 result', () => {
expect(JSON.parse(response.body)).toHaveLength(1);
});
Expand Down

0 comments on commit f7bdc99

Please sign in to comment.