From 2f164170fcb6d1f49ceccf44367b551ee6cbce91 Mon Sep 17 00:00:00 2001
From: Rafal Bielski <rafal.bielski@cern.ch>
Date: Mon, 18 May 2020 19:02:22 +0200
Subject: [PATCH] Print messages if MessageCount step fails and is required

---
 .../TrigValTools/bin/messageCounter.py        | 16 +++-
 .../python/TrigValSteering/CheckSteps.py      | 78 ++++++++-----------
 .../test/test_trig_data_v1Dev_build.py        |  8 +-
 .../test_trig_data_v1Dev_emptyMenu_build.py   |  8 +-
 4 files changed, 56 insertions(+), 54 deletions(-)

diff --git a/Trigger/TrigValidation/TrigValTools/bin/messageCounter.py b/Trigger/TrigValidation/TrigValTools/bin/messageCounter.py
index 8ec6e66ab29..44148ce9c9c 100755
--- a/Trigger/TrigValidation/TrigValTools/bin/messageCounter.py
+++ b/Trigger/TrigValidation/TrigValTools/bin/messageCounter.py
@@ -49,6 +49,9 @@ def get_parser():
     parser.add_argument('-p', '--printMessages',
                         action='store_true',
                         help='Print the messages found in analysed files')
+    parser.add_argument('--saveAll',
+                        action='store_true',
+                        help='Store all the messages into the output JSON file')
     parser.add_argument('-v', '--verbose',
                         action='store_true',
                         help='Increase output verbosity')
@@ -113,12 +116,18 @@ def print_result(summary, full_result, print_messages=False):
                 print(line, end='')  # noqa: ATL901
 
 
-def save_to_json(result, filename):
+def save_summary_to_json(result, filename):
     logging.info('Saving results to %s', filename)
     with open(filename, 'w') as f:
         json.dump(result, f, indent=4)
 
 
