Skip to content

Commit

Permalink
feature: add sensors regex ignore
Browse files Browse the repository at this point in the history
related to #145
  • Loading branch information
ljuzig committed Sep 25, 2024
1 parent 4646164 commit d34a48c
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 11 deletions.
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

_Check the [documentation](https://github.com/AstraExt/astra-monitor/blob/main/README.md#nethogs) for more details._

- **_Sensors Ignore Regex_**: Now you can use regular expressions to ignore specific sensors based on their name, category, or attribute. This feature allows for more granular control over which sensor data is displayed:
- Sensor Name: Exclude an entire sensor (the one you see in the sensors menu).
- Category: Exclude entire groups of sensors (e.g., "Package", "Core", "Edge").
- Attribute: Exclude specific attributes (e.g., Min, Max, Crit, Alarm).
This powerful filtering mechanism enables users to customize their sensor display, focusing on the most relevant information while reducing clutter from unwanted sensor data.

### Bug fixes

- Addressed `amdgpu_top` v0.8.5 data structure change, now top processes are correctly displayed
Expand Down
15 changes: 15 additions & 0 deletions schemas/org.gnome.shell.extensions.astra-monitor.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,21 @@
<summary>Sensors Temperature Unit</summary>
<description>Temperature Unit of measurement for the sensors</description>
</key>
<key name="sensors-ignored-regex" type="s">
<default>''</default>
<summary>Ignored Sensors Regex</summary>
<description>A regex to match Sensors to ignore</description>
</key>
<key name="sensors-ignored-category-regex" type="s">
<default>''</default>
<summary>Ignored Sensors Category Regex</summary>
<description>A regex to match Sensors Category to ignore</description>
</key>
<key name="sensors-ignored-attribute-regex" type="s">
<default>''</default>
<summary>Ignored Sensors Attribute Regex</summary>
<description>A regex to match Sensors Attribute to ignore</description>
</key>
<key name="sensors-source" enum="org.gnome.shell.extensions.astra-monitor.enum.source-list4">
<default>'auto'</default>
<summary>Sensors Source</summary>
Expand Down
16 changes: 5 additions & 11 deletions src/network/networkMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class NetworkMonitor extends Monitor {
private detectedMaxSpeedsValues: MaxSpeeds;
private interfaceChecks: Record<string, boolean>;
private ignored: string[];
private ignoredRegex: RegExp | null;
private ignoredRegex!: RegExp | null;

private updateNetworkIOTask: CancellableTaskManager<boolean>;
private updateRoutesTask: CancellableTaskManager<boolean>;
Expand Down Expand Up @@ -147,27 +147,21 @@ export default class NetworkMonitor extends Monitor {
});

// Regex ignored interfaces
{
const updateIgnoredRegex = () => {
const regex = Config.get_string('network-ignored-regex');
try {
if(regex === null || regex === '') this.ignoredRegex = null;
else this.ignoredRegex = new RegExp(`^${regex}$`, 'i');
} catch(e) {
this.ignoredRegex = null;
}
}
};

Config.connect(this, 'changed::network-ignored-regex', () => {
this.reset();

const regex = Config.get_string('network-ignored-regex');
try {
if(regex === null || regex === '') this.ignoredRegex = null;
else this.ignoredRegex = new RegExp(`^${regex}$`, 'i');
} catch(e) {
this.ignoredRegex = null;
}
updateIgnoredRegex();
});
updateIgnoredRegex();

Config.connect(
this,
Expand Down
39 changes: 39 additions & 0 deletions src/prefs/sensors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,45 @@ export default class Sensors {
'string'
);

const ignoredSection = PrefsUtils.addExpanderRow(
{ title: _('Ignored Sensors') },
group,
'sensors'
);

PrefsUtils.addTextInputRow(
{
title: _('Ignored Sensors Regex'),
subtitle: _('Completely ignore sensors matching this regex'),
tabs: 1,
},
'sensors-ignored-regex',
ignoredSection,
''
);

PrefsUtils.addTextInputRow(
{
title: _('Ignored Sensors Category Regex'),
subtitle: _('Categories are like "Package", "Core", "Edge", etc.'),
tabs: 1,
},
'sensors-ignored-category-regex',
ignoredSection,
''
);

PrefsUtils.addTextInputRow(
{
title: _('Ignored Sensors Attribute Regex'),
subtitle: _('Attributes are like "Alarm", "Min", "Max", "Crit", etc.'),
tabs: 1,
},
'sensors-ignored-attribute-regex',
ignoredSection,
''
);

const sourcesSection = PrefsUtils.addExpanderRow(
{ title: _('Data Sources') },
group,
Expand Down
21 changes: 21 additions & 0 deletions src/sensors/sensorsMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export default class SensorsMenu extends MenuBase {

this.createSensorsList();
this.addUtilityButtons('sensors');

Config.connect(this, 'changed::sensors-ignored-regex', () => {
this.resetSensorsList();
Utils.sensorsMonitor.requestUpdate('sensorsData');
});
Config.connect(this, 'changed::sensors-ignored-category-regex', () => {
this.resetSensorsList();
Utils.sensorsMonitor.requestUpdate('sensorsData');
});
Config.connect(this, 'changed::sensors-ignored-attribute-regex', () => {
this.resetSensorsList();
Utils.sensorsMonitor.requestUpdate('sensorsData');
});
}

createSensorsList() {
Expand All @@ -82,6 +95,14 @@ export default class SensorsMenu extends MenuBase {
}
}

resetSensorsList() {
for(const [id, sensor] of this.sensors.entries()) {
this.sensorsSection.remove_child(sensor.container);
this.sensors.delete(id);
}
this.noSensorsLabel.show();
}

updateSensorsList(sensors: Map<string, SensorNode>) {
if(sensors.size > 0) this.noSensorsLabel.hide();
else this.noSensorsLabel.show();
Expand Down
101 changes: 101 additions & 0 deletions src/sensors/sensorsMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export default class SensorsMonitor extends Monitor {
private prefTooltipSensor4Source?: SensorSource;
private prefTooltipSensor5Source?: SensorSource;

private ignoredSensorsRegex!: RegExp | null;
private ignoredSensorsCategoryRegex!: RegExp | null;
private ignoredSensorsAttributeRegex!: RegExp | null;

constructor() {
super('Sensors Monitor');

Expand Down Expand Up @@ -170,6 +174,57 @@ export default class SensorsMonitor extends Monitor {
'changed::sensors-header-tooltip-sensor5',
updateTooltipSensor5SourceBind
);

// Regex ignored sensors
const updateIgnoredSensorsRegex = () => {
const regex = Config.get_string('sensors-ignored-regex');
try {
if(regex === null || regex === '') this.ignoredSensorsRegex = null;
else this.ignoredSensorsRegex = new RegExp(`^${regex}$`, 'i');
} catch(e) {
this.ignoredSensorsRegex = null;
}
};

Config.connect(this, 'changed::sensors-ignored-regex', () => {
this.reset();
updateIgnoredSensorsRegex();
});
updateIgnoredSensorsRegex();

// Regex ignored sensors category
const updateIgnoredSensorsCategoryRegex = () => {
const regex = Config.get_string('sensors-ignored-category-regex');
try {
if(regex === null || regex === '') this.ignoredSensorsCategoryRegex = null;
else this.ignoredSensorsCategoryRegex = new RegExp(`^${regex}$`, 'i');
} catch(e) {
this.ignoredSensorsCategoryRegex = null;
}
};

Config.connect(this, 'changed::sensors-ignored-category-regex', () => {
this.reset();
updateIgnoredSensorsCategoryRegex();
});
updateIgnoredSensorsCategoryRegex();

// Regex ignored sensors attribute
const updateIgnoredSensorsAttributeRegex = () => {
const regex = Config.get_string('sensors-ignored-attribute-regex');
try {
if(regex === null || regex === '') this.ignoredSensorsAttributeRegex = null;
else this.ignoredSensorsAttributeRegex = new RegExp(`^${regex}$`, 'i');
} catch(e) {
this.ignoredSensorsAttributeRegex = null;
}
};

Config.connect(this, 'changed::sensors-ignored-attribute-regex', () => {
this.reset();
updateIgnoredSensorsAttributeRegex();
});
updateIgnoredSensorsAttributeRegex();
}

get updateFrequency() {
Expand All @@ -182,6 +237,10 @@ export default class SensorsMonitor extends Monitor {

reset() {
this.updateSensorsDataTask.cancel();

this.ignoredSensorsRegex = null;
this.ignoredSensorsCategoryRegex = null;
this.ignoredSensorsAttributeRegex = null;
}

start() {
Expand Down Expand Up @@ -327,11 +386,25 @@ export default class SensorsMonitor extends Monitor {
deviceLabel =
Utils.capitalize(split[0]) + ' - ' + split[1].replace(/}$/, '');

if(
this.ignoredSensorsRegex !== null &&
this.ignoredSensorsRegex.test(deviceLabel)
) {
continue;
}

device = { name: deviceLabel, children: new Map(), attrs: {} };
data.hwmon.children.set(deviceName, device);
}

for(const [categoryName, hwmonCategory] of hwmonDevice) {
if(
this.ignoredSensorsCategoryRegex !== null &&
this.ignoredSensorsCategoryRegex.test(categoryName)
) {
continue;
}

if(!this.shouldUpdate('hwmon', [deviceName, categoryName])) continue;

let category = device.children.get(categoryName);
Expand All @@ -341,6 +414,13 @@ export default class SensorsMonitor extends Monitor {
}

for(const [attributeName, hwmonAttribute] of hwmonCategory) {
if(
this.ignoredSensorsAttributeRegex !== null &&
this.ignoredSensorsAttributeRegex.test(attributeName)
) {
continue;
}

if(
!this.shouldUpdate('hwmon', [
deviceName,
Expand Down Expand Up @@ -406,6 +486,13 @@ export default class SensorsMonitor extends Monitor {
const parsedData = JSON.parse(lmSensorsDataValue) as any;
if(parsedData) {
for(const [deviceName, deviceData] of Object.entries(parsedData)) {
if(
this.ignoredSensorsRegex !== null &&
this.ignoredSensorsRegex.test(deviceName)
) {
continue;
}

let device = data.lm_sensors.children.get(deviceName);
if(!device) {
device = { name: deviceName, children: new Map(), attrs: {} };
Expand All @@ -426,6 +513,13 @@ export default class SensorsMonitor extends Monitor {
for(const [categoryName, categoryData] of Object.entries(
deviceData as Record<string, unknown>
)) {
if(
this.ignoredSensorsCategoryRegex !== null &&
this.ignoredSensorsCategoryRegex.test(categoryName)
) {
continue;
}

if(categoryName === 'Adapter') continue;

let category = device.children.get(categoryName);
Expand All @@ -437,6 +531,13 @@ export default class SensorsMonitor extends Monitor {
for(const [attributeName, attributeValue] of Object.entries(
categoryData as Record<string, unknown>
)) {
if(
this.ignoredSensorsAttributeRegex !== null &&
this.ignoredSensorsAttributeRegex.test(attributeName)
) {
continue;
}

const value = parseFloat(attributeValue as any);
let unit = '';
if(attributeName !== 'fan')
Expand Down

0 comments on commit d34a48c

Please sign in to comment.