def set_data(caller, result): data = [] if result["type"] == "lrg": for lr_obj in result["data"]: lrid = lr_obj.split(":")[2] lrip, lrport, lrtype = redis_client.hmget(lr_obj, "ip", "port", "lr_type") data.append({ "lrid": lrid, "ip": lrip, "port": lrport, "lr_type": lrtype, "epids": [], "star": False }) # response = {"code":200, "mark":"undecided", "data": data} ########## else: lrid = result["data"].split(":")[2] lrip, lrport, lrtype = redis_client.hmget(result["data"], "ip", "port", "lr_type") data.append({ "lrid": lrid, "ip": lrip, "port": lrport, "lr_type": lrtype, "epids": [], "star": False }) # response = {"code": 200, "mark": "fixed", "data": data} return data
def _elect_need_cluster(self, level_clusters_list, user_level, user): level_clusters = set(level_clusters_list) exist_clusters = set() if self.existing_level.has_key(user_level): for lr in self.existing_level[user_level]: lr_cluster = redis_client.hget("lr:%s:%s" % (user_level, lr), "cluster") exist_clusters.add(lr_cluster) need_clusters = list(level_clusters - exist_clusters) self.added_level[user_level] = self.existing_level[user_level] for need_cluster in need_clusters: lr = level_cluster_lr(user_level, need_cluster) self.added_level[user_level].append(lr) for lr_obj in self.added_level[user_level]: lrip, lrport, lrtype = redis_client.hmget( "lr:%s:%s" % (user_level, lr_obj), "ip", "port", "lr_type") self.added_data.append({ "lrid": lr_obj, "ip": lrip, "port": lrport, "lr_type": lrtype, "epids": [user], "star": False }) return 1 else: res = loop_choose([user_level], self.logger) if res: data = set_data(self.caller, res) for i in data: i["epids"] = [user] self.added_data.append(i) return 2 else: return 0
def best_lr(level_id, redis_lrc_list): lr = '' sysload = 10000 # uncertain for lr_obj in redis_lrc_list: lr_active, lr_sysload = redis_client.hmget(lr_obj, "active", "sysload") if lr_active == '1': if int(lr_sysload) <= sysload: lr = lr_obj sysload = int(lr_sysload) else: continue if lr: return lr # lr => lr92385aebbf2d97cf320180504100221 else: return None
def level_cluster_lr(level_id, cluster): lr_list = redis_client.keys("lr:%s:*" % level_id) lr = '' sysload = 10000 # uncertain for lr_obj in lr_list: lr_cluster, lr_active, lr_sysload = redis_client.hmget( lr_obj, "cluster", "active", "sysload") if lr_cluster == cluster: if lr_active == '1': if int(lr_sysload) <= sysload: lr = lr_obj.split(":")[2] sysload = int(lr_sysload) else: continue if lr: return lr # lr => lr92385aebbf2d97cf320180504100221 else: return None
def _fill_added(self, level, user, lrid=""): if level in self.added_level: added_level_lr = self.added_level[level] for lr in self.added_data: if lr["lrid"] in added_level_lr: lr["epids"].append(user) return True elif lrid: self.added_level[level] = [lrid] lrip, lrport, lrtype = redis_client.hmget( "lr:%s:%s" % (level, lrid), "ip", "port", "lr_type") self.added_data.append({ "lrid": lrid, "ip": lrip, "port": lrport, "lr_type": lrtype, "epids": [user], "star": False }) return False
def set_star(caller, logger): caller_level = redis_client.get("user:%s" % caller) _, lr_parents = split_level(caller_level) star_res = loop_choose(lr_parents, logger) if star_res and star_res["type"] == "lrc": star_id = star_res["data"].split(":")[2] starip, starport, startype = redis_client.hmget( star_res["data"], "ip", "port", "lr_type") res = { "lrid": star_id, "ip": starip, "port": starport, "lr_type": startype, "epids": [], "star": True } else: star = nc_config.DEFAULT_STAR_LR star["star"] = True res = star return res
def getLrnode(self, all=False): if all: sqls = """select `lr_id`,`name`,`ip`,`port`,`plevel_id`,`operator`,`cloud`,`price`, `lr_type`,`sysload`, `level_id`, `lrc_ids` from `lr_node`""" res = db.execFetch(sqls) all_lr_list = [] for record in res: all_lr_list.append({ "lrid": record[0], "name": record[1], "ip": record[2], "port": record[3], "plevel_id": record[4], "operator": record[5], "cloud": record[6], "price": record[7], "lr_type": record[8], "sysload": record[9], "level_id": record[10], "lrc_id": record[11] }) data = all_lr_list else: lr_list = redis_client.keys("lrid:*:%s" % self.lr_id) if not lr_list: return None lr = lr_list.pop() lr_ip, lr_port, lr_type = redis_client.hmget( lr, "ip", "port", "lr_type") data = { "lrid": self.lr_id, "ip": lr_ip, "port": lr_port, "lr_type": lr_type, "level_id": lr.split(":")[1] } # sqls = """select `lr_id`,`name`,`ip`,`port`,`operator`,`cloud`,`price`,`lr_type`, `plevel_id`, `sysload`, `level_id`, `lrc_ids` from lr_node where lr_id='%s'""" % self.lr_id # res = db.execFetch(sqls) # if len(res) != 0: data = {"lrid": res[0][0], "name": res[0][1], "ip": res[0][2], "port": res[0][3], "operator": res[0][4], "cloud": res[0][5], "price": res[0][6], "lr_type": res[0][7], "plevel_id": res[0][8], "sysload": res[0][9], "level_id": res[0][10], "lrc_id": res[0][11]} # else: data = nc_config.DEFAULT_LR return data
def getLrnode(self, all=False): if all: sqls = """select `lr_id`,`name`,`ip`,`port`,`plevel_id`,`cluster`,`cloud`,`price`, `lr_type`,`sysload`, `level_id`, `last_subtime`, `active` from `lr_node`""" res = db.execFetch(sqls) all_lr_list = [] for record in res: all_lr_list.append({ "lrid": record[0], "name": record[1], "ip": record[2], "port": record[3], "plevel_id": record[4], "cluster": record[5], "cloud": record[6], "price": record[7], "lr_type": record[8], "sysload": record[9], "level_id": record[10], "last_subtime": record[11], "active": record[12] }) data = all_lr_list else: lr_list = redis_client.keys("lr:*:%s" % self.lr_id) if not lr_list: return None lr = lr_list.pop() lr_ip, lr_port, lr_type, cluster, last_subtime, active = redis_client.hmget( lr, "ip", "port", "lr_type", "cluster", "last_subtime", "active") data = { "lrid": self.lr_id, "ip": lr_ip, "port": lr_port, "lr_type": lr_type, "level_id": lr.split(":")[1], "cluster": cluster, "last_subtime": last_subtime, "active": active } return data
def _set_attr(self): for lr in self.curmeeting: lrid = lr["lrid"] lr_level_list = redis_client.keys("lr:*:%s" % lrid) if lr_level_list: lr_level = lr_level_list[0].split(":")[1] if lr["star"]: self.star = lr else: if lr_level not in self.existing_level: self.existing_level[lr_level] = [lrid] else: self.existing_level[lr_level].append(lrid) self.existing_lr.append({lrid: lr}) caller_level = redis_client.get("user:%s" % self.caller) caller_level_type, caller_level_cluster = redis_client.hmget( "level:%s" % caller_level, {"level_type", "level_cluster"}) if caller_level_type == "lr" and (len(caller_level_cluster.split(":")) > 1): if caller_level in self.existing_level: self.is_lrg = True self.logger.info("Meeting type: LRG.")
def post(self): logger = logging.getLogger("nc") req = self.request reqbody = json.loads(self.request.body) # reqbody = eval(self.request.body) caller = reqbody["callerId"] logger.info(' -- %s -- "%s %s %s" - "%s" -- MSM request data: %s --' % (req.remote_ip, req.method, req.version, req.uri, req.headers["User-Agent"], reqbody)) user_list = redis_client.keys("user:%s" % caller) if not user_list: logger.error("The caller was not found, use default LR.") self.write({ "code": 404, "mark": "fixed", "data": [nc_config.DEFAULT_LR] }) # 判断该账号是否设置了强制使用的LR caller_account = caller.split("_")[2] force_res = detectForce(caller_account) if force_res["statu"]: lrid = force_res["lrid"] lr_obj = redis_client.keys("lr:*:%s" % lrid) ip, port, lr_type = redis_client.hmget(lr_obj[0], "ip", "port", "lr_type") lr_res = { "lrid": lrid, "ip": ip, "port": port, "lr_type": lr_type, "epids": [], "star": False } logger.info("The account [%s] has a specified LR [%s]" % (caller_account, force_res["lrid"])) self.write({ "code": 200, "mark": "fixed", "data": [lr_res] }) # mark: fixed/undecided # return # 含calleeList,通讯录会议 if reqbody.has_key("calleeList"): personlist = reqbody["calleeList"] personlist.append(caller) logger.info("Contacts style meeting starting, members -> [%s]" % personlist) near_level_list = [] all_person_level = {} for ep in personlist: adjacency_level = redis_client.get("user:%s" % ep) if not adjacency_level: logger.warning( "No matching level target found, please check the user_id [%s] exists." % ep) continue if adjacency_level not in near_level_list: near_level_list.append(adjacency_level) all_person_level[adjacency_level] = [ep] else: all_person_level[adjacency_level].append(ep) if len(near_level_list) < 1: nc_config.DEFAULT_LR["epids"] = personlist logger.error( "Contacts style meeting, did not find an adjacency, use default LR." ) self.write({ "code": 404, "mark": "fixed", "data": [nc_config.DEFAULT_LR] }) # return 404 elif len(near_level_list) == 1: level_id = near_level_list[0] parents_list = redis_client.lrange("ancestors:%s" % level_id, 0, -1) result = loop_choose(parents_list, logger) # result: "" || {"status": True, "type": "lrg"|"lrc", "data": lr_res|None} lr_res: [ lr:39:lr78920180518155843, lr:39:lr78945afe87b30 ] if result: data = set_data(caller, result) if len(data) == 1: response = {"code": 200, "mark": "fixed", "data": data} else: # level_type = redis_client.hget("level:%s"%level_id, "level_type") # if level_type == "lr": # star = nc_config.DEFAULT_STAR_LR # else: # star = set_star(caller, logger) star = set_star(caller, logger) for x in data: x["epids"] = personlist data.append(star) response = { "code": 200, "mark": "undecided", "data": data } logger.info( "Contacts style meeting, personlist has the same adjacency. Response: %s" % data) self.write(response) # return 200 else: nc_config.DEFAULT_LR["epids"] = personlist nc_config.DEFAULT_LR["star"] = False logger.error( "Contacts style meeting, personlist has the same adjacency, but didn't choose a valid LR. Use default LR." ) self.write({ "code": 404, "mark": "fixed", "data": [nc_config.DEFAULT_LR] }) # return 404 else: response_data = elect_func(caller, near_level_list, all_person_level, logger) epids_set = set() for i in response_data: epids = frozenset(i["epids"]) epids_set.add(epids) if len(epids_set) == len(response_data): response = { "code": 200, "mark": "fixed", "data": response_data } else: response = { "code": 200, "mark": "undecided", "data": response_data } logger.info("Contacts style meeting, effective return: %s" % response) self.write(response) # return 200 else: caller_level = redis_client.get("user:%s" % caller) caller_dlr_parents_list, caller_lr_parents_list = split_level( caller_level) if caller_dlr_parents_list: caller_lr_parents_list.insert(0, caller_dlr_parents_list[-1]) # print "$"*10, caller_meeting_level res = loop_choose(caller_lr_parents_list, logger) if not res: nc_config.DEFAULT_LR["epids"] = personlist nc_config.DEFAULT_LR["star"] = False logger.error( "Caller meeting, didn't choose a valid LR. Use default LR." ) self.write({ "code": 404, "mark": "fixed", "data": [nc_config.DEFAULT_LR] }) else: data = set_data(caller, res) for d in data: d["epids"] = [caller] if len(data) > 1: mark = "undecided" else: mark = "fixed" logger.info("Caller meeting, response: %s" % data) self.write({"code": 200, "mark": mark, "data": data})