Skip to content

Commit

Permalink
fix flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
Richardvdketterij committed Jan 17, 2024
1 parent fe86d0f commit 61c7be3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@
# \author Rein Appeldoorn

import collections
import rclpy
import socket
import psutil
import traceback

from diagnostic_msgs.msg import DiagnosticStatus

from diagnostic_updater import DiagnosticTask, Updater

import psutil

import rclpy
from rclpy.node import Node


class CpuTask(DiagnosticTask):
def __init__(self, warning_percentage=90, window=1):
DiagnosticTask.__init__(self, "CPU Information")
DiagnosticTask.__init__(self, 'CPU Information')

self._warning_percentage = int(warning_percentage)
self._readings = collections.deque(maxlen=window)
Expand All @@ -64,20 +68,20 @@ def run(self, stat):
cpu_percentages = self._get_average_reading()
cpu_average = sum(cpu_percentages) / len(cpu_percentages)

stat.add("CPU Load Average", "{:.2f}".format(cpu_average))
stat.add('CPU Load Average', '{:.2f}'.format(cpu_average))

warn = False
for idx, val in enumerate(cpu_percentages):
stat.add("CPU {} Load".format(idx), "{:.2f}".format(val))
if val > self._warning_percentage:
for idx, cpu_percentage in enumerate(cpu_percentages):
stat.add('CPU {} Load'.format(idx), '{:.2f}'.format(cpu_percentage))
if cpu_percentage > self._warning_percentage:
warn = True

if warn:
stat.summary(DiagnosticStatus.WARN,
"At least one CPU exceeds {:d} percent".format(self._warning_percentage))
'At least one CPU exceeds {} percent'.format(self._warning_percentage))
else:
stat.summary(DiagnosticStatus.OK,
"CPU Average {:.2f} percent".format(cpu_average))
'CPU Average {:.2f} percent'.format(cpu_average))

return stat

Expand All @@ -87,7 +91,7 @@ def main(args=None):

# Create the node
hostname = socket.gethostname()
node = Node('cpu_monitor_%s' % hostname.replace("-", "_"))
node = Node('cpu_monitor_%s' % hostname.replace('-', '_'))

# Declare and get parameters
node.declare_parameter('warning_percentage', 90)
Expand All @@ -111,5 +115,4 @@ def main(args=None):
except KeyboardInterrupt:
pass
except Exception:
import traceback
traceback.print_exc()
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Software License Agreement (BSD License)
#
Expand Down Expand Up @@ -35,16 +34,19 @@
import time
import unittest

from diagnostic_common_diagnostics.cpu_monitor import CpuTask

from diagnostic_msgs.msg import DiagnosticStatus

from diagnostic_updater import DiagnosticArray, Updater
from diagnostic_updater import DiagnosticStatusWrapper

import rclpy
from rclpy.node import Node
from diagnostic_common_diagnostics.cpu_monitor import CpuTask


class TestCPUMonitor(unittest.TestCase):

@classmethod
def setUpClass(cls):
rclpy.init(args=None)
Expand Down Expand Up @@ -99,7 +101,7 @@ def test_updater(self):

node = Node('cpu_monitor_test')
updater = Updater(node)
updater.setHardwareID("test_id")
updater.setHardwareID('test_id')
updater.add(CpuTask())

node.create_subscription(
Expand All @@ -113,8 +115,8 @@ def test_updater(self):
time.sleep(0.1)
elapsed_time = time.time() - start_time
if elapsed_time >= timeout:
self.fail("No diagnostics received")
self.fail('No diagnostics received')


if __name__ == "__main__":
if __name__ == '__main__':
unittest.main()

0 comments on commit 61c7be3

Please sign in to comment.