+def save_all_to_json(full_result, filename):
+    logging.info('Saving results to %s', filename)
+    with open(filename, 'w') as f:
+        json.dump(full_result, f, indent=4)
+
+
 def main():
     args = get_parser().parse_args()
     logging.basicConfig(stream=sys.stdout,
@@ -141,7 +150,10 @@ def main():
         summary = make_summary(messages)
         print_result(summary, messages, args.printMessages)
         out_file_name = 'MessageCount.{:s}.json'.format(fname)
-        save_to_json(summary, out_file_name)
+        save_summary_to_json(summary, out_file_name)
+        if args.saveAll:
+            all_out_file_name = 'Messages.{:s}.json'.format(fname)
+            save_all_to_json(messages, all_out_file_name)
 
 
 if '__main__' in __name__:
diff --git a/Trigger/TrigValidation/TrigValTools/python/TrigValSteering/CheckSteps.py b/Trigger/TrigValidation/TrigValTools/python/TrigValSteering/CheckSteps.py
index a95790e796f..883da8b5ec4 100644
--- a/Trigger/TrigValidation/TrigValTools/python/TrigValSteering/CheckSteps.py
+++ b/Trigger/TrigValidation/TrigValTools/python/TrigValSteering/CheckSteps.py
@@ -566,26 +566,27 @@ class MessageCountStep(Step):
         self.log_regex = r'(athena\..*log$|athenaHLT:.*\.out$|^log\..*to.*)'
         self.start_pattern = r'(HltEventLoopMgr|AthenaHiveEventLoopMgr).*INFO Starting loop on events'
         self.end_pattern = r'(HltEventLoopMgr.*INFO All events processed|AthenaHiveEventLoopMgr.*INFO.*Loop Finished)'
-        self.warning_threshold = None
-        self.info_threshold = None
-        self.debug_threshold = None
-        self.verbose_threshold = None
-        self.other_threshold = None
+        self.print_on_fail = None
+        self.thresholds = {}
         self.auto_report_result = True
 
     def configure(self, test):
         self.args += ' -s "{:s}"'.format(self.start_pattern)
         self.args += ' -e "{:s}"'.format(self.end_pattern)
-        if self.warning_threshold is None:
-            self.warning_threshold = 0
-        if self.info_threshold is None:
-            self.info_threshold = test.exec_steps[0].max_events
-        if self.debug_threshold is None:
-            self.debug_threshold = 0
-        if self.verbose_threshold is None:
-            self.verbose_threshold = 0
-        if self.other_threshold is None:
-            self.other_threshold = test.exec_steps[0].max_events
+        if self.print_on_fail is None:
+            self.print_on_fail = self.required
+        if self.print_on_fail:
+            self.args += ' --saveAll'
+        if 'WARNING' not in self.thresholds:
+            self.thresholds['WARNING'] = 0
+        if 'INFO' not in self.thresholds:
+            self.thresholds['INFO'] = test.exec_steps[0].max_events
+        if 'DEBUG' not in self.thresholds:
+            self.thresholds['DEBUG'] = 0
+        if 'VERBOSE' not in self.thresholds:
+            self.thresholds['VERBOSE'] = 0
+        if 'other' not in self.thresholds:
+            self.thresholds['other'] = test.exec_steps[0].max_events
         super(MessageCountStep, self).configure(test)
 
     def run(self, dry_run=False):
@@ -603,43 +604,28 @@ class MessageCountStep(Step):
             if self.auto_report_result:
                 self.report_result()
             return self.result, cmd
-        (num_warning, num_info, num_debug, num_verbose, num_other) = (0, 0, 0, 0, 0)
+
         for log_file in log_files:
             json_file = 'MessageCount.{:s}.json'.format(log_file)
+            if self.print_on_fail:
+                all_json_file = 'Messages.{:s}.json'.format(log_file)
             if not os.path.isfile(json_file):
                 self.log.warning('%s cannot open file %s', self.name, json_file)
             with open(json_file) as f:
                 summary = json.load(f)
-                num_warning += summary['WARNING']
-                num_info += summary['INFO']
-                num_debug += summary['DEBUG']
-                num_verbose += summary['VERBOSE']
-                num_other += summary['other']
-        if num_warning > self.warning_threshold:
-            self.log.info(
-                '%s Number of WARNING messages %s is higher than threshold %s',
-                self.name, num_warning, self.warning_threshold)
-            self.result += 1
-        if num_info > self.info_threshold:
-            self.log.info(
-                '%s Number of INFO messages %s is higher than threshold %s',
-                self.name, num_info, self.info_threshold)
-            self.result += 1
-        if num_debug > self.debug_threshold:
-            self.log.info(
-                '%s Number of DEBUG messages %s is higher than threshold %s',
-                self.name, num_debug, self.debug_threshold)
-            self.result += 1
-        if num_verbose > self.verbose_threshold:
-            self.log.info(
-                '%s Number of VERBOSE messages %s is higher than threshold %s',
-                self.name, num_verbose, self.verbose_threshold)
-            self.result += 1
-        if num_other > self.other_threshold:
-            self.log.info(
-                '%s Number of "other" messages %s is higher than threshold %s',
-                self.name, num_other, self.other_threshold)
-            self.result += 1
+                for level, threshold in six.iteritems(self.thresholds):
+                    if summary[level] > threshold:
+                        self.result += 1
+                        self.log.info(
+                            '%s Number of %s messages %s is higher than threshold %s',
+                            self.name, level, summary[level], threshold)
+                        if self.print_on_fail:
+                            self.log.info('%s Printing all %s messages', self.name, level)
+                            with open(all_json_file) as af:
+                                all_msg = json.load(af)
+                                for msg in all_msg[level]:
+                                    print(msg.strip())  # noqa: ATL901
+
         if self.auto_report_result:
             self.report_result()
         return self.result, cmd
diff --git a/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_build.py b/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_build.py
index 944198e03f0..cebe4851f9a 100755
--- a/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_build.py
+++ b/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_build.py
@@ -31,9 +31,11 @@ test.check_steps = CheckSteps.default_check_steps(test)
 # We are trying to lower the limits step by step
 # Ultimately there should be no per-event messages
 msgcount = test.get_step("MessageCount")
-msgcount.warning_threshold = 500
-msgcount.info_threshold = 1200
-msgcount.other_threshold = 80
+msgcount.thresholds = {
+  'WARNING': 500,
+  'INFO': 1200,
+  'other': 80
+}
 msgcount.required = True # make the test exit code depend on this step
 
 # Add a step comparing counts in the log against reference
diff --git a/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_emptyMenu_build.py b/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_emptyMenu_build.py
index 3985766c50c..6615674ef96 100755
--- a/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_emptyMenu_build.py
+++ b/Trigger/TrigValidation/TriggerTest/test/test_trig_data_v1Dev_emptyMenu_build.py
@@ -36,9 +36,11 @@ test.check_steps.remove(test.get_step("ZeroCounts"))
 
 # Overwrite default MessageCount settings
 msgcount = test.get_step("MessageCount")
-msgcount.warning_threshold = 40
-msgcount.info_threshold = 600
-msgcount.other_threshold = 40
+msgcount.thresholds = {
+  'WARNING': 40,
+  'INFO': 600,
+  'other': 40
+}
 msgcount.required = True # make the test exit code depend on this step
 
 import sys
-- 
GitLab