From 9e07d4bc15ed90bc33ccd42452d0e9ec9f64f40d Mon Sep 17 00:00:00 2001
From: atoav <dh@atoav.com>
Date: Wed, 29 Apr 2020 15:17:52 +0200
Subject: [PATCH] Add --twolines flag

---
 bbbmon/bbbmon.py   | 15 +++++++++---
 bbbmon/meetings.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/bbbmon/bbbmon.py b/bbbmon/bbbmon.py
index 42b2578..c3ec686 100755
--- a/bbbmon/bbbmon.py
+++ b/bbbmon/bbbmon.py
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 import os
 import time
+import json
 import click
 
 
@@ -60,9 +61,10 @@ def main():
 @click.option('--presenter/--no-presenter', default=True, show_default=True, help="Hide or show the presenters")
 @click.option('--presenter-id/--no-presenter-id', default=False, show_default=True, help="Hide or show the presenter IDs")
 @click.option('--short', '-s', is_flag=True, help="Print less")
+@click.option('--twolines', '-2', is_flag=True, help="Print essentials on two lines")
 @click.option('--all', '-a', 'all_', is_flag=True, help="Print all")
 @click.option('--fancy/--no-fancy', default=True, show_default=True, help="Use fancy headers")
-def meetings(ctx, short, all_, leaderboards, participants, presenter, watch, presenter_id, meetings, endpoint, fancy):
+def meetings(ctx, short, all_, twolines, leaderboards, participants, presenter, watch, presenter_id, meetings, endpoint, fancy):
     """View currently active meetings"""
     if short:
         leaderboards = False
@@ -77,10 +79,16 @@ def meetings(ctx, short, all_, leaderboards, participants, presenter, watch, pre
     config.filter_endpoints(endpoint)
     if watch is not None:
         while watch is not None:
-            list_meetings(config, leaderboards, participants, presenter, presenter_id, meetings, watch, fancy)
+            if twolines:
+                meetings_twolines(config, watch, fancy)
+            else:
+                list_meetings(config, leaderboards, participants, presenter, presenter_id, meetings, watch, fancy)
             time.sleep(watch)
     else:
-        list_meetings(config, leaderboards, participants, presenter, presenter_id, meetings, watch, fancy)
+        if twolines:
+            meetings_twolines(config, watch, fancy)
+        else:
+            list_meetings(config, leaderboards, participants, presenter, presenter_id, meetings, watch, fancy)
 
 
 
@@ -122,6 +130,5 @@ def config(ctx, new, edit, path, print_):
 
 
 
-
 if __name__ == "__main__":
     main()
\ No newline at end of file
diff --git a/bbbmon/meetings.py b/bbbmon/meetings.py
index 59720b4..cbdb227 100644
--- a/bbbmon/meetings.py
+++ b/bbbmon/meetings.py
@@ -168,4 +168,65 @@ def list_meetings(config: Config, leaderboards: bool, participants: bool, presen
 
 
 
+def meetings_twolines(config: Config, watch: int, fancy: bool):
+    """
+    For each endpoint in the configuration get the active meetings and print 
+    out an overview of the current bbb-usage
+    """
+
+    # Request Meetings from API
+    meetings = [get_meetings(e.secret, e.url, config.path) for e in config.endpoints]
+
+    # Clear screen after request is done, and before printing new data to keep
+    # blinking to a minimum
+    if watch is not None:
+        click.clear()
+
+
+    for i, endpoint in enumerate(config.endpoints):
+        meeting = meetings[i]
 
+        # Print divider if there is more than one endpoint
+        if i > 0:
+            print("="*click.get_terminal_size()[0])
+
+        # If there are no meetings, skip to next endpoint
+        if len(meeting) == 0:
+            lines = [
+                "{:^60}".format("bbb    there   are   no   meetings   currently."),
+                ""
+            ]
+            # Cut above 60 characters fill empty
+            lines = ["{:<60}".format(l[:61]) for l in lines]
+            lines = "\n".join(lines)
+            print(lines)
+            continue
+
+        n_running = len(meeting)
+        n_recording = len([m for m in meeting if m["recording"] == "true"])
+        n_participants = sum([int(m["participantCount"]) for m in meeting])
+        n_listeners = sum([int(m["listenerCount"]) for m in meeting])
+        n_voice = sum([int(m["voiceParticipantCount"]) for m in meeting])
+        n_video = sum([int(m["videoCount"]) for m in meeting])
+        n_moderator = sum([int(m["moderatorCount"]) for m in meeting])
+        avg_s = sum([int(get_duration(m).total_seconds()) for m in meeting])/float(n_running)
+        avg_s = avg_s/60./60.
+
+        if not fancy:
+            lines = [
+                "{}    rec / ses    ppl    mod   vid   mic  ear".format(endpoint.name),
+                "stats   {:>2} /  {:<2}    {:>3}    {:>3}   {:>3}   {:>3}  {:>3}"\
+                .format(n_recording, n_running, n_participants, n_moderator, n_video, n_voice, n_listeners)
+            ]
+        else:
+            lines = [
+                "{}: There are {} people in {} meetings for {:.1f}h on average"\
+                .format(endpoint.name, n_participants, n_running, avg_s),
+                "{} webcams and {} microphones are on. {} just listen. {} mods"\
+                .format(n_video, n_voice, n_listeners, n_moderator)
+            ]
+
+        # Cut above 60 characters fill empty
+        lines = ["{:^60}".format(l[:61]) for l in lines]
+        lines = "\n".join(lines)
+        print(lines)
-- 
GitLab