-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Dashboard favorites telemetry #190706
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
Dashboard favorites telemetry #190706
Changes from all commits
b90194d
2933d91
fb9f149
289bcfd
a215da3
2408e74
e01c97e
bfb2959
30a527e
c75f060
159f689
eda9054
db55912
5b7e326
d820392
8ab2d24
b1a24e9
3d266af
e5165bb
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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * 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 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import type { CoreSetup } from '@kbn/core-lifecycle-server'; | ||
| import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; | ||
| import type { estypes } from '@elastic/elasticsearch'; | ||
| import { favoritesSavedObjectName } from './favorites_saved_object'; | ||
|
|
||
| interface FavoritesUsage { | ||
| [favorite_object_type: string]: { | ||
| total: number; | ||
| total_users_spaces: number; | ||
| avg_per_user_per_space: number; | ||
| max_per_user_per_space: number; | ||
| }; | ||
| } | ||
|
|
||
| export function registerFavoritesUsageCollection({ | ||
| core, | ||
| usageCollection, | ||
| }: { | ||
| core: CoreSetup; | ||
| usageCollection: UsageCollectionSetup; | ||
| }) { | ||
| usageCollection.registerCollector( | ||
| usageCollection.makeUsageCollector<FavoritesUsage>({ | ||
| type: 'favorites', | ||
| isReady: () => true, | ||
| schema: { | ||
| DYNAMIC_KEY /* e.g. 'dashboard' */: { | ||
| total: { | ||
| type: 'long', | ||
| _meta: { description: 'Total favorite object count in this deployment' }, | ||
| }, | ||
| total_users_spaces: { | ||
| type: 'long', | ||
| _meta: { | ||
| description: | ||
| 'Total users per space that have favorited an object of this type in this deployment', | ||
| }, | ||
| }, | ||
| avg_per_user_per_space: { | ||
| type: 'double', | ||
| _meta: { | ||
| description: | ||
| 'Average favorite objects count of this type per user per space for this deployment, only counts users who have favorited at least one object of this type', | ||
| }, | ||
| }, | ||
| max_per_user_per_space: { | ||
| type: 'long', | ||
| _meta: { | ||
| description: | ||
| 'Max favorite objects count of this type per user per space for this deployment', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| fetch: async (context) => { | ||
| const favoritesIndex = await core | ||
| .getStartServices() | ||
| .then(([{ savedObjects }]) => savedObjects.getIndexForType(favoritesSavedObjectName)); | ||
|
|
||
| const response = await context.esClient.search< | ||
| unknown, | ||
| { types: estypes.AggregationsStringTermsAggregate } | ||
| >({ | ||
| index: favoritesIndex, | ||
| size: 0, | ||
| _source: false, | ||
| filter_path: ['aggregations'], | ||
| query: { | ||
| bool: { | ||
| filter: [ | ||
| { | ||
| term: { | ||
| type: 'favorites', | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| runtime_mappings: { | ||
| number_of_favorites: { | ||
| type: 'long', | ||
| script: { | ||
| source: "emit(doc['favorites.favoriteIds'].length)", | ||
|
Contributor
Author
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 way, but I like how clean it is to get sum, avg, max. I've also tried value_count aggregation which works well for total, but haven't figure how it can be used for avg and max
Member
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. If you need other alternatives to consider, there is also the stats aggregation. Running the aggregation on a runtime field seems like a perfect idea for this.
Contributor
Author
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. Thanks, I updated to stats |
||
| }, | ||
| }, | ||
| }, | ||
| aggs: { | ||
| types: { | ||
| terms: { | ||
| field: 'favorites.type', | ||
| }, | ||
| aggs: { | ||
| stats: { | ||
| stats: { | ||
| field: 'number_of_favorites', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const favoritesUsage: FavoritesUsage = {}; | ||
|
|
||
| const typesBuckets = (response.aggregations?.types?.buckets ?? | ||
| []) as estypes.AggregationsStringTermsBucket[]; | ||
|
|
||
| typesBuckets.forEach((bucket) => { | ||
| favoritesUsage[bucket.key] = { | ||
| total: bucket.stats.sum, | ||
| total_users_spaces: bucket.stats.count, | ||
| avg_per_user_per_space: bucket.stats.avg, | ||
| max_per_user_per_space: bucket.stats.max, | ||
| }; | ||
| }); | ||
|
|
||
| return favoritesUsage; | ||
| }, | ||
| }) | ||
| ); | ||
| } | ||
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.
We didn't need these mappings for the feature, but, unfortunately, I didn't know that we would need them for the snapshot telemetry. I am not sure if there is another better way
the object schema stays the same, but we add existing fields to the mapping
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.
It's been my experience as well that it's sometimes necessary to add mappings just for the sake of being able to collect snapshot telemetry.