| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-04-26 20:18:20 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  | # pylint: disable=missing-module-docstring | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import typing | 
					
						
							|  |  |  | import math | 
					
						
							|  |  |  | import contextlib | 
					
						
							|  |  |  | from timeit import default_timer | 
					
						
							|  |  |  | from operator import itemgetter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from searx.engines import engines | 
					
						
							| 
									
										
										
										
											2021-12-26 22:44:46 +01:00
										 |  |  | from .models import HistogramStorage, CounterStorage, VoidHistogram, VoidCounterStorage | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | from .error_recorder import count_error, count_exception, errors_per_engines | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | __all__ = [ | 
					
						
							|  |  |  |     "initialize", | 
					
						
							|  |  |  |     "get_engines_stats", | 
					
						
							|  |  |  |     "get_engine_errors", | 
					
						
							|  |  |  |     "histogram", | 
					
						
							|  |  |  |     "histogram_observe", | 
					
						
							|  |  |  |     "histogram_observe_time", | 
					
						
							|  |  |  |     "counter", | 
					
						
							|  |  |  |     "counter_inc", | 
					
						
							|  |  |  |     "counter_add", | 
					
						
							|  |  |  |     "count_error", | 
					
						
							|  |  |  |     "count_exception", | 
					
						
							|  |  |  | ] | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ENDPOINTS = {'search'} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | histogram_storage: typing.Optional[HistogramStorage] = None | 
					
						
							|  |  |  | counter_storage: typing.Optional[CounterStorage] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @contextlib.contextmanager | 
					
						
							|  |  |  | def histogram_observe_time(*args): | 
					
						
							|  |  |  |     h = histogram_storage.get(*args) | 
					
						
							|  |  |  |     before = default_timer() | 
					
						
							|  |  |  |     yield before | 
					
						
							|  |  |  |     duration = default_timer() - before | 
					
						
							|  |  |  |     if h: | 
					
						
							|  |  |  |         h.observe(duration) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         raise ValueError("histogram " + repr((*args,)) + " doesn't not exist") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def histogram_observe(duration, *args): | 
					
						
							|  |  |  |     histogram_storage.get(*args).observe(duration) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def histogram(*args, raise_on_not_found=True): | 
					
						
							|  |  |  |     h = histogram_storage.get(*args) | 
					
						
							|  |  |  |     if raise_on_not_found and h is None: | 
					
						
							|  |  |  |         raise ValueError("histogram " + repr((*args,)) + " doesn't not exist") | 
					
						
							|  |  |  |     return h | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def counter_inc(*args): | 
					
						
							|  |  |  |     counter_storage.add(1, *args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def counter_add(value, *args): | 
					
						
							|  |  |  |     counter_storage.add(value, *args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def counter(*args): | 
					
						
							|  |  |  |     return counter_storage.get(*args) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 22:44:46 +01:00
										 |  |  | def initialize(engine_names=None, enabled=True): | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Initialize metrics | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2021-04-24 07:19:44 +02:00
										 |  |  |     global counter_storage, histogram_storage  # pylint: disable=global-statement | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-26 22:44:46 +01:00
										 |  |  |     if enabled: | 
					
						
							|  |  |  |         counter_storage = CounterStorage() | 
					
						
							|  |  |  |         histogram_storage = HistogramStorage() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         counter_storage = VoidCounterStorage() | 
					
						
							|  |  |  |         histogram_storage = HistogramStorage(histogram_class=VoidHistogram) | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # max_timeout = max of all the engine.timeout | 
					
						
							|  |  |  |     max_timeout = 2 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     for engine_name in engine_names or engines: | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         if engine_name in engines: | 
					
						
							|  |  |  |             max_timeout = max(max_timeout, engines[engine_name].timeout) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # histogram configuration | 
					
						
							|  |  |  |     histogram_width = 0.1 | 
					
						
							|  |  |  |     histogram_size = int(1.5 * max_timeout / histogram_width) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # engines | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     for engine_name in engine_names or engines: | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         # search count | 
					
						
							|  |  |  |         counter_storage.configure('engine', engine_name, 'search', 'count', 'sent') | 
					
						
							|  |  |  |         counter_storage.configure('engine', engine_name, 'search', 'count', 'successful') | 
					
						
							|  |  |  |         # global counter of errors | 
					
						
							|  |  |  |         counter_storage.configure('engine', engine_name, 'search', 'count', 'error') | 
					
						
							|  |  |  |         # score of the engine | 
					
						
							|  |  |  |         counter_storage.configure('engine', engine_name, 'score') | 
					
						
							|  |  |  |         # result count per requests | 
					
						
							|  |  |  |         histogram_storage.configure(1, 100, 'engine', engine_name, 'result', 'count') | 
					
						
							|  |  |  |         # time doing HTTP requests | 
					
						
							|  |  |  |         histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'http') | 
					
						
							|  |  |  |         # total time | 
					
						
							|  |  |  |         # .time.request and ...response times may overlap .time.http time. | 
					
						
							|  |  |  |         histogram_storage.configure(histogram_width, histogram_size, 'engine', engine_name, 'time', 'total') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  | def get_engine_errors(engline_name_list): | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     result = {} | 
					
						
							|  |  |  |     engine_names = list(errors_per_engines.keys()) | 
					
						
							|  |  |  |     engine_names.sort() | 
					
						
							|  |  |  |     for engine_name in engine_names: | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         if engine_name not in engline_name_list: | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         error_stats = errors_per_engines[engine_name] | 
					
						
							|  |  |  |         sent_search_count = max(counter('engine', engine_name, 'search', 'count', 'sent'), 1) | 
					
						
							|  |  |  |         sorted_context_count_list = sorted(error_stats.items(), key=lambda context_count: context_count[1]) | 
					
						
							|  |  |  |         r = [] | 
					
						
							|  |  |  |         for context, count in sorted_context_count_list: | 
					
						
							|  |  |  |             percentage = round(20 * count / sent_search_count) * 5 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             r.append( | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     'filename': context.filename, | 
					
						
							|  |  |  |                     'function': context.function, | 
					
						
							|  |  |  |                     'line_no': context.line_no, | 
					
						
							|  |  |  |                     'code': context.code, | 
					
						
							|  |  |  |                     'exception_classname': context.exception_classname, | 
					
						
							|  |  |  |                     'log_message': context.log_message, | 
					
						
							|  |  |  |                     'log_parameters': context.log_parameters, | 
					
						
							|  |  |  |                     'secondary': context.secondary, | 
					
						
							|  |  |  |                     'percentage': percentage, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         result[engine_name] = sorted(r, reverse=True, key=lambda d: d['percentage']) | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  | def get_reliabilities(engline_name_list, checker_results): | 
					
						
							|  |  |  |     reliabilities = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     engine_errors = get_engine_errors(engline_name_list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for engine_name in engline_name_list: | 
					
						
							|  |  |  |         checker_result = checker_results.get(engine_name, {}) | 
					
						
							|  |  |  |         checker_success = checker_result.get('success', True) | 
					
						
							|  |  |  |         errors = engine_errors.get(engine_name) or [] | 
					
						
							|  |  |  |         if counter('engine', engine_name, 'search', 'count', 'sent') == 0: | 
					
						
							|  |  |  |             # no request | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             reliability = None | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         elif checker_success and not errors: | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             reliability = 100 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         elif 'simple' in checker_result.get('errors', {}): | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             # the basic (simple) test doesn't work: the engine is broken according to the checker | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |             # even if there is no exception | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             reliability = 0 | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2022-06-03 15:41:52 +02:00
										 |  |  |             # pylint: disable=consider-using-generator | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             reliability = 100 - sum([error['percentage'] for error in errors if not error.get('secondary')]) | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         reliabilities[engine_name] = { | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  |             'reliability': reliability, | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |             'errors': errors, | 
					
						
							| 
									
										
										
										
											2021-04-23 21:08:48 +02:00
										 |  |  |             'checker': checker_results.get(engine_name, {}).get('errors', {}), | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     return reliabilities | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def get_engines_stats(engine_name_list): | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     assert counter_storage is not None | 
					
						
							|  |  |  |     assert histogram_storage is not None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     list_time = [] | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |     max_time_total = max_result_count = None | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |     for engine_name in engine_name_list: | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         sent_count = counter('engine', engine_name, 'search', 'count', 'sent') | 
					
						
							|  |  |  |         if sent_count == 0: | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         result_count = histogram('engine', engine_name, 'result', 'count').percentage(50) | 
					
						
							|  |  |  |         result_count_sum = histogram('engine', engine_name, 'result', 'count').sum | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |         successful_count = counter('engine', engine_name, 'search', 'count', 'successful') | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |         time_total = histogram('engine', engine_name, 'time', 'total').percentage(50) | 
					
						
							| 
									
										
										
										
											2021-04-22 17:47:53 +02:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |         stats = { | 
					
						
							| 
									
										
										
										
											2021-04-22 16:05:39 +02:00
										 |  |  |             'name': engine_name, | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |             '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, | 
					
						
							| 
									
										
										
										
											2021-04-22 16:05:39 +02:00
										 |  |  |             'result_count': result_count, | 
					
						
							| 
									
										
										
										
											2021-05-21 12:09:51 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         'time': list_time, | 
					
						
							|  |  |  |         'max_time': math.ceil(max_time_total or 0), | 
					
						
							| 
									
										
										
										
											2021-04-22 12:14:11 +02:00
										 |  |  |         'max_result_count': math.ceil(max_result_count or 0), | 
					
						
							| 
									
										
										
										
											2021-04-14 17:23:15 +02:00
										 |  |  |     } |