Beispiel #1
0
def _access_covidsafe_nums():
    client = CouchDB("admin",
                     "password",
                     url='http://172.26.131.173:5984',
                     connect=True)
    session = client.session()
    db = client['tweet-covid-covidsafe']

    #Connect a designdocument name covid
    ddoc = DesignDocument(db, 'covidsafe')
    total = 0
    view = View(ddoc, 'sentiment_location', partition_key='name')

    for i in view(reduce=True, group=True, group_level=1)['rows']:
        #print(i['value'])
        total = total + i['value']

    view = View(ddoc, 'sentiment_location', partition_key='geo')

    for i in view(reduce=True, group=True, group_level=1)['rows']:
        #print(i['value'])
        total = total + i['value']

    view = View(ddoc, 'sentiment_location', partition_key='none')

    for i in view(reduce=True, group=True, group_level=1)['rows']:
        #print(i['value'])
        total = total + i['value']

    return total
Beispiel #2
0
    def __init__(self,
                 uri='http://127.0.0.1:5984',
                 uuid_batch_count=DEFAULT_UUID_BATCH_COUNT):
        """ constructor for Server object

        @param uri: uri of CouchDb host
        @param uuid_batch_count: max of uuids to get in one time
        """

        if not uri or uri is None:
            raise ValueError("Server uri is missing")

        if uri.endswith("/"):
            uri = uri[:-1]

        url = urlparse(uri)
        if url.password is not None:
            # remove username and password from uri to reduce chances of
            # them showing up in logs
            uri = url._replace(netloc=url.netloc.rsplit('@', 1)[1]).geturl()
            params = {
                "user": url.username,
                "auth_token": url.password,
                "use_basic_auth": True,
            }
        else:
            params = {"user": "", "auth_token": "", "admin_party": True}

        self.uri = uri
        self.uuid_batch_count = uuid_batch_count
        self._uuid_batch_count = uuid_batch_count
        self._uuids = deque()
        self.cloudant_client = CouchDB(url=uri, connect=True, **params)
Beispiel #3
0
 def connect(self):
     if self.client is None:
         self.client = CouchDB(self.username,
                               self.password,
                               url=self.url,
                               connect=True)
     self.db = self.client[self.database]
Beispiel #4
0
    def airports_inside(self, bounding_boxes):
        """
        Retrieves within the airports the given bounding boxes

        :param list bounding_boxes: boxes where airports are searched
        :returns: List of airports
        """
        airports = []
        try:
            client = CouchDB(None,
                             None,
                             url=self.server_url,
                             admin_party=True,
                             connect=True)
            db = cloudant.database.CloudantDatabase(client, self.db_name,
                                                    ROW_LIMIT)
            for bounding_box in bounding_boxes:
                query = self.create_query_string(bounding_box)
                optional_args = dict(query=query,
                                     include_docs=True,
                                     limit=ROW_LIMIT)
                is_query_finished = False
                while not is_query_finished:
                    resp = db.get_search_result(self.ddoc_id, self.index_name,
                                                **optional_args)
                    airports += resp["rows"]
                    optional_args.update({'bookmark': resp['bookmark']})
                    if 0 == len(resp['rows']):
                        is_query_finished = True
        finally:
            client.disconnect()
        return airports
Beispiel #5
0
 def __init__(self, db_name):
     self.client = CouchDB(USERNAME, PASSWORD, url=URL, connect=True)
     self.db_name = db_name
     self.db = None
     try:
         self.db = self.client.create_database(self.db_name)
     except:
         self.db = self.client[self.db_name]
Beispiel #6
0
 def __init__(self, db_name):
     server_config = Config(CONFIG_PATH).get('couchdb')
     self.client = CouchDB(server_config['username'],
                           server_config['password'],
                           url=server_config['server_addr'],
                           connect=True,
                           auto_renew=True)
     self.select_db(db_name)
