def create(self, creator_name, title, describe, status, end_time, strategy_confs): strategy_obj = Strategys() new_strategy_confs = [] for name, strategy_uuids, control, custom, weight in strategy_confs: strategy_uuids = strategy_uuids.split(';') new_strategy_confs.append({ 'name': name, 'custom': custom, 'control': control, 'weight': weight, 'strategy_list': [[ strategy_uuid, strategy_obj.get_thresholds(strategy_uuid), strategy_obj.get_strategy_name(strategy_uuid) ] for strategy_uuid in strategy_uuids] }) strategy_list = sorted(new_strategy_confs, key=lambda x: int(x["weight"]), reverse=True) rule_uuid = str(uuid.uuid4()) payload = { 'uuid': rule_uuid, 'user': creator_name, 'update_time': str(int(time.time())), 'end_time': datetime.strftime(end_time, '%Y-%m-%d %H:%M:%S'), 'title': title, 'describe': describe, 'status': status, 'strategys': json.dumps(strategy_list) } client = get_redis_client() rule_id = client.incr(settings.LAST_RULE_ID_KEY) payload['id'] = rule_id rule_key = 'rule:{}'.format(rule_uuid) client.hmset(rule_key, payload) return str(rule_id), rule_uuid
def _parse_data(self, request): now = str(int(time.time())) origin = request.POST uuid_ = origin.get('id') status = origin.get('status') if status not in ('on', 'off'): raise ValueError(u"状态不合法") ret = { "uuid": uuid_, "status": status, 'user': request.user.username, 'update_time': now } end_time = origin.get('end_time') # 仅修改状态 if not end_time: return ret try: datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') except (ValueError, TypeError): raise ValueError(u"结束时间格式不合法") ret['end_time'] = end_time try: title = origin['title'] except KeyError: raise ValueError(u"规则名称必须填写") try: describe = origin['describe'] except KeyError: raise ValueError(u"规则描述必须填写") names = origin.get('names', '').split(':::') weights = origin.get('weights', '').split(',') strategys = origin.get('strategys', '').split('|') controls = origin.get('controls', '').split(',') customs = origin.get('customs', '').split(':::') lst = [names, weights, strategys, controls, customs] for elem in lst[1:]: if len(elem) != len(lst[0]): raise ValueError(u"数据长度不匹配") for weight in weights: if not weight.isdigit(): raise ValueError(u"权重不是整数") strategys_list = [] for strategy_str in strategys: try: strategy = json.loads(strategy_str) except ValueError: strategy = strategy_str if isinstance(strategy, list): strategys_list.append(strategy) else: strategy_obj = Strategys() lst = [] for uuid_ in strategy.split(';'): lst.append([ uuid_, strategy_obj.get_thresholds(uuid_), strategy_obj.get_strategy_name(uuid_) ]) strategys_list.append(lst) strategys = strategys_list strategy_uuids = [] for strategy in strategys: item = [x[0] for x in strategy] item.sort() strategy_uuids.append("".join(item)) if len(set(strategy_uuids)) < len(strategy_uuids): raise ValueError(u"策略原子有重复") for key in ('title', 'describe', 'names', 'weights', 'strategys', 'controls', 'customs'): ret[key] = locals()[key] return ret
def _parse_data(self, request, *args, **kwargs): now = str(int(time.time())) origin = request.POST uuid_ = origin.get('id') status = origin.get('status') if status not in ('on', 'off'): raise ValueError(_("Status is not legal.")) ret = { "uuid": uuid_, "status": status, 'user': self.request.user.username, 'update_time': now } end_time = origin.get('end_time') # Modify Status only if not end_time: return ret try: datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S') except (ValueError, TypeError): raise ValueError(_("End time format is not legal")) ret['end_time'] = end_time try: title = origin['title'] except KeyError: raise ValueError(_("RuleName must be filled in")) try: describe = origin['describe'] except KeyError: raise ValueError(_("Rule description must be filled in")) names = origin.get('names', '').split(':::') weights = origin.get('weights', '').split(',') strategys = origin.get('strategys', '').split('|') controls = origin.get('controls', '').split(',') customs = origin.get('customs', '').split(':::') lst = [names, weights, strategys, controls, customs] for elem in lst[1:]: if len(elem) != len(lst[0]): raise ValueError(_("Data length mismatch")) for weight in weights: if not weight.isdigit(): raise ValueError(_("Weight is not an integer")) strategys_list = [] for strategy_str in strategys: try: strategy = json.loads(strategy_str) except ValueError: strategy = strategy_str if isinstance(strategy, list): strategys_list.append(strategy) else: strategy_obj = Strategys() lst = [] for uuid_ in strategy.split(';'): lst.append([ uuid_, strategy_obj.get_thresholds(uuid_), strategy_obj.get_strategy_name(uuid_) ]) strategys_list.append(lst) strategys = strategys_list strategy_uuids = [] for strategy in strategys: item = [x[0] for x in strategy] item.sort() strategy_uuids.append("".join(item)) if len(set(strategy_uuids)) < len(strategy_uuids): raise ValueError(_("Policy has a repeat.")) for key in ('title', 'describe', 'names', 'weights', 'strategys', 'controls', 'customs'): ret[key] = locals()[key] return ret