Пример #1
0
 def __init__(self,
              db,
              table_name=None,
              connection=None,
              host=None,
              port=None,
              user=None,
              password=None,
              timeout=0.3):
     if not r:
         raise ImproperlyConfigured(
             'You need to install the rethinkdb library to use the RethinkDB backend.'
         )
     if connection:
         self.connection = connection
     elif host and port:
         if user and password:
             self.connection = r.connect(host=host,
                                         port=port,
                                         db=db,
                                         user=user,
                                         password=password,
                                         timeout=timeout)
         else:
             self.connection = r.connect(host=host,
                                         port=port,
                                         db=db,
                                         timeout=timeout)
     self.db = db
     self.table_name = table_name
     if self.connection is None:
         self.connection = r.connect(db=db, timeout=timeout)
     self._create_database()
Пример #2
0
 def connect(self,*args,**kwargs):
     self.connArgs=args
     self.connKwargs=kwargs
     self.conn=r.connect(*args,**kwargs)
     self.loop = asyncio.get_event_loop()
     self.task = loop.create_task(self._watch())
     return self
Пример #3
0
    def make_connection(self, use_database):
        """
        This method implements the abstract interface
        of the Connection class which makes use of a **Singleton Borg**
        design pattern.
        See it yourself at: [[connections.py]]
        You will connect only once, using the same object.

        Note: authentication is provided with admin commands to server,
        after starting it up, using ssh from app container.
        Expecting the environment variable to contain a key.
        """
        params = {"host": RDB_HOST, "port": RDB_PORT}
        key = os.environ.get('KEYDBPASS') or None
        if key is not None:
            params["auth_key"] = key
            logger.info("Connection is pw protected")
        # else:
        #     logger.warning("Using no authentication")

        # Rethinkdb database connection
        try:
            # IMPORTANT! The chosen ORM library does not work if missing repl()
            # at connection time
            self._connection = r.connect(**params).repl()
            logger.debug("Created Connection")
        except RqlDriverError as e:
            logger.critical("Failed to connect RDB", e)
            return False

        logger.debug("Switching to database " + APP_DB)
        # Note: RDB db selection does not give error if the db does not exist
        self._connection.use(APP_DB)
        return self._connection
Пример #4
0
def connect(
    db_host="localhost", db_port=28015, database="test", user=USER, password=PASSWORD
):
    try:
        logging.info(
            f"[INFO] Connecting into RethinkDB "
            f"with options:\n\tHOST: {db_host}"
            f"\n\tPORT: {db_port}\n\tDATABASE: {database}"
        )
        connection = r.connect(
            host=db_host, port=db_port, db=database, user=user, password=password
        )
    except ReqlDriverError as err:
        logging.error(
            f"[ERROR] Error trying to connect with RethinkDB." f" Traceback: {err}"
        )
        return None
    except Exception as err:
        logging.error(
            f"[ERROR] Unknown error trying to connect with "
            f"RethinkDB. Traceback: {err}"
        )
        return None
    else:
        return connection
Пример #5
0
 def setUpClass(cls):
     cls.conn = r.connect()
     try:
         r.db_drop('test').run(cls.conn)
     except errors.ReqlOpFailedError:
         pass
     r.db_create('test').run(cls.conn)
Пример #6
0
    def make_connection(self, use_database):
        """
        This method implements the abstract interface
        of the Connection class which makes use of a **Singleton Borg**
        design pattern.
        See it yourself at: [[connections.py]]
        You will connect only once, using the same object.

        Note: authentication is provided with admin commands to server,
        after starting it up, using ssh from app container.
        Expecting the environment variable to contain a key.
        """
        params = {"host": RDB_HOST, "port": RDB_PORT}
        key = os.environ.get('KEYDBPASS') or None
        if key is not None:
            params["auth_key"] = key
            logger.info("Connection is pw protected")
        # else:
        #     logger.warning("Using no authentication")

        # Rethinkdb database connection
        try:
            # IMPORTANT! The chosen ORM library does not work if missing repl()
            # at connection time
            self._connection = r.connect(**params).repl()
            logger.debug("Created Connection")
        except RqlDriverError as e:
            logger.critical("Failed to connect RDB", e)
            return False

        logger.debug("Switching to database " + APP_DB)
        # Note: RDB db selection does not give error if the db does not exist
        self._connection.use(APP_DB)
        return self._connection
Пример #7
0
 def open_spider(self, spider):
     if self.rethinkdb_settings:
         self.table_name = self.rethinkdb_settings.pop('table_name')
         self.db_name = self.rethinkdb_settings['db']
         self.conn = r.connect(**self.rethinkdb_settings)
         table_list = r.db(self.db_name).table_list().run(self.conn)
         if self.table_name not in table_list:
             r.db(self.db_name).table_create(self.table_name).run(self.conn)
