Skip to content

Commit

Permalink
feat(core-manager): implement snapshots.list action (#3722)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner authored May 25, 2020
1 parent 052aae8 commit 7a8bb93
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
38 changes: 38 additions & 0 deletions __tests__/unit/core-manager/actions/snapshots-list.test.ts
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);
}
});
});
47 changes: 47 additions & 0 deletions packages/core-manager/src/actions/snapshots-list.ts
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;
}
}

0 comments on commit 7a8bb93

Please sign in to comment.