Skip to content

Commit

Permalink
fix: push only newly created/updated monitors (#939)
Browse files Browse the repository at this point in the history
* fix: push only newly created/updated monitors

* fix test
  • Loading branch information
vigneshshanmugam authored Jul 9, 2024
1 parent 2d26404 commit 686f95a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __tests__/push/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ heartbeat.monitors:
expect(output).toContain(
"Pushing monitors for 'test-project' project in kibana 'dummy' space"
);
expect(output).toContain('bundling 2 monitors');
expect(output).toContain('preparing 2 monitors');
expect(output).toContain('creating or updating 2 monitors');
expect(output).toContain(deleteProgress);
expect(output).toContain('✓ Pushed:');
Expand Down
12 changes: 6 additions & 6 deletions src/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function push(monitors: Monitor[], options: PushOptions) {

const { monitors: remote } = await bulkGetMonitors(options);

progress(`bundling ${monitors.length} monitors`);
progress(`preparing ${monitors.length} monitors`);
const schemas = await buildMonitorSchema(monitors, true);
const local = getLocalMonitors(schemas);

Expand All @@ -90,7 +90,8 @@ export async function push(monitors: Monitor[], options: PushOptions) {

const updatedMonitors = new Set<string>([...changedIDs, ...newIDs]);
if (updatedMonitors.size > 0) {
const chunks = getChunks(schemas, CHUNK_SIZE);
const updatedMonSchemas = schemas.filter(s => updatedMonitors.has(s.id));
const chunks = getChunks(updatedMonSchemas, CHUNK_SIZE);
for (const chunk of chunks) {
await liveProgress(
bulkPutMonitors(options, chunk),
Expand Down Expand Up @@ -217,9 +218,8 @@ export function validateSettings(opts: PushOptions) {
- CLI '--schedule <mins>'
- Config file 'monitors.schedule' field`;
} else if (opts.schedule && !ALLOWED_SCHEDULES.includes(opts.schedule)) {
reason = `Set default schedule(${
opts.schedule
}) to one of the allowed values - ${ALLOWED_SCHEDULES.join(',')}`;
reason = `Set default schedule(${opts.schedule
}) to one of the allowed values - ${ALLOWED_SCHEDULES.join(',')}`;
}

if (!reason) return;
Expand Down Expand Up @@ -288,7 +288,7 @@ export async function pushLegacy(monitors: Monitor[], options: PushOptions) {

let schemas: MonitorSchema[] = [];
if (monitors.length > 0) {
progress(`bundling ${monitors.length} monitors`);
progress(`preparing ${monitors.length} monitors`);
schemas = await buildMonitorSchema(monitors, false);
const chunks = getChunks(schemas, 10);
for (const chunk of chunks) {
Expand Down
10 changes: 5 additions & 5 deletions src/push/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ export function logDiff<T extends Set<string>>(
) {
progress(
'Monitor Diff: ' +
green(`Added(${newIDs.size}) `) +
yellow(`Updated(${changedIDs.size}) `) +
red(`Removed(${removedIDs.size}) `) +
grey(`Unchanged(${unchangedIDs.size})`)
green(`Added(${newIDs.size}) `) +
yellow(`Updated(${changedIDs.size}) `) +
red(`Removed(${removedIDs.size}) `) +
grey(`Unchanged(${unchangedIDs.size})`)
);
}

export function getChunks(arr: any[], size: number) {
export function getChunks<T>(arr: Array<T>, size: number): Array<T[]> {
const chunks = [];
for (let i = 0; i < arr.length; i += size) {
chunks.push(arr.slice(i, i + size));
Expand Down

0 comments on commit 686f95a

Please sign in to comment.