Skip to content

Commit

Permalink
fix: use total monitors for bulk get operation while pushing
Browse files Browse the repository at this point in the history
* fix push command

* bit more refactoring

* lint
  • Loading branch information
shahzad31 authored Dec 14, 2022
1 parent 6da711b commit df1ac9f
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions src/push/kibana_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,42 @@ export type GetResponse = {
export async function bulkGetMonitors(
options: PushOptions
): Promise<GetResponse> {
let afterKey = null;
let total = 0;
const monitors: MonitorHashID[] = [];
let url = generateURL(options, 'bulk_get');
do {
if (afterKey) {
url += `?search_after=${afterKey}`;
}
const resp = await sendReqAndHandleError<GetResponse>({
url,
method: 'GET',
auth: options.auth,
});
afterKey = resp.after_key;

// The first page gives the total number of monitors
if (total == 0) {
total = resp.total;
}
monitors.push(...resp.monitors);
} while (afterKey);
const allMonitors: MonitorHashID[] = [];

const resp = await fetchMonitors(options);
allMonitors.push(...resp.monitors);
let afterKey = resp.afterKey;
const total = resp.total;

return { total, monitors };
while (allMonitors.length < total) {
const resp = await fetchMonitors(options, afterKey);
allMonitors.push(...resp.monitors);
afterKey = resp.afterKey;
}

return {
total,
monitors: allMonitors,
};
}

const fetchMonitors = async (options: PushOptions, afterKey?: string) => {
let url = generateURL(options, 'bulk_get');
if (afterKey) {
url += `?search_after=${afterKey}`;
}
const resp = await sendReqAndHandleError<GetResponse>({
url,
method: 'GET',
auth: options.auth,
});
return {
afterKey: resp.after_key,
total: resp.total,
monitors: resp.monitors,
};
};

export type DeleteResponse = {
deleted_monitors: string[];
};
Expand Down

0 comments on commit df1ac9f

Please sign in to comment.