diff --git a/brian_polling_manager/error_report/cli.py b/brian_polling_manager/error_report/cli.py index f4b049196c676c910dd1850b4b0293aedbb71ddf..af27f2d8aa0a81dc6c5a856623cb5ac596b06f07 100644 --- a/brian_polling_manager/error_report/cli.py +++ b/brian_polling_manager/error_report/cli.py @@ -1,7 +1,6 @@ import logging import pathlib -import pprint -from typing import Sequence, Set, Tuple +from typing import Sequence from brian_polling_manager.interface_stats.services import influx_client from brian_polling_manager.inventory import load_interfaces from influxdb import InfluxDBClient @@ -126,7 +125,7 @@ def get_error_points(client: InfluxDBClient, time_window: str): def interface_errors( - client: InfluxDBClient, interface_info, errors, raise_on_errors=False + client: InfluxDBClient, interface_info, errors, exclusions, raise_on_errors=False ): """ Retrieves error counters from influx @@ -144,16 +143,15 @@ def interface_errors( yesterdays_data = get_error_points(client, TIME_WINDOW_YESTERDAY) result = {"interfaces": [], "excluded_interfaces": []} - exception_count = 0 - for (router, ifc), today in todays_data.items(): - yesterday = yesterdays_data.get((router, ifc), {}) + for (router, ifc), info in interface_info.items(): try: - info = interface_info[(router, ifc)] + today = todays_data[(router, ifc)] except KeyError: - logger.exception(f"{router} - {ifc} not found in inventory provider") - exception_count += 1 + logger.error(f"{router} - {ifc} not found in influx data") if raise_on_errors: raise + continue + yesterday = yesterdays_data.get((router, ifc), {}) counters = { "error_counters": {err[0]: (today[err[1]] or 0) for (err) in errors}, @@ -162,7 +160,7 @@ def interface_errors( "description": info["description"], } - if not is_excluded_interface(info["description"]): + if not is_excluded_interface(info["description"], exclusions): counters["diff"] = { err[0]: (today[err[1]] or 0) - (yesterday.get(err[1], 0) or 0) for err in errors @@ -177,7 +175,7 @@ def interface_errors( else: result["excluded_interfaces"].append(counters) - return result, exception_count + return result def is_excluded_interface(description: str, exclusions: Sequence[str]): @@ -212,16 +210,12 @@ def main(): client = influx_client(config["influx"]) all_interfaces = get_relevant_interfaces(config["inventory"]) with client: - all_error_counters = interface_errors(client, errors=ERROR_FIELDS) - for key in sorted(all_interfaces): - if key not in all_error_counters: - print(f"interface {key} not found in influx data") - continue - - errors = all_error_counters[key] - - if any(v > 0 for v in errors.values()): - print(*key, ",".join(str(c) for c in errors.values())) + all_error_counters = interface_errors( + client, + interface_info=all_interfaces, + errors=ERROR_FIELDS, + exclusions=config["exclude-interfaces"], + ) # TODO: ensure data is from the day that we're interested in (today or yesterday) # TODO: send script failures to admin email