-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Logs+] Implement Data Source APIs #156413
Changes from 8 commits
2ee74e5
93f6be6
ece2f3e
f813c19
d0af7ed
52fc67a
8cf8a1e
ed121b5
a6a2cd1
23ad878
04753ed
7ebe01c
77e1869
19f3ec5
5c5e344
c4123d5
bb0b52b
66759b9
e16e998
447e26d
edc0979
d17e7a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ export interface DataStream { | |
serviceName: string; | ||
} | null; | ||
} | ||
|
||
export type PackageDataStreamTypes = 'logs' | 'metrics' | 'traces' | 'synthetics' | 'profiling'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly not the best naming, or location? But when looking at other Fleet code I could only really find the |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
* 2.0. | ||
*/ | ||
|
||
import type { SortResults } from '@elastic/elasticsearch/lib/api/types'; | ||
|
||
import type { | ||
AssetReference, | ||
CategorySummaryList, | ||
|
@@ -13,6 +15,7 @@ import type { | |
PackageUsageStats, | ||
InstallType, | ||
InstallSource, | ||
EpmPackageInstallStatus, | ||
} from '../models/epm'; | ||
|
||
export interface GetCategoriesRequest { | ||
|
@@ -46,6 +49,26 @@ export interface GetPackagesResponse { | |
response?: PackageList; | ||
} | ||
|
||
interface InstalledPackage { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fairly minimal, and doesn't necessarily align with the advice given, r.e.:
Extra properties would have required extra queries to other locations (e.g. the registry), which is why this is so minimal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense to keep the API response minimal to avoid extra queries, we can add more fields later if needed for other use cases. |
||
name: string; | ||
version: string; | ||
status: EpmPackageInstallStatus; | ||
dataStreams: Array<{ | ||
name: string; | ||
title: string; | ||
}>; | ||
} | ||
export interface GetInstalledPackagesResponse { | ||
items: InstalledPackage[]; | ||
total: number; | ||
pageAfter?: SortResults; | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export interface GetEpmDataStreamsResponse { | ||
items: Array<{ | ||
name: string; | ||
}>; | ||
} | ||
export interface GetLimitedPackagesResponse { | ||
items: string[]; | ||
// deprecated in 8.0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ElasticsearchClient } from '@kbn/core/server'; | ||
|
||
import type { PackageDataStreamTypes } from '../../../../common/types'; | ||
|
||
import { dataStreamService } from '../../data_streams'; | ||
|
||
export async function getDataStreams(options: { | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
esClient: ElasticsearchClient; | ||
type?: PackageDataStreamTypes; | ||
datasetQuery?: string; | ||
sortDirection: 'asc' | 'desc'; | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uncategorisedOnly: boolean; | ||
}) { | ||
const { esClient, type, datasetQuery, uncategorisedOnly, sortDirection } = options; | ||
|
||
const allDataStreams = await dataStreamService.getMatchingDataStreams(esClient, { | ||
type: type ? type : '*', | ||
dataset: datasetQuery ? `*${datasetQuery}*` : '*', | ||
}); | ||
|
||
const filteredDataStreams = uncategorisedOnly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is the best name? But we need some sort of option that can differentiate between wanting all data streams or only wanting uncategorised ones, this will be important for the work within Discover. |
||
? allDataStreams.filter((stream) => { | ||
return !stream._meta || !stream._meta.managed_by || stream._meta.managed_by !== 'fleet'; | ||
}) | ||
: allDataStreams; | ||
|
||
const mappedDataStreams = filteredDataStreams.map((dataStream) => { | ||
return { name: dataStream.name }; | ||
}); | ||
|
||
const sortedDataStreams = mappedDataStreams.sort((a, b) => { | ||
return a.name.localeCompare(b.name); | ||
}); | ||
|
||
const dataStreams = sortDirection === 'asc' ? sortedDataStreams : sortedDataStreams.reverse(); | ||
|
||
return { | ||
items: dataStreams, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { getDataStreams } from './get'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,13 @@ import semverGte from 'semver/functions/gte'; | |
import type { Logger } from '@kbn/core/server'; | ||
import { withSpan } from '@kbn/apm-utils'; | ||
|
||
import type { SortResults } from '@elastic/elasticsearch/lib/api/types'; | ||
|
||
import { nodeBuilder } from '@kbn/es-query'; | ||
|
||
import { buildNode as buildFunctionNode } from '@kbn/es-query/src/kuery/node_types/function'; | ||
import { buildNode as buildWildcardNode } from '@kbn/es-query/src/kuery/node_types/wildcard'; | ||
|
||
import { | ||
installationStatuses, | ||
PACKAGE_POLICY_SAVED_OBJECT_TYPE, | ||
|
@@ -20,6 +27,7 @@ import type { | |
PackageUsageStats, | ||
PackagePolicySOAttributes, | ||
Installable, | ||
PackageDataStreamTypes, | ||
} from '../../../../common/types'; | ||
import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants'; | ||
import type { | ||
|
@@ -142,6 +150,90 @@ export async function getPackages( | |
return packageListWithoutStatus; | ||
} | ||
|
||
export async function getInstalledPackages(options: { | ||
savedObjectsClient: SavedObjectsClientContract; | ||
type?: PackageDataStreamTypes; | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nameQuery?: string; | ||
pageAfter?: SortResults; | ||
pageSize: number; | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sortDirection: 'asc' | 'desc'; | ||
}) { | ||
const { savedObjectsClient, pageAfter, pageSize, nameQuery, sortDirection, type } = options; | ||
|
||
const packageSavedObjects = await savedObjectsClient.find<Installation>({ | ||
Kerry350 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: PACKAGES_SAVED_OBJECT_TYPE, | ||
// Pagination | ||
perPage: pageSize, | ||
...(pageAfter && { searchAfter: pageAfter }), | ||
// Sort | ||
sortField: 'name', | ||
sortOrder: sortDirection, | ||
// Name filter | ||
...(nameQuery && { searchFields: ['name'] }), | ||
...(nameQuery && { search: `${nameQuery}* | ${nameQuery}` }), | ||
filter: nodeBuilder.and([ | ||
// Filter to installed packages only | ||
nodeBuilder.is( | ||
`${PACKAGES_SAVED_OBJECT_TYPE}.attributes.install_status`, | ||
installationStatuses.Installed | ||
), | ||
// Filter for a "queryable" marker | ||
buildFunctionNode( | ||
'nested', | ||
`${PACKAGES_SAVED_OBJECT_TYPE}.attributes.installed_es`, | ||
nodeBuilder.is('type', 'index_template') | ||
), | ||
// "Type" filter | ||
...(type | ||
? [ | ||
buildFunctionNode( | ||
'nested', | ||
`${PACKAGES_SAVED_OBJECT_TYPE}.attributes.installed_es`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use |
||
nodeBuilder.is('id', buildWildcardNode(`${type}-*`)) | ||
), | ||
] | ||
: []), | ||
]), | ||
}); | ||
|
||
const integrations = packageSavedObjects.saved_objects.map((integrationSavedObject) => { | ||
const { | ||
name, | ||
version, | ||
install_status: installStatus, | ||
es_index_patterns: esIndexPatterns, | ||
} = integrationSavedObject.attributes; | ||
|
||
const dataStreams = Object.entries(esIndexPatterns) | ||
.map(([key, value]) => { | ||
return { | ||
name: key, | ||
title: value, | ||
}; | ||
}) | ||
.filter((stream) => { | ||
if (!type) { | ||
return true; | ||
} else { | ||
return stream.title.startsWith(`${type}-`); | ||
} | ||
}); | ||
|
||
return { | ||
name, | ||
version, | ||
status: installStatus, | ||
dataStreams, | ||
}; | ||
}); | ||
|
||
return { | ||
items: integrations, | ||
total: packageSavedObjects.total, | ||
pageAfter: packageSavedObjects.saved_objects.at(-1)?.sort, // Enable ability to use searchAfter in subsequent queries | ||
}; | ||
} | ||
|
||
// Get package names for packages which cannot have more than one package policy on an agent policy | ||
export async function getLimitedPackages(options: { | ||
savedObjectsClient: SavedObjectsClientContract; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on advice here: https://github.com/elastic/observability-dev/issues/2639#issuecomment-1521689096