Browse Source

[refactor] metrics.get_reliabilities() - make code more readable

- init stat values by None
- drop round_or_none
- don't try to get percentage if base is 'None'

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Markus Heiser 4 years ago
parent
commit
7728e25b11
1 changed files with 51 additions and 38 deletions
  1. 51 38
      searx/metrics/__init__.py

+ 51 - 38
searx/metrics/__init__.py

@@ -156,63 +156,76 @@ def get_reliabilities(engline_name_list, checker_results):
     return reliabilities
 
 
-def round_or_none(number, digits):
-    '''return None if number is None
-    return 0 if number is 0
-    otherwise round number with "digits numbers.
-    '''
-    return round(number, digits) if number is not None else number
-
-
 def get_engines_stats(engine_name_list):
     assert counter_storage is not None
     assert histogram_storage is not None
 
     list_time = []
+    max_time_total = max_result_count = None
 
-    max_time_total = max_result_count = None  # noqa
     for engine_name in engine_name_list:
+
         sent_count = counter('engine', engine_name, 'search', 'count', 'sent')
         if sent_count == 0:
             continue
 
-        successful_count = counter('engine', engine_name, 'search', 'count', 'successful')
-
-        time_total = histogram('engine', engine_name, 'time', 'total').percentage(50)
-        time_http = histogram('engine', engine_name, 'time', 'http').percentage(50)
-        time_total_p80 = histogram('engine', engine_name, 'time', 'total').percentage(80)
-        time_http_p80 = histogram('engine', engine_name, 'time', 'http').percentage(80)
-        time_total_p95 = histogram('engine', engine_name, 'time', 'total').percentage(95)
-        time_http_p95 = histogram('engine', engine_name, 'time', 'http').percentage(95)
-
         result_count = histogram('engine', engine_name, 'result', 'count').percentage(50)
         result_count_sum = histogram('engine', engine_name, 'result', 'count').sum
-        if successful_count and result_count_sum:
-            score = counter('engine', engine_name, 'score')  # noqa
-            score_per_result = score / float(result_count_sum)
-        else:
-            score = score_per_result = 0.0
+        successful_count = counter('engine', engine_name, 'search', 'count', 'successful')
 
+        time_total = histogram('engine', engine_name, 'time', 'total').percentage(50)
         max_time_total = max(time_total or 0, max_time_total or 0)
         max_result_count = max(result_count or 0, max_result_count or 0)
 
-        time_total_is_number = time_total is not None
-
-        list_time.append({
+        stats = {
             'name': engine_name,
-            'total': round_or_none(time_total, 1),
-            'total_p80': round_or_none(time_total_p80, 1),
-            'total_p95': round_or_none(time_total_p95, 1),
-            'http': round_or_none(time_http, 1),
-            'http_p80': round_or_none(time_http_p80, 1),
-            'http_p95': round_or_none(time_http_p95, 1),
-            'processing': round(time_total - (time_http or 0), 1) if time_total_is_number else None,
-            'processing_p80': round(time_total_p80 - (time_http_p80 or 0), 1) if time_total_is_number else None,
-            'processing_p95': round(time_total_p95 - (time_http_p95 or 0), 1) if time_total_is_number else None,
-            'score': score,
-            'score_per_result': score_per_result,
+            'total': None,
+            'total_p80': None,
+            'total_p95': None,
+            'http': None,
+            'http_p80': None,
+            'http_p95': None,
+            'processing': None,
+            'processing_p80': None,
+            'processing_p95': None,
+            'score': 0,
+            'score_per_result': 0,
             'result_count': result_count,
-        })
+        }
+
+        if successful_count and result_count_sum:
+            score = counter('engine', engine_name, 'score')
+
+            stats['score'] = score
+            stats['score_per_result'] = score / float(result_count_sum)
+
+        time_http = histogram('engine', engine_name, 'time', 'http').percentage(50)
+        time_http_p80 = time_http_p95 = 0
+
+        if time_http is not None:
+
+            time_http_p80 = histogram('engine', engine_name, 'time', 'http').percentage(80)
+            time_http_p95 = histogram('engine', engine_name, 'time', 'http').percentage(95)
+
+            stats['http'] = round(time_http, 1)
+            stats['http_p80'] = round(time_http_p80, 1)
+            stats['http_p95'] = round(time_http_p95, 1)
+
+        if time_total is not None:
+
+            time_total_p80 = histogram('engine', engine_name, 'time', 'total').percentage(80)
+            time_total_p95 = histogram('engine', engine_name, 'time', 'total').percentage(95)
+
+            stats['total'] = round(time_total, 1)
+            stats['total_p80'] = round(time_total_p80, 1)
+            stats['total_p95'] = round(time_total_p95, 1)
+
+            stats['processing'] = round(time_total - (time_http or 0), 1)
+            stats['processing_p80'] = round(time_total_p80 - time_http_p80, 1)
+            stats['processing_p95'] = round(time_total_p95 - time_http_p95, 1)
+
+        list_time.append(stats)
+
     return {
         'time': list_time,
         'max_time': math.ceil(max_time_total or 0),