import { DeepPartial } from 'utility-types' ; import { merge , omit , defaultsDeep , range , mapValues , isPlainObject , flatten , } from 'lodash' ; import uuid from 'uuid' ; import { CollectTelemetryParams , collectDataTelemetry , } from '../../server/lib/apm_telemetry/collect_data_telemetry' ; interface GenerateOptions { days : number ; instances : number ; variation : { min : number ; max : number ; } ; } const randomize = ( value , instanceVariation , dailyGrowth ) => { if ( typeof value === 'boolean' ) { return Math . random ( ) > 0.5 ; } if ( typeof value === 'number' ) { return Math . round ( instanceVariation * dailyGrowth * value ) ; } return value ; } ; const mapValuesDeep = ( obj , iterator ) : Record < string , any > => mapValues ( obj , ( val , key ) => isPlainObject ( val ) ? mapValuesDeep ( val , iterator ) : iterator ( val , key ! , obj ) ) ; export async function generateSampleDocuments ( options ) { const { collectTelemetryParams , ... preferredOptions } = options ; const opts = defaultsDeep ( { days : 100 , instances : 50 , variation : { min : 0.1 , max : 4 , } , } , preferredOptions ) ; const sample = await collectDataTelemetry ( collectTelemetryParams ) ; console . log ( 'Collected telemetry' ) ; console . log ( '\n' + JSON . stringify ( sample , null , 2 ) ) ; const dateOfScriptExecution = new Date ( ) ; return flatten ( range ( 0 , opts . instances ) . map ( ( ) => { const instanceId = uuid . v4 ( ) ; const defaults = { cluster_uuid : instanceId , stack_stats : { kibana : { versions : { version : '8.0.0' , } , } , } , } ; const instanceVariation = Math . random ( ) * ( opts . variation . max - opts . variation . min ) + opts . variation . min ; return range ( 0 , opts . days ) . map ( ( dayNo ) => { const dailyGrowth = Math . pow ( 1.005 , opts . days - 1 - dayNo ) ; const timestamp = Date . UTC ( dateOfScriptExecution . getFullYear ( ) , dateOfScriptExecution . getMonth ( ) , - dayNo ) ; const generated = mapValuesDeep ( omit ( sample , 'versions' ) , ( value ) => randomize ( value , instanceVariation , dailyGrowth ) ) ; return merge ( { } , defaults , { timestamp , stack_stats : { kibana : { plugins : { apm : merge ( { } , sample , generated ) , } , } , } , } ) ; } ) ; } ) ) ; }