# Path: pyehr/utils/services.py
# def get_service_configuration(configuration_file, logger=None):
#     if not logger:
#         logger = get_logger('service_configuration')
#     parser = SafeConfigParser(allow_no_value=True)
#     parser.read(configuration_file)
#     try:
#         conf = ServiceConfig(
#             parser.get('db', 'driver'),
#             parser.get('db', 'host'),
#             parser.get('db', 'database'),
#             parser.get('db', 'versioning_database'),
#             parser.get('db', 'port'),
#             parser.get('db', 'user'),
#             parser.get('db', 'passwd'),
#             parser.get('db', 'patients_repository'),
#             parser.get('db', 'ehr_repository'),
#             parser.get('db', 'ehr_versioning_repository'),
#             parser.get('index', 'url'),
#             parser.get('index', 'database'),
#             parser.get('index', 'user'),
#             parser.get('index', 'passwd'),
#             parser.get('db_service', 'host'),
#             parser.get('db_service', 'port'),
#             parser.get('db_service', 'server_engine'),
#             parser.get('query_service', 'host'),
#             parser.get('query_service', 'port'),
#             parser.get('query_service', 'server_engine')
#         )
#         return conf
#     except NoOptionError, nopt:
#         logger.critical('Missing option %s in configuration file', nopt)
#         return None


import argparse, sys, time, json, os
from pyehr.ehr.services.dbmanager.querymanager import QueryManager, Parser
from pyehr.utils.services import get_service_configuration
from pyehr.utils import get_logger, decode_dict



def get_parser():
    parser = argparse.ArgumentParser('Run queries and measure execution times')
    parser.add_argument('--queries_file', type=str, required=True,
                        help='The JSON file with queries definitions')
    parser.add_argument('--pyehr_config', type=str, required=True,
                        help='pyEHR config file')
    parser.add_argument('--results_file', type=str, required=True,
                        help='The output file where results and times will be reported')
    parser.add_argument('--query_processes', type=int, default=1,
                        help='The number of parallel processes used to run each query (default is single-process)')
    parser.add_argument('--fetch_threshold', type=int, default=10000,
                        help='Fetch only result sets whose size is lesser or equal to this value (default 10000)')
    parser.add_argument('--log_file', type=str, help='LOG file (default=stderr)')
    parser.add_argument('--log_level', type=str, default='INFO',
                        help='LOG level (default=INFO)')
    return parser


def get_query_manager(conf_file):

# Path: pyehr/utils/services.py
# def get_service_configuration(configuration_file, logger=None):
#     if not logger:
#         logger = get_logger('service_configuration')
#     parser = SafeConfigParser(allow_no_value=True)
#     parser.read(configuration_file)
#     try:
#         conf = ServiceConfig(
#             parser.get('db', 'driver'),
#             parser.get('db', 'host'),
#             parser.get('db', 'database'),
#             parser.get('db', 'versioning_database'),
#             parser.get('db', 'port'),
#             parser.get('db', 'user'),
#             parser.get('db', 'passwd'),
#             parser.get('db', 'patients_repository'),
#             parser.get('db', 'ehr_repository'),
#             parser.get('db', 'ehr_versioning_repository'),
#             parser.get('index', 'url'),
#             parser.get('index', 'database'),
#             parser.get('index', 'user'),
#             parser.get('index', 'passwd'),
#             parser.get('db_service', 'host'),
#             parser.get('db_service', 'port'),
#             parser.get('db_service', 'server_engine'),
#             parser.get('query_service', 'host'),
#             parser.get('query_service', 'port'),
#             parser.get('query_service', 'server_engine')
#         )
#         return conf
#     except NoOptionError, nopt:
#         logger.critical('Missing option %s in configuration file', nopt)
#         return None


# Path: test/misc/test_query_performance/run_queries.py
import argparse, sys, time, json, os
from pyehr.ehr.services.dbmanager.querymanager import QueryManager, Parser
from pyehr.utils.services import get_service_configuration
from pyehr.utils import get_logger, decode_dict




def get_parser():
    parser = argparse.ArgumentParser('Run queries and measure execution times')
    parser.add_argument('--queries_file', type=str, required=True,
                        help='The JSON file with queries definitions')
    parser.add_argument('--pyehr_config', type=str, required=True,
                        help='pyEHR config file')
    parser.add_argument('--results_file', type=str, required=True,
                        help='The output file where results and times will be reported')
    parser.add_argument('--query_processes', type=int, default=1,
                        help='The number of parallel processes used to run each query (default is single-process)')
    parser.add_argument('--fetch_threshold', type=int, default=10000,
                        help='Fetch only result sets whose size is lesser or equal to this value (default 10000)')
    parser.add_argument('--log_file', type=str, help='LOG file (default=stderr)')
    parser.add_argument('--log_level', type=str, default='INFO',
                        help='LOG level (default=INFO)')
    return parser


def get_query_manager(conf_file):

    cfg = get_service_configuration(conf_file)