def save_list_to_db(inlist, db_name, object_example=None): print("save_list_to_db:" + db_name + "list len is: " + str(len(inlist))) documents = [] for obj in inlist: if object_example is not None: document = obj.build_my_document() else: val = jsonpickle.encode(obj) document = { '_id': str(obj.id), 'value': val } documents.append(document) client_save_to_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2) try: client_save_to_db.connect() db = client_save_to_db.get(db_name, remote=True) db.bulk_docs(documents) except CloudantException as exc: print("CloudantException in save_list_to_db") print(exc) time.sleep(2) try: client_save_to_db.connect() db = client_save_to_db.get(db_name, remote=True) db.bulk_docs(documents) except Exception as exc: print("second exception after CloudantException exception in save_list_to_db") print(exc) except Exception as exc: print("non CloudantException exception in save_list_to_db") print(exc) finally: client_save_to_db.disconnect()
def get_user(name): if name not in user_dictionary.keys( ): # we need to handle the creation of a new user user = User(my_name=name) user_dictionary[name] = user user.save_me_to_db() else: myclient = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter) myclient.connect() users_db = myclient.get(user_database_name, remote=True) #if users_db is not None: users_returned = QueryResult( Query(users_db, selector={'_id': { '$eq': name }})) if users_returned is not None: list = users_returned.all() for u in list: t1 = u['_id'] t2 = str(u['value']) # print(t2) dec = jsonpickle.decode(t2) user_dictionary[t1] = dec myclient.disconnect() return user_dictionary[name]
def get_from_db_by_params(user_name, region_name, place_name, db_name, object_example, asdocs=False, dates=None, word_list=None, logic='or_logic', exact=False): print("get_from_db_by_params") print(user_name + ";" + region_name + ";" + place_name + ";" + db_name + ";" + str(type(object_example)) + ";" + str(asdocs)) get_db_client = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=Replay429Adapter(retries=10, initialBackoff=0.01)) try: our_list = [] get_db_client.connect() db = get_db_client.get(db_name, remote=True) print("get_from_db_by_params logic is:" + logic) selector = generate_single_place_selector(user_name, region_name, place_name, dates, word_list, logic, exact) print("the selector is: " + str(selector)) query_result = QueryResult(Query(db, selector=selector)) if asdocs is True: for doc in query_result: our_list.append(doc) else: for doc in query_result: our_list.append(object_example.build_from_document(doc)) except CloudantException as exc: print("CloudantException in get_from_db_by_params") print(exc) return exc except Exception as exc: print("non CloudantException exception in get_from_db_by_params") print(exc) return exc finally: get_db_client.disconnect() print("get_from_db_by_params id: " + str(db_name) + " len: " + str(len(our_list))) return our_list
def get_from_db(id_list, db_name, object_example=None): result_list = [] client_get_from_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2) try: client_get_from_db.connect() db = client_get_from_db.get(db_name, remote=True) if db is None: print("error: no tweet db ready yet") return "" query_result = QueryResult(Query(db, selector={'_id': {'$in': id_list}})) index = 0 for doc in query_result: if object_example is "asdocs": result_list.append(doc) elif object_example is not None: result_list.append(object_example.build_from_document(doc)) index = + 1 else: dec = jsonpickle.decode(str(doc['value'])) result_list.append(dec) index =+ 1 print("query_result len: " + str(index)) except CloudantException as exc: print("CloudantException in get_from_db") print(exc) result_list = [] except Exception as exc: print("non CloudantException exception in get_from_db") print(exc) result_list = [] finally: client_get_from_db.disconnect() print("get_from_db id: " + str(db_name) + " len: " + str(len(result_list))) return result_list
def getLastSavedDbState(): from cloudant.client import Cloudant from cloudant.result import Result from cloudant.adapters import Replay429Adapter from credentials import getUserName, getPassword, getUrl #Establish a connection to the cloudant database by providing the credentials necessary client = Cloudant(getUserName(), getPassword(), url=getUrl(), adapter=Replay429Adapter(retries=10, initialBackoff=0.01)) client.connect() #Connect to the "news_articles" database remotely db = client.get("news_articles", remote=True) #Get all records from the database resultset = Result(db.all_docs, include_docs=True) news_articles_list = list() #Loop through the database records for result in resultset: #Add all article titles to a list. #The news article titles are unique and will be used to check if an article already exists in the database news_articles_list.append(result["doc"]["article_title"]) client.disconnect() return news_articles_list
def getdatabase(db_name, start_time, req_time): user = "******" password = "******" url = "################################" link = (url % (user, password)) ddoc_id = 'powerUsage' view_id = 'byPiTimeAndID' client = Cloudant(user, password, url=url, connect=True) db = client.get(db_name, remote=True) view = db.get_design_document(ddoc_id).get_view(view_id) views = view(startkey=[str(req_time)], endkey=[str(start_time), {}], stale='update_after', stable=True, update='lazy')['rows'] keys = [result.get('key') for result in views] values = [result.get('value') for result in views] if len(keys) == 0: # flash ('No Data in this time', 'success') df = [] pattern = '%d.%m.%Y %H:%M:%S' start = time.strftime(pattern, time.localtime(start_time)) end = time.strftime(pattern, time.localtime(req_time)) print("No data is availble for this time between ", start, "and", end) # raise TypeError ("No data is availble for this time between ", start, "and", end) else: #flash ('Data exists for this time', 'success') dfkeys = pd.DataFrame( keys, columns=['localtimestamp', 'crownid', 'macAddress']) dfvalues = pd.DataFrame(values, columns=['powerUsage', 'LSB Timestamp']) df_all = pd.concat([dfkeys, dfvalues], axis=1) df_all['Datetime'] = pd.to_datetime(df_all['localtimestamp'], unit='s', utc=True) df_all.set_index('Datetime', inplace=True) df_all.index = df_all.index.tz_convert('Europe/Amsterdam') Devicelist = validList() df = df_all.loc[np.isin(df_all['macAddress'].tolist(), Devicelist["device_macaddress"])] return (df)
def dbLoadNewArticle(article): from cloudant.client import Cloudant from cloudant.adapters import Replay429Adapter from credentials import getUserName, getPassword, getUrl client = Cloudant(getUserName(), getPassword(), url=getUrl(), adapter=Replay429Adapter(retries=10, initialBackoff=0.01)) client.connect() db = client.get("news_articles", remote=True) #Create the database record new_doc = db.create_document(article) #Verify that the record was successfully added if new_doc.exists(): print("Article was successfully added") client.disconnect()
def save_me_to_db(self): print("saving: " + self.get_name()) myclient = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter) myclient.connect() users_db = myclient.get(user_database_name, remote=True) if self.get_name() in users_db: print("user " + self.get_name() + "in Db") user_doc = users_db[self.get_name()] val = jsonpickle.encode(self) user_doc['value'] = val print("saving user") user_doc.save() else: print("creating new user") val = jsonpickle.encode(self) document = { '_id': self.get_name(), 'value': val } users_db.create_document(document) myclient.disconnect()
import plotly import plotly.graph_objs as go plots = Blueprint('plots',__name__) user = "******" password = "******" url="##################################" link=(url % (user, password)) db_name = 'default' #'anne' #''default' #'peet' #'test'peet' #'test'default' # ddoc_id = 'powerUsage' view_id = 'byPiTimeAndID' client=Cloudant(user, password, url=url, connect=True) db = client.get(db_name, remote=True) view = db.get_design_document(ddoc_id).get_view(view_id) class datastore(): device_names=[] device_ids=[] db_crownid=[] device_macaddress=[] db_macaddress=[] db_df=[] db_time =[] db_df=[] all_posts=[] time_view=[] globaldata= datastore()
def get_database(dbname, user_name, password, url): client = Cloudant(user_name, password, url=url) client.connect() return client.get(dbname, remote=True)