From 8fe8a6deb3b7ea3eca8730d5b1500c2c27c251cd Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 2 Sep 2018 13:07:43 -0400 Subject: statistics: Move stats handling to new "extras" module Move the generation of statistics to its own module. Signed-off-by: Kevin O'Connor --- klippy/extras/statistics.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 klippy/extras/statistics.py (limited to 'klippy/extras/statistics.py') diff --git a/klippy/extras/statistics.py b/klippy/extras/statistics.py new file mode 100644 index 00000000..639bcad3 --- /dev/null +++ b/klippy/extras/statistics.py @@ -0,0 +1,29 @@ +# Support for logging periodic statistics +# +# Copyright (C) 2018 Kevin O'Connor +# +# This file may be distributed under the terms of the GNU GPLv3 license. +import logging + +class PrinterStats: + def __init__(self, config): + self.printer = config.get_printer() + reactor = self.printer.get_reactor() + self.stats_timer = reactor.register_timer(self.generate_stats) + self.stats_cb = [] + def printer_state(self, state): + if state == 'ready': + self.stats_cb = [o.stats for n, o in self.printer.lookup_objects() + if hasattr(o, 'stats')] + if self.printer.get_start_args().get('debugoutput') is None: + reactor = self.printer.get_reactor() + reactor.update_timer(self.stats_timer, reactor.NOW) + def generate_stats(self, eventtime): + stats = [cb(eventtime) for cb in self.stats_cb] + if max([s[0] for s in stats]): + logging.info("Stats %.1f: %s", eventtime, + ' '.join([s[1] for s in stats])) + return eventtime + 1. + +def load_config(config): + return PrinterStats(config) -- cgit v1.2.3-70-g09d2