def __init__(self, client, bucket_name, bucket_type, search_index, struct): ''' File map structure ''' bucket = client.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['url'].assign(struct.get('url', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['checksum'].assign(struct.get('checksum', '')) self.map.registers['head'].assign(struct.get('head', '')) self.map.registers['payload'].assign(struct.get('payload', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['labels'].assign(struct.get('labels', '')) self.map.registers['hashes'].assign(struct.get('hashes', '')) self.map.registers['chunks'].assign(struct.get('chunks', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['updated'].assign(struct.get('updated', '')) self.map.registers['last_update_by'].assign( struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign( struct.get('last_update_at', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['active'].assign(struct.get('active', '')) self.map.registers['watchers'].assign(struct.get('watchers', '')) # end of the map stuff self.map.store()
def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Team map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['name'].assign(struct.get('name', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['resources'].assign(struct.get('resources', '')) self.map.registers['permissions'].assign(struct.get('permissions', '')) self.map.registers['members'].assign(struct.get('members', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) # end of the map stuff self.map.store()
def update_stats(user_id, stat, increment_flag=True): # Per user counters stored for: 'following', 'followers', 'posts' stats_bucket = client.bucket_type('user-map').bucket('stats') stats = Map(stats_bucket, user_id) if increment_flag: stats.counters[stat].increment() else: stats.counters[stat].decrement() stats.store(return_body=False)
def get_stats(user_id): # Returns user statistics stats_bucket = client.bucket_type('user-map').bucket('stats') stats = Map(stats_bucket, user_id) stats.reload() stats_dict = {} stats_dict['posts'] = stats.counters['posts'].value stats_dict['following'] = stats.counters['following'].value stats_dict['followers'] = stats.counters['followers'].value return stats_dict
def modify_team(self, account, team_uuid, struct): ''' Modify team ''' # riak search index search_index = 'mango_team_index' # riak bucket type bucket_type = 'mango_team' # riak bucket name bucket_name = 'teams' # solr query query = 'uuid_register:{0}'.format(team_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query) logging.warning(url) # pretty please, ignore this list of fields from database. IGNORE_ME = ("_yz_id", "_yz_rk", "_yz_rt", "_yz_rb", "checked", "keywords") # got http callback response got_response = [] # default return message message = {'update_complete': False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error': True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch(url, callback=handle_request) while len(got_response) == 0: # don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) team = Map(bucket, riak_key) # TODO: measure if there is value on make update_struct a yielding coroutine! message['update_complete'] = update_struct(team, struct, IGNORE_ME) except Exception as error: logging.exception(error) return message.get('update_complete', False)
def __init__( self, client, bucket_name, uuid, account, nodetype, subtype, title, description, content, keywords, start, end, url, created, checked ): ''' Stochastic event from your white-headed capuchins. ''' logging.info('pero peque~o mae') bucket_type = 'planets' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) if subtype is not bucket_name: logging.warning('Warning the following stuff do not match subtype:{0} bucket_name: {1}'.format(subtype, bucket_name)) if bucket_type is not nodetype: logging.warning('Warning the following stuff do not match nodetype:{0} bucket_type: {1}'.format(nodetype, bucket_type)) bucket.set_properties({'search_index': 'mars'}) # mars is of course our search index # now we define or map data structure self.map = Map(bucket, None) self.map.registers['uuid'].assign(uuid) self.map.registers['account'].assign(account) self.map.registers['title'].assign(title) self.map.registers['description'].assign(description) self.map.registers['content'].assign(content) self.map.registers['start'].assign(start) self.map.registers['end'].assign(end) self.map.registers['nodetype'].assign(bucket_type) self.map.registers['subtype'].assign(bucket_name) self.map.registers['url'].assign(url) for k in keywords: self.map.sets['keywords'].add(k) if checked: self.map.flags['checked'].enable() self.map.registers['created'].assign(created) # and finally we store or CRDT datatype inside our database self.map.store()
def create_users(user_bucket, friends_bucket): with open("users.txt") as f: users = f.read().splitlines() user_set = set(users) # remove dupes user_list = list(user_set) # Add user for user in user_list: user = user.strip() record = user_bucket.new(user, data={}) print "User " + user + " added... " + str(record.store()) # Add 10 random friends for i in range(10): friend = user_list[random.randint(0, len(user_list) - 1)].strip() if friend != user: friends_list = Map(friends_bucket, user) friends_list.sets['friends'].add(friend) print "Friend " + friend + " added to user " + user + "... " + str(friends_list.store(return_body=False))
def __init__(self, client, bucket_name, bucket_type, search_index, struct): ''' Task map structure ''' bucket = client.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['subject'].assign(struct.get('subject', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['data'].assign(struct.get('data', '')) self.map.registers['assign'].assign(struct.get('assign', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['source '].assign(struct.get('source', '')) self.map.registers['destination'].assign(struct.get('destination', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['start_time'].assign(struct.get('start_time', '')) self.map.registers['ack_time'].assign(struct.get('ack_time', '')) self.map.registers['stop_time'].assign(struct.get('stop_time', '')) self.map.registers['deadline'].assign(struct.get('deadline', '')) self.map.registers['duration'].assign(struct.get('duration', '')) self.map.registers['comments'].assign(struct.get('comments', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign( struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign( struct.get('last_update_at', '')) # end of the map stuff self.map.store()
def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Account map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['role'].assign(struct.get('role', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['account_type'].assign(struct.get('account_type', '')) self.map.registers['name'].assign(struct.get('name', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['nickname'].assign(struct.get('nickname', '')) self.map.registers['first_name'].assign(struct.get('first_name', '')) self.map.registers['last_name'].assign(struct.get('last_name', '')) self.map.registers['middle_name'].assign(struct.get('middle_name', '')) self.map.registers['password'].assign(struct.get('password', '')) self.map.registers['email'].assign(struct.get('email', '')) self.map.registers['phone_number'].assign(struct.get('phone_number', '')) self.map.registers['extension'].assign(struct.get('extension', '')) self.map.registers['country_code'].assign(struct.get('country_code', '')) self.map.registers['timezone'].assign(struct.get('timezone', '')) self.map.registers['company'].assign(struct.get('company', '')) self.map.registers['location'].assign(struct.get('location', '')) self.map.registers['phones'].assign(struct.get('phones', '')) self.map.registers['emails'].assign(struct.get('emails', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['orgs'].assign(struct.get('orgs', '')) self.map.registers['teams'].assign(struct.get('teams', '')) self.map.registers['members'].assign(struct.get('members', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) # end of the map stuff self.map.store()
def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Lead structure from your howler monkeys. ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) self.map.registers['payment_binder_payment_option'].assign(struct.get('payment_binder_payment_option', '')) # end of the map stuff self.map.store()
def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Task map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['subject'].assign(struct.get('subject', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['data'].assign(struct.get('data', '')) self.map.registers['assign'].assign(struct.get('assign', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['source '].assign(struct.get('source', '')) self.map.registers['destination'].assign(struct.get('destination', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['start_time'].assign(struct.get('start_time', '')) self.map.registers['ack_time'].assign(struct.get('ack_time', '')) self.map.registers['stop_time'].assign(struct.get('stop_time', '')) self.map.registers['deadline'].assign(struct.get('deadline', '')) self.map.registers['duration'].assign(struct.get('duration', '')) self.map.registers['comments'].assign(struct.get('comments', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) # end of the map stuff self.map.store()
class ContactMap(object): def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Lead structure from your howler monkeys. ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) self.map.registers['payment_binder_payment_option'].assign(struct.get('payment_binder_payment_option', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "payment_binder_payment_option":event.registers['payment_binder_payment_option'].value, "boat_effective_date":event.registers['boat_effective_date'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "payment_binder_payment_option":event.registers['payment_binder_payment_option'].value, "boat_effective_date":event.registers['boat_effective_date'].value, } return struct
class TaskMap(object): def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Task map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['subject'].assign(struct.get('subject', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['data'].assign(struct.get('data', '')) self.map.registers['assign'].assign(struct.get('assign', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['source '].assign(struct.get('source', '')) self.map.registers['destination'].assign(struct.get('destination', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['start_time'].assign(struct.get('start_time', '')) self.map.registers['ack_time'].assign(struct.get('ack_time', '')) self.map.registers['stop_time'].assign(struct.get('stop_time', '')) self.map.registers['deadline'].assign(struct.get('deadline', '')) self.map.registers['duration'].assign(struct.get('duration', '')) self.map.registers['comments'].assign(struct.get('comments', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "subject": event.registers['subject'].value, "description": event.registers['description'].value, "data": event.registers['data'].value, "assign": event.registers['assign'].value, "public": event.registers['public'].value, "source ": event.registers['source'].value, "destination": event.registers['destination'].value, "labels": event.registers['labels'].value, "start_time": event.registers['start_time'].value, "ack_time": event.registers['ack_time'].value, "stop_time": event.registers['stop_time'].value, "deadline": event.registers['deadline'].value, "duration": event.registers['duration'].value, "comments": event.registers['comments'].value, "history": event.registers['history'].value, "status": event.registers['status'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "subject": event.registers['subject'].value, "description": event.registers['description'].value, "data": event.registers['data'].value, "assign": event.registers['assign'].value, "public": event.registers['public'].value, "source ": event.registers['source'].value, "destination": event.registers['destination'].value, "labels": event.registers['labels'].value, "start_time": event.registers['start_time'].value, "ack_time": event.registers['ack_time'].value, "stop_time": event.registers['stop_time'].value, "deadline": event.registers['deadline'].value, "duration": event.registers['duration'].value, "comments": event.registers['comments'].value, "history": event.registers['history'].value, "status": event.registers['status'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return struct
def modify_file(self, account, file_uuid, struct): ''' Modify file ''' # riak search index search_index = 'sloth_file_index' # riak bucket type bucket_type = 'sloth_file' # riak bucket name bucket_name = 'files' # solr query query = 'uuid_register:{0}'.format(file_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query ) logging.warning(url) # pretty please, ignore this list of fields from database. IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords") # got callback response? got_response = [] # yours truly message = {'update_complete':False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) file = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: file.reload() old_value = file.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'",'"')) for thing in struct.get(key): old_list.append(thing) file.registers['{0}'.format(key)].assign(str(old_list)) else: new_list = [] for thing in struct.get(key): new_list.append(thing) file.registers['{0}'.format(key)].assign(str(new_list)) else: file.registers['{0}'.format(key)].assign(str(struct.get(key))) file.update() update_complete = True message['update_complete'] = True except Exception as error: logging.exception(error) return message.get('update_complete', False)
class TeamMap(object): def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Team map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['name'].assign(struct.get('name', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['resources'].assign(struct.get('resources', '')) self.map.registers['permissions'].assign(struct.get('permissions', '')) self.map.registers['members'].assign(struct.get('members', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "status": event.registers['status'].value, "name": event.registers['name'].value, "description": event.registers['description'].value, "resources": event.registers['resources'].value, "permissions": event.registers['permissions'].value, "members": event.registers['members'].value, "labels": event.registers['labels'].value, "history": event.registers['history'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "status": event.registers['status'].value, "name": event.registers['name'].value, "description": event.registers['description'].value, "resources": event.registers['resources'].value, "permissions": event.registers['permissions'].value, "members": event.registers['members'].value, "labels": event.registers['labels'].value, "history": event.registers['history'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return struct
class AccountMap(object): def __init__( self, client, bucket_name, bucket_type, search_index, struct ): ''' Account map structure ''' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['role'].assign(struct.get('role', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['account_type'].assign(struct.get('account_type', '')) self.map.registers['name'].assign(struct.get('name', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['nickname'].assign(struct.get('nickname', '')) self.map.registers['first_name'].assign(struct.get('first_name', '')) self.map.registers['last_name'].assign(struct.get('last_name', '')) self.map.registers['middle_name'].assign(struct.get('middle_name', '')) self.map.registers['password'].assign(struct.get('password', '')) self.map.registers['email'].assign(struct.get('email', '')) self.map.registers['phone_number'].assign(struct.get('phone_number', '')) self.map.registers['extension'].assign(struct.get('extension', '')) self.map.registers['country_code'].assign(struct.get('country_code', '')) self.map.registers['timezone'].assign(struct.get('timezone', '')) self.map.registers['company'].assign(struct.get('company', '')) self.map.registers['location'].assign(struct.get('location', '')) self.map.registers['phones'].assign(struct.get('phones', '')) self.map.registers['emails'].assign(struct.get('emails', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['orgs'].assign(struct.get('orgs', '')) self.map.registers['teams'].assign(struct.get('teams', '')) self.map.registers['members'].assign(struct.get('members', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_at'].assign(struct.get('last_update_at', '')) self.map.registers['last_update_by'].assign(struct.get('last_update_by', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "status": event.registers['status'].value, "role": event.registers['role'].value, "account": event.registers['account'].value, "account_type": event.registers['account_type'].value, "name": event.registers['name'].value, "description": event.registers['description'].value, "nickname": event.registers['nickname'].value, "first_name": event.registers['first_name'].value, "last_name": event.registers['last_name'].value, "middle_name": event.registers['middle_name'].value, "password": event.registers['password'].value, "email": event.registers['email'].value, "phone_number": event.registers['phone_number'].value, "extension": event.registers['extension'].value, "country_code": event.registers['country_code'].value, "timezone": event.registers['timezone'].value, "company": event.registers['company'].value, "location": event.registers['location'].value, "phones": event.registers['phones'].value, "emails": event.registers['emails'].value, "history": event.registers['history'].value, "labels": event.registers['labels'].value, "orgs": event.registers['orgs'].value, "teams": event.registers['teams'].value, "members": event.registers['members'].value, "checked": event.registers['checked'].value, "checked_at": event.registers['checked_at'].value, "checked_by": event.registers['checked_by'].value, "created_at": event.registers['created_at'].value, "created_by": event.registers['created_by'].value, "last_update_at": event.registers['last_update_at'].value, "last_update_by": event.registers['last_update_by'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "status": event.registers['status'].value, "account": event.registers['account'].value, "role": event.registers['role'].value, "account_type": event.registers['account_type'].value, "name": event.registers['name'].value, "description": event.registers['description'].value, "nickname": event.registers['nickname'].value, "first_name": event.registers['first_name'].value, "last_name": event.registers['last_name'].value, "middle_name": event.registers['middle_name'].value, "password": event.registers['password'].value, "email": event.registers['email'].value, "phone_number": event.registers['phone_number'].value, "extension": event.registers['extension'].value, "country_code": event.registers['country_code'].value, "timezone": event.registers['timezone'].value, "company": event.registers['company'].value, "location": event.registers['location'].value, "phones": event.registers['phones'].value, "emails": event.registers['emails'].value, "history": event.registers['history'].value, "labels": event.registers['labels'].value, "orgs": event.registers['orgs'].value, "teams": event.registers['teams'].value, "members": event.registers['members'].value, "checked": event.registers['checked'].value, "checked_at": event.registers['checked_at'].value, "checked_by": event.registers['checked_by'].value, "created_at": event.registers['created_at'].value, "created_by": event.registers['created_by'].value, "last_update_at": event.registers['last_update_at'].value, "last_update_by": event.registers['last_update_by'].value, } return struct
class TaskMap(object): def __init__(self, client, bucket_name, bucket_type, search_index, struct): ''' Task map structure ''' bucket = client.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['subject'].assign(struct.get('subject', '')) self.map.registers['description'].assign(struct.get('description', '')) self.map.registers['data'].assign(struct.get('data', '')) self.map.registers['assign'].assign(struct.get('assign', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['source '].assign(struct.get('source', '')) self.map.registers['destination'].assign(struct.get('destination', '')) self.map.registers['labels'].assign(struct.get('labels')) self.map.registers['start_time'].assign(struct.get('start_time', '')) self.map.registers['ack_time'].assign(struct.get('ack_time', '')) self.map.registers['stop_time'].assign(struct.get('stop_time', '')) self.map.registers['deadline'].assign(struct.get('deadline', '')) self.map.registers['duration'].assign(struct.get('duration', '')) self.map.registers['comments'].assign(struct.get('comments', '')) self.map.registers['history'].assign(struct.get('history', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['last_update_by'].assign( struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign( struct.get('last_update_at', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "subject": event.registers['subject'].value, "description": event.registers['description'].value, "data": event.registers['data'].value, "assign": event.registers['assign'].value, "public": event.registers['public'].value, "source ": event.registers['source'].value, "destination": event.registers['destination'].value, "labels": event.registers['labels'].value, "start_time": event.registers['start_time'].value, "ack_time": event.registers['ack_time'].value, "stop_time": event.registers['stop_time'].value, "deadline": event.registers['deadline'].value, "duration": event.registers['duration'].value, "comments": event.registers['comments'].value, "history": event.registers['history'].value, "status": event.registers['status'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "account": event.registers['account'].value, "subject": event.registers['subject'].value, "description": event.registers['description'].value, "data": event.registers['data'].value, "assign": event.registers['assign'].value, "public": event.registers['public'].value, "source ": event.registers['source'].value, "destination": event.registers['destination'].value, "labels": event.registers['labels'].value, "start_time": event.registers['start_time'].value, "ack_time": event.registers['ack_time'].value, "stop_time": event.registers['stop_time'].value, "deadline": event.registers['deadline'].value, "duration": event.registers['duration'].value, "comments": event.registers['comments'].value, "history": event.registers['history'].value, "status": event.registers['status'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, } return struct
def modify_remove(self, account, task_uuid, struct): ''' Modify remove ''' # riak search index search_index = 'mango_task_index' # riak bucket type bucket_type = 'mango_task' # riak bucket name bucket_name = 'tasks' # solr query query = 'uuid_register:{0}'.format(task_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query) # pretty please, ignore this list of fields from database. # if you want to include something in here, remember the _register. # example: labels_register. IGNORE_ME = ("_yz_id", "_yz_rk", "_yz_rt", "_yz_rb") # got callback response? got_response = [] # yours truly message = {'update_complete': False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error': True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch(url, callback=handle_request) while len(got_response) == 0: # Please, don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) task = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: task.reload() old_value = task.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'", '"')) new_list = [ x for x in old_list if x not in struct.get(key) ] task.registers['{0}'.format(key)].assign( str(new_list)) task.update() message['update_complete'] = True else: message['update_complete'] = False except Exception as error: logging.exception(error) return message.get('update_complete', False)
class CallMap(object): def __init__( self, client, bucket_name, uuid, account, nodetype, subtype, title, description, content, keywords, start, end, url, created, checked ): ''' Stochastic event from your white-headed capuchins. ''' logging.info('pero peque~o mae') bucket_type = 'planets' bucket = client.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) if subtype is not bucket_name: logging.warning('Warning the following stuff do not match subtype:{0} bucket_name: {1}'.format(subtype, bucket_name)) if bucket_type is not nodetype: logging.warning('Warning the following stuff do not match nodetype:{0} bucket_type: {1}'.format(nodetype, bucket_type)) bucket.set_properties({'search_index': 'mars'}) # mars is of course our search index # now we define or map data structure self.map = Map(bucket, None) self.map.registers['uuid'].assign(uuid) self.map.registers['account'].assign(account) self.map.registers['title'].assign(title) self.map.registers['description'].assign(description) self.map.registers['content'].assign(content) self.map.registers['start'].assign(start) self.map.registers['end'].assign(end) self.map.registers['nodetype'].assign(bucket_type) self.map.registers['subtype'].assign(bucket_name) self.map.registers['url'].assign(url) for k in keywords: self.map.sets['keywords'].add(k) if checked: self.map.flags['checked'].enable() self.map.registers['created'].assign(created) # and finally we store or CRDT datatype inside our database self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value @property def nodetype(self): return self.map.reload().registers['nodetype'].value @property def subtype(self): return self.map.reload().registers['subtype'].value @property def title(self): return self.map.reload().registers['title'].value @property def description(self): return self.map.reload().registers['description'].value @property def content(self): return self.map.reload().registers['content'].value @property def keywords(self): return self.map.reload().sets['keywords'].value @property def start(self): return self.map.reload().registers['start'].value @property def end(self): return self.map.reload().registers['end'].value @property def url(self): return self.map.reload().registers['url'].value @property def created(self): return self.map.reload().registers['created'].value @property def checked(self): return self.map.reload().flags['checked'].value def add_keyword(self, keyword): self.map.sets['keywords'].add(keyword) self.map.store() def remove_keyword(self, keyword): self.map.sets['keywords'].discard(keyword) self.map.store() def to_json(self): event = self.map.reload() struct = { "uuid":event.registers['uuid'].value, "account":event.registers['account'].value, "nodetype":event.registers['nodetype'].value, "subtype":event.registers['subtype'].value, "title":event.registers['title'].value, "description":event.registers['description'].value, "content":event.registers['content'].value, "keywords":event.sets['keywords'].value, "start":event.registers['start'].value, "end":event.registers['end'].value, "url":event.registers['url'].value, "created":event.registers['created'].value, "checked":event.flags['checked'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid":event.registers['uuid'].value, "account":event.registers['account'].value, "nodetype":event.registers['nodetype'].value, "subtype":event.registers['subtype'].value, "title":event.registers['title'].value, "description":event.registers['description'].value, "content":event.registers['content'].value, "keywords":event.sets['keywords'].value, "start":event.registers['start'].value, "end":event.registers['end'].value, "url":event.registers['url'].value, "created":event.registers['created'].value, "checked":event.flags['checked'].value, } return struct
def modify_remove(self, account, team_uuid, struct): ''' Modify remove ''' # riak search index search_index = 'mango_team_index' # riak bucket type bucket_type = 'mango_team' # riak bucket name bucket_name = 'teams' # solr query query = 'uuid_register:{0}'.format(team_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account.decode('utf-8')) # search query url url = "https://{0}/search/query/{1}?wt=json&q={2}&fq={3}".format( self.solr, search_index, query, filter_query ) # pretty please, ignore this list of fields from database. IGNORE_ME = ("_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords") # got callback response? got_response = [] # yours truly message = {'update_complete':False} def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # Please, don't be careless with the time. yield gen.sleep(0.0010) response = got_response[0].get('response')['docs'][0] riak_key = str(response['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) team = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: if type(struct.get(key)) == list: team.reload() old_value = team.registers['{0}'.format(key)].value if old_value: old_list = json.loads(old_value.replace("'",'"')) new_list = [x for x in old_list if x not in struct.get(key)] team.registers['{0}'.format(key)].assign(str(new_list)) team.update() message['update_complete'] = True else: message['update_complete'] = False except Exception as error: logging.exception(error) return message.get('update_complete', False)
def modify_contact(self, account, contact_uuid, struct): ''' Modify contact ''' # riak search index search_index = 'sparta_index' # riak bucket type bucket_type = 'sparta' # riak bucket name bucket_name = 'leads' # solr query query = 'uuid_register:{0}'.format(contact_uuid.rstrip('/')) # filter query filter_query = 'account_register:{0}'.format(account) # search query url url = "https://iofun.io/search/query/{0}?wt=json&q={1}&fq={2}".format( search_index, query, filter_query ) # pretty please, ignore this list of fields from database. IGNORE_ME = ["_yz_id","_yz_rk","_yz_rt","_yz_rb","checked","keywords"] got_response = [] update_complete = False def handle_request(response): ''' Request Async Handler ''' if response.error: logging.error(response.error) got_response.append({'error':True, 'message': response.error}) else: got_response.append(json.loads(response.body)) try: http_client.fetch( url, callback=handle_request ) while len(got_response) == 0: # don't be careless with the time. yield gen.sleep(0.0010) response_doc = got_response[0].get('response')['docs'][0] riak_key = str(response_doc['_yz_rk']) bucket = self.kvalue.bucket_type(bucket_type).bucket('{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) contact = Map(bucket, riak_key) for key in struct: if key not in IGNORE_ME: contact.registers['{0}'.format(key)].assign(str(struct.get(key))) contact.update() update_complete = True except Exception, e: logging.exception(e) raise gen.Return(e)
class FileMap(object): def __init__(self, client, bucket_name, bucket_type, search_index, struct): ''' File map structure ''' bucket = client.bucket_type(bucket_type).bucket( '{0}'.format(bucket_name)) bucket.set_properties({'search_index': search_index}) self.map = Map(bucket, None) # start of map structure self.map.registers['uuid'].assign(struct.get('uuid', '')) self.map.registers['url'].assign(struct.get('url', '')) self.map.registers['account'].assign(struct.get('account', '')) self.map.registers['checksum'].assign(struct.get('checksum', '')) self.map.registers['head'].assign(struct.get('head', '')) self.map.registers['payload'].assign(struct.get('payload', '')) self.map.registers['public'].assign(struct.get('public', '')) self.map.registers['labels'].assign(struct.get('labels', '')) self.map.registers['hashes'].assign(struct.get('hashes', '')) self.map.registers['chunks'].assign(struct.get('chunks', '')) self.map.registers['status'].assign(struct.get('status', '')) self.map.registers['created_by'].assign(struct.get('created_by', '')) self.map.registers['created_at'].assign(struct.get('created_at', '')) self.map.registers['updated'].assign(struct.get('updated', '')) self.map.registers['last_update_by'].assign( struct.get('last_update_by', '')) self.map.registers['last_update_at'].assign( struct.get('last_update_at', '')) self.map.registers['checked'].assign(struct.get('checked', '')) self.map.registers['checked_by'].assign(struct.get('checked_by', '')) self.map.registers['checked_at'].assign(struct.get('checked_at', '')) self.map.registers['active'].assign(struct.get('active', '')) self.map.registers['watchers'].assign(struct.get('watchers', '')) # end of the map stuff self.map.store() @property def uuid(self): return self.map.reload().registers['uuid'].value @property def account(self): return self.map.reload().registers['account'].value def to_json(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "url": event.registers['url'].value, "account": event.registers['account'].value, "checksum": event.registers['checksum'].value, "head": event.registers['head'].value, "payload": event.registers['payload'].value, "public": event.registers['public'].value, "labels": event.registers['labels'].value, "hashes": event.registers['hashes'].value, "chunks": event.registers['chunks'].value, "status": event.registers['status'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "updated": event.registers['updated'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "watchers": event.registers['watchers'].value, "active": event.registers['active'].value, } return json.dumps(struct) def to_dict(self): event = self.map.reload() struct = { "uuid": event.registers['uuid'].value, "url": event.registers['url'].value, "account": event.registers['account'].value, "checksum": event.registers['checksum'].value, "head": event.registers['head'].value, "payload": event.registers['payload'].value, "public": event.registers['public'].value, "labels": event.registers['labels'].value, "hashes": event.registers['hashes'].value, "chunks": event.registers['chunks'].value, "status": event.registers['status'].value, "created_by": event.registers['created_by'].value, "created_at": event.registers['created_at'].value, "updated": event.registers['updated'].value, "last_update_by": event.registers['last_update_by'].value, "last_update_at": event.registers['last_update_at'].value, "checked": event.registers['checked'].value, "checked_by": event.registers['checked_by'].value, "checked_at": event.registers['checked_at'].value, "watchers": event.registers['watchers'].value, "active": event.registers['active'].value, } return struct