From bc4d2d318ee61076ea7067c40626e9b4d1d80ac9 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Wed, 10 Jun 2020 13:54:46 +0200
Subject: [PATCH] Fix where --watch caused malformed requests

This happened due to config not cloning implicitly, then beeing
overwritten by sum_meetings and then not containing any URL at all
during the next iteration. Solved by creating a deepcopy
---
 bbbmon/__init__.py |  2 +-
 bbbmon/bbbmon.py   |  2 +-
 bbbmon/meetings.py | 38 ++++++++++++++++++++++----------------
 pyproject.toml     |  2 +-
 4 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/bbbmon/__init__.py b/bbbmon/__init__.py
index 0871cd5..a64f0d9 100644
--- a/bbbmon/__init__.py
+++ b/bbbmon/__init__.py
@@ -1 +1 @@
-__version__ = '0.1.30'
+__version__ = '0.1.31'
diff --git a/bbbmon/bbbmon.py b/bbbmon/bbbmon.py
index cc62771..544d3d8 100755
--- a/bbbmon/bbbmon.py
+++ b/bbbmon/bbbmon.py
@@ -53,7 +53,7 @@ def main(userconfig, watch, version):
 
     Internally bbbmon relies on the offical bbb-API, which means you need to have the server's secret in order to create a valid request. Create a new configuration with: bbbmon config --new
     """
-    __version__ = "0.1.30"
+    __version__ = "0.1.31"
 
     if version:
         print(__version__)
diff --git a/bbbmon/meetings.py b/bbbmon/meetings.py
index 9b33fb8..4ad9100 100644
--- a/bbbmon/meetings.py
+++ b/bbbmon/meetings.py
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 import time
+import copy
 import subprocess
 import hashlib
 import json
@@ -78,9 +79,11 @@ def get_meetings(secret: Secret, bbb_url: Url, user_config_path: str, sum_: bool
     try:
         d = request_meetings(secret, bbb_url, user_config_path, sum_)
     except:
+        eprint("request_meetings didn't work out...")
         return ["unreachable"]
 
     if d["meetings"] is None:
+        eprint("No meetings returned...")
         return []
 
     if type(d["meetings"]["meeting"]) is XmlListConfig:
@@ -127,9 +130,11 @@ def list_meetings(config: Config, leaderboards: bool, n: int, participants: bool
     # Request Meetings from API
     meetings = [get_meetings(e.secret, e.url, config.path, sum_) for e in config.endpoints]
 
+    config_override = copy.deepcopy(config)
+
     # Collate all meetings from all endpoints together for --sum flag
     if sum_:
-        config, meetings = sum_meetings(config, meetings)
+        config_override, meetings = sum_meetings(config_override, meetings)
 
     # Clear screen after request is done, and before printing new data to keep
     # blinking to a minimum
@@ -137,7 +142,7 @@ def list_meetings(config: Config, leaderboards: bool, n: int, participants: bool
         click.clear()
 
 
-    for i, endpoint in enumerate(config.endpoints):
+    for i, endpoint in enumerate(config_override.endpoints):
         # Print divider if there is more than one endpoint
         if i > 0:
             print()
@@ -164,11 +169,10 @@ def list_meetings(config: Config, leaderboards: bool, n: int, participants: bool
                 print("   └─── Currently no active meetings.")
             continue
 
-        # There are no unreachable endpoints in sum mode
-        if not sum_:
-            if meeting[0] == "unreachable":
-                printing.print_header(endpoint.name, "No connection..", fancy)
-                continue
+        # print this if the endpoint (or all endpoints!) is unreachable
+        if meeting[0] == "unreachable":
+            printing.print_header(endpoint.name, "No connection..", fancy)
+            continue
 
         # Make sure that meeting is an actual list
         if type(meeting) is XmlDictConfig:
@@ -226,7 +230,8 @@ def sum_meetings(config: Config, meetings:  Optional[XmlDictConfig]) ->  Optiona
     active_endpoints = [m for m in meetings if m != ["unreachable"]]
 
     if len(active_endpoints) == 0:
-        return config, None
+        eprint("No active endpoint...")
+        return config, ["unreachable"]
 
     summed_endpoint =  []
 
@@ -253,9 +258,11 @@ def meetings_twolines(config: Config, watch: int, fancy: bool, sum_:bool):
     # Request Meetings from API
     meetings = [get_meetings(e.secret, e.url, config.path, sum_) for e in config.endpoints]
 
+    config_override = copy.deepcopy(config)
+
     # Collate meetings from all endpoints together in sum mode
     if sum_:
-        config, meetings = sum_meetings(config, meetings)
+        config_override, meetings = sum_meetings(config_override, meetings)
 
     if sum_ and fancy:
         config.endpoints[0].name = "bbb"
@@ -265,7 +272,7 @@ def meetings_twolines(config: Config, watch: int, fancy: bool, sum_:bool):
     if watch is not None:
         click.clear()
 
-    for i, endpoint in enumerate(config.endpoints):
+    for i, endpoint in enumerate(config_override.endpoints):
         # Print divider if there is more than one endpoint
         if i > 0:
             print("="*click.get_terminal_size()[0])
@@ -300,11 +307,10 @@ def meetings_twolines(config: Config, watch: int, fancy: bool, sum_:bool):
             print(lines)
             continue
 
-        # There should be no unreachable meetings in sum mode
-        if not sum_:
-            if meeting[0] == "unreachable":
-                printing.print_header(endpoint.name, "No connection..", fancy)
-                continue
+        # print this if the endpoint (or all endpoints!) is unreachable
+        if meeting[0] == "unreachable":
+            printing.print_header(endpoint.name, "No connection..", fancy)
+            continue
         
         # Make sure meeting is always a list
         if type(meeting) is XmlDictConfig:
@@ -321,7 +327,7 @@ def meetings_twolines(config: Config, watch: int, fancy: bool, sum_:bool):
         avg_s = avg_s/60./60.
 
         if not fancy:
-            if config.on_server:
+            if config_override.on_server:
                 # If on server get load
                 w = subprocess.run(["w | head -1"], shell=True, stdout=subprocess.PIPE)
                 w = w.stdout.decode('utf-8').strip().split("load average:")
diff --git a/pyproject.toml b/pyproject.toml
index fe059b4..fa89388 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "bbbmon"
-version = "0.1.30"
+version = "0.1.31"
 description = "A small CLI utility to monitor bbb usage"
 authors = ["David Huss <david.huss@hfbk-hamburg.de>"]
 maintainers = ["David Huss <david.huss@hfbk-hamburg.de>"]
-- 
GitLab