Beispiel #7
0
def on_message(client, userdata, msg):
    """Call when a PUBLISH message is received from the server.

    Args:
        client ([type]): [description]
        userdata ([type]): [description]
        msg ([type]): [description]
    """
    try:
        reading = {}
        values = {}

        payload = str(msg.payload.decode("utf-8"))
        splittopic = msg.topic.split("/")
        deviceID = splittopic[1]
        topic = splittopic[2]

        # Set values. Return if it isn't the right topic to put in DB
        if topic == "moisture":
            values["moisture_level"] = payload
        elif topic == "availability":
            values["device_availability"] = payload
        elif topic == "reservoir":
            # If high moisture there is water. If low moisture there is not.
            if float(payload) >= 50.0:
                values["reservoir_empty"] = 0
            else:
                # empty
                values["reservoir_empty"] = 1
        elif topic == "pumpstatus":
            values["pump_status"] = payload
        else:
            return

        reading["type"] = topic
        reading["device_id"] = str(deviceID)
        reading["time_reading"] = int(time())
        reading["values"] = values

        print(reading)

    except Exception as e:
        print("Error processing message: " + str(e))

    # Connect to DB
    try:
        dbclient = CouchDB(environ.get("COUCHDB_USER"),
                           environ.get("COUCHDB_PASSWORD"),
                           url='http://db:5984',
                           connect=True)
        db = dbclient['plant_device_reading']
        document = db.create_document(reading)
        if document.exists():
            print("Added to DB.")
        dbclient.disconnect()

    except Exception as e:
        print("Error Writing to DB" + str(e))
Beispiel #8
0
class CouchDBHelper:
    def __init__(self,
                 database,
                 username,
                 password,
                 url='http://127.0.0.1:5984'):
        self.database = database
        self.username = username
        self.password = password
        self.url = url
        self.client = None
        self.db = None

    def create_database(self):
        self.client = CouchDB(self.username,
                              self.password,
                              url=self.url,
                              connect=True)
        self.client.create_database(self.database)

    def connect(self):
        if self.client is None:
            self.client = CouchDB(self.username,
                                  self.password,
                                  url=self.url,
                                  connect=True)
        self.db = self.client[self.database]

    def disconnect(self):
        self.client.disconnect()

    def load(self, doc_name):
        try:
            doc_json = self.db[doc_name]
        except KeyError:
            print("Unexpected error:", sys.exc_info()[0])
            raise FileNotFoundError

        return doc_json['payload']

    def save(self, doc_name, payload, overwrite=False):

        try:
            doc = self.db[doc_name]

            if not overwrite and doc.exists():
                raise FileExistsError(doc_name)

            doc['payload'] = payload
            doc.save()
            return

        except KeyError:
            pass

        # doc does not exist create
        doc = {'_id': doc_name, 'payload': payload}
        return self.db.create_document(doc)
Beispiel #9
0
def db_connect(db_name='testing3'):
    client = CouchDB("admin",
                     "admin",
                     url='http://127.0.0.1:5984',
                     connect=True)
    try:
        return client[db_name]
    except:
        return client.create_database(db_name)
