-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-manager): implement
snapshots.list
action (#3722)
- Loading branch information
1 parent
052aae8
commit 7a8bb93
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
__tests__/unit/core-manager/actions/snapshots-list.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import "jest-extended"; | ||
|
||
import { Container } from "@packages/core-kernel"; | ||
import { Action } from "@packages/core-manager/src/actions/snapshots-list"; | ||
import { Sandbox } from "@packages/core-test-framework"; | ||
|
||
let sandbox: Sandbox; | ||
let action: Action; | ||
|
||
const mockFilesystem = { | ||
directories: jest.fn().mockResolvedValue(["/path/to/file/1-5", "/path/to/file/5-10"]), | ||
size: jest.fn().mockResolvedValue(1024), | ||
}; | ||
|
||
beforeEach(() => { | ||
sandbox = new Sandbox(); | ||
|
||
sandbox.app.bind(Container.Identifiers.FilesystemService).toConstantValue(mockFilesystem); | ||
|
||
action = sandbox.app.resolve(Action); | ||
}); | ||
|
||
describe("Snapshots:List", () => { | ||
it("should have name", () => { | ||
expect(action.name).toEqual("snapshots.list"); | ||
}); | ||
|
||
it("should return list of snapshot info", async () => { | ||
const result = await action.execute({}); | ||
|
||
expect(result).toBeArray(); | ||
expect(result.length).toBe(2); | ||
for (const item of result) { | ||
expect(item.name).toBeString(); | ||
expect(item.size).toBe(4); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Container, Contracts } from "@arkecosystem/core-kernel"; | ||
import { basename, join } from "path"; | ||
|
||
import { Actions } from "../contracts"; | ||
|
||
@Container.injectable() | ||
export class Action implements Actions.Action { | ||
public name = "snapshots.list"; | ||
|
||
@Container.inject(Container.Identifiers.FilesystemService) | ||
private readonly filesystem!: Contracts.Kernel.Filesystem; | ||
|
||
public async execute(params: object): Promise<any> { | ||
return await this.getSnapshots(); | ||
} | ||
|
||
private async getSnapshotInfo(snapshot: string): Promise<any> { | ||
const response = { | ||
name: basename(snapshot), | ||
size: 0, | ||
}; | ||
|
||
for (const file of ["blocks", "transactions", "rounds", "meta.json"]) { | ||
response.size += await this.filesystem.size(join(snapshot, file)); | ||
} | ||
|
||
response.size = Math.round(response.size / 1024); | ||
|
||
return response; | ||
} | ||
|
||
private async getSnapshots(): Promise<any[]> { | ||
const snapshotsDir = `${process.env.CORE_PATH_DATA}/snapshots/`; | ||
|
||
const snapshots = await this.filesystem.directories(snapshotsDir); | ||
|
||
const response = [] as any[]; | ||
|
||
for (const snapshot of snapshots) { | ||
try { | ||
response.push(await this.getSnapshotInfo(snapshot)); | ||
} catch {} | ||
} | ||
|
||
return response; | ||
} | ||
} |