Пример #8
0
 def connect_rethink(self, db, rethink_host='localhost', auth_key=None, **kwargs):
     '''
     Connect to rethink database,
     '''
     if rethink_host == 'localhost':
         try:
             conn = r.connect(host=rethink_host, port=28015, db=db).repl()
             print("Connected to the \"" + db + "\" database")
             return conn
         except:
             raise Exception("Failed to connect to the database, " + db)
     else:
         try:
             conn = r.connect(host=rethink_host, port=28015, db=db, auth_key=auth_key).repl()
             print("Connected to the \"" + db + "\" database")
             return conn
         except:
             raise Exception("Failed to connect to the database, " + db)
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(TODO_DB))
        connection.run(r.db(TODO_DB).table_create('todos'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
Пример #10
0
    def __init__(self):
        self.db_name = "bitfinex"
        r.connect("localhost", 28015).repl()
        databases = r.db_list().run()
        if self.db_name not in databases:
            r.db_create(self.db_name).run()

        indexes = ["currency", "timestamp"]
        database_tables = r.db(self.db_name).table_list().run()
        tables = ["balance", "orders", "tickers", "trades", "run_exec"]
        for table in tables:
            if table not in database_tables:
                r.db(self.db_name).table_create(table).run()
            # indexes
            indexes_tables = r.db(self.db_name).table(table).index_list().run()
            for index in indexes:
                if index not in indexes_tables:
                    if r.db(self.db_name).table(table).has_fields(index):
                        r.db(self.db_name).table(table).index_create(index).run()
Пример #11
0
 def connect(self):
     self.conn = r.connect(self.host, self.port).repl()
     tables = r.db('dashboard').table_list().run(self.conn)
     print('tables list: {}'.format(tables))
     if ('temperatures' not in tables):
         print('creating temperatures table')
         r.db('dashboard').table_create('temperatures').run(self.conn)
     if ('humidity' not in tables):
         print('creating humidity table')
         r.db('dashboard').table_create('humidity').run(self.conn)
Пример #12
0
def drop_db():
    '''
    Drops Database
    '''
    try:
        db_name = app.config['DATABASE_NAME']
        conn = r.connect()
        if db_name in r.db_list().run(conn):
            r.db_drop(db_name).run(conn)
    except Exception as e:
        cprint("An error occured --> {0}".format(e), 'red', attrs=['bold'])
Пример #13
0
def get_rethink_connection(config):
    """ get rethink db connection  """

    rethink_conn = r.connect(
        host=config.get("RETHINKDB", "RETHINK_HOST"),
        port=config.get("RETHINKDB", "RETHINK_PORT"),
        db=config.get("RETHINKDB", "RETHINK_DB"),
        user=config.get("RETHINKDB", "RETHINK_USER"),
        password=config.get("RETHINKDB", "RETHINK_PASSWORD"),
        timeout=int(config.get("RETHINKDB", "RETHINK_TIMEOUT")),
    )
    return rethink_conn
Пример #14
0
def top_twenty_score_stream():
    '''Generator for changes to top twenty scoring characters'''
    conn = r.connect()
    query = None  # TODO: write query
    try:
        for result in query.run(conn):
            yield 'data: %s\n\n' % json.dumps(result)
    except Exception as e:
        print 'Failed with %s' % e
        yield 'ERROR\n\n'
        conn.close()
        raise e
Пример #15
0
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(DB_NAME))
        connection.run(r.db(DB_NAME).table_create('tools'))
        connection.run(r.db(DB_NAME).table_create('users'))
        connection.run(r.db(DB_NAME).table_create('categories'))
        connection.run(r.db(DB_NAME).table_create('toolboxes'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
Пример #16
0
def migrate():
    try:
        db_name = app.config['DATABASE_NAME']
        conn = r.connect(db=db_name)
        # Create the application tables if they do not exist
        lib = importlib.import_module('api.models')
        for cls in inspect.getmembers(lib, inspect.isclass):
            for base in cls[1].__bases__:
                if base.__name__ == "RethinkDBModel":
                    table_name = getattr(cls[1], '_table')
                    r.db('papers').table_create(table_name).run(conn)
                    print "Created table '{}'...".format(table_name)
        print "Running RethinkDB migration command"
    except Exception as e:
        cprint("An error occured --> {}".format(e.message), 'red', attrs=['bold'])
Пример #17
0
def connect():
    conn = r.connect("db")
    queries = [
        r.db_create(DB),
        db().table_create(REPORTS),
        db().table_create(TARGETS),
        t_reports().index_create("target_id"),
        t_reports().index_create("date"),
    ]
    for q in queries:
        try:
            q.run(conn)
        except r.RqlRuntimeError:
            pass
    return conn
Пример #18
0
    def rethinkdb_waiting_connecton(self):
        """
        Wait for the database to be up and return connection
        """
        counter = self._wait_connection

        while True:
            try:
                connection = Rethinkdb.connect(self._ip, self._port)
                return connection
            except Exception as e:
                time.sleep(1)
                counter = counter -1

                if counter <= 0:
                    self._log.warning("Can't establish connection on {}:{}".format(self._ip, self._port))
                    raise e 
Пример #19
0
    def setup(self):
        """ setup must be called before everything """
        if self.__is_setup:
            return
        self.__is_setup = True

        conn = r.connect(**self.__connect_kwargs)

        def safe_run(rsql, show_error=False):
            try:
                return rsql.run(conn)
            except r.RqlRuntimeError as e:
                if show_error:
                    logger.warning("safe_run rsql:%s, error:%s", rsql, e)
                return False

        # init databases here
        safe_run(r.db_create(self.__dbname))

        rdb = r.db(self.__dbname)
        for tbl in self.__tables.values():
            table_name = tbl['name']
            primary_key = tbl.get('primary_key', 'id')
            safe_run(rdb.table_create(table_name, primary_key=primary_key))

        # reset database
        safe_run(rdb.table("users").index_create("token"))
        safe_run(rdb.table("devices").replace(lambda q: q.without("sources")))

        # reload add idle check functions
        from .views.device import D  # must import in here

        devices = safe_run(rdb.table("devices").filter({
            "using": True
        }).pluck("udid"),
                           show_error=True)

        if devices:
            for d in devices:
                logger.debug("Device: %s is in using state", d['udid'])
                D(d['udid']).release_until_idle()

        r.set_loop_type("tornado")
Пример #20
0
async def on_message(message):
    conn = r.connect("localhost", 28015, db="rationalBridge")
    messageData={ #Convert our message into a format for json serialization.
        'timestamp':utcify(message.timestamp),
        'orig_timestamp':utcify(message.timestamp),
        'tts':message.tts,
        'discord_type':str(message.type),
        'author_id': "discord:{}".format(message.author.id),
        'author_name': "discord:{}".format(message.author.name),
        'server_id': "discord:{}".format(message.author.server.id),
        'server_name': "discord:{}".format(message.author.server.name),#This really should be rethinkd equievlent to a join, but I am lazy
        'content': message.content,
        'embeds': message.embeds,
        'room_id': "discord:{}".format(message.channel.id),
        'room_name': "discord:{}".format(message.channel.name),
    }
    if message.edited_timestamp:
        messageData['timestamp'] = utcify(message.edited_timestamp)
    if messageData['author_id'] != "discord:" + clientId:
        r.table("messages").insert(messageData).run(conn)
Пример #21
0
def wait_for_connection():
    """ Wait for rethinkdb connection at startup? """

    counter = 0
    sleep_time = 1
    testdb = True

    while testdb:
        try:
            rdb = r.connect(host=RDB_HOST, port=RDB_PORT)
            logger.info("Rethinkdb: available")
            testdb = False
            # Need a pool of connections: http://j.mp/1yNP4p0
            if g and "rdb" not in g:
                g.rdb = rdb
        except RqlDriverError:
            logger.warning("Rethinkdb: Not reachable yet")
        counter += 1
        if counter % 10 == 0:
            sleep_time += sleep_time
        time.sleep(sleep_time)

    return True
Пример #22
0
def migrate():
    '''
    Creates Database
    '''
    try:
        db_name = app.config['DATABASE_NAME']
        conn = r.connect()

        # Create Tables
        if db_name not in r.db_list().run(conn):
            db = r.db_create(db_name).run(conn)
            print("Created database '{0}'...".format(db_name))

        # Create the application tables if they do not exist
        lib = importlib.import_module('api.models')
        for cls in inspect.getmembers(lib, inspect.isclass):
            for base in cls[1].__bases__:
                if base.__name__ == "RethinkDBModel":
                    table_name = getattr(cls[1], '_table')
                    r.db(db_name).table_create(table_name).run(conn)
                    print("Created table '{0}'...".format(table_name))
        print("Running RethinkDB migration command")
    except Exception as e:
        print("An error occured --> {0}".format(e))
#dependencies
import socket
import time
from rethinkdb import r

#rethink database connector
r.connect('localhost', 28015).repl()

#to create new databases
connection = r.connect(db="ESP32")

# to create tables
# r.table_create("MPU_GYRO").run(connection)
# r.table_create("VIBRATION").run(connection)
# r.table_create("TEMPERATURE").run(connection)

#socket connection
s = socket.socket()
s.bind(('0.0.0.0', 5767))
s.listen(5)

while True:
    Time = round(time.time() * 1000)  #timeseries in milliseconds
    c, addr = s.accept()

    data = c.recv(1024).decode("utf-8")  #receiving and decoding data
    DATA = data.split()
    print(DATA)

    #inserting into first table
    r.table('MPU_GYRO').insert(
Пример #24
0
import json

from rethinkdb import r
from joblib import load
import falcon
import requests

r.connect(host='localhost', port=28015, db='omega').repl()
clf = load('model.joblib')
vector = load('vector.joblib')


def get_messages(channel, **kwargs):
    query = get_query(channel, kwargs)
    cursor = query.run()
    messages = []
    for document in cursor:
        messages.append(document)
    return messages


def delete_messages(channel, **kwargs):
    query = get_query(channel, kwargs)
    cursor = query.delete().run()
    return cursor


def get_query(channel, kwargs):
    query = r.table('messages').filter(r.row['channel'] == channel)
    if 'client' in kwargs:
        query = query.filter(r.row['client'] == kwargs['client'])
def connect_to_db(config_handler):
    r.connect(
        host=config_handler.rethink_host,
        port=config_handler.rethink_port,
        db=config_handler.rethink_db_name
    ).repl()
Пример #26
0
#!/usr/bin/env python3

import logging

import rethinkdb
from rethinkdb import r
from remodel.connection import pool
from remodel.helpers import create_tables, create_indexes

from skeleton.config import get_db_config

db_host, db_port, db_name, db_user, db_pass = get_db_config()
rethink_conn = r.connect(host=db_host, port=db_port)

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)


def mk_db():
    try:
        r.db_create(db_name).run(rethink_conn)
    except rethinkdb.errors.ReqlOpFailedError as e:
        if 'already exists' in str(e).lower():
            logger.info("db already exists, won't remake it")
        else:
            raise e


pool.configure(host=db_host,
               port=db_port,
Пример #27
0
import datetime
import traceback
import smtplib

config = RawConfigParser()
config.read('appconfig.ini')

mdb_connection = mariadb.connect(
    user=config.get('mariadb', 'user'),
    password=config.get('mariadb', 'password'),
    database=config.get('mariadb', 'database'),
    host=config.get('mariadb', 'host'),
)
mdb_cursor = mdb_connection.cursor()

r.connect(config.get('rethinkdb', 'host'), config.get('rethinkdb',
                                                      'port')).repl()

gmail_user = config.get('gmail', 'user')
gmail_password = config.get('gmail', 'password')

sent_from = gmail_user
to = [config.get('gmail', 'to')]
subject = 'Miscare neautorizata detectata'

fmt = '%Y-%m-%d %H:%M:%S'
last_update = datetime.datetime.strptime(datetime.datetime.now().strftime(fmt),
                                         fmt)

GPIO.setmode(GPIO.BCM)

MOTION_SENSOR_PIN = 4
def main(mmCIFPath, logPath):
    # Start time
    start = time.time()
    # Logging
    logging.basicConfig(
        filename=logPath,
        level=logging.DEBUG
    )

    # Connect to DB
    try:
        conn = r.connect()
        logging.info('Connected to DB')
        print('Connected to DB')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create DB and connect to it
    try:
        r.db_create('pdb_compounds').run()
        conn.use('pdb_compounds')
        logging.info('Created DB and connected to it')
        print('Created DB and connected to it')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create table
    try:
        r.db('pdb_compounds') \
            .table_create('compounds', primary_key='_chem_comp.id') \
            .run()
        logging.info('Created Table: compounds')
        print('Created Table: compounds')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Iterate through the mmCIF files and write to DB
    for cifFile in glob.iglob(os.path.join(mmCIFPath, '*.cif')):
        try:
            data = MMCIF2Dict(cifFile)
            dataJSON = json.dumps(data)         # Creates JSON string
            dataDecoded = json.loads(dataJSON)  # Needed to create valid JSON
            # Insert the data into the DB
            result = r.table('compounds').insert(dataDecoded).run()
            logging.info(
                'Insertion: ID: {id} | Error: {error} | ' \
                'Inserted: {inserted}'.format(
                    id=data['_chem_comp.id'],
                    error=result['errors'],
                    inserted=result['inserted']
                )
            )
            print('Success: ', cifFile)
        except Exception as e:
            logging.debug(
                'File: {filename} | Error: {error}'.format(
                    filename=cifFile,
                    error=e
                )
            )
            print('Error: ', cifFile)
    # Close DB Connection
    conn.close()
    logging.info('Disconnected to DB')
    print('Disconnected from DB')

    # End time
    end = time.time()
    timeTaken = (end - start) / 60
    logging.info('Time Taken: {time} mins'.format(time=timeTaken))
    print('Time Taken: {time} mins'.format(time=timeTaken))
Пример #29
0
def _SpQueryWorker(query,pipe,*args,**kwargs):
    conn = r.connect(*args,**kwargs)
    for event in query.run(conn):
        pipe.send(event)
Пример #30
0
import discord
import os
from rethinkdb import r
from dotenv import load_dotenv

load_dotenv(verbose=True)

db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
token = os.getenv('DISCORD_TOKEN')

client = discord.Client()
conn = r.connect(db_host, db_port).repl()


def get_temperature():
    cursor = r.db('dashboard') \
              .table('temperatures') \
              .order_by(r.desc('timestamp')) \
              .run(conn)
    return cursor[0]['value'], cursor[0]['timestamp']


def get_humidity():
    cursor = r.db('dashboard') \
              .table('humidity') \
              .order_by(r.desc('timestamp')) \
              .run(conn)
    return cursor[0]['value'], cursor[0]['timestamp']

Пример #31
0
 def connect(self):
     self._conn = r.connect(host=self.host,
                            port=self.port,
                            auth_key=self.auth_key,
                            db=self.db)
Пример #32
0
def before_request():
    g.rdb_conn = r.connect(host=RDB_HOST, port=RDB_PORT, db_name=DB_NAME)
Пример #33
0
from fastapi import FastAPI, WebSocket
from rethinkdb import r
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


conn = r.connect('192.168.1.185', 28015).repl()


@app.on_event("startup")
async def startup():
    """Perform startup activities."""
    # If you have any startup activities that need to be defined,
    # define them here.
    # conn = r.connect('192.168.1.185', 28015).repl()


@app.on_event("shutdown")
async def shutdown():
    """Perform shutdown activities."""
    # If you have any shutdown activities that need to be defined,
    # define them here.
    pass
Пример #34
0
 def __init__(self, *args, **kwargs):
     self.c = r.connect(*args, **kwargs)
Пример #35
0
from rethinkdb import r

r.connect('localhost', 28015).repl()
r.db_create('cosmic-retro').run()

# Table creation
r.db('cosmic-retro').table_create('feedbacks').run()
Пример #36
0
#dependecies
from rethinkdb import r

#connecting to localhost
r.connect('localhost', 28015).repl()

#creating connection to the database 
connection = r.connect(db='DB_NAME')

#creating tables
r.table_create("table_name").run(connection)

variable = r.table("table_name")

#inserting into database
variable.insert({
    'id' : ---,
    'name' : ---,
    
}).run(connection)

#to print the database
for _ in variable.run(connection):
    print(_)
Пример #37
0
#!/use/bin/env python
# coding: utf-8
#

import os
import sys
import time

from rethinkdb import r

deadline = time.time() + 10
while time.time() < deadline:
    try:
        r.connect(os.environ.get("RDB_HOST", "localhost"))
        print("RethinkDB is running")
        sys.exit(0)
    except Exception as e:
        time.sleep(.2)
        #print(e, end="")

sys.exit("RethinkDB is not started")
Пример #38
0
 def __init__(self, *args, **kwargs):
     self.c = r.connect(*args, **kwargs)
Пример #39
0
# Copyright 2010-2012 RethinkDB, all rights reserved.

from rethinkdb import r

# Connections
conn = r.connect('newton', 5151)
conn = r.connect('newton')              # Default port
conn = r.connect()                      # Localhost, default port
conn = r.connect([('newton', 5151),
                  ('dr-doom', 5252)])   # A list in case of failure
  # Note: reconnect automatically or force them to call connect again?

# Get the entire table (the following queries are equivalent)
q = r.db('foo').table('bar')
q = r.table('foo.bar')

# Default database
conn = r.connect('newton', db='foo')
q = r.table('bar').run(conn)

conn.run(q)       # Either throws an error, or returns a Python iterator (or an element, see below)
# => iterator or scalar
conn.run([q1, q2, q3]) # Packing queries, asynchronisity of queries?
# => [a1, a2, a3]

# We can also start a query off a connection
r.db('foo').table('bar').run(conn)
q.run(conn)

# Default connection mode
r.set_default_conn(conn)