import type { Client } from '@elastic/elasticsearch' ; import { times } from 'lodash' ; import { v4 as uuid } from 'uuid' ; import { ESTestIndexTool , ES_TEST_INDEX_NAME } from '../../../../../common/lib' ; export const END_DATE = '2020-01-01T00:00:00Z' ; export const DOCUMENT_SOURCE = 'queryDataEndpointTests' ; export const DOCUMENT_REFERENCE = '-na-' ; export async function createEsDocuments ( es , esTestIndexTool , endDate = END_DATE , intervals = 1 , intervalMillis = 1000 , groups = 2 ) { const endDateMillis = Date . parse ( endDate ) - intervalMillis / 2 ; times ( intervals , ( interval ) => { const date = endDateMillis - interval * intervalMillis ; const testedValue = 2 ** interval ; times ( groups , ( group ) => { createEsDocument ( es , date , testedValue + group , `group-${ group }` ) ; } ) ; } ) ; const totalDocuments = intervals * groups ; await esTestIndexTool . waitForDocs ( DOCUMENT_SOURCE , DOCUMENT_REFERENCE , totalDocuments ) ; } async function createEsDocument ( es , epochMillis , testedValue , group ) { const document = { source : DOCUMENT_SOURCE , reference : DOCUMENT_REFERENCE , date : new Date ( epochMillis ) . toISOString ( ) , date_epoch_millis : epochMillis , testedValue , group , } ; const response = await es . index ( { id : uuid ( ) , index : ES_TEST_INDEX_NAME , body : document , } ) ; if ( response . result !== 'created' ) { throw new Error ( `document not created: ${ JSON . stringify ( response ) }` ) ; } }