Beispiel #10
0
def call_for_feature(feature,
                     model,
                     tokenizer,
                     couchdb: CouchDB,
                     redis,
                     backfill=False):
    response = {"meta": {"next_token": None}}
    start_time, end_time = get_time_interval(feature, backfill)

    page = 0
    while "next_token" in response["meta"] and page < 10:
        next_token = response["meta"]["next_token"]
        print("Getting tweets for %s between %s and %s%s" %
              (feature["name"], start_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
               end_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
               ", next_token: %s..." % next_token if next_token else ""))
        page += 1

        # Get tweets from Twitter
        gt1 = time()
        response = api.get(endpoint="2/tweets/search/all",
                           params=create_params(feature, next_token,
                                                start_time, end_time),
                           couchdb=couchdb,
                           redis=redis)
        gt2 = time()
        print("%.2fs to get %s tweets from %s" %
              (gt2 - gt1, len(response["data"]) if "data" in response else 0,
               feature["name"]))

        update_location_info(feature, start_time, end_time, backfill, couchdb)

        if "data" not in response or len(response["data"]) == 0:
            break

        tweets = format_response(response, feature)

        # Add category prediction
        pt1 = time()
        for tweet in tweets:
            predictor = Predictor(tweet["text"],
                                  model,
                                  tokenizer,
                                  threshold=0.55)
            prediction = predictor.get_tweet_prediction()
            tweet["category"] = prediction
        pt2 = time()
        print("%.2fs to predict" % (pt2 - pt1))

        # Save to CouchDB
        t1 = time()
        couchdb.connect()
        couchdb["twitter"].bulk_docs(tweets)
        couchdb.disconnect()
        t2 = time()
        print("%.2fs to save" % (t2 - t1))
Beispiel #11
0
class DatabaseConn(AbstractContextManager):
    def __init__(self, config):
        self._config = config

        self._db = CouchDB(config['username'],
                           config['password'],
                           url=config['url'],
                           connect=True)
        self._bulk_db = None
        self._bulk_db_lock = RLock()
        self._user_db = None
        self._user_db_lock = RLock()
        self._relevant_db = None
        self._relevant_db_lock = RLock()

    @property
    @contextmanager
    def bulk_db(self):
        if self._bulk_db is None:
            self._bulk_db = self._get_or_create_db(self._config['database'])
        with self._bulk_db_lock:
            yield self._bulk_db

    @property
    @contextmanager
    def user_db(self):
        if self._user_db is None:
            self._user_db = self._get_or_create_db(self._config['user_db'])
        with self._user_db_lock:
            yield self._user_db

    @property
    @contextmanager
    def relevant_db(self):
        if self._relevant_db is None:
            self._relevant_db = self._get_or_create_db(
                self._config['relevant_db'])
        with self._relevant_db_lock:
            yield self._relevant_db

    def _get_or_create_db(self, db_name):
        if db_name in self._db:
            return self._db[db_name]

        db = self._db.create_database(db_name)
        if db.exists():
            return db
        raise DatabaseError('failed to create database {}'.format(db_name))

    def __enter__(self):
        self._db.connect()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self._db.disconnect()
class CouchConnector:
    couch_client = None
    connector_config = None
    sins_config_map = {}
    buffer_count = 0

    def __init__(self, config_path):
        try:
            config = configparser.ConfigParser()
            config.read(config_path)
            self.connector_config = config['couchdb']

            self.sins_config = config['sins']
            for data_key in self.sins_config.keys():
                data_val = self.sins_config[data_key]
                arr = data_val.split(":")
                self.sins_config_map[arr[0]] = (arr[1], arr[2], arr[3], arr[4])

            print(self.sins_config_map)
            self.couch_client = CouchDB(self.connector_config['username'],
                                        self.connector_config['password'],
                                        url=self.connector_config['url'],
                                        connect=True)
        except Exception as e:
            print('init')
            traceback.print_exc()
            print(str(e))

    def buffer_tweets_object(self, sin_key):
        try:
            #print('buffer_tweets_object')
            sin_config_tup = self.sins_config_map[sin_key]
            #print(sin_config_tup)
            ddoc = DesignDocument(self.couch_client[sin_config_tup[0]],
                                  sin_config_tup[1])
            # Construct a View
            view = View(ddoc, sin_config_tup[2])
            # print("limit:"+self.connector_config['buffer']+","+"buffer:"+str(self.buffer_count))
            rows = view(include_docs=True,
                        limit=int(sin_config_tup[3]),
                        skip=self.buffer_count)['rows']
            self.buffer_count = self.buffer_count + int(sin_config_tup[3])
            #print(rows)
            return rows
        except Exception as e:
            print('get_tweets')
            print(str(e))

    def release_connection(self):
        try:
            self.couch_client.disconnect()
        except Exception as e:
            traceback.print_exc()
            print(str(e))
