Skip to content

Commit c798812

Browse files
committed
refactor get_all_environments to minimize exports and be more consistent and clean
1 parent 94c3b48 commit c798812

File tree

15 files changed

+41
-45
lines changed

15 files changed

+41
-45
lines changed

x-pack/plugins/apm/common/environment_filter_values.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@
66

77
import { i18n } from '@kbn/i18n';
88

9-
export const ENVIRONMENT_ALL = 'ENVIRONMENT_ALL';
10-
export const ENVIRONMENT_NOT_DEFINED = 'ENVIRONMENT_NOT_DEFINED';
9+
const ENVIRONMENT_ALL_VALUE = 'ENVIRONMENT_ALL';
10+
const ENVIRONMENT_NOT_DEFINED_VALUE = 'ENVIRONMENT_NOT_DEFINED';
1111

12-
const ENVIRONMENT_ALL_LABEL = i18n.translate('xpack.apm.environment.allLabel', {
13-
defaultMessage: 'All',
14-
});
15-
const ENVIRONMENT_NOT_DEFINED_LABEL = i18n.translate(
16-
'xpack.apm.filter.environment.notDefinedLabel',
17-
{ defaultMessage: 'Not defined' }
18-
);
12+
const environmentLabels: Record<string, string> = {
13+
[ENVIRONMENT_ALL_VALUE]: i18n.translate(
14+
'xpack.apm.filter.environment.allLabel',
15+
{ defaultMessage: 'All' }
16+
),
17+
[ENVIRONMENT_NOT_DEFINED_VALUE]: i18n.translate(
18+
'xpack.apm.filter.environment.notDefinedLabel',
19+
{ defaultMessage: 'Not defined' }
20+
),
21+
};
1922

