2020import { encryptTelemetry } from './collectors' ;
2121import { CallCluster } from '../../elasticsearch' ;
2222import { UsageCollectionSetup } from '../../../../plugins/usage_collection/server' ;
23+ import { ESLicense } from './telemetry_collection/get_local_license' ;
2324
2425export type EncryptedStatsGetterConfig = { unencrypted : false } & {
2526 server : any ;
@@ -45,22 +46,38 @@ export interface StatsCollectionConfig {
4546 end : string | number ;
4647}
4748
49+ export interface BasicStatsPayload {
50+ timestamp : string ;
51+ cluster_uuid : string ;
52+ cluster_name : string ;
53+ version : string ;
54+ cluster_stats : object ;
55+ collection ?: string ;
56+ stack_stats : object ;
57+ }
58+
4859export type StatsGetterConfig = UnencryptedStatsGetterConfig | EncryptedStatsGetterConfig ;
4960export type ClusterDetailsGetter = ( config : StatsCollectionConfig ) => Promise < ClusterDetails [ ] > ;
50- export type StatsGetter = (
61+ export type StatsGetter < T extends BasicStatsPayload = BasicStatsPayload > = (
62+ clustersDetails : ClusterDetails [ ] ,
63+ config : StatsCollectionConfig
64+ ) => Promise < T [ ] > ;
65+ export type LicenseGetter = (
5166 clustersDetails : ClusterDetails [ ] ,
5267 config : StatsCollectionConfig
53- ) => Promise < any [ ] > ;
68+ ) => Promise < { [ clusterUuid : string ] : ESLicense | undefined } > ;
5469
55- interface CollectionConfig {
70+ interface CollectionConfig < T extends BasicStatsPayload > {
5671 title : string ;
5772 priority : number ;
5873 esCluster : string ;
59- statsGetter : StatsGetter ;
74+ statsGetter : StatsGetter < T > ;
6075 clusterDetailsGetter : ClusterDetailsGetter ;
76+ licenseGetter : LicenseGetter ;
6177}
6278interface Collection {
6379 statsGetter : StatsGetter ;
80+ licenseGetter : LicenseGetter ;
6481 clusterDetailsGetter : ClusterDetailsGetter ;
6582 esCluster : string ;
6683 title : string ;
@@ -70,8 +87,15 @@ export class TelemetryCollectionManager {
7087 private usageGetterMethodPriority = - 1 ;
7188 private collections : Collection [ ] = [ ] ;
7289
73- public setCollection = ( collectionConfig : CollectionConfig ) => {
74- const { title, priority, esCluster, statsGetter, clusterDetailsGetter } = collectionConfig ;
90+ public setCollection = < T extends BasicStatsPayload > ( collectionConfig : CollectionConfig < T > ) => {
91+ const {
92+ title,
93+ priority,
94+ esCluster,
95+ statsGetter,
96+ clusterDetailsGetter,
97+ licenseGetter,
98+ } = collectionConfig ;
7599
76100 if ( typeof priority !== 'number' ) {
77101 throw new Error ( 'priority must be set.' ) ;
@@ -88,10 +112,14 @@ export class TelemetryCollectionManager {
88112 throw Error ( 'esCluster name must be set for the getCluster method.' ) ;
89113 }
90114 if ( ! clusterDetailsGetter ) {
91- throw Error ( 'Cluser UUIds method is not set.' ) ;
115+ throw Error ( 'Cluster UUIds method is not set.' ) ;
116+ }
117+ if ( ! licenseGetter ) {
118+ throw Error ( 'License getter method not set.' ) ;
92119 }
93120
94121 this . collections . unshift ( {
122+ licenseGetter,
95123 statsGetter,
96124 clusterDetailsGetter,
97125 esCluster,
@@ -141,7 +169,19 @@ export class TelemetryCollectionManager {
141169 return ;
142170 }
143171
144- return await collection . statsGetter ( clustersDetails , statsCollectionConfig ) ;
172+ const [ stats , licenses ] = await Promise . all ( [
173+ collection . statsGetter ( clustersDetails , statsCollectionConfig ) ,
174+ collection . licenseGetter ( clustersDetails , statsCollectionConfig ) ,
175+ ] ) ;
176+
177+ return stats . map ( stat => {
178+ const license = licenses [ stat . cluster_uuid ] ;
179+ return {
180+ ...( license ? { license } : { } ) ,
181+ ...stat ,
182+ collectionSource : collection . title ,
183+ } ;
184+ } ) ;
145185 } ;
146186
147187 public getOptInStats = async ( optInStatus : boolean , config : StatsGetterConfig ) => {
0 commit comments