Beispiel #13
0
def CreateDBHandler(req):
	if not req.body:
		glob.LOGGER("[MOD] Invalid Request: Null body")
		print("[MOD] Invalid Request: Null body")
		return HttpResponse(status=400)
	body = JSON.loads(req.body)
	if 'Whos_Asking' not in body or not body['Whos_Asking']:
		glob.LOGGER("[MOD] Invalid Request: Null Whos_Asking")
		print("[MOD] Invalid Request: Null Whos_Asking")
		return HttpResponse(status=400)
	if 'Password' not in body or not body['Password']:
		glob.LOGGER("[MOD] Invalid Request: Null Password")
		print("[MOD] Invalid Request: Null Password")
		return HttpResponse(status=400)
	if 'Database_Name' not in body or not body['Database_Name']:
		glob.LOGGER("[MOD] Invalid Request: Null database name")
		print("[MOD] Invalid Request: Null database name")
		return HttpResponse(status=400)

	glob.LOGGER("[MOD] Create DB request by {}".format(body['Whos_Asking']))
	print("[MOD] Create DB request by {}".format(body['Whos_Asking']))
	populate = body['Populate'] if 'Populate' in body and body['Populate'] is not None else False
	
	try:
		admin_couch_client = CouchDB(
			body['Whos_Asking'],
			body['Password'],
			url = glob.COUCH_HOST+':'+glob.COUCH_PORT,
			connect = True
		)
	except KeyError:
		glob.LOGGER("[MOD] Couch Admin not found")
		print("[MOD] Couch Admin not found")
		return HttpResponse(status=404)
	except Exception as err:
		glob.LOGGER("[MOD] Could not connect to couch as {} \n {}".format(body[ 'Whos_Asking' ], err))
		print("[MOD] Could not connect to couch as {} \n {}".format(body[ 'Whos_Asking' ], err))
		return HttpResponse(status=500)

	try:
		admin_couch_client[ str(body[ 'Database_Name' ]) ].exists()
		glob.LOGGER("[MOD] Database {} already exists".format(body[ 'Database_Name' ]))
		print("[MOD] Database {} already exists".format(body[ 'Database_Name' ]))
		return HttpResponse(status=406)
	except KeyError:
		glob.LOGGER("[MOD] Creating new {} database".format(body[ 'Database_Name' ]))
		print("[MOD] Creating new {} database".format(body[ 'Database_Name' ]))
		admin_couch_client.create_database(body[ 'Database_Name' ])
		if populate:
			if PopulateDatabase(admin_couch_client, body[ 'Database_Name' ], populate):
				return HttpResponse(status=200)
			else:
				return HttpResponse(status=500)
Beispiel #14
0
def get_db_connection():
    client = CouchDB(config['DBUSER'],
                     config['DBPASS'],
                     url=config['COUCHDB'],
                     connect=True,
                     auto_renew=True)

    db = client[config['DBNAME']]
    try:
        yield db
    finally:
        client.disconnect()
 def __init__(self, config_path):
     try:
         config = configparser.ConfigParser()
         config.read(config_path)
         self.connector_config = config['couchdb']
         self.couch_client = CouchDB(self.connector_config['username'],
                                     self.connector_config['password'],
                                     url=self.connector_config['url'],
                                     connect=True)
     except Exception as e:
         print('init')
         print(str(e))
Beispiel #16
0
 def __init__(self, url):
   # logging.debug(url)
   import yurl
   # noinspection PyArgumentList
   parse_result = yurl.URL(url=url)
   import re
   url_without_credentials = re.sub(parse_result.username + ":" + parse_result.authorization, "", url)
   from cloudant.client import CouchDB
   self.client = CouchDB(user=parse_result.username, auth_token=parse_result.authorization,
                         url=url_without_credentials, connect=True, auto_renew=True)
   # logging.debug(self.client)
   assert self.client is not None, logging.error(self.client)