20-
export const ALL_OPTION = {
21-
value: ENVIRONMENT_ALL,
22-
text: ENVIRONMENT_ALL_LABEL,
23+
export const ENVIRONMENT_ALL = {
24+
value: ENVIRONMENT_ALL_VALUE,
25+
text: environmentLabels[ENVIRONMENT_ALL_VALUE],
2326
};
24-
export const NOT_DEFINED_OPTION = {
25-
value: ENVIRONMENT_NOT_DEFINED,
26-
text: ENVIRONMENT_NOT_DEFINED_LABEL,
27+
28+
export const ENVIRONMENT_NOT_DEFINED = {
29+
value: ENVIRONMENT_NOT_DEFINED_VALUE,
30+
text: environmentLabels[ENVIRONMENT_NOT_DEFINED_VALUE],
2731
};
2832

2933
export function getEnvironmentLabel(environment: string) {
30-
if (environment === ENVIRONMENT_ALL) {
31-
return ENVIRONMENT_ALL_LABEL;
32-
}
33-
if (environment === ENVIRONMENT_NOT_DEFINED) {
34-
return ENVIRONMENT_NOT_DEFINED_LABEL;
35-
}
36-
return environment;
34+
return environmentLabels[environment] || environment;
3735
}

x-pack/plugins/apm/public/components/shared/EnvironmentFilter/index.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import { useUrlParams } from '../../../hooks/useUrlParams';
1212
import { history } from '../../../utils/history';
1313
import { fromQuery, toQuery } from '../Links/url_helpers';
1414
import {
15-
ALL_OPTION,
16-
NOT_DEFINED_OPTION,
1715
ENVIRONMENT_ALL,
1816
ENVIRONMENT_NOT_DEFINED,
1917
} from '../../../../common/environment_filter_values';
@@ -24,7 +22,7 @@ function updateEnvironmentUrl(
2422
environment?: string
2523
) {
2624
const nextEnvironmentQueryParam =
27-
environment !== ENVIRONMENT_ALL ? environment : undefined;
25+
environment !== ENVIRONMENT_ALL.value ? environment : undefined;
2826
history.push({
2927
...location,
3028
search: fromQuery({
@@ -44,16 +42,16 @@ const SEPARATOR_OPTION = {
4442

4543
function getOptions(environments: string[]) {
4644
const environmentOptions = environments
47-
.filter((env) => env !== ENVIRONMENT_NOT_DEFINED)
45+
.filter((env) => env !== ENVIRONMENT_NOT_DEFINED.value)
4846
.map((environment) => ({
4947
value: environment,
5048
text: environment,
5149
}));
5250

5351
return [
54-
ALL_OPTION,
55-
...(environments.includes(ENVIRONMENT_NOT_DEFINED)
56-
? [NOT_DEFINED_OPTION]
52+
ENVIRONMENT_ALL,
53+
...(environments.includes(ENVIRONMENT_NOT_DEFINED.value)
54+
? [ENVIRONMENT_NOT_DEFINED]
5755
: []),
5856
...(environmentOptions.length > 0 ? [SEPARATOR_OPTION] : []),
5957
...environmentOptions,
@@ -78,7 +76,7 @@ export function EnvironmentFilter() {
7876
defaultMessage: 'environment',
7977
})}
8078
options={getOptions(environments)}
81-
value={environment || ENVIRONMENT_ALL}
79+
value={environment || ENVIRONMENT_ALL.value}
8280
onChange={(event) => {
8381
updateEnvironmentUrl(location, event.target.value);
8482
}}

x-pack/plugins/apm/public/components/shared/ErrorRateAlertTrigger/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function ErrorRateAlertTrigger(props: Props) {
4343
threshold: 25,
4444
windowSize: 1,
4545
windowUnit: 'm',
46-
environment: ENVIRONMENT_ALL,
46+
environment: ENVIRONMENT_ALL.value,
4747
};
4848

4949
const params = {

x-pack/plugins/apm/public/components/shared/TransactionDurationAlertTrigger/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function TransactionDurationAlertTrigger(props: Props) {
5858
windowSize: 5,
5959
windowUnit: 'm',
6060
transactionType: transactionTypes[0],
61-
environment: ENVIRONMENT_ALL,
61+
environment: ENVIRONMENT_ALL.value,
6262
};
6363

6464
const params = {

x-pack/plugins/apm/public/components/shared/TransactionDurationAnomalyAlertTrigger/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function TransactionDurationAnomalyAlertTrigger(props: Props) {
5252
windowUnit: 'm',
5353
transactionType: transactionTypes[0],
5454
serviceName,
55-
environment: urlParams.environment || ENVIRONMENT_ALL,
55+
environment: urlParams.environment || ENVIRONMENT_ALL.value,
5656
anomalyScore: 75,
5757
};
5858

x-pack/plugins/apm/public/hooks/useEnvironments.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import { useMemo } from 'react';
88
import { useFetcher } from './useFetcher';
99
import {
10-
ALL_OPTION,
10+
ENVIRONMENT_ALL,
1111
ENVIRONMENT_NOT_DEFINED,
1212
} from '../../common/environment_filter_values';
1313
import { callApmApi } from '../services/rest/createCallApmApi';
1414

1515
function getEnvironmentOptions(environments: string[]) {
1616
const environmentOptions = environments
17-
.filter((env) => env !== ENVIRONMENT_NOT_DEFINED)
17+
.filter((env) => env !== ENVIRONMENT_NOT_DEFINED.value)
1818
.map((environment) => ({
1919
value: environment,
2020
text: environment,
2121
}));
2222

23-
return [ALL_OPTION, ...environmentOptions];
23+
return [ENVIRONMENT_ALL, ...environmentOptions];
2424
}
2525

2626
export function useEnvironments({

x-pack/plugins/apm/server/lib/alerts/register_error_rate_alert_type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function registerErrorRateAlertType({
7575
});
7676

7777
const environmentTerm =
78-
alertParams.environment === ENVIRONMENT_ALL
78+
alertParams.environment === ENVIRONMENT_ALL.value
7979
? []
8080
: [{ term: { [SERVICE_ENVIRONMENT]: alertParams.environment } }];
8181

x-pack/plugins/apm/server/lib/alerts/register_transaction_duration_alert_type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function registerTransactionDurationAlertType({
8989
});
9090

9191
const environmentTerm =
92-
alertParams.environment === ENVIRONMENT_ALL
92+
alertParams.environment === ENVIRONMENT_ALL.value
9393
? []
9494
: [{ term: { [SERVICE_ENVIRONMENT]: alertParams.environment } }];
9595

x-pack/plugins/apm/server/lib/environments/get_all_environments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function getAllEnvironments({
4848
terms: {
4949
field: SERVICE_ENVIRONMENT,
5050
size: 100,
51-
missing: includeMissing ? ENVIRONMENT_NOT_DEFINED : undefined,
51+
missing: includeMissing ? ENVIRONMENT_NOT_DEFINED.value : undefined,
5252
},
5353
},
5454
},

x-pack/plugins/apm/server/lib/helpers/convert_ui_filters/__test__/get_environment_ui_filter_es.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('getEnvironmentUiFilterES', () => {
2121
});
2222

2323
it('should create a filter for missing service environments', () => {
24-
const uiFilterES = getEnvironmentUiFilterES(ENVIRONMENT_NOT_DEFINED);
24+
const uiFilterES = getEnvironmentUiFilterES(ENVIRONMENT_NOT_DEFINED.value);
2525
expect(uiFilterES).toHaveLength(1);
2626
expect(uiFilterES[0]).toHaveProperty(
2727
['bool', 'must_not', 'exists', 'field'],

0 commit comments

Comments
 (0)