Skip to content

Commit

Permalink
Fan speed, autoreload
Browse files Browse the repository at this point in the history
  • Loading branch information
C-D-Lewis committed Oct 23, 2024
1 parent 036bdca commit db2d45c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 18 deletions.
22 changes: 22 additions & 0 deletions apps/monitor/src/plugins/system-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ const getFrequencyPerc = () => {
}
};

/**
* Get current fan speed.
*
* @returns {number} Fan speed in RPM.
*/
const getFanSpeed = () => {
try {
const speed = parseInt(
execSync('cat /sys/devices/platform/cooling_fan/hwmon/*/fan1_input').toString(),
10,
);

return speed || 0;
} catch (e) {
log.error(e);
return 0;
}
};

/**
* Log metrics for system stats.
*
Expand All @@ -69,6 +88,8 @@ module.exports = async (args = {}) => {

const freqPerc = getFrequencyPerc();

const fanSpeed = getFanSpeed();

// Metrics with 'Perc' in the name are treated as a 0-100% range in the dashboard
updateMetrics({
cpu: cpuMinute,
Expand All @@ -78,6 +99,7 @@ module.exports = async (args = {}) => {
diskPerc,
tempRaw,
freqPerc,
fanSpeed,
});
} catch (e) {
log.error(e);
Expand Down
36 changes: 35 additions & 1 deletion dashboard2/src/components/AppArea.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Fabricate } from 'fabricate.js';
import { Fabricate, FabricateComponent } from 'fabricate.js';
import { AppState } from '../types';
import StatRow from './StatRow';
import AppLoader from './AppLoader';
import AppCard from './AppCard';
import DeviceMetrics from './DeviceMetrics';
import { fetchMetricNames } from '../services/conduitService';

declare const fabricate: Fabricate<AppState>;

Expand All @@ -20,6 +21,38 @@ const NoDeviceLabel = () => fabricate('Text')
}))
.setText('No device selected');

/**
* RefreshProgressBar component.
*
* @returns {FabricateComponent} RefreshProgressBar component.
*/
const RefreshProgressBar = () => {
let handle: NodeJS.Timer;
let progress = 100;

return fabricate('div')
.setStyles(({ palette }) => ({
width: '100%',
height: '5px',
backgroundColor: palette.secondary,
transition: '0.5s',
}))
.onCreate((el, state) => {
handle = setInterval(() => {
el.setStyles({ width: `${progress}%` });

// 30s
progress = Math.max(0, progress - 1.67);
if (progress === 0) {
progress = 100;

fetchMetricNames(state);
}
}, 1000);
})
.onDestroy(() => clearInterval(handle));
};

/**
* AppArea component.
*
Expand Down Expand Up @@ -63,6 +96,7 @@ const AppArea = () => {
el.setChildren([
StatRow({ device: selectedDevice }),
fabricate.conditional((s) => !areAppsLoaded(s), AppLoader),
fabricate.conditional(areAppsLoaded, RefreshProgressBar),
fabricate.conditional(areAppsLoaded, DeviceMetrics),
fabricate.conditional(areAppsLoaded, AppCardList),
]);
Expand Down
10 changes: 3 additions & 7 deletions dashboard2/src/components/DeviceMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '../types';
import Theme from '../theme';
import { BUCKET_SIZE } from '../constants';
import { fetchMetric, sendConduitPacket } from '../services/conduitService';
import { fetchMetric, fetchMetricNames } from '../services/conduitService';

declare const fabricate: Fabricate<AppState>;

Expand All @@ -21,6 +21,7 @@ const METRIC_NAME_MAP: Record<string, string> = {
discPerc: 'Disk (%)',
tempRaw: 'Temperature',
freqPerc: 'CPU Frequency (%)',
fanSpeed: 'Fan Speed (RPM)',
};

/**
Expand Down Expand Up @@ -175,7 +176,6 @@ const MetricContainer = ({ name } : { name: string }) => fabricate('Column')
border: `solid 2px ${palette.grey6}`,
}))
.setNarrowStyles({
width: '100%',
margin: '10px 0px',
})
.setChildren([
Expand Down Expand Up @@ -204,11 +204,7 @@ const DeviceMetrics = () => fabricate('Row')
})
.onCreate(async (el, state) => {
// Get the metrics available, each graph loads its own
fabricate.update({ metricNames: [] });
const {
message: metricNames,
} = await sendConduitPacket(state, { to: 'monitor', topic: 'getMetricNames' });
fabricate.update({ metricNames });
fetchMetricNames(state);
})
.onUpdate(async (el, state, keys) => {
const { selectedDevice, metricNames } = state;
Expand Down
32 changes: 22 additions & 10 deletions dashboard2/src/services/conduitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ export const fetchDeviceApps = async (state: AppState, device: Device) => {
*/
export const fetchMetric = async (state: AppState, name: string) => {
const dataKey = fabricate.buildKey('metricData', name);
fabricate.update(
dataKey,
{
buckets: [],
minTime: 0,
maxTime: 0,
minValue: 0,
maxValue: 0,
},
);
// fabricate.update(
// dataKey,
// {
// buckets: [],
// minTime: 0,
// maxTime: 0,
// minValue: 0,
// maxValue: 0,
// },
// );

const res = await sendConduitPacket(
state,
Expand Down Expand Up @@ -214,3 +214,15 @@ export const fetchMetric = async (state: AppState, name: string) => {
},
);
};

/**
* Fetch metric names, triggering update of graphs if they are shown.
*
* @param {AppState} state - App state.
*/
export const fetchMetricNames = async (state: AppState) => {
const {
message: metricNames,
} = await sendConduitPacket(state, { to: 'monitor', topic: 'getMetricNames' });
fabricate.update({ metricNames });
};

0 comments on commit db2d45c

Please sign in to comment.