import os
import logging
import random
import asyncio
from .constants import SERVICE

VERBOSE = ' --verbose'


class SiriDB:

    LOG_LEVEL = 'CRITICAL'

    def __init__(self,
                 dbname='dbtest',
                 time_precision='s',
                 buffer_path='',
                 duration_log='1d',
                 duration_num='1w',
                 buffer_size=1024,
                 **unused):
        self.dbname = dbname
        self.time_precision = time_precision
        self.buffer_path = buffer_path
        self.duration_log = duration_log
        self.duration_num = duration_num
        self.buffer_size = buffer_size
        self.servers = []

    async def create_on(self, server, sleep=None):
        await asyncio.sleep(1)

        logging.info('Create database {} on {}'.format(
            self.dbname,
            server.name))

        rc = os.system(
            '{service} '
            '-u sa -p siri -s {addr} '
            'new-database '
            '--db-name {dbname} '
            '--time-precision {time_precision} '
            '--duration-log {duration_log} '
            '--duration-num {duration_num} '
            '--buffer-size {buffer_size}'
            '{verbose}'.format(
                service=SERVICE,
                addr=server.addr,
                verbose=VERBOSE if self.LOG_LEVEL == 'DEBUG'
                else ' >/dev/null',
                **vars(self)))

        assert rc == 0, 'Expected rc = 0 but got rc = {}'.format(rc)

        self.servers.append(server)

        if sleep:
            await asyncio.sleep(sleep)

    async def add_database(
                self,
                server,
                dbname='dbtest',
                time_precision='s',
                buffer_path='',
                duration_log='1d',
                duration_num='1w',
                buffer_size=1024,
                sleep=None):

        logging.info('Create database {} on {}'.format(
            dbname,
            server.name))

        rc = os.system(
            '{service} '
            '-u sa -p siri -s {addr} '
            'new-database '
            '--db-name {dbname} '
            '--time-precision {time_precision} '
            '--duration-log {duration_log} '
            '--duration-num {duration_num} '
            '--buffer-size {buffer_size}'
            '{verbose}'.format(
                service=SERVICE,
                addr=server.addr,
                dbname=dbname,
                time_precision=time_precision,
                duration_log=duration_log,
                duration_num=duration_num,
                buffer_size=buffer_size,
                verbose=VERBOSE if self.LOG_LEVEL == 'DEBUG'
                else ' >/dev/null'))

        assert rc == 0, 'Expected rc = 0 but got rc = {}'.format(rc)

        self.servers.append(server)

        if sleep:
            await asyncio.sleep(sleep)

    async def add_replica(
                self,
                server,
                pool,
                username='iris',
                password='siri',
                remote_server=None,
                sleep=None):

        if remote_server is None:
            remote_server = random.choice(self.servers)

        rc = os.system(
            '{service} '
            '-u sa -p siri -s {addr} '
            'new-replica '
            '--db-name {dbname} '
            '--db-server {dbaddr} '
            '--db-user {dbuser} '
            '--db-password {dbpassword} '
            '--pool {pool} '
            '--force{verbose}'.format(
                service=SERVICE,
                addr=server.addr,
                dbaddr=remote_server.addr,
                dbuser=username,
                dbpassword=password,
                pool=pool,
                verbose=VERBOSE if self.LOG_LEVEL == 'DEBUG'
                else ' >/dev/null',
                **vars(self)))

        assert rc == 0, 'Expected rc = 0 but got rc = {}'.format(rc)

        self.servers.append(server)

        if sleep:
            await asyncio.sleep(sleep)

    async def add_pool(
                self,
                server,
                username='iris',
                password='siri',
                remote_server=None,
                sleep=None):

        if remote_server is None:
            remote_server = random.choice(self.servers)

        rc = os.system(
            '{service} '
            '-u sa -p siri -s {addr} '
            'new-pool '
            '--db-name {dbname} '
            '--db-server {dbaddr} '
            '--db-user {dbuser} '
            '--db-password {dbpassword} '
            '--force{verbose}'.format(
                service=SERVICE,
                addr=server.addr,
                dbaddr=remote_server.addr,
                dbuser=username,
                dbpassword=password,
                verbose=VERBOSE if self.LOG_LEVEL == 'DEBUG'
                else ' >/dev/null',
                **vars(self)))

        assert rc == 0, 'Expected rc = 0 but got rc = {}'.format(rc)

        self.servers.append(server)

        if sleep:
            await asyncio.sleep(sleep)

@@ -1589,1319 +1589,8 @@
 p)%0A%0A
-    async def add_database(%0A                self,%0A                server,%0A                dbname='dbtest',%0A                time_precision='s',%0A                buffer_path='',%0A                duration_log='1d',%0A                duration_num='1w',%0A                buffer_size=1024,%0A                sleep=None):%0A%0A        logging.info('Create database %7B%7D on %7B%7D'.format(%0A            dbname,%0A            server.name))%0A%0A        rc = os.system(%0A            '%7Bservice%7D '%0A            '-u sa -p siri -s %7Baddr%7D '%0A            'new-database '%0A            '--db-name %7Bdbname%7D '%0A            '--time-precision %7Btime_precision%7D '%0A            '--duration-log %7Bduration_log%7D '%0A            '--duration-num %7Bduration_num%7D '%0A            '--buffer-size %7Bbuffer_size%7D'%0A            '%7Bverbose%7D'.format(%0A                service=SERVICE,%0A                addr=server.addr,%0A                dbname=dbname,%0A                time_precision=time_precision,%0A                duration_log=duration_log,%0A                duration_num=duration_num,%0A                buffer_size=buffer_size,%0A                verbose=VERBOSE if self.LOG_LEVEL == 'DEBUG'%0A                else ' %3E/dev/null'))%0A%0A        assert rc == 0, 'Expected rc = 0 but got rc = %7B%7D'.format(rc)%0A%0A        self.servers.append(server)%0A%0A        if sleep:%0A            await asyncio.sleep(sleep)%0A%0A
     

update itest