@@ -50,10 +50,12 @@ import {
5050 bulkGetMonitors ,
5151 bulkPutMonitors ,
5252 createMonitorsLegacy ,
53- CHUNK_SIZE ,
53+ BATCH_SIZE ,
54+ MAX_PAYLOAD_SIZE_KB ,
5455} from './kibana_api' ;
5556import {
56- getChunks ,
57+ getBatches ,
58+ getSizedBatches ,
5759 isBulkAPISupported ,
5860 isLightweightMonitorSupported ,
5961 logDiff ,
@@ -102,7 +104,10 @@ export async function push(monitors: Monitor[], options: PushOptions) {
102104 for ( const value of sizes . values ( ) ) {
103105 totalSize += value ;
104106 }
105- progress ( 'total size of the monitors payload is ' + printBytes ( totalSize ) ) ;
107+ progress (
108+ `total size of the ${ sizes . size } monitors payload is ` +
109+ printBytes ( totalSize )
110+ ) ;
106111 }
107112
108113 if ( options . dryRun ) {
@@ -115,11 +120,19 @@ export async function push(monitors: Monitor[], options: PushOptions) {
115120 const updatedMonitors = new Set < string > ( [ ...changedIDs , ...newIDs ] ) ;
116121 if ( updatedMonitors . size > 0 ) {
117122 const updatedMonSchemas = schemas . filter ( s => updatedMonitors . has ( s . id ) ) ;
118- const chunks = getChunks ( updatedMonSchemas , CHUNK_SIZE ) ;
119- for ( const chunk of chunks ) {
123+ const batches = getSizedBatches (
124+ updatedMonSchemas ,
125+ sizes ,
126+ MAX_PAYLOAD_SIZE_KB ,
127+ BATCH_SIZE
128+ ) ;
129+ if ( batches . length > 1 ) {
130+ progress ( `Monitors will be pushed as ${ batches . length } batches.` ) ;
131+ }
132+ for ( const batch of batches ) {
120133 await liveProgress (
121- bulkPutMonitors ( options , chunk ) ,
122- `creating or updating ${ chunk . length } monitors`
134+ bulkPutMonitors ( options , batch ) ,
135+ `creating or updating ${ batch . length } monitors`
123136 ) ;
124137 }
125138 }
@@ -136,11 +149,11 @@ export async function push(monitors: Monitor[], options: PushOptions) {
136149 options . yes
137150 ) ;
138151 }
139- const chunks = getChunks ( Array . from ( removedIDs ) , CHUNK_SIZE ) ;
140- for ( const chunk of chunks ) {
152+ const batches = getBatches ( Array . from ( removedIDs ) , BATCH_SIZE ) ;
153+ for ( const batch of batches ) {
141154 await liveProgress (
142- bulkDeleteMonitors ( options , chunk ) ,
143- `deleting ${ chunk . length } monitors`
155+ bulkDeleteMonitors ( options , batch ) ,
156+ `deleting ${ batch . length } monitors`
144157 ) ;
145158 }
146159 }
@@ -317,14 +330,18 @@ export async function pushLegacy(monitors: Monitor[], options: PushOptions) {
317330 }
318331
319332 let schemas : MonitorSchema [ ] = [ ] ;
333+ let sizes : Map < string , number > = new Map ( ) ;
320334 if ( monitors . length > 0 ) {
321335 progress ( `preparing ${ monitors . length } monitors` ) ;
322- ( { schemas } = await buildMonitorSchema ( monitors , false ) ) ;
323- const chunks = getChunks ( schemas , 10 ) ;
324- for ( const chunk of chunks ) {
336+ ( { schemas, sizes } = await buildMonitorSchema ( monitors , false ) ) ;
337+ const batches = getSizedBatches ( schemas , sizes , MAX_PAYLOAD_SIZE_KB , 10 ) ;
338+ if ( batches . length > 1 ) {
339+ progress ( `Monitors will be pushed as ${ batches . length } batches.` ) ;
340+ }
341+ for ( const batch of batches ) {
325342 await liveProgress (
326- createMonitorsLegacy ( { schemas : chunk , keepStale : true , options } ) ,
327- `creating or updating ${ chunk . length } monitors`
343+ createMonitorsLegacy ( { schemas : batch , keepStale : true , options } ) ,
344+ `creating or updating ${ batch . length } monitors`
328345 ) ;
329346 }
330347 } else {
0 commit comments