From ec834935381e5cfb4ebc3820ed2f668d0fc7855d Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Wed, 19 May 2021 11:24:18 +0200 Subject: [PATCH 1/4] [fix] offline engine: don't crash on time recording --- searx/network/__init__.py | 12 +++++++----- searx/results.py | 7 ++++--- searx/webapp.py | 8 ++++---- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/searx/network/__init__.py b/searx/network/__init__.py index 981b2261a..587198144 100644 --- a/searx/network/__init__.py +++ b/searx/network/__init__.py @@ -44,7 +44,8 @@ def reset_time_for_thread(): def get_time_for_thread(): - return THREADLOCAL.total_time + """returns thread's total time or None""" + return THREADLOCAL.__dict__.get('total_time') def set_timeout_for_thread(timeout, start_time=None): @@ -57,10 +58,11 @@ def set_context_network_name(network_name): def get_context_network(): - try: - return THREADLOCAL.network - except AttributeError: - return get_network() + """If set return thread's network. + + If unset, return value from :py:obj:`get_network`. + """ + return THREADLOCAL.__dict__.get('network') or get_network() def request(method, url, **kwargs): diff --git a/searx/results.py b/searx/results.py index a1c1d8527..d0cb4df3f 100644 --- a/searx/results.py +++ b/searx/results.py @@ -371,11 +371,12 @@ class ResultContainer: self.unresponsive_engines.add((engine_name, error_type, error_message, suspended)) def add_timing(self, engine_name, engine_time, page_load_time): - self.timings.append({ + timing = { 'engine': engines[engine_name].shortcut, 'total': engine_time, - 'load': page_load_time - }) + 'load': page_load_time, + } + self.timings.append(timing) def get_timings(self): return self.timings diff --git a/searx/webapp.py b/searx/webapp.py index 0ea82d098..7df12af18 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -568,7 +568,7 @@ def post_request(response): timings_total = ['total_' + str(i) + '_' + v['engine'] + ';dur=' + str(round(v['total'] * 1000, 3)) for i, v in enumerate(timings)] timings_load = ['load_' + str(i) + '_' + v['engine'] + - ';dur=' + str(round(v['load'] * 1000, 3)) for i, v in enumerate(timings)] + ';dur=' + str(round(v['load'] * 1000, 3)) for i, v in enumerate(timings) if v.get('load')] timings_all = timings_all + timings_total + timings_load response.headers.add('Server-Timing', ', '.join(timings_all)) return response @@ -923,9 +923,9 @@ def preferences(): result_count = int(result_count_sum / float(successful_count)) if successful_count else 0 stats[e.name] = { - 'time': median if median else None, - 'rate80': rate80 if rate80 else None, - 'rate95': rate95 if rate95 else None, + 'time': median, + 'rate80': rate80, + 'rate95': rate95, 'warn_timeout': e.timeout > settings['outgoing']['request_timeout'], 'supports_selected_language': _is_selected_language_supported(e, request.preferences), 'result_count': result_count, From 3014463fedd5a4a1e4a7077578a8095e12390c9a Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Wed, 19 May 2021 11:25:32 +0200 Subject: [PATCH 2/4] [fix] metrics: processing time = total time if there is no http time It was previsouly None Fix /stats --- searx/metrics/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/searx/metrics/__init__.py b/searx/metrics/__init__.py index c2dcfaae4..867daa068 100644 --- a/searx/metrics/__init__.py +++ b/searx/metrics/__init__.py @@ -157,7 +157,11 @@ def get_reliabilities(engline_name_list, checker_results): def round_or_none(number, digits): - return round(number, digits) if number else number + '''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): @@ -192,6 +196,8 @@ def get_engines_stats(engine_name_list): 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({ 'name': engine_name, 'total': round_or_none(time_total, 1), @@ -200,9 +206,9 @@ def get_engines_stats(engine_name_list): '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, 1) if time_total else None, - 'processing_p80': round(time_total_p80 - time_http_p80, 1) if time_total else None, - 'processing_p95': round(time_total_p95 - time_http_p95, 1) if time_total else None, + '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, 'result_count': result_count, From 99aaf86b739b8b93ec0f133822753e8c5ea96e0e Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Wed, 19 May 2021 11:28:57 +0200 Subject: [PATCH 3/4] [fix] offline engines: fix templates /stats and /preferences --- searx/templates/oscar/preferences.html | 6 ++++-- searx/templates/oscar/stats.html | 12 ++++++------ searx/templates/simple/preferences.html | 6 ++++-- searx/templates/simple/stats.html | 13 ++++++------- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html index 33fb41061..352b90eed 100644 --- a/searx/templates/oscar/preferences.html +++ b/searx/templates/oscar/preferences.html @@ -25,11 +25,13 @@ {{- "" -}} {%- if stats[engine_name].time != None -%} {{- stats[engine_name].time -}}{{- "" -}} -