Skip to content

Commit 7a8bb93

Browse files
feat(core-manager): implement snapshots.list action (#3722)
1 parent 052aae8 commit 7a8bb93

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import "jest-extended";
2+
3+
import { Container } from "@packages/core-kernel";
4+
import { Action } from "@packages/core-manager/src/actions/snapshots-list";
5+
import { Sandbox } from "@packages/core-test-framework";
6+
7+
let sandbox: Sandbox;
8+
let action: Action;
9+
10+
const mockFilesystem = {
11+
directories: jest.fn().mockResolvedValue(["/path/to/file/1-5", "/path/to/file/5-10"]),
12+
size: jest.fn().mockResolvedValue(1024),
13+
};
14+
15+
beforeEach(() => {
16+
sandbox = new Sandbox();
17+
18+
sandbox.app.bind(Container.Identifiers.FilesystemService).toConstantValue(mockFilesystem);
19+
20+
action = sandbox.app.resolve(Action);
21+
});
22+
23+
describe("Snapshots:List", () => {
24+
it("should have name", () => {
25+
expect(action.name).toEqual("snapshots.list");
26+
});
27+
28+
it("should return list of snapshot info", async () => {
29+
const result = await action.execute({});
30+
31+
expect(result).toBeArray();
32+
expect(result.length).toBe(2);
33+
for (const item of result) {
34+
expect(item.name).toBeString();
35+
expect(item.size).toBe(4);
36+
}
37+
});
38+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Container, Contracts } from "@arkecosystem/core-kernel";
2+
import { basename, join } from "path";
3+
4+
import { Actions } from "../contracts";
5+
6+
@Container.injectable()
7+
export class Action implements Actions.Action {
8+
public name = "snapshots.list";
9+
10+
@Container.inject(Container.Identifiers.FilesystemService)
11+
private readonly filesystem!: Contracts.Kernel.Filesystem;
12+
13+
public async execute(params: object): Promise<any> {
14+
return await this.getSnapshots();
15+
}
16+
17+
private async getSnapshotInfo(snapshot: string): Promise<any> {
18+
const response = {
19+
name: basename(snapshot),
20+
size: 0,
21+
};
22+
23+
for (const file of ["blocks", "transactions", "rounds", "meta.json"]) {
24+
response.size += await this.filesystem.size(join(snapshot, file));
25+
}
26+
27+
response.size = Math.round(response.size / 1024);
28+
29+
return response;
30+
}
31+
32+
private async getSnapshots(): Promise<any[]> {
33+
const snapshotsDir = `${process.env.CORE_PATH_DATA}/snapshots/`;
34+
35+
const snapshots = await this.filesystem.directories(snapshotsDir);
36+
37+
const response = [] as any[];
38+
39+
for (const snapshot of snapshots) {
40+
try {
41+
response.push(await this.getSnapshotInfo(snapshot));
42+
} catch {}
43+
}
44+
45+
return response;
46+
}
47+
}

0 commit comments

Comments
 (0)