Ejemplo n.º 1
0
    def topology_to_storage(self, topology):
        """
        Encode topology segment identifiers to database address.

        A database server usually has the concept of multiple databases,
        each with multiple tables. With other databases than RDBMS,
        they might be named differently, but the concept in general
        is the same.

        When mapping the topology quadruple (realm, network, gateway, node) in the form of:

            - realm + network = database name
            - gateway + node  = table name

        We have a perfect fit for computing the slot where to store the measurements.

        """

        # TODO: Investigate using tags additionally to / instead of database.measurement

        # data: Regular endpoint
        # loop: WeeWX (TODO: Move to specific vendor configuration.)
        if topology.slot.startswith('data') or topology.slot.startswith('loop'):
            suffix = 'sensors'
        elif topology.slot.startswith('event'):
            suffix = 'events'
        else:
            suffix = 'unknown'

        # Use topology information as blueprint for storage address
        storage = Bunch(topology)

        # Format and sanitize all input parameters used for database addressing
        sanitize = self.sanitize_db_identifier
        storage.label       = sanitize('{}-{}'.format(storage.gateway, storage.node))
        storage.database    = sanitize('{}_{}'.format(storage.realm, storage.network))
        storage.measurement = sanitize('{}_{}_{}'.format(storage.gateway, storage.node, suffix))
        storage.measurement_events = sanitize('{}_{}_{}'.format(storage.gateway, storage.node, 'events'))

        return storage