Skip to content

Commit

Permalink
Replace hardcoded saved object type names with constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Apr 23, 2020
1 parent b6b658e commit 0a8d0d3
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 43 deletions.
8 changes: 5 additions & 3 deletions x-pack/plugins/ingest_manager/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import {
AGENT_TYPE_TEMPORARY,
AGENT_POLLING_THRESHOLD_MS,
AGENT_TYPE_PERMANENT,
AGENT_SAVED_OBJECT_TYPE,
} from '../constants';

export function buildKueryForOnlineAgents() {
return `agents.last_checkin >= now-${(3 * AGENT_POLLING_THRESHOLD_MS) / 1000}s`;
return `${AGENT_SAVED_OBJECT_TYPE}.last_checkin >= now-${(3 * AGENT_POLLING_THRESHOLD_MS) /
1000}s`;
}

export function buildKueryForOfflineAgents() {
return `agents.type:${AGENT_TYPE_TEMPORARY} AND agents.last_checkin < now-${(3 *
return `${AGENT_SAVED_OBJECT_TYPE}.type:${AGENT_TYPE_TEMPORARY} AND ${AGENT_SAVED_OBJECT_TYPE}.last_checkin < now-${(3 *
AGENT_POLLING_THRESHOLD_MS) /
1000}s`;
}

export function buildKueryForErrorAgents() {
return `agents.type:${AGENT_TYPE_PERMANENT} AND agents.last_checkin < now-${(4 *
return `${AGENT_SAVED_OBJECT_TYPE}.type:${AGENT_TYPE_PERMANENT} AND ${AGENT_SAVED_OBJECT_TYPE}.last_checkin < now-${(4 *
AGENT_POLLING_THRESHOLD_MS) /
1000}s`;
}
2 changes: 1 addition & 1 deletion x-pack/plugins/ingest_manager/dev_docs/api/agents_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
## Example

```js
GET /api/ingest_manager/fleet/agents?kuery=agents.last_checkin:2019-10-01T13:42:54.323Z
GET /api/ingest_manager/fleet/agents?kuery=fleet-agents.last_checkin:2019-10-01T13:42:54.323Z
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import React, { useState, useEffect } from 'react';
import { IFieldType } from 'src/plugins/data/public';
// @ts-ignore
import { EuiSuggest, EuiSuggestItemProps } from '@elastic/eui';
import { useDebounce } from '../hooks';
import { useStartDeps } from '../hooks/use_deps';
import { INDEX_NAME } from '../constants';
import { useDebounce, useStartDeps } from '../hooks';
import { INDEX_NAME, AGENT_SAVED_OBJECT_TYPE } from '../constants';

const DEBOUNCE_SEARCH_MS = 150;
const HIDDEN_FIELDS = ['agents.actions'];
const HIDDEN_FIELDS = [`${AGENT_SAVED_OBJECT_TYPE}.actions`];

interface Suggestion {
label: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export { PLUGIN_ID, EPM_API_ROUTES, AGENT_CONFIG_SAVED_OBJECT_TYPE } from '../../../../common';
export {
PLUGIN_ID,
EPM_API_ROUTES,
AGENT_API_ROUTES,
AGENT_CONFIG_SAVED_OBJECT_TYPE,
AGENT_EVENT_SAVED_OBJECT_TYPE,
AGENT_SAVED_OBJECT_TYPE,
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
DATASOURCE_SAVED_OBJECT_TYPE,
} from '../../../../common';

export const BASE_PATH = '/app/ingestManager';
export const EPM_PATH = '/epm';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, { Fragment, useRef, useState } from 'react';
import { EuiConfirmModal, EuiOverlayMask } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { AGENT_SAVED_OBJECT_TYPE } from '../../../constants';
import { sendDeleteAgentConfigs, useCore, sendRequest } from '../../../hooks';

interface Props {
Expand Down Expand Up @@ -122,7 +123,7 @@ export const AgentConfigDeleteProvider: React.FunctionComponent<Props> = ({ chil
path: `/api/ingest_manager/fleet/agents`,
method: 'get',
query: {
kuery: `agents.config_id : (${agentConfigsToCheck.join(' or ')})`,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id : (${agentConfigsToCheck.join(' or ')})`,
},
});
setAgentsCount(data?.total || 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { EuiCallOut, EuiConfirmModal, EuiOverlayMask, EuiSpacer } from '@elastic
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { useCore, sendRequest, sendDeleteDatasource, useConfig } from '../../../hooks';
import { AGENT_API_ROUTES } from '../../../../../../common/constants';
import { AgentConfig } from '../../../../../../common/types/models';
import { AGENT_API_ROUTES, AGENT_SAVED_OBJECT_TYPE } from '../../../constants';
import { AgentConfig } from '../../../types';

interface Props {
agentConfig: AgentConfig;
Expand Down Expand Up @@ -51,7 +51,7 @@ export const DatasourceDeleteProvider: React.FunctionComponent<Props> = ({
query: {
page: 1,
perPage: 1,
kuery: `agents.config_id : ${agentConfig.id}`,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id : ${agentConfig.id}`,
},
});
setAgentsCount(data?.total || 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import React from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiTitle, EuiFlexGroup, EuiFlexItem, EuiText } from '@elastic/eui';
import { EuiSpacer } from '@elastic/eui';
import { useGetPackageInstallStatus } from '../../hooks';
import { InstallStatus, PackageInfo } from '../../../../types';
import { InstallationButton } from './installation_button';
import { useGetDatasources } from '../../../../hooks';
import { DATASOURCE_SAVED_OBJECT_TYPE } from '../../../../constants';
import { useGetPackageInstallStatus } from '../../hooks';
import { InstallationButton } from './installation_button';

const NoteLabel = () => (
<FormattedMessage
Expand All @@ -26,7 +27,7 @@ export const SettingsPanel = (
const { data: datasourcesData } = useGetDatasources({
perPage: 0,
page: 1,
kuery: `datasources.package.name:${props.name}`,
kuery: `${DATASOURCE_SAVED_OBJECT_TYPE}.package.name:${props.name}`,
});
const { name, title, removable } = props;
const packageInstallStatus = getPackageInstallStatus(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage, FormattedTime } from '@kbn/i18n/react';
import { AGENT_EVENT_SAVED_OBJECT_TYPE } from '../../../../types';
import { Agent, AgentEvent } from '../../../../types';
import { usePagination, useGetOneAgentEvents } from '../../../../hooks';
import { SearchBar } from '../../../../components/search_bar';
Expand Down Expand Up @@ -130,7 +131,11 @@ export const AgentEventsTable: React.FunctionComponent<{ agent: Agent }> = ({ ag
<EuiSpacer size="l" />
<EuiFlexGroup>
<EuiFlexItem>
<SearchBar value={search} onChange={setSearch} fieldPrefix={'agent_events'} />
<SearchBar
value={search}
onChange={setSearch}
fieldPrefix={AGENT_EVENT_SAVED_OBJECT_TYPE}
/>
</EuiFlexItem>
<EuiFlexItem grow={null}>
<EuiButton color="secondary" iconType="refresh" onClick={onClickRefresh}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import { SearchBar } from '../../../components/search_bar';
import { AgentHealth } from '../components/agent_health';
import { AgentUnenrollProvider } from '../components/agent_unenroll_provider';
import { AgentStatusKueryHelper } from '../../../services';
import { FLEET_AGENT_DETAIL_PATH, AGENT_CONFIG_DETAILS_PATH } from '../../../constants';
import {
FLEET_AGENT_DETAIL_PATH,
AGENT_CONFIG_DETAILS_PATH,
AGENT_SAVED_OBJECT_TYPE,
} from '../../../constants';

const NO_WRAP_TRUNCATE_STYLE: CSSProperties = Object.freeze({
overflow: 'hidden',
Expand Down Expand Up @@ -180,7 +184,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
if (kuery) {
kuery = `(${kuery}) and`;
}
kuery = `${kuery} agents.config_id : (${selectedConfigs
kuery = `${kuery} ${AGENT_SAVED_OBJECT_TYPE}.config_id : (${selectedConfigs
.map(config => `"${config}"`)
.join(' or ')})`;
}
Expand Down Expand Up @@ -394,7 +398,7 @@ export const AgentListPage: React.FunctionComponent<{}> = () => {
});
setSearch(newSearch);
}}
fieldPrefix="agents"
fieldPrefix={AGENT_SAVED_OBJECT_TYPE}
/>
</EuiFlexItem>
<EuiFlexItem grow={2}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EuiText,
} from '@elastic/eui';
import { FormattedMessage, FormattedDate } from '@kbn/i18n/react';
import { ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE } from '../../../constants';
import {
usePagination,
useGetEnrollmentAPIKeys,
Expand All @@ -29,7 +30,6 @@ import {
import { EnrollmentAPIKey } from '../../../types';
import { SearchBar } from '../../../components/search_bar';
import { NewEnrollmentTokenFlyout } from './components/new_enrollment_key_flyout';
import {} from '@elastic/eui';
import { ConfirmEnrollmentTokenDelete } from './components/confirm_delete_modal';

const NO_WRAP_TRUNCATE_STYLE: CSSProperties = Object.freeze({
Expand Down Expand Up @@ -251,7 +251,7 @@ export const EnrollmentTokenListPage: React.FunctionComponent<{}> = () => {
});
setSearch(newSearch);
}}
fieldPrefix="enrollment_api_keys"
fieldPrefix={ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RequestHandler } from 'src/core/server';
import bluebird from 'bluebird';
import { appContextService, agentConfigService, datasourceService } from '../../services';
import { listAgents } from '../../services/agents';
import { AGENT_SAVED_OBJECT_TYPE } from '../../constants';
import {
GetAgentConfigsRequestSchema,
GetOneAgentConfigRequestSchema,
Expand Down Expand Up @@ -50,7 +51,7 @@ export const getAgentConfigsHandler: RequestHandler<
showInactive: true,
perPage: 0,
page: 1,
kuery: `agents.config_id:${agentConfig.id}`,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id:${agentConfig.id}`,
}).then(({ total: agentTotal }) => (agentConfig.agents = agentTotal)),
{ concurrency: 10 }
);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ingest_manager/server/services/agent_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AgentConfigService {
public async ensureDefaultAgentConfig(soClient: SavedObjectsClientContract) {
const configs = await soClient.find<AgentConfig>({
type: AGENT_CONFIG_SAVED_OBJECT_TYPE,
filter: 'agent_configs.attributes.is_default:true',
filter: `${AGENT_CONFIG_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});

if (configs.total === 0) {
Expand Down Expand Up @@ -244,7 +244,7 @@ class AgentConfigService {
public async getDefaultAgentConfigId(soClient: SavedObjectsClientContract) {
const configs = await soClient.find({
type: AGENT_CONFIG_SAVED_OBJECT_TYPE,
filter: 'agent_configs.attributes.is_default:true',
filter: `${AGENT_CONFIG_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});

if (configs.saved_objects.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
AgentActionSOAttributes,
AgentEvent,
} from '../../../common/types/models';
import { AGENT_TYPE_PERMANENT } from '../../../common/constants';
import { AGENT_TYPE_PERMANENT, AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { acknowledgeAgentActions } from './acks';
import { appContextService } from '../app_context';
import { IngestManagerAppContext } from '../../plugin';
Expand All @@ -31,7 +31,7 @@ describe('test agent acks services', () => {
Promise.resolve({
id: 'action1',
references: [],
type: 'agent_actions',
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
agent_id: 'id',
Expand All @@ -48,7 +48,7 @@ describe('test agent acks services', () => {
{
id: 'action1',
references: [],
type: 'agent_actions',
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
agent_id: 'id',
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('test agent acks services', () => {
{
id: 'action1',
references: [],
type: 'agent_actions',
type: AGENT_ACTION_SAVED_OBJECT_TYPE,
attributes: {
type: 'CONFIG_CHANGE',
agent_id: 'id',
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/ingest_manager/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ export async function listAgents(

if (kuery && kuery !== '') {
// To ensure users dont need to know about SO data structure...
filters.push(kuery.replace(/agents\./g, 'agents.attributes.'));
filters.push(
kuery.replace(
new RegExp(`${AGENT_SAVED_OBJECT_TYPE}\.`, 'g'),
`${AGENT_SAVED_OBJECT_TYPE}.attributes.`
)
);
}

if (showInactive === false) {
const agentActiveCondition = `agents.attributes.active:true AND not agents.attributes.type:${AGENT_TYPE_EPHEMERAL}`;
const recentlySeenEphemeralAgent = `agents.attributes.active:true AND agents.attributes.type:${AGENT_TYPE_EPHEMERAL} AND agents.attributes.last_checkin > ${Date.now() -
const agentActiveCondition = `${AGENT_SAVED_OBJECT_TYPE}.attributes.active:true AND not ${AGENT_SAVED_OBJECT_TYPE}.attributes.type:${AGENT_TYPE_EPHEMERAL}`;
const recentlySeenEphemeralAgent = `${AGENT_SAVED_OBJECT_TYPE}.attributes.active:true AND ${AGENT_SAVED_OBJECT_TYPE}.attributes.type:${AGENT_TYPE_EPHEMERAL} AND ${AGENT_SAVED_OBJECT_TYPE}.attributes.last_checkin > ${Date.now() -
3 * AGENT_POLLING_THRESHOLD_MS}`;
filters.push(`(${agentActiveCondition}) OR (${recentlySeenEphemeralAgent})`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export async function getAgentEvents(
type: AGENT_EVENT_SAVED_OBJECT_TYPE,
filter:
kuery && kuery !== ''
? kuery.replace(/agent_events\./g, 'agent_events.attributes.')
? kuery.replace(
new RegExp(`${AGENT_EVENT_SAVED_OBJECT_TYPE}\.`, 'g'),
`${AGENT_EVENT_SAVED_OBJECT_TYPE}.attributes.`
)
: undefined,
perPage,
page,
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/ingest_manager/server/services/agents/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { SavedObjectsClientContract } from 'src/core/server';
import { getAgent, listAgents } from './crud';
import { AGENT_EVENT_SAVED_OBJECT_TYPE } from '../../constants';
import { AGENT_EVENT_SAVED_OBJECT_TYPE, AGENT_SAVED_OBJECT_TYPE } from '../../constants';
import { AgentStatus, Agent } from '../../types';

import {
Expand Down Expand Up @@ -72,8 +72,8 @@ export async function getAgentStatusForConfig(
page: 1,
kuery: configId
? kuery
? `(${kuery}) and (agents.config_id:"${configId}")`
: `agents.config_id:"${configId}"`
? `(${kuery}) and (${AGENT_SAVED_OBJECT_TYPE}.config_id:"${configId}")`
: `${AGENT_SAVED_OBJECT_TYPE}.config_id:"${configId}"`
: kuery,
})
)
Expand All @@ -91,7 +91,9 @@ export async function getAgentStatusForConfig(
async function getEventsCount(soClient: SavedObjectsClientContract, configId?: string) {
const { total } = await soClient.find({
type: AGENT_EVENT_SAVED_OBJECT_TYPE,
filter: configId ? `agent_events.attributes.config_id:"${configId}"` : undefined,
filter: configId
? `${AGENT_EVENT_SAVED_OBJECT_TYPE}.attributes.config_id:"${configId}"`
: undefined,
perPage: 0,
page: 1,
sortField: 'timestamp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function updateAgentsForConfigId(
let page = 1;
while (hasMore) {
const { agents } = await listAgents(soClient, {
kuery: `agents.config_id:"${configId}"`,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id:"${configId}"`,
page: page++,
perPage: 1000,
showInactive: true,
Expand All @@ -46,7 +46,7 @@ export async function unenrollForConfigId(soClient: SavedObjectsClientContract,
let page = 1;
while (hasMore) {
const { agents } = await listAgents(soClient, {
kuery: `agents.config_id:"${configId}"`,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.config_id:"${configId}"`,
page: page++,
perPage: 1000,
showInactive: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export async function listEnrollmentApiKeys(
sortOrder: 'DESC',
filter:
kuery && kuery !== ''
? kuery.replace(/enrollment_api_keys\./g, 'enrollment_api_keys.attributes.')
? kuery.replace(
new RegExp(`${ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE}\.`, 'g'),
`${ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE}.attributes.`
)
: undefined,
});

Expand Down Expand Up @@ -80,7 +83,7 @@ export async function deleteEnrollmentApiKeyForConfigId(
const { items } = await listEnrollmentApiKeys(soClient, {
page: page++,
perPage: 100,
kuery: `enrollment_api_keys.config_id:${configId}`,
kuery: `${ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE}.config_id:${configId}`,
});

if (items.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ingest_manager/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OutputService {
public async ensureDefaultOutput(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find<Output>({
type: OUTPUT_SAVED_OBJECT_TYPE,
filter: 'outputs.attributes.is_default:true',
filter: `${OUTPUT_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});

if (!outputs.saved_objects.length) {
Expand Down Expand Up @@ -44,7 +44,7 @@ class OutputService {
public async getDefaultOutputId(soClient: SavedObjectsClientContract) {
const outputs = await soClient.find({
type: OUTPUT_SAVED_OBJECT_TYPE,
filter: 'outputs.attributes.is_default:true',
filter: `${OUTPUT_SAVED_OBJECT_TYPE}.attributes.is_default:true`,
});

if (!outputs.saved_objects.length) {
Expand Down

0 comments on commit 0a8d0d3

Please sign in to comment.