2013-10-14 23:09:13 +02:00
|
|
|
|
2013-10-17 00:32:32 +02:00
|
|
|
'''
|
|
|
|
searx is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
searx is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with searx. If not, see < http://www.gnu.org/licenses/ >.
|
|
|
|
|
|
|
|
(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
|
|
|
|
'''
|
|
|
|
|
2016-11-19 17:51:19 +01:00
|
|
|
from os.path import realpath, dirname
|
2014-02-05 20:24:31 +01:00
|
|
|
import sys
|
2016-07-04 22:46:43 +02:00
|
|
|
from flask_babel import gettext
|
2014-07-07 13:59:27 +02:00
|
|
|
from operator import itemgetter
|
2014-02-05 20:24:31 +01:00
|
|
|
from searx import settings
|
2015-01-09 04:13:05 +01:00
|
|
|
from searx import logger
|
2016-11-19 17:51:19 +01:00
|
|
|
from searx.utils import load_module
|
2015-01-09 04:13:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
logger = logger.getChild('engines')
|
2013-10-14 23:09:13 +02:00
|
|
|
|
|
|
|
engine_dir = dirname(realpath(__file__))
|
2013-10-23 23:54:46 +02:00
|
|
|
|
2013-10-15 19:11:43 +02:00
|
|
|
engines = {}
|
2013-10-14 23:09:13 +02:00
|
|
|
|
2013-10-17 21:06:28 +02:00
|
|
|
categories = {'general': []}
|
2016-11-19 17:51:19 +01:00
|
|
|
_initialized = False
|
2013-10-17 21:06:28 +02:00
|
|
|
|
2014-01-31 15:45:18 +01:00
|
|
|
engine_shortcuts = {}
|
2016-02-19 15:13:01 +01:00
|
|
|
engine_default_args = {'paging': False,
|
|
|
|
'categories': ['general'],
|
|
|
|
'language_support': True,
|
|
|
|
'safesearch': False,
|
|
|
|
'timeout': settings['outgoing']['request_timeout'],
|
|
|
|
'shortcut': '-',
|
2016-02-20 00:21:56 +01:00
|
|
|
'disabled': False,
|
|
|
|
'suspend_end_time': 0,
|
2016-07-17 18:42:30 +02:00
|
|
|
'continuous_errors': 0,
|
|
|
|
'time_range_support': False}
|
2014-01-31 15:45:18 +01:00
|
|
|
|
2014-01-19 22:59:01 +01:00
|
|
|
|
2014-12-13 19:26:40 +01:00
|
|
|
def load_engine(engine_data):
|
2016-09-28 22:30:05 +02:00
|
|
|
|
|
|
|
if '_' in engine_data['name']:
|
|
|
|
logger.error('Engine name conains underscore: "{}"'.format(engine_data['name']))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
engine_module = engine_data['engine']
|
|
|
|
|
2016-07-25 23:36:52 +02:00
|
|
|
try:
|
2016-11-19 17:51:19 +01:00
|
|
|
engine = load_module(engine_module + '.py', engine_dir)
|
2016-07-25 23:36:52 +02:00
|
|
|
except:
|
2016-09-28 22:30:05 +02:00
|
|
|
logger.exception('Cannot load engine "{}"'.format(engine_module))
|
2016-07-25 23:36:52 +02:00
|
|
|
return None
|
2014-01-31 04:35:23 +01:00
|
|
|
|
2013-10-23 23:54:46 +02:00
|
|
|
for param_name in engine_data:
|
|
|
|
if param_name == 'engine':
|
|
|
|
continue
|
|
|
|
if param_name == 'categories':
|
2014-01-19 00:17:02 +01:00
|
|
|
if engine_data['categories'] == 'none':
|
2013-11-04 21:46:23 +01:00
|
|
|
engine.categories = []
|
|
|
|
else:
|
2014-01-19 22:59:01 +01:00
|
|
|
engine.categories = map(
|
|
|
|
str.strip, engine_data['categories'].split(','))
|
2013-10-23 23:54:46 +02:00
|
|
|
continue
|
2014-01-19 00:17:02 +01:00
|
|
|
setattr(engine, param_name, engine_data[param_name])
|
2014-01-31 15:45:18 +01:00
|
|
|
|
2016-02-19 15:13:01 +01:00
|
|
|
for arg_name, arg_value in engine_default_args.iteritems():
|
|
|
|
if not hasattr(engine, arg_name):
|
|
|
|
setattr(engine, arg_name, arg_value)
|
2015-01-31 23:11:45 +01:00
|
|
|
|
2014-01-31 15:45:18 +01:00
|
|
|
# checking required variables
|
2013-10-25 23:41:14 +02:00
|
|
|
for engine_attr in dir(engine):
|
|
|
|
if engine_attr.startswith('_'):
|
|
|
|
continue
|
2014-01-20 02:31:20 +01:00
|
|
|
if getattr(engine, engine_attr) is None:
|
2015-01-09 04:13:05 +01:00
|
|
|
logger.error('Missing engine config attribute: "{0}.{1}"'
|
2015-05-02 15:45:17 +02:00
|
|
|
.format(engine.name, engine_attr))
|
2013-10-25 23:41:14 +02:00
|
|
|
sys.exit(1)
|
2014-01-31 15:45:18 +01:00
|
|
|
|
2014-01-19 22:59:01 +01:00
|
|
|
engine.stats = {
|
|
|
|
'result_count': 0,
|
|
|
|
'search_count': 0,
|
|
|
|
'page_load_time': 0,
|
|
|
|
'score_count': 0,
|
|
|
|
'errors': 0
|
|
|
|
}
|
2014-01-31 15:45:18 +01:00
|
|
|
|
2016-02-19 15:13:01 +01:00
|
|
|
for category_name in engine.categories:
|
|
|
|
categories.setdefault(category_name, []).append(engine)
|
|
|
|
|
|
|
|
if engine.shortcut in engine_shortcuts:
|
|
|
|
logger.error('Engine config error: ambigious shortcut: {0}'.format(engine.shortcut))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
engine_shortcuts[engine.shortcut] = engine.name
|
2013-10-15 18:19:06 +02:00
|
|
|
|
2014-12-13 19:26:40 +01:00
|
|
|
return engine
|
2014-01-31 15:45:18 +01:00
|
|
|
|
2014-01-19 22:59:01 +01:00
|
|
|
|
2013-10-27 01:02:28 +02:00
|
|
|
def get_engines_stats():
|
2014-01-05 23:13:53 +01:00
|
|
|
# TODO refactor
|
2013-10-27 02:50:24 +02:00
|
|
|
pageloads = []
|
|
|
|
results = []
|
2013-10-27 14:21:23 +01:00
|
|
|
scores = []
|
2013-10-27 20:45:21 +01:00
|
|
|
errors = []
|
2014-01-05 23:13:53 +01:00
|
|
|
scores_per_result = []
|
2013-10-27 01:02:28 +02:00
|
|
|
|
2014-01-19 22:59:01 +01:00
|
|
|
max_pageload = max_results = max_score = max_errors = max_score_per_result = 0 # noqa
|
2013-10-27 01:02:28 +02:00
|
|
|
for engine in engines.values():
|
|
|
|
if engine.stats['search_count'] == 0:
|
|
|
|
continue
|
2014-01-19 22:59:01 +01:00
|
|
|
results_num = \
|
|
|
|
engine.stats['result_count'] / float(engine.stats['search_count'])
|
|
|
|
load_times = engine.stats['page_load_time'] / float(engine.stats['search_count']) # noqa
|
2013-10-27 14:21:23 +01:00
|
|
|
if results_num:
|
2014-01-19 22:59:01 +01:00
|
|
|
score = engine.stats['score_count'] / float(engine.stats['search_count']) # noqa
|
2014-01-05 23:13:53 +01:00
|
|
|
score_per_result = score / results_num
|
2013-10-27 14:21:23 +01:00
|
|
|
else:
|
2014-01-05 23:13:53 +01:00
|
|
|
score = score_per_result = 0.0
|
2013-10-27 02:50:24 +02:00
|
|
|
max_results = max(results_num, max_results)
|
|
|
|
max_pageload = max(load_times, max_pageload)
|
2013-10-27 14:21:23 +01:00
|
|
|
max_score = max(score, max_score)
|
2014-01-05 23:13:53 +01:00
|
|
|
max_score_per_result = max(score_per_result, max_score_per_result)
|
2013-10-27 20:45:21 +01:00
|
|
|
max_errors = max(max_errors, engine.stats['errors'])
|
2013-10-27 02:50:24 +02:00
|
|
|
pageloads.append({'avg': load_times, 'name': engine.name})
|
|
|
|
results.append({'avg': results_num, 'name': engine.name})
|
2013-10-27 14:21:23 +01:00
|
|
|
scores.append({'avg': score, 'name': engine.name})
|
2013-10-27 20:45:21 +01:00
|
|
|
errors.append({'avg': engine.stats['errors'], 'name': engine.name})
|
2014-01-19 22:59:01 +01:00
|
|
|
scores_per_result.append({
|
|
|
|
'avg': score_per_result,
|
|
|
|
'name': engine.name
|
|
|
|
})
|
2013-10-27 02:50:24 +02:00
|
|
|
|
|
|
|
for engine in pageloads:
|
2014-10-01 19:16:34 +02:00
|
|
|
if max_pageload:
|
|
|
|
engine['percentage'] = int(engine['avg'] / max_pageload * 100)
|
|
|
|
else:
|
|
|
|
engine['percentage'] = 0
|
2013-10-27 02:50:24 +02:00
|
|
|
|
|
|
|
for engine in results:
|
2014-10-01 19:16:34 +02:00
|
|
|
if max_results:
|
|
|
|
engine['percentage'] = int(engine['avg'] / max_results * 100)
|
|
|
|
else:
|
|
|
|
engine['percentage'] = 0
|
2013-10-27 02:50:24 +02:00
|
|
|
|
2013-10-27 14:21:23 +01:00
|
|
|
for engine in scores:
|
2014-10-01 19:16:34 +02:00
|
|
|
if max_score:
|
|
|
|
engine['percentage'] = int(engine['avg'] / max_score * 100)
|
|
|
|
else:
|
|
|
|
engine['percentage'] = 0
|
2013-10-27 14:21:23 +01:00
|
|
|
|
2014-01-05 23:13:53 +01:00
|
|
|
for engine in scores_per_result:
|
2014-10-01 19:16:34 +02:00
|
|
|
if max_score_per_result:
|
2014-10-19 12:41:04 +02:00
|
|
|
engine['percentage'] = int(engine['avg']
|
|
|
|
/ max_score_per_result * 100)
|
2014-10-01 19:16:34 +02:00
|
|
|
else:
|
|
|
|
engine['percentage'] = 0
|
2014-01-05 23:13:53 +01:00
|
|
|
|
2013-10-27 20:45:21 +01:00
|
|
|
for engine in errors:
|
|
|
|
if max_errors:
|
2014-01-19 22:59:01 +01:00
|
|
|
engine['percentage'] = int(float(engine['avg']) / max_errors * 100)
|
2013-10-27 20:45:21 +01:00
|
|
|
else:
|
|
|
|
engine['percentage'] = 0
|
|
|
|
|
2014-01-19 22:59:01 +01:00
|
|
|
return [
|
|
|
|
(
|
2014-01-22 00:17:49 +01:00
|
|
|
gettext('Page loads (sec)'),
|
|
|
|
sorted(pageloads, key=itemgetter('avg'))
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gettext('Number of results'),
|
2014-01-19 22:59:01 +01:00
|
|
|
sorted(results, key=itemgetter('avg'), reverse=True)
|
|
|
|
),
|
|
|
|
(
|
2014-01-22 00:17:49 +01:00
|
|
|
gettext('Scores'),
|
|
|
|
sorted(scores, key=itemgetter('avg'), reverse=True)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gettext('Scores per result'),
|
2014-01-19 22:59:01 +01:00
|
|
|
sorted(scores_per_result, key=itemgetter('avg'), reverse=True)
|
|
|
|
),
|
2014-01-22 00:17:49 +01:00
|
|
|
(
|
|
|
|
gettext('Errors'),
|
|
|
|
sorted(errors, key=itemgetter('avg'), reverse=True)
|
|
|
|
),
|
2014-01-19 22:59:01 +01:00
|
|
|
]
|
2014-12-13 19:26:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
if 'engines' not in settings or not settings['engines']:
|
2015-01-09 04:13:05 +01:00
|
|
|
logger.error('No engines found. Edit your settings.yml')
|
2014-12-13 19:26:40 +01:00
|
|
|
exit(2)
|
|
|
|
|
|
|
|
for engine_data in settings['engines']:
|
|
|
|
engine = load_engine(engine_data)
|
2016-07-25 23:36:52 +02:00
|
|
|
if engine is not None:
|
|
|
|
engines[engine.name] = engine
|