Skip to content

Commit

Permalink
Remove call to find total
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 7, 2021
1 parent b6c982c commit 44bfa52
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 45 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ export const ENABLE_CASE_CONNECTOR = false;
if (ENABLE_CASE_CONNECTOR) {
SAVED_OBJECT_TYPES.push(SUB_CASE_SAVED_OBJECT);
}

export const MAX_DOCS_PER_PAGE = 10_000;
42 changes: 5 additions & 37 deletions x-pack/plugins/cases/server/services/cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
groupTotalAlertsByID,
SavedObjectFindOptionsKueryNode,
} from '../../common';
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { ENABLE_CASE_CONNECTOR, MAX_DOCS_PER_PAGE } from '../../../common/constants';
import { defaultPage, defaultPerPage } from '../../routes/api';
import {
CASE_SAVED_OBJECT,
Expand Down Expand Up @@ -818,17 +818,9 @@ export class CasesService {
});
}

const stats = await unsecuredSavedObjectsClient.find<SubCaseAttributes>({
fields: [],
page: 1,
perPage: 1,
sortField: defaultSortField,
...cloneDeep(options),
type: SUB_CASE_SAVED_OBJECT,
});
return unsecuredSavedObjectsClient.find<SubCaseAttributes>({
page: 1,
perPage: stats.total,
perPage: MAX_DOCS_PER_PAGE,
sortField: defaultSortField,
...cloneDeep(options),
type: SUB_CASE_SAVED_OBJECT,
Expand Down Expand Up @@ -903,21 +895,11 @@ export class CasesService {
...cloneDeep(options),
});
}
// get the total number of comments that are in ES then we'll grab them all in one go
const stats = await unsecuredSavedObjectsClient.find<CommentAttributes>({
type: CASE_COMMENT_SAVED_OBJECT,
fields: [],
page: 1,
perPage: 1,
sortField: defaultSortField,
// spread the options after so the caller can override the default behavior if they want
...cloneDeep(options),
});

return unsecuredSavedObjectsClient.find<CommentAttributes>({
type: CASE_COMMENT_SAVED_OBJECT,
page: 1,
perPage: stats.total,
perPage: MAX_DOCS_PER_PAGE,
sortField: defaultSortField,
...cloneDeep(options),
});
Expand Down Expand Up @@ -1020,19 +1002,12 @@ export class CasesService {
}: GetReportersArgs): Promise<SavedObjectsFindResponse<ESCaseAttributes>> {
try {
this.log.debug(`Attempting to GET all reporters`);
const firstReporters = await unsecuredSavedObjectsClient.find({
type: CASE_SAVED_OBJECT,
fields: ['created_by', OWNER_FIELD],
page: 1,
perPage: 1,
filter: cloneDeep(filter),
});

return await unsecuredSavedObjectsClient.find<ESCaseAttributes>({
type: CASE_SAVED_OBJECT,
fields: ['created_by', OWNER_FIELD],
page: 1,
perPage: firstReporters.total,
perPage: MAX_DOCS_PER_PAGE,
filter: cloneDeep(filter),
});
} catch (error) {
Expand All @@ -1047,19 +1022,12 @@ export class CasesService {
}: GetTagsArgs): Promise<SavedObjectsFindResponse<ESCaseAttributes>> {
try {
this.log.debug(`Attempting to GET all cases`);
const firstTags = await unsecuredSavedObjectsClient.find({
type: CASE_SAVED_OBJECT,
fields: ['tags', OWNER_FIELD],
page: 1,
perPage: 1,
filter: cloneDeep(filter),
});

return await unsecuredSavedObjectsClient.find<ESCaseAttributes>({
type: CASE_SAVED_OBJECT,
fields: ['tags', OWNER_FIELD],
page: 1,
perPage: firstTags.total,
perPage: MAX_DOCS_PER_PAGE,
filter: cloneDeep(filter),
});
} catch (error) {
Expand Down
10 changes: 2 additions & 8 deletions x-pack/plugins/cases/server/services/user_actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CASE_USER_ACTION_SAVED_OBJECT,
CASE_SAVED_OBJECT,
SUB_CASE_SAVED_OBJECT,
MAX_DOCS_PER_PAGE,
} from '../../../common/constants';
import { ClientArgs } from '..';

Expand All @@ -36,19 +37,12 @@ export class CaseUserActionService {
try {
const id = subCaseId ?? caseId;
const type = subCaseId ? SUB_CASE_SAVED_OBJECT : CASE_SAVED_OBJECT;
const caseUserActionInfo = await unsecuredSavedObjectsClient.find<CaseUserActionAttributes>({
type: CASE_USER_ACTION_SAVED_OBJECT,
fields: [],
hasReference: { type, id },
page: 1,
perPage: 1,
});

return await unsecuredSavedObjectsClient.find<CaseUserActionAttributes>({
type: CASE_USER_ACTION_SAVED_OBJECT,
hasReference: { type, id },
page: 1,
perPage: caseUserActionInfo.total,
perPage: MAX_DOCS_PER_PAGE,
sortField: 'action_at',
sortOrder: 'asc',
});
Expand Down

0 comments on commit 44bfa52

Please sign in to comment.