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
def test_reduce_setter(self): """ Test that the reduce setter works """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertIsNone(view.get('reduce')) view.reduce = '_count' self.assertEqual(view.get('reduce'), '_count')
def test_map_getter(self): """ Test that the map getter works """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertIsNone(view.map) view.map = 'function (doc) {\n emit(doc._id, 1);\n}' self.assertIsInstance(view.map, _Code) self.assertEqual(view.map, 'function (doc) {\n emit(doc._id, 1);\n}')
def test_reduce_getter(self): """ Test that the reduce getter works """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertIsNone(view.reduce) view.reduce = '_count' self.assertIsInstance(view.reduce, _Code) self.assertEqual(view.reduce, '_count')
def updateViews(symptoms_db, covidsafe_db, covid_db): # Update Views ddoc_symptoms = DesignDocument(symptoms_db, 'symptoms') view = View(ddoc_symptoms, 'symptoms_location') covidsafe_ddoc = DesignDocument(covidsafe_db, 'covidsafe') view = View(covidsafe_ddoc, 'symptoms_location') covid_ddoc = DesignDocument(covid_db, 'covid') view = View(covid_ddoc, 'symptoms_location')
def test_map_setter(self): """ Test that the map setter works """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertIsNone(view.get('map')) view.map = 'function (doc) {\n emit(doc._id, 1);\n}' self.assertEqual(view.get('map'), 'function (doc) {\n emit(doc._id, 1);\n}')
def test_map_setter(self): """ Test that the map setter works """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertIsNone(view.get('map')) view.map = 'function (doc) {\n emit(doc._id, 1);\n}' self.assertEqual( view.get('map'), 'function (doc) {\n emit(doc._id, 1);\n}' )
def getDocumentRangeKey(self, design, view, is_desc=True, start_key="", end_key="", limit=False ): global mydb view_connect = View(DesignDocument(mydb,document_id=design),view) if limit: return view_connect(descending=is_desc, startkey=start_key, endkey=end_key, limit=limit)['rows'] else: return view_connect(descending=is_desc, startkey=start_key, endkey=end_key)['rows']
def test_constructor(self): """ Test instantiating a View """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001', 'function (doc) {\n emit(doc._id, 1);\n}', '_count', dbcopy='{0}-copy'.format(self.db.database_name)) self.assertEqual(view.design_doc, ddoc) self.assertEqual(view.view_name, 'view001') self.assertIsInstance(view['map'], _Code) self.assertEqual(view['map'], 'function (doc) {\n emit(doc._id, 1);\n}') self.assertIsInstance(view['reduce'], _Code) self.assertEqual(view['reduce'], '_count') self.assertEqual(view['dbcopy'], '{0}-copy'.format(self.db.database_name)) self.assertEqual( view, { 'map': 'function (doc) {\n emit(doc._id, 1);\n}', 'reduce': '_count', 'dbcopy': '{0}-copy'.format(self.db.database_name) })
def listaDeFansubs(request): #Conexão Com o Banco de Dados db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs_fansubs'] #Lista o Fansubs Cadastrados busca_fansubs = View(db['_design/ahs_fansubs'], 'mostra-todos') lista_de_fansubs = lista_resultados(busca_fansubs()['rows']) #Ordena pelo título lista_de_fansubs.sort(key=itemgetter(1)) #Cria Contexto de resposta contexto = {} contexto['alfabeto'] = alfabeto contexto['listaDeFansubs'] = lista_de_fansubs return render(request, 'listaDeFansubs.html', contexto) # client = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True) # db = client['ahs'] # lista_resultados_da_busca = lambda y: [list(x.values()) for x in y] # http://192.168.2.211:5984/animes/_design/ulitmos/_view/ultimos_animes?descending=true&limit=2&include_docs=true # http://192.168.2.211:5984/ahs/_design/ahs/_view/busca-alfabetica?key="A"
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
def test_retrieve_view_url(self): """ Test the retrieval of the View url """ ddoc = DesignDocument(self.db, 'ddoc001') view = View(ddoc, 'view001') self.assertEqual(view.url, '/'.join( (ddoc.document_url, '_view/view001')))
def get_transactions(self, page, pagesize): print("Portfolio.get_transactions()") skip = page * pagesize ddoc = DesignDocument(self.db, 'transactions') ddoc.fetch() view = View(ddoc, 'history') return view(include_docs=False, limit=pagesize, skip=skip, reduce=False)['rows']
def getDocumentByKey(self, design, view, key=False, is_desc=True, limit=False): global mydb view_connect = View(DesignDocument(mydb,document_id=design),view) if key: if limit: return view_connect(descending=is_desc, key=key, limit=limit)['rows'] else: return view_connect(descending=is_desc, key=key)['rows'] else: return view_connect(descending=is_desc)['rows']
def buscaAlfabetica(request, letra='A'): db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs'] busca_animes = View(db['_design/ahs'], 'busca-alfabetica') contexto = {} contexto['resultado'] = busca_animes(key=str(letra))['rows'] contexto['alfabeto'] = alfabeto return render(request, 'buscaAlfabetica.html', contexto)
def detalhesAnimes(request, id=None): db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs'] busca_animes = View(db['_design/ahs'], 'anime-detalhes') contexto = {} contexto['anime'] = busca_animes(key=str(id))['rows'][0] contexto['alfabeto'] = alfabeto return render(request, 'detalhesAnimes.html', contexto)
def raw_view(self, view_path, params): params = deepcopy(params) params.pop('dynamic_properties', None) if view_path == '_all_docs': return self.cloudant_database.all_docs(**params) else: view_path = view_path.split('/') assert len(view_path) == 4 view = View(DesignDocument(self.cloudant_database, view_path[1]), view_path[3]) return view(**params)
def index(request): db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs'] ultimos_animes_adicionados = View(db['_design/ahs'], 'ultimos_adicionados') #Cria Contexto de retorno contexto = {} contexto['animes_recomendados'] = db.get_view_result( '_design/ahs', 'ani-recomendados') contexto['ultimos_lancamentos'] = ultimos_animes_adicionados( include_docs=True, descending=True, limit=10)['rows'] contexto['alfabeto'] = alfabeto return render(request, 'index.html', contexto)
def test_view_callable_with_non_existing_view(self): """ Test error condition when view used does not exist remotely. """ self.populate_db_with_documents() # The view "missing-view" does not exist in the remote database view = View(DesignDocument(self.db, 'ddoc001'), 'missing-view', 'function (doc) {\n emit(doc._id, 1);\n}') self.assertIsInstance(view, View) try: for row in view.result: self.fail('Above statement should raise an Exception') except requests.HTTPError as err: self.assertEqual(err.response.status_code, 404)
def get_timestamp(symptoms_db): """ Update view and get last timestamp """ try: ddoc = DesignDocument(symptoms_db, 'timestamp') view = View(ddoc, 'timestamp') for row in view(descending=True, limit=1)['rows']: timestamp = (row['key']) except: ddoc = DesignDocument(symptoms_db, 'timestamp') ddoc.add_view('timestamp', 'function(doc){\n emit(doc.timestamp,1)}') ddoc.save() timestamp = 1 return timestamp
def adminAnimes(request, letra='A'): db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs'] busca_animes = View(db['_design/ahs'], 'busca-alfabetica') lista_de_animes = lista_resultados(busca_animes(key=str(letra))['rows']) #Verifica se existe legenda para o anime for i, anime in enumerate(lista_de_animes): lista_de_animes[i].append( os.path.exists('anihubsub/siteweb/static/legendas/' + anime[3])) #Cria Contexto de retorno contexto = {} contexto['resultado'] = lista_de_animes contexto['alfabeto'] = alfabeto return render(request, 'adminAnimes.html', contexto)
def run(self, logger=DEFAULT_LOGGER): """ TODO """ logger.info("Scanning database for conflicted documents...") # Start timer start_time = datetime.datetime.now() # Open CSV file self._init_csv_file() # Iterate over conflicted documents in view result set # TODO: QUESTION: Will this work for rate-limited Cloudant accounts (e.g. HTTP 429)? view = View(ddoc=self._ddoc, view_name=constants.VIEW_NAME) index = 0 for row in view.result: self._process_row(index, row) index += 1 if index == 0: logger.info("No conflicted documents found in database.") # Close CSV file self._shutdown_csv_file() # Stop timer end_time = datetime.datetime.now() elapsed_time = (end_time - start_time).total_seconds() * 1000 # ms # Print status message logger.info( "Successfully scanned database for conflicted documents (%d ms).", elapsed_time) return True
def buffer_tweets_object(self): try: ddoc = DesignDocument( self.couch_client[self.connector_config['database']], '_design/dataview') # Construct a View view = View(ddoc, 'get_data') #print("limit:"+self.connector_config['buffer']+","+"buffer:"+str(self.buffer_count)) rows = view(include_docs=True, limit=int(self.connector_config['buffer']), skip=self.buffer_count)['rows'] self.buffer_count = self.buffer_count + int( self.connector_config['buffer']) return rows except Exception as e: print('get_tweets') print(str(e))
def create_views(client): """ Create views if doesn't exist, and connect to databases """ try: symptoms_db = client['symptoms'] except: symptoms_db = client.create_database('symptoms') ddoc_symptoms = DesignDocument(symptoms_db, 'symptoms') ddoc_symptoms.add_view( 'symptoms_location', 'function (doc) {\n for(var i in doc.symptoms){\n if(doc.symptoms[i]=="cough"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fever"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="throat"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fatigue"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="breathing"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="headache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="bodyache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="stloss"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="vomit"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="diarrhoea"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}}}', '_count') view = View(ddoc_symptoms, 'symptoms_location') #Create a mapreduce function to group the documents by symptoms and location ddoc_symptoms.save() covidsymptom_db = client['tweet-covid-symptom'] #CovidSafe Database and create views #Create a covidsafe designdocument for views #Create a mapreduce function to group the documents by sentiment and location covidsafe_db = client['tweet-covid-covidsafe'] covidsafe_ddoc = DesignDocument(covidsafe_db, 'covidsafe') covidsafe_ddoc.add_view( 'sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count') try: covidsafe_ddoc.save() except: pass #Covid Database, create views, and mapreduce functions covid_db = client['tweet-covid'] covid_ddoc = DesignDocument(covid_db, 'covid') covid_ddoc.add_view('sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count') try: covid_ddoc.save() except: pass return symptoms_db, covidsymptom_db, covidsafe_db, covid_db
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 _access_symptoms_nums(): client = CouchDB("admin", "password", url='http://172.26.131.173:5984', connect=True) session = client.session() #print('Username: {0}'.format(session['userCtx']['name'])) #print('Databases: {0}'.format(client.all_dbs())) #connect database in the couchDB db = client['symptoms'] #Create a designdocument for views ddoc = DesignDocument(db, 'symptoms') view = View(ddoc, 'symptoms_location') total = 0 for i in view(reduce=True, group=True, group_level=1)['rows']: #print(i['value']) total = total + i['value'] return total
from cloudant.client import CouchDB from cloudant.view import View from cloudant.error import CloudantClientException vote_map = """\ function(doc) { if(doc.pollID && doc.votes) { emit(doc.pollID, { pollID: doc.pollID, votes: doc.votes }); } } """ client = CouchDB(None, None, url='http://' + os.environ['DB_NAME'] + ':5984', admin_party=True, connect=True) try: couch = client.create_database(dbname=os.environ['DB_NAME'], throw_on_exists=True) except CloudantClientException: couch = client[os.environ['DB_NAME']] ddoc = DesignDocument(couch, '_design/' + os.environ['DB_NAME']) ddoc.add_view(os.environ['DB_NAME'], vote_map) if ddoc.exists() is not True: ddoc.save() vote_view = View(ddoc, os.environ['DB_NAME'])
url='http://172.26.131.173:5984', connect=True) session = client.session() #print('Username: {0}'.format(session['userCtx']['name'])) #print('Databases: {0}'.format(client.all_dbs())) #connect database in the couchDB db = client['tweet-covid'] #Connect a designdocument name covid ddoc = DesignDocument(db, 'covid') #tweet-covid seperated by SA4_source #view for all tweets include geo location view = View(ddoc, 'sentiment_location', partition_key='geo') #the number of negative/positive/neutral tweets related to covid with open('covid_geo.json', 'w') as f: for row in view(reduce=True, group=True, group_level=1)['rows']: json.dump(row, f) #the number of negative/positive/neutral tweets related to covid including SA4 with open('covid_geo_location.json', 'w') as f: for row in view(reduce=True, group=True)['rows']: json.dump(row, f) #tweet-covid seperated by SA4_source #view for all tweets include city name view = View(ddoc, 'sentiment_location', partition_key='name')
connect=True) session = client.session() except Exception as e: print('Connection unsuccessful') sys.exit() try: symptoms_db = client['symptoms'] except: symptoms_db = client.create_database('symptoms') ddoc_symptoms = DesignDocument(symptoms_db, 'symptoms') ddoc_symptoms.add_view( 'symptoms_location', 'function (doc) {\n for(var i in doc.symptoms){\n if(doc.symptoms[i]=="cough"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fever"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="throat"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="fatigue"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="breathing"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="headache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="bodyache"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="stloss"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="vomit"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}\n if(doc.symptoms[i]=="diarrhoea"){\n emit([doc.symptoms[i],doc.SA4,doc.SA4_source],1);}}}', '_count') view = View(ddoc_symptoms, 'symptoms_location') #Create a mapreduce function to group the documents by symptoms and location ddoc_symptoms.save() #Connect to covidsymptom database in the couchDB covidsymptom_db = client['tweet-covid-symptom'] #CovidSafe Database and create views #Create a covidsafe designdocument for views #Create a mapreduce function to group the documents by sentiment and location covidsafe_db = client['tweet-covid-covidsafe'] covidsafe_ddoc = DesignDocument(covidsafe_db, 'covidsafe') covidsafe_ddoc.add_view('sentiment_location', 'function(doc){\n emit([doc.sentiment,doc.SA4],1)}', '_count')
from cloudant.client import CouchDB from cloudant.design_document import DesignDocument from cloudant.view import View import json #connect couchDB client = CouchDB("admin", "password", url='http://172.26.131.173:5984', connect=True) session = client.session() #print('Username: {0}'.format(session['userCtx']['name'])) #print('Databases: {0}'.format(client.all_dbs())) #connect database in the couchDB db = client['symptoms'] #Create a designdocument for views ddoc = DesignDocument(db, 'symptoms') view = View(ddoc, 'symptoms_location') #the number of tweets in each symptom with open('symptoms.json', 'w') as f: for row in view(reduce=True, group=True, group_level=1)['rows']: json.dump(row, f) #the number of tweets in each symptom including their SA4 and SA4_source with open('symptoms_location.json', 'w') as f: for row in view(reduce=True, group=True)['rows']: json.dump(row, f)
def init(): views = DesignDocument(bdd, 'listas') nombres = View(views, 'nombre') lists = nombres()['rows'] top = Frame(tk) bot = Frame(tk) #Crea un boton por cada lista for i in lists: #Cuando se clicka una de las lista elimina lo que esta en pantalla y muestra las tareas def muestraLista(id): #Accedo al documento que se ha clickado doc = bdd[id] #Quito los frames para poder listar las tareas de la lista en la que estoy top.destroy() bot.destroy() new_top = Frame(tk) new_bot = Frame(tk) #Elimina lo que esta en pantalla y ejecuta el método para recargar la vista principal def atras(): new_top.destroy() new_bot.destroy() init() #Para cada tarea imprimirla con los botones e implementacion de los botones for counter, t in enumerate(doc["tasks"]): top3 = Frame(new_top) var2 = StringVar() label2 = Label(top3, textvariable=var2) if (t["status"] == "1"): var2.set("✅ " + t["name"]) b3 = Button(top3, text="Deshacer", command=lambda c=doc, d=counter: deshacer_tarea(c, d)) else: var2.set("❌ " + t["name"]) b3 = Button( top3, text="Hacer", command=lambda c=doc, d=counter: hacer_tarea(c, d)) label2.pack(side=LEFT) def eliminar_tarea(doc1, t1): doc1["tasks"].remove(t1) doc1.save() new_top.destroy() new_bot.destroy() muestraLista(doc1["_id"]) def hacer_tarea(doc1, t1): doc1["tasks"][t1]["status"] = "1" doc1.save() new_top.destroy() new_bot.destroy() muestraLista(doc1["_id"]) def deshacer_tarea(doc1, t1): doc1["tasks"][t1]["status"] = "0" doc1.save() new_top.destroy() new_bot.destroy() muestraLista(doc1["_id"]) b4 = Button( top3, text="Eliminar", command=lambda c=doc, d=t: eliminar_tarea(c, d)) b3.pack(side=LEFT) b4.pack(side=RIGHT) top3.pack() atras = Button(new_top, text="Atrás", command=atras) atras.pack() new_top.pack() #Parte de abajo para añadir tasks #Input para crear una nueva task nueva_task = Entry(new_bot) def añadir_task(): if (nueva_task.get() != ""): diccionario_task = { "name": nueva_task.get(), "status": "0" } doc["tasks"].append(diccionario_task) doc.save() new_top.destroy() new_bot.destroy() muestraLista(doc["_id"]) crear_task = Button(new_bot, text="Crear", command=añadir_task) var_task = StringVar() label_task = Label(new_bot, textvariable=var_task) var_task.set("Nueva Tarea") label_task.pack() nueva_task.pack(side=LEFT) crear_task.pack(side=RIGHT) new_bot.pack(side=BOTTOM) #Para cada lista crea etiquetas y botones top2 = Frame(top) top2.pack() var1 = StringVar() label1 = Label(top2, textvariable=var1) var1.set(i["value"]) label1.pack(side=LEFT) b = Button(top2, text="Ver", command=lambda c=i["key"]: muestraLista(c)) def eliminaLista(id): doc = bdd[id] doc.delete() top.destroy() bot.destroy() init() b2 = Button(top2, text="Eliminar", command=lambda c=i["key"]: eliminaLista(c)) b2.pack(side=RIGHT) b.pack() top.pack() bot.pack(side=BOTTOM) #Input para crear una nueva lista nueva_lista = Entry(bot) def crearLista(): if (nueva_lista.get() != ""): data = {"name": nueva_lista.get(), "tasks": []} bdd.create_document(data) top.destroy() bot.destroy() init() #Parte de abajo para crear nuevas listas crear = Button(bot, text="Crear", command=crearLista) var = StringVar() label = Label(bot, textvariable=var) var.set("Nueva Lista") label.pack() nueva_lista.pack(side=LEFT) crear.pack(side=RIGHT)