Unify functions and fix restart indicator

This commit is contained in:
Nikopol 2024-06-01 21:32:04 +04:00
parent 3b04fdb03d
commit 128c316e5d

View File

@ -93,139 +93,150 @@ def scalepoints(points):
maxcanv = h * scl maxcanv = h * scl
return sclpoint, maxcanv return sclpoint, maxcanv
def movepoints(points, insert): def movepoints(points, insertq, restarts, insertr):
i = 1 i = 1
tmp = points[0] tmp = points[0]
tmp2 = 0 tmp2 = 0
points[0] = insert points[0] = insertq
while i < 15: while i < len(points):
if i == 14: if i == len(points) - 1:
points[i] = tmp points[i] = tmp
else: else:
tmp2 = points[i] tmp2 = points[i]
points[i] = tmp points[i] = tmp
tmp = tmp2 tmp = tmp2
i += 1 i += 1
return points i = 1
tmp3 = restarts[0]
tmp4 = False
restarts[0] = insertr
while i < len(restarts):
if i == len(restarts) - 1:
restarts[i] = tmp3
else:
tmp4 = restarts[i]
restarts[i] = tmp3
tmp3 = tmp4
i += 1
return points, restarts
def getdata(prev, flaunch): def parsedata(prev, flaunch):
data = str(urllib.request.urlopen("http://10.0.0.28:4500/metrics").read()).split("\\n")
count = 0
i = 0
try: try:
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": while data[i] != "# TYPE blocky_query_total counter":
i += 1 i += 1
i += 1
while True:
if data[i] == "# HELP blocky_request_duration_ms Request duration distribution":
break
else:
count += int(re.sub('blocky_query_total{.*?} ', '', data[i]))
i += 1
except: except:
if flaunch == True: if flaunch == True:
return -1 return -1
else: else:
return -1, 0 return 0, -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, 0
i += 1
if flaunch == True: if flaunch == True:
return count return count
else: else:
return count, count - prev return count, count - prev
def drawres32(sclpo, maxcanv, failture): def drawout(sclpo, maxcanv, failture, restart):
with canvas(device) as draw: match device.height:
d1 = 1 case 32:
d2 = device.width - 66 with canvas(device) as draw:
now = datetime.datetime.now() d1 = 1
while d1 < 15: d2 = device.width - 66
draw.line((d2 + 10, 31 - sclpo[d1 - 1], d2, 31 - sclpo[d1]), fill="white") now = datetime.datetime.now()
d1 += 1 while d1 < len(sclpo):
d2 -= 10 draw.line((d2 + 10, 31 - sclpo[d1 - 1], d2, 31 - sclpo[d1]), fill="white")
draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white") d1 += 1
draw.text((0, 0), str(maxcanv), font=smafont, fill="white") d2 -= 10
draw.text((0, device.height / 2 - 4), str(int(maxcanv / 2)), font=smafont, fill="white") draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white")
draw.text((0, device.height - 9), "0", font=smafont, fill="white") draw.text((0, 0), str(maxcanv), font=smafont, fill="white")
#draw.text((0, device.height - 14), "\uf011", font=icons, fill="white") draw.text((0, device.height / 2 - 4), str(int(maxcanv / 2)), font=smafont, fill="white")
if failture: draw.text((0, device.height - 9), "0", font=smafont, fill="white")
draw.text((device.width - 50, 0), "\uf071", font=icons, fill="white") if failture:
draw.text((device.width - 50, 0), "\uf071", font=icons, fill="white")
def drawres64(sclpo, maxcanv, failture, restart): if restart:
with canvas(device) as draw: draw.text((device.width - 30, 0), "\uf021", font=icons, fill="white")
d1 = 1 case 64:
d2 = device.width - 10 with canvas(device) as draw:
now = datetime.datetime.now() d1 = 1
while d1 < 15: d2 = device.width - 10
draw.line((d2 + 10, 47 - sclpo[d1 - 1], d2, 47 - sclpo[d1]), fill="white") now = datetime.datetime.now()
d1 += 1 while d1 < len(sclpo):
d2 -= 10 draw.line((d2 + 10, 47 - sclpo[d1 - 1], d2, 47 - sclpo[d1]), fill="white")
draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white") d1 += 1
draw.text((0, 0), str(maxcanv), font=smafont, fill="white") d2 -= 10
draw.text((0, (device.height - 19) / 2 - 2), str(int(maxcanv / 2)), font=smafont, fill="white") draw.text((device.width - 53, device.height - 19), now.strftime("%H:%M"), font=regfont, fill="white")
draw.text((0, device.height - 26), "0", font=smafont, fill="white") draw.text((0, 0), str(maxcanv), font=smafont, fill="white")
#draw.text((0, device.height - 14), "\uf011", font=icons, fill="white") draw.text((0, (device.height - 19) / 2 - 2), str(int(maxcanv / 2)), font=smafont, fill="white")
if failture: draw.text((0, device.height - 26), "0", font=smafont, fill="white")
draw.text((2, device.height - 14), "\uf071", font=icons, fill="white") if failture:
if restart: draw.text((2, device.height - 14), "\uf071", font=icons, fill="white")
draw.text((22, device.height - 14), "\uf021", font=icons, fill="white") if restart:
draw.text((22, device.height - 14), "\uf021", font=icons, fill="white")
case _:
with canvas(device) as draw:
draw.text((0,0), "Display not supported", font=smafont, fill="white")
def getdata(oldtotal):
total, query = parsedata(oldtotal, False)
if query == -1:
return oldtotal, 0, True, False
if total < oldtotal:
return total, 0, False, True
return total, query, False, False
def main(): def main():
print("Blocky Graph Monitor for OLED v1.4") print("Blocky Graph Monitor for OLED v0.5 B")
print("(C) Nikopol 2024") print("(C) Nikopol 2024")
today_last_time = "NaN" today_last_time = "NaN"
last_hrs = "NaN" last_hrs = "NaN"
i = 0
failture = False failture = False
restart = False restart = False
resticker = 0 rstate = False
device.contrast(0) device.contrast(0)
points = [0] * 15 pointsmap = [0] * 15
total = getdata(0, True) restartmap = [False] * len(pointsmap)
total = parsedata(0, True)
if total == -1: if total == -1:
failture = True failture = True
while True: while True:
now = datetime.datetime.now() now = datetime.datetime.now()
today_time = now.strftime("%H:%M") today_time = now.strftime("%H:%M")
hrs = now.strftime("%H") hrs = now.strftime("%H")
if today_time != today_last_time: #if today_time != today_last_time:
if 1:
today_last_time = today_time today_last_time = today_time
if hrs != last_hrs: #if hrs != last_hrs:
#if 1: if 1:
last_hrs = hrs last_hrs = hrs
if resticker > 0: total, query, failture, rstate = getdata(total)
resticker = resticker + 1 #rstate = True
elif resticker >= 15:
resticker = 0
restart = False
total, query = getdata(total, False)
print(total) print(total)
print(query) print(query)
if total == -1: pointsmap, restartmap = movepoints(pointsmap, query, restartmap, rstate)
failture = True sclpo, maxcanv = scalepoints(pointsmap)
else: restart = False
failture = False while i < len(restartmap):
if query < 0: print(restartmap[i])
restart = True if restartmap[i] == True:
query = 0 restart = True
resticker = 1 i += 1
points = movepoints(points, query) i = 0
sclpo, maxcanv = scalepoints(points) drawout(sclpo, maxcanv, failture, restart)
if device.height == 32:
drawres32(sclpo, maxcanv, failture)
else:
drawres64(sclpo, maxcanv, failture, restart)
time.sleep(1) time.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
try: try:
print("\n".join(sys.argv))
device = get_device() device = get_device()
regfont = make_font("TerminusTTF-4.49.3.ttf", 22) regfont = make_font("TerminusTTF-4.49.3.ttf", 22)
smafont = make_font("TerminusTTF-4.49.3.ttf", 12) smafont = make_font("TerminusTTF-4.49.3.ttf", 12)