From cf37ccb7e3e04dd52f2fd1705e3b1cd60d6b27a6 Mon Sep 17 00:00:00 2001 From: Midgard Date: Fri, 27 Oct 2023 14:18:51 +0200 Subject: [PATCH] Initial commit --- README.md | 17 +++++++ check_numberdealers.py | 107 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 README.md create mode 100755 check_numberdealers.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..eb33917 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# check_numberdealers + +Verify that no errors have been made in the counting in ~numberdealers and ~numberdealers-ng + +When errors are detected, a nice Markdown list detailing them is produced. + +## Dependencies +- Python +- [mmcli](https://git.zeus.gent/midgard/mmcli) + +## Usage +``` +$ mmcli cat zeus/numberdealers | ./check_numberdealers.py # Fetching this whole channel is gonna take half a minute +$ mmcli cat zeus/numberdealers-ng | ./check_numberdealers.py +``` + +See mmcli documentation for how to provide your credentials. diff --git a/check_numberdealers.py b/check_numberdealers.py new file mode 100755 index 0000000..7175cda --- /dev/null +++ b/check_numberdealers.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 + +import sys +import re +import json + +NUMBER_EMOJI = { + "zero": "0", + "one": "1", + "two": "2", + "three": "3", + "four": "4", + "five": "5", + "six": "6", + "seven": "7", + "eight": "8", + "nine": "9", +} + +URL_PREFIX = "https://mattermost.zeus.gent/zeus/pl/" + +second_last_number = None +second_last_line = {"id": None} +last_number = None +last_line = {"id": None} +messages = [] +start_number = None +for line in sys.stdin: + line = json.loads(line) + # Ignore non-message posts (e.g. join/leave) + if line.get("type") is not None: + continue + + if "message" in line and line["message"] != "": + message = line["message"] + message = re.sub(r"^[#>]* *|[*_`]*", "", message) + for emoji, numb in NUMBER_EMOJI.items(): + message = re.sub(f":{emoji}: *", numb, message) + message = message.strip() + else: + try: + message = line["metadata"]["files"][0]["name"].split(".")[0] + except (KeyError, IndexError): + messages.append( + f"- [Unrecognized post]({URL_PREFIX}{line['id']})" + ) + continue + + if line.get("edit_at") is not None: + messages.append( + f"- Edited message [{message}]({URL_PREFIX}{line['id']})" + ) + + m = re.fullmatch(r"-?[1-9][0-9]*|0", message) + if not m: + messages.append( + f"- Non-number message [{message}]({URL_PREFIX}{line['id']})" + ) + else: + number = int(m.group(0)) + if last_number is None: + start_number = number + last_number = number - 1 + second_last_number = number - 2 + + if number != last_number + 1: + if number == last_number: + messages.append( + f"- Duplicate [{number}]({URL_PREFIX}{line['id']})" + ) + elif number == second_last_number + 1 and last_number != second_last_number + 1: + messages.pop() + messages.append( + f"- Stray [{last_number}]({URL_PREFIX}{last_line['id']})" + ) + elif number == second_last_number + 2 and last_number != second_last_number + 1: + messages.pop() + messages.append( + f"- [{last_number}]({URL_PREFIX}{last_line['id']}) should have been {number - 1}" + ) + elif number == last_number + 2: + messages.append( + f"- Skipped [{last_number + 1}]({URL_PREFIX}{line['id']})" + ) + else: + messages.append( + f"- Going from [{last_number}]({URL_PREFIX}{last_line['id']}) " + f"to [{number}]({URL_PREFIX}{line['id']})" + ) + + second_last_number = last_number + second_last_line = last_line + last_number = number + last_line = line + +if start_number is None and messages == []: + print("No input data") +else: + if start_number is None: + print("No valid number messages!") + else: + print(f"Checked from {start_number} up to {number}") + if messages: + print("Errors:") + print("\n".join(messages)) + else: + print("No errors! 🎉")