From d240a08d08ac592f479e382f8a39d0e95f7583ad Mon Sep 17 00:00:00 2001 From: Nikopol Date: Sat, 10 Aug 2024 22:25:47 +0400 Subject: [PATCH] v0.7, undrunk the codeand make it more readable --- dnsmonitor.py | 65 ++++++++------ dnsmonitor.py.save | 211 --------------------------------------------- 2 files changed, 39 insertions(+), 237 deletions(-) delete mode 100644 dnsmonitor.py.save diff --git a/dnsmonitor.py b/dnsmonitor.py index 12251d4..f804110 100644 --- a/dnsmonitor.py +++ b/dnsmonitor.py @@ -76,18 +76,18 @@ def scalepoints(points): h = 31 else: h = 46 - sclpoint = [0] * 15 + sclpoint = [0] * len(points) scl = 1 maxpnt = 0 i = 0 - while i <= 14: + while i <= len(points) -1: if points[i] > maxpnt: maxpnt = points[i] i += 1 while h * scl < maxpnt: scl += 1 i = 0 - while i <= 14: + while i <= len(points) -1: sclpoint[i] = int(points[i] / scl) i += 1 maxcanv = h * scl @@ -147,6 +147,7 @@ def parsedata(prev, flaunch): def drawout(sclpo, maxcanv, failture, restart): match device.height: case 32: + # todo: backport with canvas(device) as draw: d1 = 1 d2 = device.width - 66 @@ -165,21 +166,29 @@ def drawout(sclpo, maxcanv, failture, restart): draw.text((device.width - 30, 0), "\uf021", font=icons, fill="white") case 64: with canvas(device) as draw: - d1 = 1 - d2 = device.width - 10 - now = datetime.datetime.now() - while d1 < len(sclpo): - draw.line((d2 + 10, 47 - sclpo[d1 - 1], d2, 47 - sclpo[d1]), fill="white") - d1 += 1 - d2 -= 10 - draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white") + # Graph + currpoint = 1 + targetpx = 118 + while currpoint < len(sclpo): + draw.line((targetpx + 10, 47 - sclpo[currpoint - 1], targetpx, 47 - sclpo[currpoint]), fill="white") + draw.rectangle((targetpx - 1, 47 - sclpo[currpoint] - 1, targetpx + 1, 47 - sclpo[currpoint] + 1), fill="white") + currpoint += 1 + targetpx -= 10 + # Draw black boxes to make sure that graph will not interfere + draw.rectangle((0, 48, 128, 64), fill="black") + draw.rectangle((0, 0, 6 * len(str(maxcanv)), 9), fill="black") + draw.rectangle((0, 19, 6 * len(str(maxcanv // 2)), 28), fill="black") + draw.rectangle((0, 37, 6, 47), fill="black") + # Numbers on the left draw.text((0, 0), str(maxcanv), font=smafont, fill="white") - draw.text((0, (device.height - 19) / 2 - 2), str(int(maxcanv / 2)), font=smafont, fill="white") - draw.text((0, device.height - 26), "0", font=smafont, fill="white") + draw.text((0, 19), str(maxcanv // 2), font=smafont, fill="white") + draw.text((0, 37), "0", font=smafont, fill="white") + # Statusbar, non-constant height values are due how fonts render here. + draw.text((75, 45), datetime.datetime.now().strftime("%H:%M"), font=regfont, fill="white") if failture: - draw.text((2, device.height - 14), "\uf071", font=icons, fill="white") + draw.text((2, 50), "\uf071", font=icons, fill="white") if restart: - draw.text((22, device.height - 14), "\uf021", font=icons, fill="white") + draw.text((22, 50), "\uf021", font=icons, fill="white") case _: with canvas(device) as draw: draw.text((0,0), "Display not supported", font=smafont, fill="white") @@ -193,8 +202,9 @@ def getdata(oldtotal): return total, query, False, False def main(): - print("Blocky Graph Monitor for OLED v0.5 B") + print("Blocky Graph Monitor for OLED v0.7") print("(C) Nikopol 2024") + today_last_time = "NaN" last_hrs = "NaN" i = 0 @@ -202,8 +212,8 @@ def main(): restart = False rstate = False device.contrast(0) - pointsmap = [0] * 15 - restartmap = [False] * len(pointsmap) + pointsmap = [0] * 14 + restartmap = [True] * len(pointsmap) total = parsedata(0, True) if total == -1: failture = True @@ -211,18 +221,21 @@ def main(): now = datetime.datetime.now() today_time = now.strftime("%H:%M") hrs = now.strftime("%H") - #if today_time != today_last_time: - if 1: + if today_time != today_last_time: + #if 1: today_last_time = today_time - #if hrs != last_hrs: - if 1: + if hrs != last_hrs: + #if 1: last_hrs = hrs + # Grab fresh data total, query, failture, rstate = getdata(total) - #rstate = True - print(total) - print(query) + #print(total) + #print(query) + # Now write it to (raw) array and move older data pointsmap, restartmap = movepoints(pointsmap, query, restartmap, rstate) + # ...and scale it to screen size sclpo, maxcanv = scalepoints(pointsmap) + # also write restart map restart = False while i < len(restartmap): print(restartmap[i]) @@ -236,7 +249,7 @@ def main(): if __name__ == "__main__": try: - print("\n".join(sys.argv)) + #print("\n".join(sys.argv)) device = get_device() regfont = make_font("TerminusTTF-4.49.3.ttf", 22) smafont = make_font("TerminusTTF-4.49.3.ttf", 12) diff --git a/dnsmonitor.py.save b/dnsmonitor.py.save deleted file mode 100644 index fa47008..0000000 --- a/dnsmonitor.py.save +++ /dev/null @@ -1,211 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# Copyright (c) 2014-18 Richard Hull and contributors -# See LICENSE.rst for details. -# PYTHON_ARGCOMPLETE_OK - -import math -import time -import datetime -import random -import urllib.request -import re -import sys -import random -#from demo_opts import get_device -from luma.core import cmdline, error -from luma.core.render import canvas -from pathlib import Path -from PIL import ImageFont - -def make_font(name, size): - return ImageFont.truetype(name, size) - -def display_settings(device, args): - """ - Display a short summary of the settings. - - :rtype: str - """ - iface = '' - display_types = cmdline.get_display_types() - if args.display not in display_types['emulator']: - iface = f'Interface: {args.interface}\n' - - lib_name = cmdline.get_library_for_display_type(args.display) - if lib_name is not None: - lib_version = cmdline.get_library_version(lib_name) - else: - lib_name = lib_version = 'unknown' - - import luma.core - version = f'luma.{lib_name} {lib_version} (luma.core {luma.core.__version__})' - - return f'Version: {version}\nDisplay: {args.display}\n{iface}Dimensions: {device.width} x {device.height}\n{"-" * 60}' - - -def get_device(actual_args=None): - """ - Create device from command-line arguments and return it. - """ - if actual_args is None: - actual_args = sys.argv[1:] - parser = cmdline.create_parser(description='luma.examples arguments') - args = parser.parse_args(actual_args) - - if args.config: - # load config from file - config = cmdline.load_config(args.config) - args = parser.parse_args(config + actual_args) - - # create device - try: - device = cmdline.create_device(args) - print(display_settings(device, args)) - return device - - except error.Error as e: - parser.error(e) - return None - - - -def scalepoints(points): - if device.height == 32: - h = 31 - else: - h = 46 - sclpoint = [0] * 15 - scl = 1 - maxpnt = 0 - i = 0 - while i <= 14: - if points[i] > maxpnt: - maxpnt = points[i] - i += 1 - while h * scl < maxpnt: - scl += 1 - i = 0 - while i <= 14: - sclpoint[i] = int(points[i] / scl) - i += 1 - maxcanv = h * scl - return sclpoint, maxcanv - -def movepoints(points, insert): - i = 1 - tmp = points[0] - tmp2 = 0 - points[0] = insert - while i < 15: - if i == 14: - points[i] = tmp - else: - tmp2 = points[i] - points[i] = tmp - tmp = tmp2 - i += 1 - return points - -def getdata(prev, flaunch): - data = str(urllib.request.urlopen("http://10.0.0.28:4500/metrics").read()).split("\\n") - count = 0 - i = 0 - while data[i] != "# TYPE blocky_query_total counter": - i += 1 - i += 1 - while True: - if data[i] == "# HELP blocky_request_duration_ms Request duration distribution": - break - else: - try: - count += int(re.sub('blocky_query_total{.*?} ', '', data[i])) - except: - if flaunch == True: - return -1 - else: - return -1, -1 - i += 1 - if flaunch == True: - return count - else: - return count, count-prev - -def drawres32(sclpo, maxcanv, failture): - with canvas(device) as draw: - d1 = 1 - d2 = device.width - 66 - now = datetime.datetime.now() - while d1 < 15: - draw.line((d2 + 10, 31 - sclpo[d1 - 1], d2, 31 - sclpo[d1]), fill="white") - d1 += 1 - d2 -= 10 - draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white") - draw.text((0, 0), str(maxcanv), font=smafont, fill="white") - draw.text((0, device.height / 2 - 4), str(int(maxcanv / 2)), font=smafont, fill="white") - draw.text((0, device.height - 9), "0", font=smafont, fill="white") - #draw.text((0, device.height - 14), "\uf011", font=icons, fill="white") - if failture: - draw.text((device.width - 50, 0), "\uf071", font=icons, fill="white") - -def drawres64(sclpo, maxcanv, failture): - with canvas(device) as draw: - d1 = 1 - d2 = device.width - 10 - now = datetime.datetime.now() - while d1 < 15: - draw.line((d2 + 10, 47 - sclpo[d1 - 1], d2, 47 - sclpo[d1]), fill="white") - d1 += 1 - d2 -= 10 - draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white") - draw.text((0, 0), str(maxcanv), font=smafont, fill="white") - draw.text((0, (device.height - 19) / 2 - 2), str(int(maxcanv / 2)), font=smafont, fill="white") - draw.text((0, device.height - 26), "0", font=smafont, fill="white") - #draw.text((0, device.height - 14), "\uf011", font=icons, fill="white") - if failture: - draw.text((0, device.height - 14), "\uf071", font=icons, fill="white") - - -def main(): - print("Blocky Graph Monitor for OLED v1.2") - today_last_time = "NaN" - last_hrs = "NaN" - failture = False - device.contrast(0) - points = [0] * 15 - total = getdata(0, True) - while True: - now = datetime.datetime.now() - today_time = now.strftime("%H:%M") - hrs = now.strftime("%H") - - if today_time != today_last_time: - today_last_time = today_time - if hrs != last_hrs: - #if 1: - last_hrs = hrs - total, query = getdata(total, False) - if total == -1: - failture = True - else: - failture = False - points = movepoints(points, query) - sclpo, maxcanv = scalepoints(points) - if device.height == 32: - drawres32(sclpo, maxcanv, failture) - else: - drawres64(sclpo, maxcanv, failture) - - - time.sleep(1) - - -if __name__ == "__main__": - try: - device = get_device() - regfont = make_font("TerminusTTF-4.49.3.ttf", 22) - smafont = make_font("TerminusTTF-4.49.3.ttf", 12) - icons = make_font("fontawesome-webfont.ttf", 15) - main() - except KeyboardInterrupt: - pass