Skip to content

Commit

Permalink
Use cat snapshot to get the number of snapshot for a repo
Browse files Browse the repository at this point in the history
Signed-off-by: Sandeep Kumawat <[email protected]>
  • Loading branch information
Sandeep Kumawat committed Jan 22, 2025
1 parent bed34dd commit aeba297
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"engines": {
"yarn": "^1.21.1"
}
}
}
16 changes: 16 additions & 0 deletions server/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,22 @@ export interface GetSnapshotResponse {
snapshots: GetSnapshot[];
}

export interface CatSnapshotsResponse {
snapshots: CatSnapshots[];
}

export interface CatSnapshots {
id: string;
status: string;
start_epoch: number;
end_epoch: number;
duration: number;
indices: number;
successful_shards: number;
failed_shards: number;
total_shards: number;
}

export interface CreateSnapshotResponse {
snapshot: GetSnapshot;
}
Expand Down
27 changes: 21 additions & 6 deletions server/services/SnapshotManagementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
AcknowledgedResponse,
CreateSnapshotResponse,
RestoreSnapshotResponse,
CatSnapshots,
} from "../models/interfaces";
import { FailedServerResponse, ServerResponse } from "../models/types";
import { MDSEnabledClientService } from "./MDSEnabledClientService";
Expand Down Expand Up @@ -539,12 +540,8 @@ export default class SnapshotManagementService extends MDSEnabledClientService {
})) as CatRepository[];

for (let i = 0; i < res.length; i++) {
const getSnapshotRes: GetSnapshotResponse = (await callWithRequest("snapshot.get", {
repository: res[i].id,
snapshot: "_all",
ignore_unavailable: true,
})) as GetSnapshotResponse;
res[i].snapshotCount = getSnapshotRes.snapshots.length;
let catSnapshotResponse = await this._safecatSnapshotForRepo(callWithRequest, res[i].id);
res[i].snapshotCount = catSnapshotResponse?.length;
}

return response.custom({
Expand All @@ -559,6 +556,24 @@ export default class SnapshotManagementService extends MDSEnabledClientService {
}
};

private async _safecatSnapshotForRepo(
callWithRequest: (endpoint: string, clientParams: Record<string, any>, options?: LegacyCallAPIOptions | undefined) => Promise<unknown>,
repositoryName: string
): Promise<CatSnapshots[] | undefined> {
try {
const catSnapshotRes: CatSnapshots[] = (await callWithRequest("cat.snapshots", {
format: "json",
repository: repositoryName,
ignore_unavailable: true,
})) as CatSnapshots[];

return catSnapshotRes;
} catch (err) {
console.error(`Index Management - SnapshotManagementService - _safeCatSnapshot:`, err);
return undefined;
}
}

deleteRepository = async (
context: RequestHandlerContext,
request: OpenSearchDashboardsRequest,
Expand Down

0 comments on commit aeba297

Please sign in to comment.