Beispiel #1
0
    def topology_to_storage(self, topology, message_type=None):
        """
        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.

        The topology information (realm, node) will
        get mapped to the database name and measurement name to
        compute the storage location for measurements.

            - realm + node     = database name
            - sensors | events = table name

        """

        # Derive database table suffix from message type.
        table_suffix = self.get_table_suffix(topology, message_type)

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

        # Format and sanitize all input parameters used for database addressing.
        # Todo: Investigate using tags additionally to / instead of only "storage.measurement".
        sanitize = self.sanitize_db_identifier
        storage.label = sanitize('{}'.format(storage.node))
        storage.database = sanitize('{}_{}'.format(storage.realm,
                                                   storage.node))
        storage.measurement = sanitize('{}'.format(table_suffix))
        storage.measurement_events = sanitize('{}'.format('events'))

        return storage
Beispiel #2
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
        # Todo: Move specific stuff about WeeWX or Tasmota to some device-specific knowledgebase.

        # data:     Regular endpoint
        # loop:     WeeWX
        # SENSOR:   Sonoff-Tasmota
        if topology.slot.startswith('data') or topology.slot.startswith('loop') \
                or topology.slot.endswith('SENSOR') or topology.slot.endswith('STATE'):
            suffix = 'sensors'

        elif topology.slot.startswith('event'):
            suffix = 'events'

        else:
            suffix = 'unknown'

        # Use topology information as blueprint for storage address
        storage = SmartBunch(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
Beispiel #3
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 = SmartBunch(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