def get_tweet_ids_by_city(database, city): id_list = [] view_result = database.view('_design/twitter/_view/tweetIdByCity?key="' + city + '"') try: view_result.total_rows except: view = ViewDefinition( 'twitter', 'tweetIdByCity', '''function(doc) { if (doc.type === "tweet"){ emit(doc.city, doc.id); } }''') view.get_doc(database) view.sync(database) view_result = database.view( '_design/twitter/_view/tweetIdByCity?key=\"' + city + '\"') if not view_result.total_rows: return [] for item in view_result: tweet_id = item.value if tweet_id not in id_list: id_list.append(tweet_id) return id_list
def get_twitter_id_from_couch(database): id_list = [] view_result = database.view('_design/twitter/_view/byTwitterID') try: view_result.total_rows except: view = ViewDefinition( 'twitter', 'byTwitterID', '''function(doc) { if (doc.type === "sentiment"){ emit(doc._id, doc.id); } }''') view.get_doc(database) view.sync(database) view_result = database.view('_design/twitter/_view/byTwitterID') for item in view_result: twitter_id = item.value if twitter_id not in id_list: id_list.append(twitter_id) return id_list
def createViewUsers(self): view = ViewDefinition( 'tweets', "users", '''function(doc) { emit(doc.user["id"], doc.done); }''') view.get_doc(self.db) view.sync(self.db) print("user view should be created")
def sync_views(self): for url, view in self.config.viewdata["views"].items(): segments = url.split("/") designdoc = segments[0] name = "/".join(segments[1:]) view = ViewDefinition(designdoc, name, view[0], view[1]) view.get_doc(self.db) view.sync(self.db)
def create_views(self): print("++ Creating views") view = ViewDefinition( 'get_crashes', 'all', '''function(doc) { emit(doc.crashhash,doc.crashcount); }''') view.get_doc(self.db) view.sync(self.db)
def createView(self, viewName): view = ViewDefinition( 'tweets', viewName, '''function(doc) { emit(doc.text, doc.coordinates); }''') view.get_doc(self.db) view.sync(self.db) print("create view user, view should be created")
def test_views_creation(self): book_definition = yaml.load( open(DATADIR%'test_couchish_book.yaml').read() ) dvd_definition = yaml.load( open(DATADIR%'test_couchish_dvd.yaml').read() ) post_definition = yaml.load( open(DATADIR%'test_couchish_post.yaml').read() ) author_definition = yaml.load( open(DATADIR%'test_couchish_author.yaml').read() ) views_definition = yaml.load( open(DATADIR%'test_couchish_views.yaml').read() ) models_definition = {'book': book_definition, 'author': author_definition,'post': post_definition, 'dvd': dvd_definition} viewdata = get_views(models_definition, views_definition) for url, view in viewdata['views'].items(): designdoc = url.split('/')[0] view = ViewDefinition(designdoc, url, view[0]) view.get_doc(self.db) view.sync(self.db)
def p20141208(self, request, *args): from MyRingCouchDB import MyRingCouchDB from MyRingUser import MyRingUser from env_config import COUCHDB_USER, COUCHDB_PASS from couchdb.design import ViewDefinition MCD = MyRingCouchDB() self.couch = MCD.instantiate_couchdb_as_admin() self.couch.resource.credentials = (COUCHDB_USER, COUCHDB_PASS) print('patch_20141208') print(current_user.username) #db = self.couch[self.user_database] #user = MyRingUser.load(db,current_user) user = self.MAM.select_user(current_user.username) if user: for ring in user['rings']: db_ringname = current_user.username + '_' + str( ring['ringname']) print(db_ringname) db2 = self.couch[db_ringname] if not RingsSchema.load(db2, 'schema'): schema = RingsSchema.load(db2, 'blueprint') print('Schema:', schema) schema._id = 'schema' schema.store(db2) view = ViewDefinition( 'ring', 'schema', ''' function(doc) { if(doc._id=='schema'){ emit(doc._id, doc) } } ''') view.get_doc(db2) view.sync(db2) d = { 'rq': current_user, 'template': 'avispa_rest/tools/flashresponsejson.html' } return d
def create_views(self, db): # https://markhaa.se/posts/couchdb-views-in-python/ # create the two views we will need (by week and by suburb) # note that if they already exist in the DB it won't do anything view_by_week = ViewDefinition('sentimentDocs', 'byWeek', MAP_BY_WEEK, reduce_fun=REDUCE_STATS) view_by_week.get_doc(self.DB[db]) view_by_week.sync(self.DB[db]) view_by_suburb = ViewDefinition('sentimentDocs', 'bySuburb', MAP_BY_SUBURB, reduce_fun=REDUCE_STATS) view_by_suburb.get_doc(self.DB[db]) view_by_suburb.sync(self.DB[db]) view_by_week = ViewDefinition('sentimentDocs', 'byWeek2', MAP_BY_WEEK2, reduce_fun=REDUCE_STATS) view_by_week.get_doc(self.DB[db]) view_by_week.sync(self.DB[db]) view_by_suburb = ViewDefinition('sentimentDocs', 'bySuburb2', MAP_BY_SUBURB2, reduce_fun=REDUCE_STATS) view_by_suburb.get_doc(self.DB[db]) view_by_suburb.sync(self.DB[db])
def test_load_couchdb(self): """ Test that resource instances and tiles load into a mobile survey's couchdb """ post_data = { "datadownloadconfig": {"count":1000,"download":True,"resources":['d84a098c-368b-11e8-bafc-c4b301baab9f'],"custom":""}, "startdate":"2018-01-29", "tilecache":"", "enddate":"2018-03-30", "createdby_id":1, "bounds":{"type":"FeatureCollection","features":[]}, "cards":['fe035187-368b-11e8-bf56-c4b301baab9f', '4215f135-369c-11e8-9544-c4b301baab9f'], "lasteditedby_id":1, "groups":[], "active":False, "users":[], "id": str(self.mobile_survey.id), "name":"test survey", "description":"a description" } response_json = self.post_mobile_survey(post_data) couch = couchdb.Server(settings.COUCHDB_URL) resources = 0 tiles = 0 if 'project_' + str(self.mobile_survey.id) in couch: db = couch['project_' + str(self.mobile_survey.id)] view = ViewDefinition('tests', 'all', '''function(doc) {emit(doc._id, null);}''') view.get_doc(db) view.sync(db) for item in db.view('_design/tests/_view/all', include_docs=True): if item.doc['type'] == 'tile': tiles += 1 if item.doc['type'] == 'resource': resources += 1 # tile_count = len(db.find({'selector': {'type': 'tile'}})) # resource_count = len(db.find({'selector': {'type': 'resource'}})) else: print '{0} is not in couch'.format('project_' + str(self.mobile_survey.id)) self.assertEqual(tiles, 2) self.assertEqual(resources,1)
couch_services = filter(lambda s: s['name'] == 'todo-couch-db', value) if len(couch_services) != 0: couch_service = couch_services[0] url = couch_service['credentials']['url'] couch = couchdb.Server(url) try: db = couch['bluemix-todo'] except: db = couch.create('bluemix-todo') view = ViewDefinition( 'todos', 'allTodos', '''function(doc){if(doc.title && doc.completed != null){emit(doc.order,{title: doc.title,completed: doc.completed})}}''', "_count") view_doc = view.get_doc(db) if not view_doc: # If the view does not exist create it view.sync(db) # Schedule something that will remove ToDos if we get too many sched = Scheduler() sched.start() def cleanup(): results = db.view('todos/allTodos') if len(results) > 0: count = results.rows[0]['value'] if count > 20: id = db.view('todos/allTodos', reduce=False).rows[0]['id']
import couchdb from couchdb.design import ViewDefinition admin = 'admin' password = '******' server = couchdb.Server( 'http://*****:*****@115.146.86.96:5984') #connecting to the server #DB server connection db = server['twitter'] #view creation view1 = ViewDefinition( 'Names', 'Cities', 'function(doc) {emit(doc.tweet_data.place.name, doc.sentiment);}') view1.get_doc(db) view1.sync(db) view2 = ViewDefinition( 'Names', 'Language', 'function(doc) {emit(doc.tweet_data.place.name, doc.tweet_data.user.lang);}' ) view2.get_doc(db) view2.sync(db) view3 = ViewDefinition( 'Names', 'Texts', 'function(doc) {emit(doc.tweet_data.place.name, doc.tweet_data.text);}') view3.get_doc(db) view3.sync(db)
import couchdb from couchdb.design import ViewDefinition admin = 'admin' password = '******' server = couchdb.Server( 'http://*****:*****@115.146.86.96:5984') #connecting to the server #DB server connection db = server['income'] #view creation view1 = ViewDefinition('City', 'Adelaide', 'function(doc) {emit(doc.Adelaide, 1);}') view1.get_doc(db) view1.sync(db) view2 = ViewDefinition('City', 'Brisbane', 'function(doc) {emit(doc.Brisbane, 1);}') view2.get_doc(db) view2.sync(db) view3 = ViewDefinition('City', 'Canberra', 'function(doc) {emit(doc.Canberra, 1);}') view3.get_doc(db) view3.sync(db) view4 = ViewDefinition('City', 'Melbourne', 'function(doc) {emit(doc.Melbourne, 1);}') view4.get_doc(db) view4.sync(db)
def userdb_set_db_views(self, user_database=None): if not user_database: user_database = self.user_database self.lggr.info('#couchdb_call:' + user_database + '->userdb_set_db_views()') db = self.couch[user_database] view = ViewDefinition( 'auth', 'userbyemail', ''' function(doc) { if(doc.email) { emit(doc.email,doc); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'auth', 'userbyhandle', ''' function(doc) { if(doc.email) { emit(doc._id,doc); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'auth', 'userbasic', ''' function(doc) { if(doc.email) { var x = {}; x['name'] = doc.name; x['location'] = doc.location; x['is_org'] = doc.is_org; if(doc.profilepic===''){ x['profilepic'] = ''; }else{ parts = doc.profilepic.split(',') if(parts[0]==''){ x['profilepic'] = parts[1]; }else{ x['profilepic'] = parts[0]; } } emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'rings', 'count', ''' function(doc) { if(doc.email) { var x = {}; for (var key in doc.rings){ if(!doc.rings[key]['deleted']){ x[doc.rings[key]['ringname']]=doc.rings[key]['count']; } } emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'rings', 'origin', ''' function(doc) { if(doc.email) { var x = {}; for (var key in doc.rings){ if(!doc.rings[key]['deleted']){ x[doc.rings[key]['origin']] = doc.rings[key]['origin']; } } emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'rings', 'roles', ''' function(doc) { if(doc.email) { var x = {}; for (var key in doc.rings){ if(!doc.rings[key]['deleted']){ x[doc.rings[key]['ringname']] = new Object(); x[doc.rings[key]['ringname']]['owner']=doc.rings[key]['owner']; x[doc.rings[key]['ringname']]['capturist']=doc.rings[key]['capturist']; x[doc.rings[key]['ringname']]['moderator']=doc.rings[key]['moderator']; } } emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'orgs', 'peopleteams', ''' function(doc) { if(doc.is_org) { var x = {}; x['people']=doc.people; x['teams']=doc.teams; emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'orgs', 'user2orgs', ''' function(doc) { if(doc.is_org) { var x = {}; x['handle']=doc._id; x['name']='' if(doc.name){ x['name']=doc.name; } x['location']='' if(doc.location){ x['location']=doc.location; } x['profilepic']=''; if(doc.profilepic){ if(doc.profilepic===''){ x['profilepic'] = ''; }else{ parts = doc.profilepic.split(',') if(parts[0]==''){ x['profilepic'] = parts[1]; }else{ x['profilepic'] = parts[0]; } } } for (var key in doc.people){ emit(doc.people[key]['handle'],x); } } } ''') view.get_doc(db) view.sync(db) view = ViewDefinition( 'orgs', 'invitations', ''' function(doc) { if(doc.is_org) { var x = {}; x['invitations']=doc.invitations; emit(doc._id,x); } } ''') view.get_doc(db) view.sync(db) return True
vcap_json = json.loads(os.environ['VCAP_SERVICES']) for key, value in vcap_json.iteritems(): couch_services = filter(lambda s: s['name'] == 'todo-couch-db', value) if len(couch_services) != 0: couch_service = couch_services[0] username = couch_service['credentials']['username'] password = couch_service['credentials']['password'] url = couch_service['credentials']['url'] couch = couchdb.Server(url) if len(username) != 0 and len(password) != 0: couch.resource.credentials = (username, password) db = couch['bluemix-todo'] view = ViewDefinition('todos', 'allTodos', '''function(doc){if(doc.title && doc.completed != null){emit(doc.order,{title: doc.title,completed: doc.completed})}}''', "_count") view_doc = view.get_doc(db) if not view_doc: # If the view does not exist create it view.sync(db) # Schedule something that will remove ToDos if we get too many sched = Scheduler() sched.start() def cleanup(): results = db.view('todos/allTodos') if len(results) > 0: count = results.rows[0]['value'] if count > 20: id = db.view('todos/allTodos', reduce=False).rows[0]['id'] db.delete(db.get(id)) sched.add_interval_job(cleanup, seconds=30)
import couchdb from couchdb.design import ViewDefinition #abc couch = couchdb.Server('http://*****:*****@115.146.85.196:5984/') database = couch['mostfollowers'] topfollowers = ViewDefinition( 'topfollowers', 'followers', 'function (doc) { if(doc.user_id) {emit(doc.followers, doc.user_id);}}') topfollowers.get_doc(database) topfollowers.sync(database)
import couchdb from couchdb.design import ViewDefinition #abc couch = couchdb.Server('http://*****:*****@115.146.85.196:5984/') database = couch['mosttweets'] view = ViewDefinition('toptweets', 'tweets', 'function (doc) { emit(doc.user_id, doc.user_id);}') view.get_doc(database) view.sync(database)
import couchdb from couchdb.design import ViewDefinition admin = 'admin' password = '******' server = couchdb.Server('http://*****:*****@115.146.86.96:5984') db = server['sentiment_ratio'] view1 = ViewDefinition('Res', 'res', 'function(doc) {emit(doc.results, 1);}') view1.get_doc(db) view1.sync(db) db1 = server['sentiment_sum'] view2 = ViewDefinition('Res', 'res', 'function(doc) {emit(doc.results, 1);}') view2.get_doc(db1) view2.sync(db1) db2 = server['posts_per_city'] view2 = ViewDefinition('Res', 'res', 'function(doc) {emit(doc.count, 1);}') view2.get_doc(db2) view2.sync(db2) db3 = server['dist_user'] view3 = ViewDefinition('Res', 'res', 'function(doc) {emit(doc.distcount, 1);}') view3.get_doc(db3) view3.sync(db3)
import couchdb from couchdb.design import ViewDefinition admin = 'admin' password = '******' server = couchdb.Server( 'http://*****:*****@115.146.86.96:5984') #connecting to the server #DB server connection db = server['income'] #view creation view1 = ViewDefinition('City', 'Adelaide', 'function(doc) {emit(doc.Adelaide, 1);}') view1.get_doc(db) view1.sync(db) view2 = ViewDefinition('City', 'Brisbane', 'function(doc) {emit(doc.Brisbane, 1);}') view2.get_doc(db) view2.sync(db) view3 = ViewDefinition('City', 'Canberra', 'function(doc) {emit(doc.Canberra, 1);}') view3.get_doc(db) view3.sync(db) view4 = ViewDefinition('City', 'Melbourne', 'function(doc) {emit(doc.Melbourne, 1);}') view4.get_doc(db) view4.sync(db) view5 = ViewDefinition('City', 'Perth', 'function(doc) {emit(doc.Perth, 1);}') view5.get_doc(db) view5.sync(db)
def create_view(db, design_doc, name, map_fun, reduce_fun=None): view = ViewDefinition(design_doc, name, map_fun, reduce_fun) view.get_doc(db) view.sync(db)