Skip to content

Commit

Permalink
Modified getEnvironmentUiFilterES return type from `ESFilter | undefi…
Browse files Browse the repository at this point in the history
…ned` to `ESFilter[]` for ease of use.
  • Loading branch information
ogupte committed Jul 11, 2020
1 parent f150299 commit 4bf17ee
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function createAnomalyDetectionJob({
filter: [
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ exists: { field: TRANSACTION_DURATION } },
...[getEnvironmentUiFilterES(environment)],
...getEnvironmentUiFilterES(environment),
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
import { getEnvironmentUiFilterES } from '../get_environment_ui_filter_es';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../../../common/elasticsearch_fieldnames';
import { ESFilter } from '../../../../../typings/elasticsearch';

describe('getEnvironmentUiFilterES', () => {
it('should return undefined, when environment is undefined', () => {
it('should return empty array, when environment is undefined', () => {
const uiFilterES = getEnvironmentUiFilterES();
expect(uiFilterES).toBeUndefined();
expect(uiFilterES).toHaveLength(0);
});

it('should create a filter for a service environment', () => {
const uiFilterES = getEnvironmentUiFilterES('test') as ESFilter;
expect(uiFilterES).toHaveProperty(['term', SERVICE_ENVIRONMENT], 'test');
const uiFilterES = getEnvironmentUiFilterES('test');
expect(uiFilterES).toHaveLength(1);
expect(uiFilterES[0]).toHaveProperty(['term', SERVICE_ENVIRONMENT], 'test');
});

it('should create a filter for missing service environments', () => {
const uiFilterES = getEnvironmentUiFilterES(
ENVIRONMENT_NOT_DEFINED
) as ESFilter;
expect(uiFilterES).toHaveProperty(
const uiFilterES = getEnvironmentUiFilterES(ENVIRONMENT_NOT_DEFINED);
expect(uiFilterES).toHaveLength(1);
expect(uiFilterES[0]).toHaveProperty(
['bool', 'must_not', 'exists', 'field'],
SERVICE_ENVIRONMENT
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ import { ESFilter } from '../../../../typings/elasticsearch';
import { ENVIRONMENT_NOT_DEFINED } from '../../../../common/environment_filter_values';
import { SERVICE_ENVIRONMENT } from '../../../../common/elasticsearch_fieldnames';

export function getEnvironmentUiFilterES(
environment?: string
): ESFilter | undefined {
export function getEnvironmentUiFilterES(environment?: string): ESFilter[] {
if (!environment) {
return undefined;
return [];
}

if (environment === ENVIRONMENT_NOT_DEFINED) {
return {
bool: { must_not: { exists: { field: SERVICE_ENVIRONMENT } } },
};
return [{ bool: { must_not: { exists: { field: SERVICE_ENVIRONMENT } } } }];
}
return {
term: { [SERVICE_ENVIRONMENT]: environment },
};
return [{ term: { [SERVICE_ENVIRONMENT]: environment } }];
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,19 @@ export function getUiFiltersES(uiFilters: UIFilters) {
};
}) as ESFilter[];

// remove undefined items from list
const esFilters = [
getKueryUiFilterES(uiFilters.kuery),
getEnvironmentUiFilterES(uiFilters.environment),
]
.filter((filter) => !!filter)
.concat(mappedFilters) as ESFilter[];
...getKueryUiFilterES(uiFilters.kuery),
...getEnvironmentUiFilterES(uiFilters.environment),
].concat(mappedFilters) as ESFilter[];

return esFilters;
}

function getKueryUiFilterES(kuery?: string) {
if (!kuery) {
return;
return [];
}

const ast = esKuery.fromKueryExpression(kuery);
return esKuery.toElasticsearchQuery(ast) as ESFilter;
return [esKuery.toElasticsearchQuery(ast) as ESFilter];
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ export async function getServiceMapServiceNodeInfo({
const filter: ESFilter[] = [
{ range: rangeFilter(start, end) },
{ term: { [SERVICE_NAME]: serviceName } },
...getEnvironmentUiFilterES(environment),
];

const environmentFilter = getEnvironmentUiFilterES(environment);
if (environmentFilter) {
filter.push(environmentFilter);
}

const minutes = Math.abs((end - start) / (1000 * 60));
const taskParams = { setup, minutes, filter };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ export async function getDerivedServiceAnnotations({
const filter: ESFilter[] = [
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ term: { [SERVICE_NAME]: serviceName } },
...getEnvironmentUiFilterES(environment),
];

const environmentFilter = getEnvironmentUiFilterES(environment);

if (environmentFilter) {
filter.push(environmentFilter);
}

const versions =
(
await client.search({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export async function getStoredAnnotations({
logger: Logger;
}): Promise<Annotation[]> {
try {
const environmentFilter = getEnvironmentUiFilterES(environment);

const response: ESSearchResponse<ESAnnotation, any> = (await apiCaller(
'search',
{
Expand All @@ -51,7 +49,7 @@ export async function getStoredAnnotations({
{ term: { 'annotation.type': 'deployment' } },
{ term: { tags: 'apm' } },
{ term: { [SERVICE_NAME]: serviceName } },
...(environmentFilter ? [environmentFilter] : []),
...getEnvironmentUiFilterES(environment),
],
},
},
Expand Down

0 comments on commit 4bf17ee

Please sign in to comment.