-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add usage collection for savedObject tagging (#83160)
* add so tagging usage collection * update telemetry mappings * fix types * remove check on esClient presence * update schema and README
- Loading branch information
1 parent
d3e7dc5
commit 3f8111b
Showing
16 changed files
with
1,035 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,53 @@ | ||
# SavedObjectsTagging | ||
|
||
Add tagging capability to saved objects | ||
Add tagging capability to saved objects | ||
|
||
## Integrating tagging on a new object type | ||
|
||
In addition to use the UI api to plug the tagging feature in your application, there is a couple | ||
things that needs to be done on the server: | ||
|
||
### Add read-access to the `tag` SO type to your feature's capabilities | ||
|
||
In order to be able to fetch the tags assigned to an object, the user must have read permission | ||
for the `tag` saved object type. Which is why all features relying on SO tagging must update | ||
their capabilities. | ||
|
||
```typescript | ||
features.registerKibanaFeature({ | ||
id: 'myFeature', | ||
// ... | ||
privileges: { | ||
all: { | ||
// ... | ||
savedObject: { | ||
all: ['some-type'], | ||
read: ['tag'], // <-- HERE | ||
}, | ||
}, | ||
read: { | ||
// ... | ||
savedObject: { | ||
all: [], | ||
read: ['some-type', 'tag'], // <-- AND HERE | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
### Update the SOT telemetry collector schema to add the new type | ||
|
||
The schema is located here: `x-pack/plugins/saved_objects_tagging/server/usage/schema.ts`. You | ||
just need to add the name of the SO type you are adding. | ||
|
||
```ts | ||
export const tagUsageCollectorSchema: MakeSchemaFrom<TaggingUsageData> = { | ||
// ... | ||
types: { | ||
dashboard: perTypeSchema, | ||
visualization: perTypeSchema, | ||
// <-- add your type here | ||
}, | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
x-pack/plugins/saved_objects_tagging/server/usage/fetch_tag_usage_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ElasticsearchClient } from 'src/core/server'; | ||
import { TaggingUsageData, ByTypeTaggingUsageData } from './types'; | ||
|
||
/** | ||
* Manual type reflection of the `tagDataAggregations` resulting payload | ||
*/ | ||
interface AggregatedTagUsageResponseBody { | ||
aggregations: { | ||
by_type: { | ||
buckets: Array<{ | ||
key: string; | ||
doc_count: number; | ||
nested_ref: { | ||
tag_references: { | ||
doc_count: number; | ||
tag_id: { | ||
buckets: Array<{ | ||
key: string; | ||
doc_count: number; | ||
}>; | ||
}; | ||
}; | ||
}; | ||
}>; | ||
}; | ||
}; | ||
} | ||
|
||
export const fetchTagUsageData = async ({ | ||
esClient, | ||
kibanaIndex, | ||
}: { | ||
esClient: ElasticsearchClient; | ||
kibanaIndex: string; | ||
}): Promise<TaggingUsageData> => { | ||
const { body } = await esClient.search<AggregatedTagUsageResponseBody>({ | ||
index: [kibanaIndex], | ||
ignore_unavailable: true, | ||
filter_path: 'aggregations', | ||
body: { | ||
size: 0, | ||
query: { | ||
bool: { | ||
must: [hasTagReferenceClause], | ||
}, | ||
}, | ||
aggs: tagDataAggregations, | ||
}, | ||
}); | ||
|
||
const byTypeUsages: Record<string, ByTypeTaggingUsageData> = {}; | ||
const allUsedTags = new Set<string>(); | ||
let totalTaggedObjects = 0; | ||
|
||
const typeBuckets = body.aggregations.by_type.buckets; | ||
typeBuckets.forEach((bucket) => { | ||
const type = bucket.key; | ||
const taggedDocCount = bucket.doc_count; | ||
const usedTagIds = bucket.nested_ref.tag_references.tag_id.buckets.map( | ||
(tagBucket) => tagBucket.key | ||
); | ||
|
||
totalTaggedObjects += taggedDocCount; | ||
usedTagIds.forEach((tagId) => allUsedTags.add(tagId)); | ||
|
||
byTypeUsages[type] = { | ||
taggedObjects: taggedDocCount, | ||
usedTags: usedTagIds.length, | ||
}; | ||
}); | ||
|
||
return { | ||
usedTags: allUsedTags.size, | ||
taggedObjects: totalTaggedObjects, | ||
types: byTypeUsages, | ||
}; | ||
}; | ||
|
||
const hasTagReferenceClause = { | ||
nested: { | ||
path: 'references', | ||
query: { | ||
bool: { | ||
must: [ | ||
{ | ||
term: { | ||
'references.type': 'tag', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const tagDataAggregations = { | ||
by_type: { | ||
terms: { | ||
field: 'type', | ||
}, | ||
aggs: { | ||
nested_ref: { | ||
nested: { | ||
path: 'references', | ||
}, | ||
aggs: { | ||
tag_references: { | ||
filter: { | ||
term: { | ||
'references.type': 'tag', | ||
}, | ||
}, | ||
aggs: { | ||
tag_id: { | ||
terms: { | ||
field: 'references.id', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { createTagUsageCollector } from './tag_usage_collector'; |
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/saved_objects_tagging/server/usage/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { MakeSchemaFrom } from '../../../../../src/plugins/usage_collection/server'; | ||
import { TaggingUsageData, ByTypeTaggingUsageData } from './types'; | ||
|
||
const perTypeSchema: MakeSchemaFrom<ByTypeTaggingUsageData> = { | ||
usedTags: { type: 'integer' }, | ||
taggedObjects: { type: 'integer' }, | ||
}; | ||
|
||
export const tagUsageCollectorSchema: MakeSchemaFrom<TaggingUsageData> = { | ||
usedTags: { type: 'integer' }, | ||
taggedObjects: { type: 'integer' }, | ||
|
||
types: { | ||
dashboard: perTypeSchema, | ||
visualization: perTypeSchema, | ||
map: perTypeSchema, | ||
}, | ||
}; |
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/saved_objects_tagging/server/usage/tag_usage_collector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Observable } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
import { SharedGlobalConfig } from 'src/core/server'; | ||
import { UsageCollectionSetup } from '../../../../../src/plugins/usage_collection/server'; | ||
import { TaggingUsageData } from './types'; | ||
import { fetchTagUsageData } from './fetch_tag_usage_data'; | ||
import { tagUsageCollectorSchema } from './schema'; | ||
|
||
export const createTagUsageCollector = ({ | ||
usageCollection, | ||
legacyConfig$, | ||
}: { | ||
usageCollection: UsageCollectionSetup; | ||
legacyConfig$: Observable<SharedGlobalConfig>; | ||
}) => { | ||
return usageCollection.makeUsageCollector<TaggingUsageData>({ | ||
type: 'saved_objects_tagging', | ||
isReady: () => true, | ||
schema: tagUsageCollectorSchema, | ||
fetch: async ({ esClient }) => { | ||
const { kibana } = await legacyConfig$.pipe(take(1)).toPromise(); | ||
return fetchTagUsageData({ esClient, kibanaIndex: kibana.index }); | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.