From 303e42535cbaa5b7f99b1ba02c0cdb79a2a36710 Mon Sep 17 00:00:00 2001 From: John Knox Date: Thu, 29 Apr 2021 07:38:31 -0700 Subject: [PATCH] Finish converting CPU plugin to Sandy Summary: Finishes off by converting the rest of the UI. Reviewed By: mweststrate Differential Revision: D28061468 fbshipit-source-id: 8d87d3344fe3937470e566d8c425efc97f38333c --- desktop/plugins/public/cpu/index.tsx | 347 ++++++++++----------------- 1 file changed, 121 insertions(+), 226 deletions(-) diff --git a/desktop/plugins/public/cpu/index.tsx b/desktop/plugins/public/cpu/index.tsx index 89c825c4e..fbd9371ac 100644 --- a/desktop/plugins/public/cpu/index.tsx +++ b/desktop/plugins/public/cpu/index.tsx @@ -15,23 +15,16 @@ import { Panel, theme, Layout, + DetailSidebar, + DataTable, + DataTableColumn, } from 'flipper-plugin'; import adb from 'adbkit'; import TemperatureTable from './TemperatureTable'; -import {Button, Typography} from 'antd'; +import {Button, Typography, Switch} from 'antd'; import {PlayCircleOutlined, PauseCircleOutlined} from '@ant-design/icons'; -import { - Toolbar, - ManagedTable, - colors, - styled, - DetailSidebar, - ToggleButton, -} from 'flipper'; -import React, {useState} from 'react'; - -type TableRows = any; +import React, {useCallback, useState} from 'react'; // we keep vairable name with underline for to physical path mappings on device type CPUFrequency = { @@ -60,56 +53,6 @@ type CPUState = { type ShellCallBack = (output: string) => any; -const ColumnSizes = { - cpu_id: '10%', - scaling_cur_freq: 'flex', - scaling_min_freq: 'flex', - scaling_max_freq: 'flex', - cpuinfo_min_freq: 'flex', - cpuinfo_max_freq: 'flex', -}; - -const Columns = { - cpu_id: { - value: 'CPU ID', - resizable: true, - }, - scaling_cur_freq: { - value: 'Scaling Current', - resizable: true, - }, - scaling_min_freq: { - value: 'Scaling MIN', - resizable: true, - }, - scaling_max_freq: { - value: 'Scaling MAX', - resizable: true, - }, - cpuinfo_min_freq: { - value: 'MIN Frequency', - resizable: true, - }, - cpuinfo_max_freq: { - value: 'MAX Frequency', - resizable: true, - }, - scaling_governor: { - value: 'Scaling Governor', - resizable: true, - }, -}; - -const Heading = styled.div({ - fontWeight: 'bold', - fontSize: 13, - display: 'block', - marginBottom: 10, - '&:not(:first-child)': { - marginTop: 20, - }, -}); - // check if str is a number function isNormalInteger(str: string) { const n = Math.floor(Number(str)); @@ -328,10 +271,14 @@ export function devicePlugin(client: PluginClient<{}, {}>) { }; const onStartMonitor = () => { - if (intervalID) { + if (cpuState.get().monitoring) { return; } + cpuState.update((draft) => { + draft.monitoring = true; + }); + for (let i = 0; i < cpuState.get().cpuCount; ++i) { readAvailableGovernors(i).then((output) => { cpuState.update((draft) => { @@ -341,6 +288,9 @@ export function devicePlugin(client: PluginClient<{}, {}>) { } const update = async () => { + if (!cpuState.get().monitoring) { + return; + } const promises = []; for (let i = 0; i < cpuState.get().cpuCount; ++i) { promises.push(readCoreFrequency(i)); @@ -352,23 +302,15 @@ export function devicePlugin(client: PluginClient<{}, {}>) { }; intervalID = setTimeout(update, 500); - cpuState.update((draft) => { - draft.monitoring = true; - }); }; const onStopMonitor = () => { - if (!intervalID) { - return; - } else { - clearInterval(intervalID); - intervalID = null; - cpuState.update((draft) => { - draft.monitoring = false; - return draft; - }); - cleanup(); - } + intervalID && clearInterval(intervalID); + intervalID = null; + cpuState.update((draft) => { + draft.monitoring = false; + return draft; + }); }; const cleanup = () => { @@ -450,6 +392,59 @@ export function devicePlugin(client: PluginClient<{}, {}>) { }; } +const columns: DataTableColumn[] = [ + {key: 'cpu_id', title: 'CPU ID'}, + {key: 'scaling_cur_freq', title: 'Current Frequency'}, + {key: 'scaling_min_freq', title: 'Scaling min'}, + {key: 'scaling_max_freq', title: 'Scaling max'}, + {key: 'cpuinfo_min_freq', title: 'CPU min'}, + {key: 'cpuinfo_max_freq', title: 'CPU max'}, + {key: 'scaling_governor', title: 'Scaling governor'}, +]; + +const cpuSidebarColumns: DataTableColumn[] = [ + { + key: 'key', + title: 'key', + wrap: true, + }, + { + key: 'value', + title: 'value', + wrap: true, + }, +]; + +const getRowStyle = (freq: CPUFrequency) => { + if (freq.scaling_cur_freq == -2) { + return { + backgroundColor: theme.backgroundWash, + color: theme.textColorPrimary, + fontWeight: 700, + }; + } else if ( + freq.scaling_min_freq != freq.cpuinfo_min_freq && + freq.scaling_min_freq > 0 && + freq.cpuinfo_min_freq > 0 + ) { + return { + backgroundColor: theme.warningColor, + color: theme.textColorPrimary, + fontWeight: 700, + }; + } else if ( + freq.scaling_max_freq != freq.cpuinfo_max_freq && + freq.scaling_max_freq > 0 && + freq.cpuinfo_max_freq > 0 + ) { + return { + backgroundColor: theme.backgroundWash, + color: theme.textColorSecondary, + fontWeight: 700, + }; + } +}; + export function Component() { const instance = usePlugin(devicePlugin); const { @@ -463,92 +458,20 @@ export function Component() { const [selectedIds, setSelectedIds] = useState([]); - const buildRow = (freq: CPUFrequency, idx: number) => { - const selected = selectedIds.indexOf(idx) >= 0; - let style = {}; - if (freq.scaling_cur_freq == -2) { - style = { - style: { - backgroundColor: colors.blueTint30, - color: colors.white, - fontWeight: 700, - }, - }; - } else if ( - freq.scaling_min_freq != freq.cpuinfo_min_freq && - freq.scaling_min_freq > 0 && - freq.cpuinfo_min_freq > 0 - ) { - style = { - style: { - backgroundColor: selected ? colors.red : colors.redTint, - color: colors.red, - fontWeight: 700, - }, - }; - } else if ( - freq.scaling_max_freq != freq.cpuinfo_max_freq && - freq.scaling_max_freq > 0 && - freq.cpuinfo_max_freq > 0 - ) { - style = { - style: { - backgroundColor: colors.yellowTint, - color: colors.yellow, - fontWeight: 700, - }, - }; - } - + const buildRow = (freq: CPUFrequency) => { return { - columns: { - cpu_id: {value: CPU_{freq.cpu_id}}, - scaling_cur_freq: { - value: ( - - {formatFrequency(freq.scaling_cur_freq)} - - ), - }, - scaling_min_freq: { - value: ( - - {formatFrequency(freq.scaling_min_freq)} - - ), - }, - scaling_max_freq: { - value: ( - - {formatFrequency(freq.scaling_max_freq)} - - ), - }, - cpuinfo_min_freq: { - value: ( - - {formatFrequency(freq.cpuinfo_min_freq)} - - ), - }, - cpuinfo_max_freq: { - value: ( - - {formatFrequency(freq.cpuinfo_max_freq)} - - ), - }, - scaling_governor: { - value: {freq.scaling_governor}, - }, - }, - key: freq.cpu_id, - - style, + core: freq.cpu_id, + cpu_id: `CPU_${freq.cpu_id}`, + scaling_cur_freq: formatFrequency(freq.scaling_cur_freq), + scaling_min_freq: formatFrequency(freq.scaling_min_freq), + scaling_max_freq: formatFrequency(freq.scaling_max_freq), + cpuinfo_min_freq: formatFrequency(freq.cpuinfo_min_freq), + cpuinfo_max_freq: formatFrequency(freq.cpuinfo_max_freq), + scaling_governor: freq.scaling_governor, }; }; - const frequencyRows = (cpuFreqs: Array): TableRows => { + const frequencyRows = (cpuFreqs: Array) => { return cpuFreqs.map(buildRow); }; @@ -560,28 +483,24 @@ export function Component() { return ( {freq.scaling_available_freqs.map((freq, idx) => { - const style: React.CSSProperties = {}; - if ( + const bold = freq == info.scaling_cur_freq || freq == info.scaling_min_freq || - freq == info.scaling_max_freq - ) { - style.fontWeight = 'bold'; - } + freq == info.scaling_max_freq; return ( - + {formatFrequency(freq)} {freq == info.scaling_cur_freq && ( - + {' '} (scaling current) )} {freq == info.scaling_min_freq && ( - (scaling min) + (scaling min) )} {freq == info.scaling_max_freq && ( - (scaling max) + (scaling max) )}
@@ -600,13 +519,8 @@ export function Component() { const buildSidebarRow = (key: string, val: any) => { return { - columns: { - key: {value: {key}}, - value: { - value: val, - }, - }, key: key, + value: val, }; }; @@ -634,34 +548,14 @@ export function Component() { return null; } const id = selectedIds[0]; - const cols = { - key: { - value: 'key', - resizable: true, - }, - value: { - value: 'value', - resizable: true, - }, - }; - const colSizes = { - key: '35%', - value: 'flex', - }; return ( - - CPU_{id} - - + CPU Details: CPU_{id} + ); }; @@ -672,7 +566,10 @@ export function Component() { } return ( - + {cpuState.thermalAccessible ? ( ) : ( @@ -683,9 +580,14 @@ export function Component() { ); }; + const setSelected = useCallback((selected: any) => { + setSelectedIds(selected ? [selected.core] : []); + }, []); + return ( - - + + CPU Info + {cpuState.monitoring ? ( )}   {cpuState.hardwareInfo} - Thermal Information - CPU Details {cpuState.displayCPUDetail && selectedIds.length == 0 && ' (Please select a core in the table below)'} - + - - { - setSelectedIds(selectedIds.map(parseInt)); - }} - /> - {renderCPUSidebar()} - {renderThermalSidebar()} - - + + {renderCPUSidebar()} + {renderThermalSidebar()} + ); }