1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-17 18:00:12 +01:00

[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 collections import defaultdict
from operator import itemgetter from operator import itemgetter
from threading import RLock from threading import RLock
from typing import List, NamedTuple from typing import List, NamedTuple, Set
from urllib.parse import urlparse, unquote from urllib.parse import urlparse, unquote
from searx import logger from searx import logger
@ -145,6 +145,12 @@ class Timing(NamedTuple):
load: float load: float
class UnresponsiveEngine(NamedTuple):
engine: str
error_type: str
suspended: bool
class ResultContainer: class ResultContainer:
"""docstring for ResultContainer""" """docstring for ResultContainer"""
@ -176,7 +182,7 @@ class ResultContainer:
self.engine_data = defaultdict(dict) self.engine_data = defaultdict(dict)
self._closed = False self._closed = False
self.paging = False self.paging = False
self.unresponsive_engines = set() self.unresponsive_engines: Set[UnresponsiveEngine] = set()
self.timings: List[Timing] = [] self.timings: List[Timing] = []
self.redirect_url = None self.redirect_url = None
self.on_result = lambda _: True self.on_result = lambda _: True
@ -409,9 +415,9 @@ class ResultContainer:
return 0 return 0
return resultnum_sum / len(self._number_of_results) 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: 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): 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)) 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 html import escape
from io import StringIO from io import StringIO
import typing import typing
from typing import List, Dict from typing import List, Dict, Iterable
import urllib import urllib
from urllib.parse import urlencode from urllib.parse import urlencode
@ -56,7 +56,7 @@ from searx import (
searx_debug, searx_debug,
) )
from searx.data import ENGINE_DESCRIPTIONS 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_defaults import OUTPUT_FORMATS
from searx.settings_loader import get_default_settings_path from searx.settings_loader import get_default_settings_path
from searx.exceptions import SearxParameterException 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 = [] translated_errors = []
# make a copy unresponsive_engines to avoid "RuntimeError: Set changed size # make a copy unresponsive_engines to avoid "RuntimeError: Set changed size
# during iteration" it happens when an engine modifies the ResultContainer # during iteration" it happens when an engine modifies the ResultContainer
# after the search_multiple_requests method has stopped waiting # after the search_multiple_requests method has stopped waiting
for unresponsive_engine in list(unresponsive_engines): for unresponsive_engine in unresponsive_engines:
error_user_text = exception_classname_to_text.get(unresponsive_engine[1]) error_user_text = exception_classname_to_text.get(unresponsive_engine.error_type)
if not error_user_text: if not error_user_text:
error_user_text = exception_classname_to_text[None] error_user_text = exception_classname_to_text[None]
error_msg = gettext(error_user_text) error_msg = gettext(error_user_text)
if unresponsive_engine[2]: if unresponsive_engine.suspended:
error_msg = gettext('Suspended') + ': ' + error_msg 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]) return sorted(translated_errors, key=lambda e: e[0])