[typing] add results.UnresponsiveEngine

This commit is contained in:
Martin Fischer 2022-01-17 11:11:39 +01:00
parent 0c6a09cae3
commit 193b0efd12
2 changed files with 17 additions and 11 deletions

View file

@ -2,7 +2,7 @@ import re
from collections import defaultdict
from operator import itemgetter
from threading import RLock
from typing import List, NamedTuple
from typing import List, NamedTuple, Set
from urllib.parse import urlparse, unquote
from searx import logger
@ -145,6 +145,12 @@ class Timing(NamedTuple):
load: float
class UnresponsiveEngine(NamedTuple):
engine: str
error_type: str
suspended: bool
class ResultContainer:
"""docstring for ResultContainer"""
@ -176,7 +182,7 @@ class ResultContainer:
self.engine_data = defaultdict(dict)
self._closed = False
self.paging = False
self.unresponsive_engines = set()
self.unresponsive_engines: Set[UnresponsiveEngine] = set()
self.timings: List[Timing] = []
self.redirect_url = None
self.on_result = lambda _: True
@ -409,9 +415,9 @@ class ResultContainer:
return 0
return resultnum_sum / len(self._number_of_results)
def add_unresponsive_engine(self, engine_name, error_type, suspended=False):
def add_unresponsive_engine(self, engine_name: str, error_type: str, suspended: bool = False):
if engines[engine_name].display_error_messages:
self.unresponsive_engines.add((engine_name, error_type, suspended))
self.unresponsive_engines.add(UnresponsiveEngine(engine_name, error_type, suspended))
def add_timing(self, engine_name: str, engine_time: float, page_load_time: float):
self.timings.append(Timing(engine_name, total=engine_time, load=page_load_time))

View file

@ -15,7 +15,7 @@ from timeit import default_timer
from html import escape
from io import StringIO
import typing
from typing import List, Dict
from typing import List, Dict, Iterable
import urllib
from urllib.parse import urlencode
@ -56,7 +56,7 @@ from searx import (
searx_debug,
)
from searx.data import ENGINE_DESCRIPTIONS
from searx.results import Timing
from searx.results import Timing, UnresponsiveEngine
from searx.settings_defaults import OUTPUT_FORMATS
from searx.settings_loader import get_default_settings_path
from searx.exceptions import SearxParameterException
@ -844,21 +844,21 @@ def search():
)
def __get_translated_errors(unresponsive_engines):
def __get_translated_errors(unresponsive_engines: Iterable[UnresponsiveEngine]):
translated_errors = []
# make a copy unresponsive_engines to avoid "RuntimeError: Set changed size
# during iteration" it happens when an engine modifies the ResultContainer
# after the search_multiple_requests method has stopped waiting
for unresponsive_engine in list(unresponsive_engines):
error_user_text = exception_classname_to_text.get(unresponsive_engine[1])
for unresponsive_engine in unresponsive_engines:
error_user_text = exception_classname_to_text.get(unresponsive_engine.error_type)
if not error_user_text:
error_user_text = exception_classname_to_text[None]
error_msg = gettext(error_user_text)
if unresponsive_engine[2]:
if unresponsive_engine.suspended:
error_msg = gettext('Suspended') + ': ' + error_msg
translated_errors.append((unresponsive_engine[0], error_msg))
translated_errors.append((unresponsive_engine.engine, error_msg))
return sorted(translated_errors, key=lambda e: e[0])