class SqlDbRecorder(RecordHandler):
    log = logging.getLogger(__name__)

    def __init__(self):
        super(SqlDbRecorder, self).__init__()
        self._queue = Queue(1000)
        self._repositery = Repositories()
        SqlRecordCountHandler(self._queue).start()

    def record_traffic(self, record):
        self._queue.put(record)
        try:
            with session_scope() as _session:
                self._repositery.create_record(_session, **record.as_dict())
        except Exception as e:
            self.log.exception(
                "Exception %s happened during recording traffic" % e)
class SqlRecordCountHandler(RecordCountHandler):
    def __init__(self, queue):
        super(SqlRecordCountHandler, self).__init__(queue)
        self._repositery = Repositories()

    def _create_record_count(self):
        created = time.time()
        for proto in list(self._proto_record_count.keys()):
            success_count = self._proto_record_count[proto]['success']
            failure_count = self._proto_record_count[proto]['failure']
            if success_count > 0 or failure_count > 0:
                with session_scope() as _session:
                    self._repositery.create_record_count(
                        _session, proto, success_count, failure_count, created)
                self._proto_record_count[proto]['success'] = 0
                self._proto_record_count[proto]['failure'] = 0

    def _create_latency_stats(self):
        created = time.time()
        with session_scope() as _session:
            self._repositery.create_latency_stats(_session, self._latency_sum,
                                                  self._samples, created)
        self._latency_sum = 0
        self._samples = 0
 def __init__(self, queue):
     super(SqlRecordCountHandler, self).__init__(queue)
     self._repositery = Repositories()
 def __init__(self):
     super(SqlDbRecorder, self).__init__()
     self._queue = Queue(1000)
     self._repositery = Repositories()
     SqlRecordCountHandler(self._queue).start()
예제 #5
0
 def __init__(self):
     self._repository = Repositories()
     self.log = logging.getLogger(__name__)
예제 #6
0
class DBConnectedState(ConnectedState):
    """Stores the connected state in Relational Database"""
    def __init__(self):
        self._repository = Repositories()
        self.log = logging.getLogger(__name__)

    def update_connected_state(self, endpoint, servers=None, clients=None):
        """
        Update connected state for an endpoint
        :param endpoint: endpoint ip
        :type endpoint: str
        :param servers: list of (protocol, port) tuple
        :type servers: list
        :param clients: list of clients
        :type clients: list
        """
        with session_scope() as session:
            self._repository.connected_state.update(session,
                                                    endpoint,
                                                    servers=servers,
                                                    clients=clients)

    def create_connected_state(self,
                               endpoint=None,
                               servers=None,
                               clients=None):
        """Create connected state for an endpoint
        :param endpoint: endpoint ip
        :type endpoint: str
        :param servers: list of (protocol, port) tuple
        :type servers: list
        :param clients: list of clients
        :type clients: list
        """
        with session_scope() as session:
            self._repository.create_connected_state(session,
                                                    endpoint=endpoint,
                                                    servers=servers,
                                                    clients=clients)

    def get_connected_state(self, endpoint=None):
        """
        Get connected state for an endpoint if specified
        :param endpoint: endpoint ip
        :type endpoint: str
        """
        with session_scope() as session:
            filters = {}
            if endpoint:
                filters['endpoint'] = endpoint
            return self._repository.connected_state.get_all(session, **filters)

    def delete_connected_state(self, endpoint=None):
        """
        Delete connected state for an endpoint
        :param endpoint: endpoint ip
        :type endpoint: str
        """
        with session_scope() as session:
            if endpoint:
                self._repository.connected_state.delete(session,
                                                        endpoint=endpoint)
            else:
                self._repository.connected_state.delete_all(session)
            session.commit()

    def get_servers(self, endpoint):
        """
        Get Servers for an endpoint
        :param endpoint: endpoint ip
        :type endpoint: str
        """
        with session_scope() as session:
            return self._repository.connected_state.get_servers(
                session, endpoint)

    def get_clients(self, endpoint):
        """
        Get Clients for an endpoint
        :param endpoint: endpoint ip
        :type endpoint: str
        """
        with session_scope() as session:
            return self._repository.connected_state.get_clients(
                session, endpoint)
예제 #7
0
 def __init__(self):
     self._repository = Repositories()