Beispiel #17
0
def update_location_info(feature, start_time: datetime, end_time: datetime,
                         backfill: bool, couchdb: CouchDB):
    try:
        couchdb.connect()
        doc = couchdb["features"][feature["_id"]]
        if not backfill:
            doc["newest"] = int(end_time.timestamp())
        else:
            doc["oldest"] = int(start_time.timestamp())

        doc.save()
    finally:
        couchdb.disconnect()
Beispiel #18
0
    def __init__(self, config):
        self._config = config

        self._db = CouchDB(config['username'],
                           config['password'],
                           url=config['url'],
                           connect=True)
        self._bulk_db = None
        self._bulk_db_lock = RLock()
        self._user_db = None
        self._user_db_lock = RLock()
        self._relevant_db = None
        self._relevant_db_lock = RLock()
Beispiel #19
0
def authenticate_user(username, password, db_users=None, client=None):
    try:
        # username = '******'
        # password = '******'
        user_client = CouchDB(username,
                              password,
                              url=config.COUCHDB_URL,
                              connect=True)
        user_client.disconnect()
        user = get_user(username, db_users=db_users, client=client)
        # user.fetch()
        return user
    except HTTPError:
        return False
Beispiel #20
0
class CloudantApiClient(ClientInterface):
    def __init__(self, url):
        # logging.debug(url)
        import yurl
        # noinspection PyArgumentList
        parse_result = yurl.URL(url=url)
        import re
        url_without_credentials = re.sub(
            parse_result.username + ":" + parse_result.authorization, "", url)
        from cloudant.client import CouchDB
        self.client = CouchDB(user=parse_result.username,
                              auth_token=parse_result.authorization,
                              url=url_without_credentials,
                              connect=True,
                              auto_renew=True)
        # logging.debug(self.client)
        assert self.client is not None, logging.error(self.client)

    def get_database(self, db_name):
        # noinspection PyTypeChecker
        db = self.client.get(db_name, default=None)
        if db is not None:
            return db
        else:
            return self.client.create_database(db_name)

    def get_database_interface(self,
                               db_name_backend,
                               db_name_frontend=None,
                               external_file_store=None,
                               db_type=None):
        db_name_frontend_final = db_name_frontend if db_name_frontend is not None else db_name_backend
        if db_type == "ullekhanam_db":
            return BookPortionsCouchdb(
                some_collection=self.get_database(db_name=db_name_backend),
                db_name_frontend=db_name_frontend_final,
                external_file_store=external_file_store)
        elif db_type == "users_db":
            db_name_frontend_final = db_name_frontend if db_name_frontend is not None else "users"
            return UsersCouchdb(
                some_collection=self.get_database(db_name=db_name_backend),
                db_name_frontend=db_name_frontend_final)
        else:
            return CloudantApiDatabase(
                db=self.get_database(db_name=db_name_backend),
                db_name_frontend=db_name_frontend_final,
                external_file_store=external_file_store)

    def delete_database(self, db_name):
        self.client.delete_database(db_name)
Beispiel #21
0
    def __init__(self, timeout=None):
        self.client = CouchDB(self.username,
                              self.password,
                              url=self.url,
                              timeout=timeout,
                              auto_renew=True)
        self.client.connect()

        if self.dbname in self.client.all_dbs():
            logging.info('Database exists - connecting to it.')
            self.database = self.client[self.dbname]
        else:
            logging.warn('Database does not exist - creating it.')
            self.database = self.client.create_database(self.dbname)
