Commit 29da4b11 authored by Alberts S's avatar Alberts S
Browse files

Handle config via base class

parent e90e597b
......@@ -3,6 +3,7 @@ import logging
import sys
import asyncssh
import yaml
logging.basicConfig(stream=sys.stdout, format="%(levelname)-8s:%(name)-24s.%(funcName)-40s:%(message)-s")
logging.getLogger("CapybaraNetty").setLevel(logging.INFO)
......@@ -22,7 +23,13 @@ class MetaCapybaraNetty(type):
class CapybaraNetty(metaclass=MetaCapybaraNetty):
def __init__(self):
pass
self.config = {"external_targets_for_optimizations": [], "pinger_interval": 10}
self.setup_config()
def setup_config(self):
with open("config.yaml", "r") as f:
self.config = self.config | yaml.safe_load(f)
return self.config
@staticmethod
async def ssh_exec(ip, username, command):
......
......@@ -13,20 +13,11 @@ from Visualizer import Visualizer
class Controller(CapybaraNetty):
def __init__(self):
super().__init__()
self.config = {}
self.setup_config()
self.hosts = Inventory().get_hosts()
self.routers = list()
self.pingers = list()
self.visualizer = Visualizer()
def setup_config(self):
self.config["external_targets_for_optimizations"] = []
self.config["pinger_interval"] = 10
with open("config.yaml", "r") as f:
self.config = self.config | yaml.safe_load(f)
return self.config
@classmethod
async def run(cls):
self = cls()
......@@ -65,10 +56,7 @@ class Controller(CapybaraNetty):
concurrent_tasks = [
*await self.run_pinger_daemon(),
self.visualizer.run_daemon(
interval_seconds=self.config["visualizer_interval"],
pinger_data_fn=self.get_host_latency_data,
external_targets_for_optimizations=self.config["external_targets_for_optimizations"],
optimization_threshold_ms=self.config["optimization_threshold_ms"],
),
]
......
......@@ -124,12 +124,10 @@ class Visualizer(CapybaraNetty):
plt.savefig(output_name, bbox_inches="tight")
plt.close()
async def run_daemon(
self, interval_seconds, pinger_data_fn, external_targets_for_optimizations, optimization_threshold_ms
):
async def run_daemon(self, pinger_data_fn):
while True:
self.optimization_threshold_ms = optimization_threshold_ms
self.optimization_threshold_ms = self.config["optimization_threshold_ms"]
self.pinger_data = pinger_data_fn()
self.external_targets_for_optimizations = external_targets_for_optimizations
self.external_targets_for_optimizations = self.config["external_targets_for_optimizations"]
await self.run()
await asyncio.sleep(interval_seconds)
await asyncio.sleep(self.config["visualizer_interval"])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment