def GET(self): nodes_info = db_manager.get_all_nodes() videos_info = db_manager.get_all_videos() nodes_info2 = [] videos_info2 = [] n_nodes = [0, 0, 0] # Server / cache / user # Convert 'chunk indexes' to ints for each in nodes_info: each.stored_chunks = ast.literal_eval(str(each.stored_chunks)) if each.stored_chunks is not None: if len(each.stored_chunks.keys()) == 0: continue for key, val in each.stored_chunks.items(): stored_chunk_str = str(val) stored_chunk_list = map(int, ast.literal_eval(stored_chunk_str)) val = stored_chunk_list.sort() each.stored_chunks[key] = stored_chunk_list # Convert storages to lists for each in nodes_info: nodes_info2.append([each.id, str(each.type_of_node), str(each.ip), str(each.port), str(each.watching_video), each.stored_chunks]) #nodes_info2.append([each.id, str(each.type_of_node), str(each.ip), str(each.port), str(each.watching_video), ast.literal_eval(str(each.stored_chunks))]) if str(each.type_of_node) == 'server': n_nodes[0] = n_nodes[0] + 1 elif str(each.type_of_node) == 'cache': n_nodes[1] = n_nodes[1] + 1 elif str(each.type_of_node) == 'user': n_nodes[2] = n_nodes[2] + 1 for each in videos_info: videos_info2.append([each.id, str(each.vname), each.n_of_frames, each.code_param_n, each.code_param_k, each.total_size, each.chunk_size, each.last_chunk_size]) print '[tracker.py] nodes_info ', nodes_info2 print '[tracker.py] n_nodes ', n_nodes print '[tracker.py] videos_info ', videos_info2 server_load = get_server_load() average_server_load = [sum(server_load[0])/len(server_load[0]), sum(server_load[1])/len(server_load[1])] return render.overview(nodes_info2, n_nodes, videos_info2, server_load, average_server_load)
def GET(self): if session.get('login', False): raise web.seeother('/overview') else: nodes_info = db_manager.get_all_nodes() videos_info = db_manager.get_all_videos() points = db_manager.get_all_points() accounts = db_manager.get_all_accounts() n_nodes = [0, 0, 0] num_accounts = 0 num_videos_watched = 0 # Convert storages to lists for each in nodes_info: if str(each.type_of_node) == 'server': n_nodes[0] = n_nodes[0] + 1 elif str(each.type_of_node) == 'cache': n_nodes[1] = n_nodes[1] + 1 elif str(each.type_of_node) == 'user': n_nodes[2] = n_nodes[2] + 1 num_accounts = len(accounts) - 1 # not counting admin for each in points: if each.owned_videos != None: num_videos_watched = num_videos_watched + (len(each.owned_videos.split("_")) - 1) return render.index(n_nodes, num_accounts, num_videos_watched)
def GET(self, request_str): req_valid, req_type, req_arg = self.parse_request(request_str) if req_valid == False: return 'Invalid request' else: # REQUEST NODE INFO if req_type == 'RESET': db_manager.remove_server() db_manager.remove_server_for_cache() db_manager.remove_all_videos() db_manager.remove_all_nodes() elif req_type == 'GET_SERVER_ADDRESS': res = db_manager.get_server() return str(res[0].ip) + ' ' + str(res[0].port) elif req_type == 'GET_SERVER_ADDRESS_FOR_CACHE': print 'get_server_address_for_cache' res = db_manager.get_server_for_cache() return str(res[0].ip) + ' ' + str(res[0].port) elif req_type == 'GET_CACHES_ADDRESS': # req = "user-hyunah-1 & 10" arg_user_name = req_arg.split('_')[0] arg_num_of_caches = req_arg.split('_')[1] n_of_current_caches = db_manager.get_num_of_caches() n_of_returned_caches = min(n_of_current_caches, int(arg_num_of_caches)) print '[tracker.py] n_of_returned_caches', n_of_returned_caches caches = db_manager.get_many_caches(arg_user_name, n_of_returned_caches) ret_str = '' for cache in caches: ret_str = ret_str + str(cache.ip) + ' ' + str(cache.port) + '\n' return ret_str # NODE REGISTER elif req_type == 'REGISTER_USER': # req_arg = "143.243.23.13_324" arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_watching_video = req_arg.split('_')[2] db_manager.add_user(arg_ip, arg_port, arg_watching_video) print '[tracker.py] Accessing...' print '[tracker.py] user_pop', user_population user_population[str(arg_watching_video)] += 1 log_load() return 'User is registered' elif req_type == 'REGISTER_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] db_manager.add_cache(arg_ip, arg_port) return 'Cache is registered' elif req_type == 'REGISTER_SERVER': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] # remove existing server & videos db_manager.add_server(arg_ip, arg_port) return 'Server is registered' elif req_type == 'REGISTER_SERVER_FOR_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] # remove existing server & videos db_manager.add_server_for_cache(arg_ip, arg_port) return 'Server is registered' # VIDEO REGISTER elif req_type == 'REGISTER_VIDEO': print 'add video' split_arg = req_arg.split('_') arg_vname = split_arg[0] arg_n_of_frames = split_arg[1] arg_code_param_n = split_arg[2] arg_code_param_k = split_arg[3] arg_total_size = split_arg[4] arg_chunk_size = split_arg[5] arg_last_chunk_size = split_arg[6] db_manager.add_video(arg_vname, arg_n_of_frames, arg_code_param_n, arg_code_param_k, arg_total_size, arg_chunk_size, arg_last_chunk_size) if str(arg_vname) not in user_population.keys(): user_population[str(arg_vname)] = 0 print '[tracker.py] arg_vname', str(arg_vname) print '[tracker.py] user_pop', user_population return 'Video is registered' elif req_type == 'GET_ALL_VIDEOS': videos = db_manager.get_all_videos() ret_str = '' for video in videos: ret_str = ret_str + str(video.id) + ' ' + str(video.vname) + ' ' + str(video.n_of_frames) + ' ' + str(video.code_param_n) + ' ' + str(video.code_param_k) + ' ' + str(video.total_size) + ' ' + str(video.chunk_size) + ' ' + str(video.last_chunk_size) + '\n' return ret_str elif req_type == 'REMOVE_SERVER': db_manager.remove_server() return 'Server is removed' elif req_type == 'REMOVE_SERVER_FOR_CACHE': db_manager.remove_server_for_cache() return 'Server for cache is removed' elif req_type == 'REMOVE_USER': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_watching_video = req_arg.split('_')[2] db_manager.remove_user(arg_ip, arg_port, arg_watching_video) user_population[str(arg_watching_video)] -= 1 log_load() return 'User is removed' elif req_type == 'REMOVE_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] db_manager.remove_cache(arg_ip, arg_port) return 'Cache is removed' elif req_type == 'UPDATE_CHUNKS_FOR_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_vname = req_arg.split('_')[2] arg_chunk_str = req_arg.split('_')[3] db_manager.add_chunks_for_cache(arg_ip, arg_port, arg_vname, arg_chunk_str) elif req_type == 'UPDATE_SERVER_LOAD': arg_vname = req_arg.split('_')[0] arg_n_of_chks = req_arg.split('_')[1] db_manager.add_server_load(arg_vname, arg_n_of_chks)
def GET(self, request_str): req_valid, req_type, req_arg = self.parse_request(request_str) if req_valid == False: return 'Invalid request' else: # REQUEST NODE INFO if req_type == 'RESET': db_manager.remove_server() db_manager.remove_server_for_cache() db_manager.remove_all_videos() db_manager.remove_all_nodes() db_manager.remove_all_caches_from_account_cache() elif req_type == 'GET_SERVER_ADDRESS': if session.get('login', False): res = db_manager.get_server() return str(res[0].ip) + ' ' + str(res[0].port) else: raise web.seeother('/login') elif req_type == 'GET_SERVER_ADDRESS_FOR_CACHE': if session.get('login', False): print 'get_server_address_for_cache' res = db_manager.get_server_for_cache() return str(res[0].ip) + ' ' + str(res[0].port) else: raise web.seeother('/login') elif req_type == 'GET_CACHES_ADDRESS': if session.get('login', False): # req = "user-hyunah-1 & 10" arg_user_name = req_arg.split('_')[0] arg_num_of_caches = req_arg.split('_')[1] n_of_current_caches = db_manager.get_num_of_caches() n_of_returned_caches = min(n_of_current_caches, int(arg_num_of_caches)) print '[tracker.py] n_of_returned_caches', n_of_returned_caches caches = db_manager.get_many_caches(arg_user_name, n_of_returned_caches) ret_str = '' for cache in caches: ret_str = ret_str + str(cache.ip) + ' ' + str(cache.port) + '\n' return ret_str else: raise web.seeother('/login') # NODE REGISTER elif req_type == 'REGISTER_USER': if session.get('login', False): un = session.user_name # req_arg = "143.243.23.13_324" arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_watching_video = req_arg.split('_')[2] #INSERT HERE #Get the list of owned videos here owned_videos= db_manager.get_owned_videos(un) if arg_watching_video not in owned_videos: #If requested video is not in the list, subtract 5 points res = db_manager.update_owned_videos(un, arg_watching_video) #If not enough points, return that says so if res == 'Not enough points': return 'Not enough points' db_manager.add_user(arg_ip, arg_port, arg_watching_video) print '[tracker.py] Accessing...' print '[tracker.py] user_pop', user_population user_population[str(arg_watching_video)] += 1 log_load() return 'User is registered' else: raise web.seeother('/login') elif req_type == 'REGISTER_CACHE': if session.get('login', False): ## arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] multiplier = req_arg.split('_')[2] #arg_config = req_arg.split('_')[2] TODO: cache size db_manager.add_cache(arg_ip, arg_port) un = session.user_name db_manager.add_cache_to_account_cache(un, arg_ip, arg_port, multiplier) return 'Cache is registered' else: raise web.seeother('/login') elif req_type == 'REGISTER_SERVER': #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] # remove existing server & videos db_manager.add_server(arg_ip, arg_port) return 'Server is registered' elif req_type == 'REGISTER_SERVER_FOR_CACHE': #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] # remove existing server & videos db_manager.add_server_for_cache(arg_ip, arg_port) return 'Server is registered' # VIDEO REGISTER elif req_type == 'REGISTER_VIDEO': #CURRENTLY NO SERVER ACCOUNT BUT WE SHOULD HAVE ONE print 'add video' split_arg = req_arg.split('_') arg_vname = split_arg[0] arg_n_of_frames = split_arg[1] arg_code_param_n = split_arg[2] arg_code_param_k = split_arg[3] arg_total_size = split_arg[4] arg_chunk_size = split_arg[5] arg_last_chunk_size = split_arg[6] db_manager.add_video(arg_vname, arg_n_of_frames, arg_code_param_n, arg_code_param_k, arg_total_size, arg_chunk_size, arg_last_chunk_size) if str(arg_vname) not in user_population.keys(): user_population[str(arg_vname)] = 0 print '[tracker.py] arg_vname', str(arg_vname) print '[tracker.py] user_pop', user_population return 'Video is registered' elif req_type == 'GET_OWNED_VIDEOS': if session.get('login', False): un = session.user_name owned_videos= db_manager.get_owned_videos(un) ## web.header('Content-Type', 'application/json') return json.dumps(owned_videos) else: raise web.seeother('/login') elif req_type == 'GET_ALL_VIDEOS': #users can still get all videos videos = db_manager.get_all_videos() ret_str = '' for video in videos: ret_str = ret_str + str(video.id) + ' ' + str(video.vname) + ' ' + str(video.n_of_frames) + ' ' + str(video.code_param_n) + ' ' + str(video.code_param_k) + ' ' + str(video.total_size) + ' ' + str(video.chunk_size) + ' ' + str(video.last_chunk_size) + '\n' return ret_str elif req_type == 'REMOVE_SERVER': db_manager.remove_server() return 'Server is removed' elif req_type == 'REMOVE_SERVER_FOR_CACHE': db_manager.remove_server_for_cache() return 'Server for cache is removed' elif req_type == 'REMOVE_USER': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_watching_video = req_arg.split('_')[2] db_manager.remove_user(arg_ip, arg_port, arg_watching_video) user_population[str(arg_watching_video)] -= 1 log_load() return 'User is removed' elif req_type == 'REMOVE_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] db_manager.remove_cache(arg_ip, arg_port) ## un = session.user_name #Right now not using username as input, because the server can remove any user it wants. #Need to have server logged in as admin for its extra priverlages db_manager.remove_cache_from_account_cache(ip=arg_ip, port=arg_port) return 'Cache is removed' elif req_type == 'UPDATE_CHUNKS_FOR_CACHE': arg_ip = req_arg.split('_')[0] arg_port = req_arg.split('_')[1] arg_vname = req_arg.split('_')[2] arg_chunk_str = req_arg.split('_')[3] db_manager.add_chunks_for_cache(arg_ip, arg_port, arg_vname, arg_chunk_str) elif req_type == 'UPDATE_SERVER_LOAD': arg_vname = req_arg.split('_')[0] arg_n_of_chks = req_arg.split('_')[1] db_manager.add_server_load(arg_vname, arg_n_of_chks) elif req_type == 'CACHE_DATA_VIS': return render.user_to_cache_data() elif req_type == 'CACHE_TO_USER_DATA': return render.cache_to_user_data() elif req_type =='GET_CACHE_DATA': web.header('Content-Type', 'application/json') user_data = dv.get_user_logs_as_json() return json.dumps(user_data) #currently reading from file. must later have post request to store data into db elif req_type =='GET_CACHE_DATA2': web.header('Content-Type', 'application/json') user_data = dv.get_user_logs_as_json() user_data = dv.rearrange_data_for_caches(user_data) return json.dumps(user_data) #currently reading from file. must later have post request to store data into db elif req_type == 'VERIFY_ACCOUNT': username = req_arg.split('_')[0] password = req_arg.split('_')[1] if len(db_manager.verify_account(username, password)) == 0: return False else: return True
def GET(self): ## if session.get('login', False): print 'SUCCESSFULLY DETECTS LOGIN' nodes_info = db_manager.get_all_nodes() videos_info = db_manager.get_all_videos() points = db_manager.get_all_points() accounts = db_manager.get_all_accounts() account_caches = db_manager.get_all_account_cache() nodes_info2 = [] videos_info2 = [] points2 = [] accounts2 = [] account_cache2 = [] n_nodes = [0, 0, 0] # Server / cache / user if session.user_name == 'admin': #admin ## # Convert 'chunk indexes' to ints for each in nodes_info: each.stored_chunks = ast.literal_eval(str(each.stored_chunks)) if each.stored_chunks is not None: if len(each.stored_chunks.keys()) == 0: continue for key, val in each.stored_chunks.items(): stored_chunk_str = str(val) stored_chunk_list = map(int, ast.literal_eval(stored_chunk_str)) val = stored_chunk_list.sort() each.stored_chunks[key] = stored_chunk_list # Convert storages to lists for each in nodes_info: nodes_info2.append([each.id, str(each.type_of_node), str(each.ip), str(each.port), str(each.watching_video), each.stored_chunks]) if str(each.type_of_node) == 'server': n_nodes[0] = n_nodes[0] + 1 elif str(each.type_of_node) == 'cache': n_nodes[1] = n_nodes[1] + 1 elif str(each.type_of_node) == 'user': n_nodes[2] = n_nodes[2] + 1 for each in videos_info: videos_info2.append([each.id, str(each.vname), each.n_of_frames, each.code_param_n, each.code_param_k, each.total_size, each.chunk_size, each.last_chunk_size]) for each in points: points2.append([each.id, str(each.user_name), round(each.bytes_uploaded/1000000, 3), each.points, each.owned_videos]) for each in accounts: accounts2.append([each.id, str(each.user_name), str(each.password), str(each.email_address)]) for each in account_caches: account_cache2.append([each.id, str(each.user_name), str(each.ip), str(each.port), str(each.bytes_uploaded), str(each.multiplier)]) print '[tracker.py] nodes_info ', nodes_info2 print '[tracker.py] n_nodes ', n_nodes print '[tracker.py] videos_info ', videos_info2 print '[tracker.py] points_info ', points2 print '[tracker.py] accounts', accounts2 print '[tracker.py] account_cache', account_cache2 server_load = get_server_load() smoothing_filter = np.ones(10)/10 #controls smoothing on the load to users smoothing_filter2 = np.ones(10)/10 #controls smoothing on the load to caches server_load_user = np.convolve(server_load[0],smoothing_filter) server_load_cache = np.convolve(server_load[1],smoothing_filter2) ## filtered_server_load = [] filtered_server_load.append(server_load_user.tolist()) filtered_server_load.append(server_load_cache.tolist()) average_server_load = [sum(filtered_server_load[0])/len(filtered_server_load[0]), sum(filtered_server_load[1])/len(filtered_server_load[1])] #return 'hi' return render.overview(nodes_info2, n_nodes, videos_info2, filtered_server_load, average_server_load, points2, accounts2, account_cache2) else: #normal user rank = [] num_user = 0 num_movie = 0 num_cache = 0 for each in nodes_info: if str(each.type_of_node) == 'server': n_nodes[0] = n_nodes[0] + 1 elif str(each.type_of_node) == 'cache': n_nodes[1] = n_nodes[1] + 1 elif str(each.type_of_node) == 'user': n_nodes[2] = n_nodes[2] + 1 if each.ip.split('-')[1] == session.user_name: num_user = num_user + 1 nodes_info2.append(["user", str(each.ip), str(each.watching_video)]) for each in points: if each.user_name == session.user_name: points2.append(int(each.points)) #points earned video_owned = each.owned_videos.split("_") video_owned.pop(0) points2.append(len(video_owned)) #num of video watched points2.append(each.bytes_uploaded/1000000) #bytes uploaded if each.owned_videos != None: num_movie = len(each.owned_videos.split("_"))-1 if each.user_name != 'admin': rank.append([each.user_name, num_movie, each.bytes_uploaded]) rank = sorted(rank, key=lambda user: user[2], reverse=True)[:5] #rank the users by points, get top 5 for each in rank: #each[2] = round(each[2]/1000000, 3) each[2] = each[2]/1000000 for each in accounts: if each.user_name == session.user_name: accounts2 = [each.id, str(each.user_name), str(each.password), str(each.email_address)] for each in account_caches: if each.user_name == session.user_name: num_cache = num_cache + 1 if each.multiplier == 0.25: speed = 'slow' elif each.multiplier == 0.5: speed = 'medium' else: speed = 'high' account_cache2.append(["cache", str(each.ip), str(each.port), str(each.bytes_uploaded), speed]) return render.user_overview(session.user_name, points2, accounts2, account_cache2, num_cache, num_user, nodes_info2, rank, n_nodes, video_owned) else: raise web.seeother('/login')