Skip to content

Commit c459af0

Browse files
skumawat2025Sandeep Kumawat
and
Sandeep Kumawat
authored
Use cat snapshot to get the number of snapshot for a repo (#1242)
Signed-off-by: Sandeep Kumawat <[email protected]> Co-authored-by: Sandeep Kumawat <[email protected]>
1 parent bed34dd commit c459af0

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858
"engines": {
5959
"yarn": "^1.21.1"
6060
}
61-
}
61+
}

server/models/interfaces.ts

+16
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,22 @@ export interface GetSnapshotResponse {
409409
snapshots: GetSnapshot[];
410410
}
411411

412+
export interface CatSnapshotsResponse {
413+
snapshots: CatSnapshots[];
414+
}
415+
416+
export interface CatSnapshots {
417+
id: string;
418+
status: string;
419+
start_epoch: number;
420+
end_epoch: number;
421+
duration: number;
422+
indices: number;
423+
successful_shards: number;
424+
failed_shards: number;
425+
total_shards: number;
426+
}
427+
412428
export interface CreateSnapshotResponse {
413429
snapshot: GetSnapshot;
414430
}

server/services/SnapshotManagementService.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
AcknowledgedResponse,
2727
CreateSnapshotResponse,
2828
RestoreSnapshotResponse,
29+
CatSnapshots,
2930
} from "../models/interfaces";
3031
import { FailedServerResponse, ServerResponse } from "../models/types";
3132
import { MDSEnabledClientService } from "./MDSEnabledClientService";
@@ -539,12 +540,8 @@ export default class SnapshotManagementService extends MDSEnabledClientService {
539540
})) as CatRepository[];
540541

541542
for (let i = 0; i < res.length; i++) {
542-
const getSnapshotRes: GetSnapshotResponse = (await callWithRequest("snapshot.get", {
543-
repository: res[i].id,
544-
snapshot: "_all",
545-
ignore_unavailable: true,
546-
})) as GetSnapshotResponse;
547-
res[i].snapshotCount = getSnapshotRes.snapshots.length;
543+
let catSnapshotResponse = await this._safecatSnapshotForRepo(callWithRequest, res[i].id);
544+
res[i].snapshotCount = catSnapshotResponse?.length;
548545
}
549546

550547
return response.custom({
@@ -559,6 +556,24 @@ export default class SnapshotManagementService extends MDSEnabledClientService {
559556
}
560557
};
561558

559+
private async _safecatSnapshotForRepo(
560+
callWithRequest: (endpoint: string, clientParams: Record<string, any>, options?: LegacyCallAPIOptions | undefined) => Promise<unknown>,
561+
repositoryName: string
562+
): Promise<CatSnapshots[] | undefined> {
563+
try {
564+
const catSnapshotRes: CatSnapshots[] = (await callWithRequest("cat.snapshots", {
565+
format: "json",
566+
repository: repositoryName,
567+
ignore_unavailable: true,
568+
})) as CatSnapshots[];
569+
570+
return catSnapshotRes;
571+
} catch (err) {
572+
console.error(`Index Management - SnapshotManagementService - _safeCatSnapshot:`, err);
573+
return undefined;
574+
}
575+
}
576+
562577
deleteRepository = async (
563578
context: RequestHandlerContext,
564579
request: OpenSearchDashboardsRequest,

0 commit comments

Comments
 (0)