mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-20 15:06:19 -05:00
Creating Controller service which will control WROLPi services, admin, SMART, disks, and status. nginx will fallback to Controller UI when React UI is unavailable (such as during upgrades).
This commit is contained in:
parent
344fe9035f
commit
b9158b60fe
66 changed files with 9920 additions and 327 deletions
|
|
@ -1,69 +1,10 @@
|
|||
import React from 'react';
|
||||
import {Container} from "semantic-ui-react";
|
||||
import {getStatus, saveSettings} from "../../api";
|
||||
import {PageContainer, TabLinks, Toggle} from "../Common";
|
||||
import {PageContainer, TabLinks} from "../Common";
|
||||
import {Route, Routes} from "react-router";
|
||||
import {ThemeContext} from "../../contexts/contexts";
|
||||
import {Header, Loader, Segment} from "../Theme";
|
||||
import {DownloadsPage} from "./Downloads";
|
||||
import {SettingsPage} from "./Settings";
|
||||
import {StatusPage} from "./Status";
|
||||
|
||||
class WROLMode extends React.Component {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
WROLMode: false,
|
||||
}
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
try {
|
||||
const {wrol_mode} = await getStatus();
|
||||
this.setState({ready: true, WROLMode: wrol_mode});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
toggleWROLMode = async (checked) => {
|
||||
// Handle WROL Mode toggling by itself so that other settings are not modified.
|
||||
let config = {
|
||||
wrol_mode: checked,
|
||||
}
|
||||
await saveSettings(config);
|
||||
this.setState({disabled: !this.state.WROLMode, WROLMode: checked});
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.ready === false) {
|
||||
return <Loader active inline='centered'/>
|
||||
}
|
||||
|
||||
return <ThemeContext.Consumer>
|
||||
{({t}) => <Container fluid>
|
||||
|
||||
<Segment>
|
||||
<Header as="h1">WROL Mode</Header>
|
||||
<Header as='h4'>
|
||||
Enable read-only mode. No content can be deleted or modified. Enable this when the SHTF and you
|
||||
want to prevent any potential loss of data.
|
||||
</Header>
|
||||
<p {...t}>
|
||||
Note: User settings and tags can still be modified.
|
||||
</p>
|
||||
<Toggle
|
||||
checked={this.state.WROLMode}
|
||||
onChange={this.toggleWROLMode}
|
||||
label={this.state.WROLMode ? 'WROL Mode Enabled' : 'WROL Mode Disabled'}
|
||||
/>
|
||||
</Segment>
|
||||
|
||||
</Container>}
|
||||
</ThemeContext.Consumer>
|
||||
}
|
||||
}
|
||||
import {ControllerPage} from "./ControllerPage";
|
||||
|
||||
export default function AdminRoute() {
|
||||
|
||||
|
|
@ -71,7 +12,7 @@ export default function AdminRoute() {
|
|||
{text: 'Downloads', to: '/admin', key: 'admin', end: true},
|
||||
{text: 'Settings', to: '/admin/settings', key: 'settings'},
|
||||
{text: 'Status', to: '/admin/status', key: 'status'},
|
||||
{text: 'WROL Mode', to: '/admin/wrol', key: 'wrol'},
|
||||
{text: 'Controller', to: '/admin/controller', key: 'controller'},
|
||||
];
|
||||
|
||||
return <PageContainer>
|
||||
|
|
@ -80,7 +21,7 @@ export default function AdminRoute() {
|
|||
<Route path='/' exact element={<DownloadsPage/>}/>
|
||||
<Route path='settings' exact element={<SettingsPage/>}/>
|
||||
<Route path='status' exact element={<StatusPage/>}/>
|
||||
<Route path='wrol' exact element={<WROLMode/>}/>
|
||||
<Route path='controller' exact element={<ControllerPage/>}/>
|
||||
</Routes>
|
||||
</PageContainer>
|
||||
}
|
||||
|
|
|
|||
609
app/src/components/admin/ControllerPage.js
Normal file
609
app/src/components/admin/ControllerPage.js
Normal file
|
|
@ -0,0 +1,609 @@
|
|||
import React from 'react';
|
||||
import {Container, Icon} from "semantic-ui-react";
|
||||
import {Button, Header, Loader, Modal, Segment, Table} from "../Theme";
|
||||
import {
|
||||
APIButton,
|
||||
HotspotToggle,
|
||||
ThrottleToggle,
|
||||
Toggle,
|
||||
} from "../Common";
|
||||
import {useDockerized} from "../../hooks/customHooks";
|
||||
import {
|
||||
getServices,
|
||||
startService,
|
||||
stopService,
|
||||
restartService,
|
||||
enableService,
|
||||
disableService,
|
||||
getServiceLogs,
|
||||
getDisks,
|
||||
getMounts,
|
||||
getSmartStatus,
|
||||
restartServices,
|
||||
} from "../../api/controller";
|
||||
import {toast} from "react-semantic-toasts-2";
|
||||
import {RestartButton, ShutdownButton} from "./Settings";
|
||||
|
||||
|
||||
// Service status color mapping
|
||||
const statusColors = {
|
||||
running: 'green',
|
||||
stopped: 'red',
|
||||
failed: 'red',
|
||||
unknown: 'yellow',
|
||||
};
|
||||
|
||||
|
||||
function ServiceRow({service, onAction, dockerized}) {
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
const [logsOpen, setLogsOpen] = React.useState(false);
|
||||
const [logs, setLogs] = React.useState('');
|
||||
const [logsLoading, setLogsLoading] = React.useState(false);
|
||||
|
||||
const handleAction = async (action, actionFn) => {
|
||||
setLoading(true);
|
||||
try {
|
||||
await actionFn(service.name);
|
||||
toast({
|
||||
type: 'success',
|
||||
title: `${action} ${service.name}`,
|
||||
description: `Successfully ${action.toLowerCase()}ed ${service.name}`,
|
||||
time: 3000,
|
||||
});
|
||||
if (onAction) onAction();
|
||||
} catch (e) {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: `${action} Failed`,
|
||||
description: e.message,
|
||||
time: 5000,
|
||||
});
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleViewLogs = async () => {
|
||||
setLogsOpen(true);
|
||||
setLogsLoading(true);
|
||||
try {
|
||||
const result = await getServiceLogs(service.name, 100);
|
||||
setLogs(result.logs || 'No logs available');
|
||||
} catch (e) {
|
||||
setLogs(`Error fetching logs: ${e.message}`);
|
||||
} finally {
|
||||
setLogsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const isRunning = service.status === 'running';
|
||||
const isStopped = service.status === 'stopped';
|
||||
const statusColor = statusColors[service.status] || 'grey';
|
||||
|
||||
// Build view URL if service is viewable
|
||||
let viewUrl = null;
|
||||
if (service.viewable && service.port && isRunning) {
|
||||
const protocol = service.use_https ? 'https' : 'http';
|
||||
const host = window.location.hostname;
|
||||
const path = service.view_path || '';
|
||||
viewUrl = `${protocol}://${host}:${service.port}${path}`;
|
||||
}
|
||||
|
||||
return (
|
||||
<Table.Row>
|
||||
<Table.Cell>
|
||||
<strong>{service.name}</strong>
|
||||
{service.description && <div style={{fontSize: '0.9em', color: '#888'}}>{service.description}</div>}
|
||||
</Table.Cell>
|
||||
<Table.Cell>{service.port || '-'}</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Icon name='circle' color={statusColor}/>
|
||||
{service.status}
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
{isRunning ? (
|
||||
<Button
|
||||
size='small'
|
||||
icon='stop'
|
||||
color='red'
|
||||
disabled={loading}
|
||||
loading={loading}
|
||||
onClick={() => handleAction('Stop', stopService)}
|
||||
/>
|
||||
) : (
|
||||
<Button
|
||||
size='small'
|
||||
icon='play'
|
||||
color='green'
|
||||
disabled={loading}
|
||||
loading={loading}
|
||||
onClick={() => handleAction('Start', startService)}
|
||||
/>
|
||||
)}
|
||||
<Button
|
||||
size='small'
|
||||
icon='refresh'
|
||||
color='yellow'
|
||||
disabled={loading}
|
||||
loading={loading}
|
||||
onClick={() => handleAction('Restart', restartService)}
|
||||
/>
|
||||
<Modal
|
||||
open={logsOpen}
|
||||
onClose={() => setLogsOpen(false)}
|
||||
onOpen={handleViewLogs}
|
||||
trigger={<Button size='small' icon='file text' content='Logs' color='blue'/>}
|
||||
size='fullscreen'
|
||||
>
|
||||
<Modal.Header>Logs: {service.name}</Modal.Header>
|
||||
<Modal.Content scrolling>
|
||||
{logsLoading ? (
|
||||
<Loader active inline='centered'/>
|
||||
) : (
|
||||
<pre style={{
|
||||
whiteSpace: 'pre-wrap',
|
||||
wordWrap: 'break-word',
|
||||
maxHeight: '400px',
|
||||
overflow: 'auto',
|
||||
backgroundColor: '#1a1a1a',
|
||||
color: '#00ff00',
|
||||
padding: '1em',
|
||||
fontFamily: 'monospace',
|
||||
fontSize: '0.85em',
|
||||
}}>
|
||||
{logs}
|
||||
</pre>
|
||||
)}
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<Button onClick={() => setLogsOpen(false)}>Close</Button>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
{viewUrl && (
|
||||
<Button
|
||||
size='small'
|
||||
icon='external'
|
||||
content='View'
|
||||
as='a'
|
||||
href={viewUrl}
|
||||
target='_blank'
|
||||
rel='noopener noreferrer'
|
||||
color='violet'
|
||||
/>
|
||||
)}
|
||||
</Table.Cell>
|
||||
{!dockerized && (
|
||||
<Table.Cell>
|
||||
<Toggle
|
||||
checked={service.enabled === true}
|
||||
disabled={service.enabled === null || loading}
|
||||
onChange={async (checked) => {
|
||||
if (checked) {
|
||||
await handleAction('Enable', enableService);
|
||||
} else {
|
||||
await handleAction('Disable', disableService);
|
||||
}
|
||||
}}
|
||||
label={service.enabled ? 'Enabled' : 'Disabled'}
|
||||
/>
|
||||
</Table.Cell>
|
||||
)}
|
||||
</Table.Row>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function ServicesSection() {
|
||||
const [services, setServices] = React.useState([]);
|
||||
const [loading, setLoading] = React.useState(true);
|
||||
const [error, setError] = React.useState(null);
|
||||
const [restarting, setRestarting] = React.useState(false);
|
||||
const dockerized = useDockerized();
|
||||
|
||||
const handleRestartServices = async () => {
|
||||
setRestarting(true);
|
||||
try {
|
||||
await restartServices();
|
||||
toast({
|
||||
type: 'success',
|
||||
title: 'Services Restarting',
|
||||
description: 'All WROLPi services are being restarted.',
|
||||
time: 5000,
|
||||
});
|
||||
// Refresh service list after restart initiated
|
||||
fetchServices();
|
||||
} catch (e) {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Restart Failed',
|
||||
description: e.message,
|
||||
time: 5000,
|
||||
});
|
||||
} finally {
|
||||
setRestarting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const fetchServices = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const result = await getServices();
|
||||
setServices(Array.isArray(result) ? result : []);
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
setServices([]);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
React.useEffect(() => {
|
||||
fetchServices();
|
||||
// Refresh every 10 seconds
|
||||
const interval = setInterval(fetchServices, 10000);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
if (loading && services.length === 0) {
|
||||
return <Segment>
|
||||
<Header as='h3'>Services</Header>
|
||||
<Loader active inline='centered'/>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Segment>
|
||||
<Header as='h3'>Services</Header>
|
||||
<p style={{color: 'red'}}>Error loading services: {error}</p>
|
||||
<Button onClick={fetchServices}>Retry</Button>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
return (
|
||||
<Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='server'/>
|
||||
Services
|
||||
</Header>
|
||||
<Table unstackable striped>
|
||||
<Table.Header>
|
||||
<Table.Row>
|
||||
<Table.HeaderCell>Service</Table.HeaderCell>
|
||||
<Table.HeaderCell>Port</Table.HeaderCell>
|
||||
<Table.HeaderCell>Status</Table.HeaderCell>
|
||||
<Table.HeaderCell>Actions</Table.HeaderCell>
|
||||
{!dockerized && <Table.HeaderCell>Boot</Table.HeaderCell>}
|
||||
</Table.Row>
|
||||
</Table.Header>
|
||||
<Table.Body>
|
||||
{services.map(service => (
|
||||
<ServiceRow
|
||||
dockerized={dockerized}
|
||||
key={service.name}
|
||||
service={service}
|
||||
onAction={fetchServices}
|
||||
/>
|
||||
))}
|
||||
</Table.Body>
|
||||
<Table.Footer>
|
||||
<Table.Row>
|
||||
<Table.HeaderCell colSpan={dockerized ? 4 : 5}>
|
||||
<APIButton
|
||||
color='yellow'
|
||||
onClick={handleRestartServices}
|
||||
confirmContent='Are you sure you want to restart all WROLPi services?'
|
||||
confirmButton='Restart Services'
|
||||
disabled={restarting}
|
||||
>
|
||||
<Icon name='refresh'/>
|
||||
{restarting ? 'Restarting...' : 'Restart All Services'}
|
||||
</APIButton>
|
||||
</Table.HeaderCell>
|
||||
</Table.Row>
|
||||
</Table.Footer>
|
||||
</Table>
|
||||
{services.length === 0 && (
|
||||
<p>No services found. Services may not be available in this environment.</p>
|
||||
)}
|
||||
</Segment>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Helper to get health status color from SMART assessment
|
||||
const getHealthColor = (assessment) => {
|
||||
if (!assessment) return 'grey';
|
||||
const upper = assessment.toUpperCase();
|
||||
if (upper === 'PASS') return 'green';
|
||||
if (upper === 'WARN') return 'yellow';
|
||||
if (upper === 'FAIL') return 'red';
|
||||
return 'grey';
|
||||
};
|
||||
|
||||
// Helper to format power-on hours
|
||||
const formatPowerOnHours = (hours) => {
|
||||
if (hours === null || hours === undefined) return '-';
|
||||
const days = Math.floor(hours / 24);
|
||||
const years = Math.floor(days / 365);
|
||||
if (years > 0) return `${hours.toLocaleString()} hrs (${years}y ${days % 365}d)`;
|
||||
if (days > 0) return `${hours.toLocaleString()} hrs (${days}d)`;
|
||||
return `${hours} hrs`;
|
||||
};
|
||||
|
||||
|
||||
function SmartDetailsModal({drive, open, onClose}) {
|
||||
if (!drive) return null;
|
||||
|
||||
const healthColor = getHealthColor(drive.assessment);
|
||||
|
||||
return (
|
||||
<Modal open={open} onClose={onClose} size='small'>
|
||||
<Modal.Header>
|
||||
<Icon name='heartbeat'/>
|
||||
SMART Details: {drive.device}
|
||||
</Modal.Header>
|
||||
<Modal.Content>
|
||||
<Table definition unstackable>
|
||||
<Table.Body>
|
||||
<Table.Row>
|
||||
<Table.Cell width={6}>Model</Table.Cell>
|
||||
<Table.Cell>{drive.model || '-'}</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Serial</Table.Cell>
|
||||
<Table.Cell>{drive.serial || '-'}</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Capacity</Table.Cell>
|
||||
<Table.Cell>{drive.capacity || '-'}</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Assessment</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Icon name='circle' color={healthColor}/>
|
||||
{drive.assessment || 'Unknown'}
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Temperature</Table.Cell>
|
||||
<Table.Cell>
|
||||
{drive.temperature !== null && drive.temperature !== undefined
|
||||
? `${drive.temperature}°C`
|
||||
: '-'}
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Power-On Hours</Table.Cell>
|
||||
<Table.Cell>{formatPowerOnHours(drive.power_on_hours)}</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Reallocated Sectors</Table.Cell>
|
||||
<Table.Cell style={{color: drive.reallocated_sectors > 0 ? 'orange' : 'inherit'}}>
|
||||
{drive.reallocated_sectors !== null && drive.reallocated_sectors !== undefined
|
||||
? drive.reallocated_sectors
|
||||
: '-'}
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>Pending Sectors</Table.Cell>
|
||||
<Table.Cell style={{color: drive.pending_sectors > 0 ? 'orange' : 'inherit'}}>
|
||||
{drive.pending_sectors !== null && drive.pending_sectors !== undefined
|
||||
? drive.pending_sectors
|
||||
: '-'}
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
<Table.Cell>SMART Enabled</Table.Cell>
|
||||
<Table.Cell>{drive.smart_enabled ? 'Yes' : 'No'}</Table.Cell>
|
||||
</Table.Row>
|
||||
</Table.Body>
|
||||
</Table>
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<Button onClick={onClose}>Close</Button>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function DiskSection() {
|
||||
const [disks, setDisks] = React.useState([]);
|
||||
const [mounts, setMounts] = React.useState([]);
|
||||
const [smartDrives, setSmartDrives] = React.useState([]);
|
||||
const [smartAvailable, setSmartAvailable] = React.useState(true);
|
||||
const [loading, setLoading] = React.useState(true);
|
||||
const [error, setError] = React.useState(null);
|
||||
const [selectedDrive, setSelectedDrive] = React.useState(null);
|
||||
const [detailsOpen, setDetailsOpen] = React.useState(false);
|
||||
const dockerized = useDockerized();
|
||||
|
||||
const fetchDiskInfo = async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const [disksResult, mountsResult, smartResult] = await Promise.allSettled([
|
||||
getDisks(),
|
||||
getMounts(),
|
||||
getSmartStatus(),
|
||||
]);
|
||||
|
||||
if (disksResult.status === 'fulfilled') {
|
||||
setDisks(Array.isArray(disksResult.value) ? disksResult.value : []);
|
||||
}
|
||||
if (mountsResult.status === 'fulfilled') {
|
||||
setMounts(Array.isArray(mountsResult.value) ? mountsResult.value : []);
|
||||
}
|
||||
if (smartResult.status === 'fulfilled' && smartResult.value) {
|
||||
// Handle new API format: {available: bool, drives: [...]}
|
||||
const smartData = smartResult.value;
|
||||
setSmartAvailable(smartData.available !== false);
|
||||
setSmartDrives(Array.isArray(smartData.drives) ? smartData.drives : []);
|
||||
} else {
|
||||
setSmartAvailable(false);
|
||||
setSmartDrives([]);
|
||||
}
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleShowDetails = (drive) => {
|
||||
setSelectedDrive(drive);
|
||||
setDetailsOpen(true);
|
||||
};
|
||||
|
||||
React.useEffect(() => {
|
||||
fetchDiskInfo();
|
||||
}, []);
|
||||
|
||||
if (dockerized) {
|
||||
return (
|
||||
<Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='disk'/>
|
||||
Disk Management
|
||||
</Header>
|
||||
<p>Disk management is not available in Docker environments.</p>
|
||||
</Segment>
|
||||
);
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return <Segment>
|
||||
<Header as='h3'>Disk Management</Header>
|
||||
<Loader active inline='centered'/>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
return (
|
||||
<Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='disk'/>
|
||||
Disk Management
|
||||
</Header>
|
||||
|
||||
{error && <p style={{color: 'orange'}}>Some disk information unavailable: {error}</p>}
|
||||
|
||||
<Header as='h4'>Current Mounts</Header>
|
||||
{mounts.length > 0 ? (
|
||||
<Table unstackable striped compact>
|
||||
<Table.Header>
|
||||
<Table.Row>
|
||||
<Table.HeaderCell>Device</Table.HeaderCell>
|
||||
<Table.HeaderCell>Mount Point</Table.HeaderCell>
|
||||
<Table.HeaderCell>Filesystem</Table.HeaderCell>
|
||||
</Table.Row>
|
||||
</Table.Header>
|
||||
<Table.Body>
|
||||
{mounts.map((mount, idx) => (
|
||||
<Table.Row key={idx}>
|
||||
<Table.Cell>{mount.device}</Table.Cell>
|
||||
<Table.Cell>{mount.mount_point}</Table.Cell>
|
||||
<Table.Cell>{mount.fstype}</Table.Cell>
|
||||
</Table.Row>
|
||||
))}
|
||||
</Table.Body>
|
||||
</Table>
|
||||
) : (
|
||||
<p>No mounts found.</p>
|
||||
)}
|
||||
|
||||
<Header as='h4'>SMART Health</Header>
|
||||
{!smartAvailable ? (
|
||||
<p style={{color: '#888'}}>SMART monitoring not available (pySMART not installed or not supported)</p>
|
||||
) : smartDrives.length > 0 ? (
|
||||
<Table unstackable striped compact>
|
||||
<Table.Header>
|
||||
<Table.Row>
|
||||
<Table.HeaderCell>Device</Table.HeaderCell>
|
||||
<Table.HeaderCell>Health</Table.HeaderCell>
|
||||
<Table.HeaderCell>Temperature</Table.HeaderCell>
|
||||
<Table.HeaderCell>Actions</Table.HeaderCell>
|
||||
</Table.Row>
|
||||
</Table.Header>
|
||||
<Table.Body>
|
||||
{smartDrives.map((drive) => (
|
||||
<Table.Row key={drive.device}>
|
||||
<Table.Cell>{drive.device}</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Icon
|
||||
name='circle'
|
||||
color={getHealthColor(drive.assessment)}
|
||||
/>
|
||||
{drive.assessment || 'Unknown'}
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
{drive.temperature !== null && drive.temperature !== undefined
|
||||
? `${drive.temperature}°C`
|
||||
: '-'}
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Button
|
||||
size='small'
|
||||
icon='info'
|
||||
content='Details'
|
||||
onClick={() => handleShowDetails(drive)}
|
||||
/>
|
||||
</Table.Cell>
|
||||
</Table.Row>
|
||||
))}
|
||||
</Table.Body>
|
||||
</Table>
|
||||
) : (
|
||||
<p>No SMART-capable drives detected.</p>
|
||||
)}
|
||||
|
||||
<SmartDetailsModal
|
||||
drive={selectedDrive}
|
||||
open={detailsOpen}
|
||||
onClose={() => setDetailsOpen(false)}
|
||||
/>
|
||||
|
||||
<Button onClick={fetchDiskInfo} style={{marginTop: '1em'}}>
|
||||
<Icon name='refresh'/>
|
||||
Refresh
|
||||
</Button>
|
||||
</Segment>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function AdminControlsSection() {
|
||||
const dockerized = useDockerized();
|
||||
|
||||
return (
|
||||
<Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='settings'/>
|
||||
Hardware Controls
|
||||
</Header>
|
||||
|
||||
<HotspotToggle/>
|
||||
<ThrottleToggle/>
|
||||
|
||||
{!dockerized && (
|
||||
<div style={{marginTop: '1em'}}>
|
||||
<RestartButton/>
|
||||
<ShutdownButton/>
|
||||
</div>
|
||||
)}
|
||||
</Segment>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function ControllerPage() {
|
||||
return (
|
||||
<Container fluid>
|
||||
<AdminControlsSection/>
|
||||
<ServicesSection/>
|
||||
<DiskSection/>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
export default ControllerPage;
|
||||
169
app/src/components/admin/ControllerPage.test.js
Normal file
169
app/src/components/admin/ControllerPage.test.js
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
import React from 'react';
|
||||
import {render, screen, waitFor} from '@testing-library/react';
|
||||
import {BrowserRouter} from 'react-router';
|
||||
import {ThemeContext, SettingsContext} from '../../contexts/contexts';
|
||||
import {ControllerPage} from './ControllerPage';
|
||||
|
||||
// Mock the controller API
|
||||
jest.mock('../../api/controller', () => ({
|
||||
getServices: jest.fn().mockResolvedValue([
|
||||
{
|
||||
name: 'wrolpi-api',
|
||||
port: 8081,
|
||||
status: 'running',
|
||||
viewable: true,
|
||||
view_path: '/docs',
|
||||
enabled: true,
|
||||
},
|
||||
{
|
||||
name: 'wrolpi-app',
|
||||
port: 3000,
|
||||
status: 'running',
|
||||
viewable: true,
|
||||
view_path: '',
|
||||
enabled: true,
|
||||
},
|
||||
]),
|
||||
startService: jest.fn().mockResolvedValue({success: true}),
|
||||
stopService: jest.fn().mockResolvedValue({success: true}),
|
||||
restartService: jest.fn().mockResolvedValue({success: true}),
|
||||
enableService: jest.fn().mockResolvedValue({success: true}),
|
||||
disableService: jest.fn().mockResolvedValue({success: true}),
|
||||
getServiceLogs: jest.fn().mockResolvedValue({logs: 'Sample log output'}),
|
||||
getDisks: jest.fn().mockResolvedValue([]),
|
||||
getMounts: jest.fn().mockResolvedValue([]),
|
||||
getSmartStatus: jest.fn().mockResolvedValue({}),
|
||||
restartServices: jest.fn().mockResolvedValue({success: true}),
|
||||
}));
|
||||
|
||||
|
||||
// Mock useDockerized hook
|
||||
jest.mock('../../hooks/customHooks', () => ({
|
||||
useDockerized: jest.fn().mockReturnValue(false),
|
||||
useHotspot: jest.fn().mockReturnValue({on: true, setHotspot: jest.fn()}),
|
||||
useThrottle: jest.fn().mockReturnValue({on: false, setThrottle: jest.fn()}),
|
||||
}));
|
||||
|
||||
// Mock Common.js components that have complex dependencies
|
||||
jest.mock('../Common', () => {
|
||||
const actual = jest.requireActual('../Common');
|
||||
return {
|
||||
...actual,
|
||||
HotspotToggle: () => <div data-testid="hotspot-toggle">HotspotToggle</div>,
|
||||
ThrottleToggle: () => <div data-testid="throttle-toggle">ThrottleToggle</div>,
|
||||
Toggle: ({label, checked, onChange}) => (
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={checked || false}
|
||||
onChange={e => onChange && onChange(e.target.checked)}
|
||||
/>
|
||||
{label}
|
||||
</label>
|
||||
),
|
||||
APIButton: ({children, onClick, disabled}) => (
|
||||
<button onClick={onClick} disabled={disabled}>{children}</button>
|
||||
),
|
||||
};
|
||||
});
|
||||
|
||||
// Mock Settings.js components
|
||||
jest.mock('./Settings', () => ({
|
||||
RestartButton: () => <button data-testid="restart-button">Restart</button>,
|
||||
ShutdownButton: () => <button data-testid="shutdown-button">Shutdown</button>,
|
||||
}));
|
||||
|
||||
const defaultTheme = {
|
||||
t: {style: {}},
|
||||
inverted: false,
|
||||
};
|
||||
|
||||
const defaultSettings = {
|
||||
settings: {},
|
||||
saveSettings: jest.fn(),
|
||||
fetchSettings: jest.fn(),
|
||||
};
|
||||
|
||||
const renderControllerPage = () => {
|
||||
return render(
|
||||
<BrowserRouter>
|
||||
<ThemeContext.Provider value={defaultTheme}>
|
||||
<SettingsContext.Provider value={defaultSettings}>
|
||||
<ControllerPage/>
|
||||
</SettingsContext.Provider>
|
||||
</ThemeContext.Provider>
|
||||
</BrowserRouter>
|
||||
);
|
||||
};
|
||||
|
||||
describe('ControllerPage', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
// Reset useDockerized to false for regular tests
|
||||
const {useDockerized} = require('../../hooks/customHooks');
|
||||
useDockerized.mockReturnValue(false);
|
||||
});
|
||||
|
||||
test('renders Services section', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Services')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
test('renders Disk Management section', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Disk Management')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
test('renders Hardware Controls section', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Hardware Controls')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
test('calls getServices on mount', async () => {
|
||||
const {getServices} = require('../../api/controller');
|
||||
renderControllerPage();
|
||||
// Verify getServices was called
|
||||
await waitFor(() => {
|
||||
expect(getServices).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
test('shows Restart and Shutdown buttons in non-Docker mode', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByTestId('restart-button')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('shutdown-button')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('ControllerPage in Docker mode', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
const {useDockerized} = require('../../hooks/customHooks');
|
||||
useDockerized.mockReturnValue(true);
|
||||
});
|
||||
|
||||
test('hides reboot/shutdown buttons in Docker mode', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Hardware Controls')).toBeInTheDocument();
|
||||
});
|
||||
// Shutdown and Restart buttons should not be visible
|
||||
expect(screen.queryByTestId('shutdown-button')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('restart-button')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('shows disk management unavailable message in Docker mode', async () => {
|
||||
renderControllerPage();
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Disk management is not available in Docker environments.')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import React from "react";
|
||||
import {SettingsContext, StatusContext} from "../../contexts/contexts";
|
||||
import {checkUpgrade, postRestart, postShutdown, triggerUpgrade} from "../../api";
|
||||
import {checkUpgrade, postRestart, postShutdown, saveSettings as saveSettingsApi, triggerUpgrade} from "../../api";
|
||||
import {
|
||||
Button,
|
||||
Divider,
|
||||
|
|
@ -14,10 +14,8 @@ import {ButtonGroup, Container, Dimmer, Dropdown, GridColumn, GridRow, Icon, Inp
|
|||
import {
|
||||
APIButton,
|
||||
ErrorMessage,
|
||||
HotspotToggle,
|
||||
InfoPopup,
|
||||
RefreshHeader,
|
||||
ThrottleToggle,
|
||||
Toggle,
|
||||
useMessageDismissal,
|
||||
WROLModeMessage
|
||||
|
|
@ -107,8 +105,8 @@ function UpgradeSegment() {
|
|||
try {
|
||||
const response = await triggerUpgrade();
|
||||
if (response.ok) {
|
||||
// Redirect to maintenance page
|
||||
window.location.href = '/maintenance.html';
|
||||
// Redirect to Controller UI for upgrade status
|
||||
window.location.href = '/controller/?upgrade=true';
|
||||
}
|
||||
} catch (e) {
|
||||
setUpgrading(false);
|
||||
|
|
@ -250,6 +248,47 @@ function toApiLogLevel(logLevel) {
|
|||
}[logLevel]
|
||||
}
|
||||
|
||||
function WROLModeSection() {
|
||||
const {settings, fetchSettings} = React.useContext(SettingsContext);
|
||||
const wrolMode = settings?.wrol_mode || false;
|
||||
|
||||
const toggleWROLMode = async (checked) => {
|
||||
try {
|
||||
await saveSettingsApi({wrol_mode: checked});
|
||||
await fetchSettings(); // Refresh context immediately
|
||||
} catch (e) {
|
||||
toast({
|
||||
type: 'error',
|
||||
title: 'Failed to toggle WROL Mode',
|
||||
description: e.message,
|
||||
time: 5000,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='shield'/>
|
||||
WROL Mode
|
||||
</Header>
|
||||
<p>
|
||||
Enable read-only mode. No content can be deleted or modified.
|
||||
Enable this when the SHTF and you want to prevent any potential loss of data.
|
||||
</p>
|
||||
<p style={{fontSize: '0.9em', color: '#888'}}>
|
||||
Note: User settings and tags can still be modified.
|
||||
</p>
|
||||
<Toggle
|
||||
checked={wrolMode}
|
||||
onChange={toggleWROLMode}
|
||||
label={wrolMode ? 'WROL Mode Enabled' : 'WROL Mode Disabled'}
|
||||
/>
|
||||
</Segment>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
export function SettingsPage() {
|
||||
const [disabled, setDisabled] = React.useState(false);
|
||||
const [editSpecialDirectories, setEditSpecialDirectories] = React.useState(false);
|
||||
|
|
@ -579,15 +618,6 @@ export function SettingsPage() {
|
|||
body = <ErrorMessage>Unable to fetch settings</ErrorMessage>
|
||||
}
|
||||
|
||||
const controlSegment = <Segment>
|
||||
<Header as='h3'>Control WROLPi</Header>
|
||||
<HotspotToggle/>
|
||||
<ThrottleToggle/>
|
||||
|
||||
<RestartButton/>
|
||||
<ShutdownButton/>
|
||||
</Segment>;
|
||||
|
||||
const configsSegment = <Segment>
|
||||
<RefreshHeader
|
||||
header='Configs'
|
||||
|
|
@ -610,8 +640,6 @@ export function SettingsPage() {
|
|||
return <Container fluid>
|
||||
<WROLModeMessage content='Settings are disabled because WROL Mode is enabled.'/>
|
||||
|
||||
{controlSegment}
|
||||
|
||||
<Segment>
|
||||
<Header as='h2'>Settings</Header>
|
||||
{body}
|
||||
|
|
@ -621,6 +649,8 @@ export function SettingsPage() {
|
|||
|
||||
<UpgradeSegment/>
|
||||
|
||||
<WROLModeSection/>
|
||||
|
||||
<Segment>
|
||||
<Header as='h1'>Browser Settings</Header>
|
||||
<APIButton onClick={clearAll}>Show All Hints</APIButton>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue