Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoST volume names #112

Merged
merged 9 commits into from Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/components/localNode/LeftPane.js
Expand Up @@ -22,10 +22,15 @@ const Header = styled.div`
margin-bottom: 30px;
`;

type CapacityAllocation = {
id: number,
label: string
};

type Props = {
isInProgress: boolean,
switchMode: (mode: number) => void,
capacity: any,
capacity: CapacityAllocation,
progress: number,
resetNodeSettings: Action,
getLocalNodeSetupProgress: Action
Expand Down
60 changes: 33 additions & 27 deletions app/components/localNode/LeftPaneSetup.js
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import styled from 'styled-components';
import { connect } from 'react-redux';
import { setLocalNodeStorage, getDrivesList, getAvailableSpace } from '/redux/localNode/actions';
import { setLocalNodeStorage, getDrivesList } from '/redux/localNode/actions';
import { smColors, localNodeModes } from '/vars';
import { SmButton, SmDropdown } from '/basicComponents';
import type { Action } from '/types';
Expand Down Expand Up @@ -70,18 +70,28 @@ const formatNumber = (num?: number) => {
return formatter.format(num.toFixed(2));
};

const getElementIndex = (elementsList: any[], element: any) => (element ? elementsList.findIndex((elem) => elem.id === element.id) : -1);
const getElementIndex = (elementsList: Volume[], element: Volume) => (element ? elementsList.findIndex((elem) => elem.id === element.id) : -1);

type CapacityAllocation = {
id: number,
label: string
};

type Volume = {
id: string,
mountPoint: string,
label: string,
availableDiskSpace: { bytes: number, readable: string },
capacityAllocationsList: CapacityAllocation[]
};

type Props = {
switchMode: (mode: number) => void,
drives: any[],
capacity: any,
capacityAllocationsList: any[],
drive: any,
availableDiskSpace: { bytes: number, readable: string },
drives: Volume[],
capacity: CapacityAllocation,
drive: Volume,
setLocalNodeStorage: Action,
getDrivesList: Action,
getAvailableSpace: Action,
fiatRate: number
};

Expand All @@ -93,9 +103,9 @@ type State = {
class LeftPaneSetup extends Component<Props, State> {
constructor(props: Props) {
super(props);
const { getDrivesList, drive, capacity, drives, capacityAllocationsList } = this.props;
const { getDrivesList, drive, capacity, drives } = this.props;
const selectedDriveIndex = getElementIndex(drives, drive);
const selectedCapacityIndex = getElementIndex(capacityAllocationsList, capacity);
const selectedCapacityIndex = drive ? getElementIndex(drive.capacityAllocationsList, capacity) : -1;
this.state = {
selectedCapacityIndex,
selectedDriveIndex
Expand All @@ -104,24 +114,28 @@ class LeftPaneSetup extends Component<Props, State> {
}

render() {
const { drives, capacityAllocationsList, availableDiskSpace, fiatRate } = this.props;
const { drives, fiatRate } = this.props;
const { selectedCapacityIndex, selectedDriveIndex } = this.state;

return (
<Wrapper>
<SubHeader>Select Drive</SubHeader>
<Row>
<SmDropdown data={drives} selectedItemIndex={selectedDriveIndex} onPress={this.handleSelectDrive} />
<LabelWrapper>{selectedDriveIndex !== -1 && `You have ${availableDiskSpace && availableDiskSpace.readable} free on your drive`}</LabelWrapper>
<LabelWrapper>{selectedDriveIndex !== -1 && `You have ${drives[selectedDriveIndex].availableDiskSpace.readable} free on your drive`}</LabelWrapper>
</Row>
<SubHeader>Choose how much storage to allocate for the local node</SubHeader>
<Row>
<SmDropdown data={capacityAllocationsList} selectedItemIndex={selectedCapacityIndex} onPress={this.handleSelectCapacity} />
<SmDropdown
data={selectedDriveIndex !== -1 && drives[selectedDriveIndex].capacityAllocationsList}
selectedItemIndex={selectedCapacityIndex}
onPress={this.handleSelectCapacity}
/>
<LabelWrapper>
{selectedCapacityIndex !== -1 && (
<React.Fragment>
earn ~ {getProjectedSmcEarnings(capacityAllocationsList[selectedCapacityIndex].id)} SMC each week*{' '}
<FiatRateEstimation> = {getProjectedSmcEarnings(fiatRate * capacityAllocationsList[selectedCapacityIndex].id)} USD*</FiatRateEstimation>
earn ~ {getProjectedSmcEarnings(drives[selectedDriveIndex].capacityAllocationsList[selectedCapacityIndex].id)} SMC each week*{' '}
<FiatRateEstimation> = {getProjectedSmcEarnings(fiatRate * drives[selectedDriveIndex].capacityAllocationsList[selectedCapacityIndex].id)} USD*</FiatRateEstimation>
</React.Fragment>
)}
</LabelWrapper>
Expand All @@ -143,35 +157,27 @@ class LeftPaneSetup extends Component<Props, State> {
}

handleStartSetup = () => {
const { setLocalNodeStorage, drives, capacityAllocationsList, switchMode } = this.props;
const { setLocalNodeStorage, drives, switchMode } = this.props;
const { selectedCapacityIndex, selectedDriveIndex } = this.state;
setLocalNodeStorage({ capacity: capacityAllocationsList[selectedCapacityIndex], drive: drives[selectedDriveIndex] });
setLocalNodeStorage({ capacity: drives[selectedDriveIndex].capacityAllocationsList[selectedCapacityIndex], drive: drives[selectedDriveIndex] });
switchMode(localNodeModes.PROGRESS);
};

handleSelectDrive = ({ index }: { index: number }) => {
const { getAvailableSpace, drives } = this.props;
const drive: any = drives[index];
getAvailableSpace(drive.mountPoint);
this.setState({ selectedDriveIndex: index });
};
handleSelectDrive = ({ index }: { index: number }) => this.setState({ selectedDriveIndex: index, selectedCapacityIndex: -1 });

handleSelectCapacity = ({ index }: { index: number }) => this.setState({ selectedCapacityIndex: index });
}

const mapStateToProps = (state) => ({
capacity: state.localNode.capacity,
capacityAllocationsList: state.localNode.capacityAllocationsList,
drive: state.localNode.drive,
drives: state.localNode.drives,
availableDiskSpace: state.localNode.availableDiskSpace,
fiatRate: state.wallet.fiatRate
});

const mapDispatchToProps = {
setLocalNodeStorage,
getDrivesList,
getAvailableSpace
getDrivesList
};

LeftPaneSetup = connect(
Expand Down
15 changes: 0 additions & 15 deletions app/infra/diskStorageService/diskStorageService.js
Expand Up @@ -2,9 +2,6 @@
import { ipcRenderer } from 'electron';
import { ipcConsts } from '/vars';

const getBytesfromGb = (Gb: number) => Gb * 1073741824;
const DRIVE_SPACE_BUFFER = 50; // GB

class diskStorageService {
static getDriveList = () => {
ipcRenderer.send(ipcConsts.GET_DRIVE_LIST);
Expand All @@ -17,18 +14,6 @@ class diskStorageService {
});
});
};

static getAvailableDiskSpace = ({ path }: { path: string }) => {
ipcRenderer.send(ipcConsts.GET_AVAILABLE_DISK_SPACE, { path });
return new Promise<string, Error>((resolve: Function, reject: Function) => {
ipcRenderer.once(ipcConsts.GET_AVAILABLE_DISK_SPACE_SUCCESS, (event, availableSpace) => {
resolve(availableSpace - getBytesfromGb(DRIVE_SPACE_BUFFER));
});
ipcRenderer.once(ipcConsts.GET_AVAILABLE_DISK_SPACE_FAILURE, (event, args) => {
reject(args);
});
});
};
}

export default diskStorageService;
39 changes: 0 additions & 39 deletions app/redux/localNode/actions.js
Expand Up @@ -5,35 +5,12 @@ import { Action, Dispatch } from '/types';

export const SET_ALLOCATION: string = 'SET_ALLOCATION';
export const GET_DRIVES_LIST: string = 'GET_DRIVES_LIST';
export const GET_AVAILABLE_DISK_SPACE: string = 'GET_AVAILABLE_DISK_SPACE';
export const RESET_NODE_SETTINGS: string = 'RESET_NODE_SETTINGS';
export const SET_LOCAL_NODE_SETUP_PROGRESS: string = 'SET_LOCAL_NODE_SETUP_PROGRESS';
export const SET_TOTAL_EARNINGS: string = 'SET_TOTAL_EARNINGS';
export const SET_UPCOMING_EARNINGS: string = 'SET_UPCOMING_EARNINGS';
export const SET_AWARDS_ADDRESS: string = 'SET_AWARDS_ADDRESS';

const getBytesFromGb = (Gb: number) => Gb * 1073741824;

const getReadableSpace = (spaceInBytes: number) => {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (spaceInBytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(spaceInBytes) / Math.log(1024)));
return `${Math.round(spaceInBytes / 1024 ** i)} ${sizes[i]}`;
};

const getAllocatedSpaceList = (availableDiskSpace: ?number, increment: number = getBytesFromGb(150)): Action => {
const allocatedSpaceList = [];
if (availableDiskSpace) {
for (let i = increment; i < availableDiskSpace; i += increment) {
allocatedSpaceList.push({
id: i,
label: getReadableSpace(i)
});
}
}
return allocatedSpaceList;
};

export const getDrivesList = (): Action => async (dispatch: Dispatch): Dispatch => {
try {
const drives = await diskStorageService.getDriveList();
Expand All @@ -44,22 +21,6 @@ export const getDrivesList = (): Action => async (dispatch: Dispatch): Dispatch
}
};

export const getAvailableSpace = (path: string): Action => async (dispatch: Dispatch): Dispatch => {
try {
const availableDiskSpace = await diskStorageService.getAvailableDiskSpace({ path });
dispatch({
type: GET_AVAILABLE_DISK_SPACE,
payload: {
availableDiskSpace: { bytes: availableDiskSpace, readable: getReadableSpace(availableDiskSpace) },
capacityAllocationsList: getAllocatedSpaceList(availableDiskSpace)
}
});
} catch (err) {
dispatch({ type: GET_AVAILABLE_DISK_SPACE, payload: { availableDiskSpace: null } });
throw err;
}
};

export const resetNodeSettings = (): Action => ({ type: RESET_NODE_SETTINGS });

export const getLocalNodeSetupProgress = (): Action => async (dispatch: Dispatch): Dispatch => {
Expand Down
19 changes: 1 addition & 18 deletions app/redux/localNode/reducer.js
@@ -1,23 +1,12 @@
// @flow
import type { Action } from '/types';
import { LOGOUT } from '/redux/auth/actions';
import {
SET_ALLOCATION,
RESET_NODE_SETTINGS,
GET_DRIVES_LIST,
GET_AVAILABLE_DISK_SPACE,
SET_LOCAL_NODE_SETUP_PROGRESS,
SET_TOTAL_EARNINGS,
SET_UPCOMING_EARNINGS,
SET_AWARDS_ADDRESS
} from './actions';
import { SET_ALLOCATION, RESET_NODE_SETTINGS, GET_DRIVES_LIST, SET_LOCAL_NODE_SETUP_PROGRESS, SET_TOTAL_EARNINGS, SET_UPCOMING_EARNINGS, SET_AWARDS_ADDRESS } from './actions';

const initialState = {
drive: null,
capacity: null,
drives: [],
capacityAllocationsList: [],
availableDiskSpace: null,
progress: null,
totalEarnings: null,
upcomingEarnings: null,
Expand All @@ -39,12 +28,6 @@ const reducer = (state: any = initialState, action: Action) => {
} = action;
return { ...state, drives };
}
case GET_AVAILABLE_DISK_SPACE: {
const {
payload: { availableDiskSpace, capacityAllocationsList }
} = action;
return { ...state, availableDiskSpace, capacityAllocationsList };
}
case LOGOUT:
case RESET_NODE_SETTINGS:
return initialState;
Expand Down
3 changes: 0 additions & 3 deletions app/vars/ipcConsts.js
Expand Up @@ -18,9 +18,6 @@ const ipcConsts = {
GET_DRIVE_LIST: 'GET_DRIVE_LIST',
GET_DRIVE_LIST_SUCCESS: 'GET_DRIVE_LIST_SUCCESS',
GET_DRIVE_LIST_FAILURE: 'GET_DRIVE_LIST_FAILURE',
GET_AVAILABLE_DISK_SPACE: 'GET_AVAILABLE_DISK_SPACE',
GET_AVAILABLE_DISK_SPACE_SUCCESS: 'GET_AVAILABLE_DISK_SPACE_SUCCESS',
GET_AVAILABLE_DISK_SPACE_FAILURE: 'GET_AVAILABLE_DISK_SPACE_FAILURE',
PRINT: 'PRINT',
OPEN_WALLET_BACKUP_DIRECTORY: 'OPEN_WALLET_BACKUP_DIRECTORY',
OPEN_WALLET_BACKUP_DIRECTORY_SUCCESS: 'OPEN_WALLET_BACKUP_DIRECTORY_SUCCESS',
Expand Down
69 changes: 39 additions & 30 deletions desktop/diskStorageManager.js
@@ -1,46 +1,55 @@
// @flow
import os from 'os';
import drivelist from 'drivelist';
import { ipcConsts } from '../app/vars';

const checkDiskSpace = require('check-disk-space');
const si = require('systeminformation');

const getMountPoint = (drive: any): string => {
if (drive.mountpoints && drive.mountpoints.length) {
return `${drive.mountpoints[0].path}${os.type() === 'win32' ? '\\' : ''}`;
}
return '/';
const getBytesfromGb = (Gb: number) => Gb * 1073741824;

const getReadableSpace = (spaceInBytes: number) => {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (spaceInBytes === 0) return '0 Byte';
const i = parseInt(Math.floor(Math.log(spaceInBytes) / Math.log(1024)));
return `${Math.round(spaceInBytes / 1024 ** i)} ${sizes[i]}`;
};

type Drive = {
id: string,
mountPoint: string,
label: string
const COMMITMENT_DEFAULT_SIZE = 200; // GB
const DRIVE_SPACE_BUFFER = 50; // GB

const getAllocatedSpaceList = (availableDiskSpace: ?number, increment: number = getBytesfromGb(COMMITMENT_DEFAULT_SIZE)): { id: number, label: string }[] => {
const allocatedSpaceList = [];
if (availableDiskSpace) {
for (let i = increment; i < availableDiskSpace; i += increment) {
allocatedSpaceList.push({
id: i,
label: getReadableSpace(i)
});
}
}
return allocatedSpaceList;
};

class DiskStorageManager {
static getDriveList = ({ event }: { event: any }) => {
drivelist.list((error, drives) => {
if (error) {
event.sender.send(ipcConsts.GET_DRIVE_LIST_FAILURE, error.message);
} else {
const filteredDrives = drives.filter((drive: any) => drive.mountpoints && drive.mountpoints.length);
const mappedDrives: Drive[] = filteredDrives.map((drive: any) => {
static getDriveList = async ({ event }: { event: any }) => {
Promise.all([si.blockDevices(), si.fsSize()])
.then(([mountpoints, sizeMountpoints]) => {
const mountedDrives = mountpoints.filter((mountPoint) => !!mountPoint.mount && !mountPoint.mount.includes('private')); // yields only mounted and non VM
const validSizeMountpoints = sizeMountpoints.filter((mountPoint) => !mountPoint.mount.includes('private'));
const mappedDrives = mountedDrives.map((mountPoint) => {
const volume = validSizeMountpoints.find((validVolume) => validVolume.mount === mountPoint.mount);
const availableSpace = volume ? volume.size - volume.used - Math.max(0, getBytesfromGb(DRIVE_SPACE_BUFFER)) : 0;
return {
id: drive.raw,
mountPoint: getMountPoint(drive),
label: drive.device
id: mountPoint.name,
mountPoint: mountPoint.mount,
label: (os.type() === 'Darwin' || os.type() === 'Linux') && !!mountPoint.label ? mountPoint.label : mountPoint.name,
availableDiskSpace: { bytes: availableSpace, readable: getReadableSpace(availableSpace) },
capacityAllocationsList: getAllocatedSpaceList(availableSpace)
};
});
event.sender.send(ipcConsts.GET_DRIVE_LIST_SUCCESS, mappedDrives);
}
});
};

static getAvailableSpace = ({ event, path }: { event: any, path: string }) => {
checkDiskSpace(path).then((diskSpace) => {
event.sender.send(ipcConsts.GET_AVAILABLE_DISK_SPACE_SUCCESS, diskSpace.free);
});
})
.catch((error) => {
event.sender.send(ipcConsts.GET_DRIVE_LIST_FAILURE, error.message);
});
};
}

Expand Down
4 changes: 0 additions & 4 deletions desktop/eventListners.js
Expand Up @@ -29,10 +29,6 @@ const subscribeToEventListeners = ({ mainWindow }) => {
DiskStorageManager.getDriveList({ event });
});

ipcMain.on(ipcConsts.GET_AVAILABLE_DISK_SPACE, async (event, request) => {
DiskStorageManager.getAvailableSpace({ event, ...request });
});

ipcMain.on(ipcConsts.GET_BALANCE, async (event, request) => {
netService.getBalance({ event, ...request });
});
Expand Down
1 change: 1 addition & 0 deletions desktop/menu.js
Expand Up @@ -11,6 +11,7 @@ class MenuBuilder {
if (process.env.NODE_ENV !== 'production' || process.env.DEBUG_PROD === 'true') {
this.addInspectElementMenu();
}

const template = this.buildMenuTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
Expand Down