Beispiel #22
0
def check_folder_for_files_from_tokens(task_id, dummy, number, **context):
    xcom_results = context['ti'].xcom_pull(task_ids=task_id)
    tokens = list(xcom_results['token_ids'])
    token_type = xcom_results['token_type']
    pc = picas_cred()
    client = CouchDB(pc.user,
                     pc.password,
                     url='https://picas-lofar.grid.surfsara.nl:6984',
                     connect=True)
    db = client[pc.database]
    tokenlist = TokenList(token_type=token_type, database=db)
    for token_id in tokens:
        tokenlist.append(
            caToken(database=db, token_type=token_type, token_id=token_id))
    tokenlist.fetch()
    for t in tokenlist:
        if t['status'] != 'done':
            raise RuntimeError(
                "Token {} is not in status 'done' but in status {}".format(
                    t['_id'], t['status']))
    print("All jobs in status 'done' ")
    locations = [t['Results_location'] for t in tokenlist]
    if len(locations) < number:
        print("Only {} files found!".format(len(locations)))
    for output_file in locations:
        c = subprocess.Popen(['uberftp', '-ls', output_file],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        out = c.communicate()
        if not out[0].decode().strip().split('\n')[0]:
            print("{} not found".format(output_file))
Beispiel #23
0
 def _connect(self):
     self.log.debug("{!s} CONNECTING".format(self))
     self.client = CouchDB(self._auth[0],
                           self._auth[1],
                           url=self._couch_db_url,
                           connect=True,
                           auto_renew=True)
Beispiel #24
0
def connect():
    """
    Conect to the CouchDB server.
    """

    if __DB_ADDRESS  is None or \
       __DB_PASSWORD is None or \
       __DB_USERNAME is None:

        print("Database not initialized.")

        return None

    try:

        g._client = CouchDB(__DB_USERNAME,
                            __DB_PASSWORD,
                            url=__DB_ADDRESS + ':5984',
                            connect=True)

    except Exception as e:

        print("Error when connecting to database")

        pass
Beispiel #25
0
 def __init__(self):
     client = CouchDB(os.environ['COUCHDB_USER'],
                      os.environ['COUCHDB_PASSWORD'],
                      url=os.environ['COUCHDB_HOST'],
                      connect=True,
                      auto_renew=True)
     self.db = client["leihlokal"]
    def getContextManager(self, isOnBoot=False):
        db_url = os.getenv("DB_URL")
        if db_url and db_url.strip() != "":
            creds = db_url.split("//")[1].split("@")[0].split(":")
            while self.down:
                try:
                    db = CouchDB(
                        creds[0],
                        creds[1],
                        url=db_url,
                        connect=True,
                        auto_renew=True,
                        timeout=20,
                    )
                    print("DATABASE UP: Database connection recovered.")
                    self.down = False
                    return db
                except Exception as e:

                    if self.test:
                        raise e

                    if isOnBoot:
                        print(e)
                        print(
                            "Database is not available on boot. Entering dead spin. No services online."
                        )
                        time.sleep(DEFAULT_CONTEXT_MANAGER_RETRY_WAIT)
                        self.down = True
                    else:
                        time.sleep(DEFAULT_CONTEXT_MANAGER_RETRY_WAIT)
        else:
            raise Exception("DB_URL not provided.")
Beispiel #27
0
 def set_up_client(self, auto_connect=False, encoder=None):
     if os.environ.get('RUN_CLOUDANT_TESTS') is None:
         admin_party = False
         if (os.environ.get('ADMIN_PARTY')
                 and os.environ.get('ADMIN_PARTY') == 'true'):
             admin_party = True
         self.user = os.environ.get('DB_USER', None)
         self.pwd = os.environ.get('DB_PASSWORD', None)
         self.url = os.environ['DB_URL']
         self.client = CouchDB(self.user,
                               self.pwd,
                               admin_party,
                               url=self.url,
                               connect=auto_connect,
                               encoder=encoder)
     else:
         self.account = os.environ.get('CLOUDANT_ACCOUNT')
         self.user = os.environ.get('DB_USER')
         self.pwd = os.environ.get('DB_PASSWORD')
         self.url = os.environ.get(
             'DB_URL', 'https://{0}.cloudant.com'.format(self.account))
         self.client = Cloudant(self.user,
                                self.pwd,
                                url=self.url,
                                x_cloudant_user=self.account,
                                connect=auto_connect,
                                encoder=encoder)
Beispiel #28
0
def init_connect(retries=0):
    '''
	Connect to CouchDB, keep trying if fails to connect after 10s
	'''
    try:
        client = CouchDB("admin",
                         "password",
                         url='http://172.26.132.48:5984',
                         connect=True)
        session = client.session()
    except Exception as e:
        print('Connection unsuccessful' + str(retries))
        time.sleep(10)
        return init_connect(retries + 1)

    return client
Beispiel #29
0
    def __init__(self, uri='http://127.0.0.1:5984', uuid_batch_count=DEFAULT_UUID_BATCH_COUNT):

        """ constructor for Server object

        @param uri: uri of CouchDb host
        @param uuid_batch_count: max of uuids to get in one time
        """

        if not uri or uri is None:
            raise ValueError("Server uri is missing")

        if uri.endswith("/"):
            uri = uri[:-1]

        url = urlparse(uri)
        if url.password is not None:
            # remove username and password from uri to reduce chances of
            # them showing up in logs
            uri = url._replace(netloc=url.netloc.rsplit('@', 1)[1]).geturl()
            params = {
                "user": url.username,
                "auth_token": url.password,
                "use_basic_auth": True,
            }
        else:
            params = {"user": "", "auth_token": "", "admin_party": True}

        self.uri = uri
        self.uuid_batch_count = uuid_batch_count
        self._uuid_batch_count = uuid_batch_count
        self._uuids = deque()
        self.cloudant_client = CouchDB(url=uri, connect=True, **params)
Beispiel #30
0
def view_covid(name, partition_key="", area_col="area_code", var_col="variable", val_col="count", reduce=True,
               group=True, group_level=2,
               docid="", dbname="",
               ip="", username="******", password="******", port=5984, connect=True):
    """
    View database
    """

    url = f"http://{ip}:{port}"
    client = CouchDB(username, password, url=url, connect=connect)
    db = client[dbname]  # database
    ddoc = DesignDocument(db, docid)

    # View
    view = View(ddoc, name, partition_key=partition_key)
    area_codes = []
    variables = []
    counts = []
    for row in view(reduce=reduce, group=group, group_level=group_level)['rows']:
        var, code = row["key"]  # variable, area code
        variables.append(var)
        area_codes.append(code)
        counts.append(row["value"])

    # Data
    data = pd.DataFrame(
        {area_col: map(str, area_codes), var_col: variables, val_col: counts})  # area code in geo-map string stype

    return data
Beispiel #31
0
def db_cleanup(request):

    username = os.environ['USERNAME']
    password = os.environ['PASSWORD']
    url = os.environ['URL']

    client = CouchDB(username, password, url=url, connect=True)

    def remove_doc():
        try:
            db = client[DB_NAME]
            test_conf = db['test_doc']
            test_conf.delete()

            client.delete_database(DB_NAME)
            print("Deleted")
        except KeyError:
            print("Nothing to deleted")

    def db_shutdown():
        client.disconnect()

    remove_doc()

    request.addfinalizer(remove_doc)

    return client
Beispiel #32
0
class Server(object):
    """ Server object that allows you to access and manage a couchdb node.
    A Server object can be used like any `dict` object.
    """

    def __init__(self, uri='http://127.0.0.1:5984', uuid_batch_count=DEFAULT_UUID_BATCH_COUNT):

        """ constructor for Server object

        @param uri: uri of CouchDb host
        @param uuid_batch_count: max of uuids to get in one time
        """

        if not uri or uri is None:
            raise ValueError("Server uri is missing")

        if uri.endswith("/"):
            uri = uri[:-1]

        url = urlparse(uri)
        if url.password is not None:
            # remove username and password from uri to reduce chances of
            # them showing up in logs
            uri = url._replace(netloc=url.netloc.rsplit('@', 1)[1]).geturl()
            params = {
                "user": url.username,
                "auth_token": url.password,
                "use_basic_auth": True,
            }
        else:
            params = {"user": "", "auth_token": "", "admin_party": True}

        self.uri = uri
        self.uuid_batch_count = uuid_batch_count
        self._uuid_batch_count = uuid_batch_count
        self._uuids = deque()
        self.cloudant_client = CouchDB(url=uri, connect=True, **params)

    @property
    def _request_session(self):
        return self.cloudant_client.r_session

    def info(self):
        """ info of server

        @return: dict

        """
        try:
            resp = self._request_session.get(self.uri)
            resp.raise_for_status()
        except Exception:
            return UNKOWN_INFO

        return resp.json()

    def all_dbs(self):
        """ get list of databases in CouchDb host

        """
        return self.cloudant_client.all_dbs()

    def get_db(self, dbname, **params):
        """
        Try to return a Database object for dbname.

        """
        return Database(self._db_uri(dbname), server=self, **params)

    def create_db(self, dbname, **params):
        """ Create a database on CouchDb host

        @param dname: str, name of db
        @param param: custom parameters to pass to create a db. For
        example if you use couchdbkit to access to cloudant or bigcouch:

            Ex: q=12 or n=4

        See https://github.com/cloudant/bigcouch for more info.

        @return: Database instance if it's ok or dict message
        """
        return self.get_db(dbname, create=True, **params)

    get_or_create_db = create_db
    get_or_create_db.__doc__ = """
        Try to return a Database object for dbname. If
        database doest't exist, it will be created.

        """

    def delete_db(self, dbname):
        """
        Delete database
        """
        try:
            del self[dbname]
        except CloudantClientException as e:
            raise ResourceNotFound(six.text_type(e))

    #TODO: maintain list of replications
    def replicate(self, source, target, **params):
        """
        simple handler for replication

        @param source: str, URI or dbname of the source
        @param target: str, URI or dbname of the target
        @param params: replication options

        More info about replication here :
        http://wiki.apache.org/couchdb/Replication

        """
        replicator = cloudant.replicator.Replication(self.cloudant_client)
        source_db = Database(self.cloudant_client, source)
        target_db = Database(self.cloudant_client, target)
        return replicator.create_replication(source_db, target_db, **params)

    def active_tasks(self):
        """ return active tasks """
        resp = self._request_session.get(urljoin(self.uri, '/_active_tasks'))
        resp.raise_for_status()
        return resp.json()

    def uuids(self, count=1):
        resp = self._request_session.get(urljoin(self.uri, '/_uuids'), params={'count': count})
        resp.raise_for_status()
        return resp.json()

    def next_uuid(self, count=None):
        """
        return an available uuid from couchdbkit
        """
        if count is not None:
            self._uuid_batch_count = count
        else:
            self._uuid_batch_count = self.uuid_batch_count

        try:
            return self._uuids.pop()
        except IndexError:
            self._uuids.extend(self.uuids(count=self._uuid_batch_count)["uuids"])
            return self._uuids.pop()

    def __getitem__(self, dbname):
        return Database(self._db_uri(dbname), server=self)

    def __delitem__(self, dbname):
        self.cloudant_client.delete_database(dbname)

    def __contains__(self, dbname):
        try:
            self.cloudant_client[dbname]
        except KeyError:
            return False
        return True

    def __iter__(self):
        for dbname in self.all_dbs():
            yield Database(self._db_uri(dbname), server=self)

    def __len__(self):
        return len(self.all_dbs())

    def __nonzero__(self):
        return (len(self) > 0)

    def _db_uri(self, dbname):
        if dbname.startswith("/"):
            dbname = dbname[1:]

        dbname = url_quote(dbname, safe=":")
        return "/".join([self.uri, dbname])