How to use the translators.base_translator.BaseTranslator function in translators

To help you get started, we’ve selected a few translators examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github smartsdk / ngsi-timeseries-api / src / translators / sql_translator.py View on Github external
isn't.

    :param entity: the entity.
    :return: the type string if there's an type, `None` otherwise.
    """
    return maybe_map(str, safe_get_value(entity, NGSI_TYPE))


# TODO: Refactor
# I suggest we refactor both this and the Crate translator using something
# like SQLAlchemy if we want to keep the same approach of doing everything
# in Python, but this isn't exactly a good thing for performance---way too
# many calls on each insert! Perhaps we should come up with a more efficient
# design or at least consider stored procs.

class SQLTranslator(base_translator.BaseTranslator):
    NGSI_TO_SQL = NGSI_TO_SQL
    config = SQLTranslatorConfig()

    def _refresh(self, entity_types, fiware_service=None):
        """
        Used for testing purposes only!
        Refreshing ensures a query after an insert retrieves the inserted data.
        :param entity_types: list(str) list of entity types whose tables will be
         refreshed
        """
        table_names = [self._et2tn(et, fiware_service) for et in entity_types]
        table_names.append(METADATA_TABLE_NAME)
        self.cursor.execute("refresh table {}".format(','.join(table_names)))

    def _prepare_data_table(self, table_name, table, fiware_service):
        raise NotImplementedError
github smartsdk / ngsi-timeseries-api / src / translators / rethink.py View on Github external
def translate_to_ngsi(self, entries):
        for e in entries:
            entity = {}
            for k, v in e.items():
                if k == 'id':
                    # RethinkDB gives an id to each object, which we ignore for now.
                    continue

                elif k == 'entity_type':
                    entity['type'] = v

                elif k == 'entity_id':
                    entity['id'] = v

                elif k == base_translator.BaseTranslator.TIME_INDEX_NAME:
                    entity[k] = v

                else:
                    t = ATTR_TO_TYPE[k]
                    entity[k] = {'value': v, 'type': t}
            yield entity
github smartsdk / ngsi-timeseries-api / src / translators / rethink.py View on Github external
import rethinkdb as rt

from translators import base_translator
from utils.common import ATTR_TO_TYPE


class RethinkTranslator(base_translator.BaseTranslator):

    TABLE_NAME = "notifications"

    def __init__(self, host, port=28015, db_name="test"):
        super(RethinkTranslator, self).__init__(host, port, db_name)
        self.conn = None


    def setup(self):
        self.conn = rt.connect(self.host, self.port)
        # rt.db(self.db_name).table_drop(self.TABLE_NAME).run(self.conn)
        self.create_table()


    def dispose(self):
        rt.db(self.db_name).table_drop(self.TABLE_NAME).run(self.conn)
github smartsdk / ngsi-timeseries-api / src / translators / rethink.py View on Github external
def translate_from_ngsi(self, entities):
        for entity in entities:
            entry = {}
            for k in entity:
                if k == 'type':
                    entry['entity_type'] = entity[k]
                elif k == 'id':
                    entry['entity_id'] = entity[k]
                elif k == base_translator.BaseTranslator.TIME_INDEX_NAME:
                    entry[k] = entity[k]
                else:
                    entry[k] = entity[k]["value"]
            yield entry
github smartsdk / ngsi-timeseries-api / src / translators / influx.py View on Github external
from influxdb import InfluxDBClient
from translators import base_translator
from utils.common import ATTR_TO_TYPE


class InfluxTranslator(base_translator.BaseTranslator):

    def __init__(self, host, port=8086, db_name="ngsi-tsdb"):
        super(InfluxTranslator, self).__init__(host, port, db_name)
        self.client = InfluxDBClient(host, port, 'root', 'root')


    def setup(self):
        self.client.create_database(self.db_name)


    def dispose(self):
        self.client.drop_database(self.db_name)


    def translate_from_ngsi(self, entities):
        """