def get_detail_by_record(record_id=None): record = db.session.query(OperationRecord).filter( OperationRecord.record_id == record_id).first() if record is None: abort(404, "record is not found") username = UserCache.get(record.uid).nickname \ if UserCache.get(record.uid).nickname \ else UserCache.get(record.uid).username timestamp = record.timestamp.strftime("%Y-%m-%d %H:%M:%S") attr_history = db.session.query(CIAttributeHistory).filter( CIAttributeHistory.record_id == record_id).all() rel_history = db.session.query(CIRelationHistory).filter( CIRelationHistory.record_id == record_id).all() attr_dict, rel_dict = dict(), {"add": [], "delete": []} for attr_h in attr_history: attr_dict[CIAttributeCache.get(attr_h.attr_id).attr_alias] = { "old": attr_h.old, "new": attr_h.new, "operate_type": attr_h.operate_type } manager = CIManager() for rel_h in rel_history: _, first = manager.get_ci_by_id(rel_h.first_ci_id) _, second = manager.get_ci_by_id(rel_h.second_ci_id) rel_dict[rel_h.operate_type].append( (first, rel_h.relation_type, second)) return jsonify(username=username, timestamp=timestamp, attr_history=attr_dict, rel_history=rel_dict)
def get_ci(ci_id=None): fields = request.args.get("fields", "").strip().split(",") fields = filter(lambda x: x != "", fields) ret_key = request.args.get("ret_key", "name") if ret_key not in ('name', 'alias', 'id'): ret_key = 'name' manager = CIManager() ci = manager.get_ci_by_id(ci_id, ret_key=ret_key, fields=fields) return jsonify(ci_id=ci_id, ci=ci)
def create_ci(): ci_type = request.values.get("ci_type") _no_attribute_policy = request.values.get("_no_attribute_policy", "ignore") ci_dict = dict() for k, v in request.values.iteritems(): if k != "ci_type" and not k.startswith("_"): ci_dict[k] = v.strip() manager = CIManager() current_app.logger.debug(ci_dict) ci_id = manager.add(ci_type, exist_policy="reject", _no_attribute_policy=_no_attribute_policy, **ci_dict) return jsonify(ci_id=ci_id)
def get_cis_by_type(type_id=None): fields = request.args.get("fields", "").strip().split(",") fields = filter(lambda x: x != "", fields) ret_key = request.args.get("ret_key", "name") if ret_key not in ('name', 'alias', 'id'): ret_key = 'name' page = get_page(request.values.get("page", 1)) count = get_per_page(request.values.get("count")) manager = CIManager() res = manager.get_cis_by_type(type_id, ret_key=ret_key, fields=fields, page=page, per_page=count) return jsonify(type_id=type_id, numfound=res[0], total=len(res[2]), page=res[1], cis=res[2])
def get_heartbeat(): page = get_page(request.values.get("page", 1)) ci_type = request.values.get("ci_type", "").strip() try: ci_type = CITypeCache.get(ci_type).type_id except: return jsonify(numfound=0, result=[]) agent_status = request.values.get("agent_status", None) if agent_status: agent_status = int(agent_status) numfound, result = CIManager().get_heartbeat(page, ci_type, agent_status=agent_status) return jsonify(numfound=numfound, result=result)
def update_ci(): if request.data: args = dict() _args = request.data.split("&") for arg in _args: if arg: args[arg.split("=")[0]] = \ urllib.unquote(urllib.unquote(arg.split("=")[1])) else: args = request.values ci_type = args.get("ci_type") _no_attribute_policy = args.get("_no_attribute_policy", "ignore") ci_dict = dict() for k, v in args.items(): if k != "ci_type" and not k.startswith("_"): ci_dict[k] = v.strip() manager = CIManager() ci_id = manager.add(ci_type, exist_policy="replace", _no_attribute_policy=_no_attribute_policy, **ci_dict) return jsonify(ci_id=ci_id)
def add_heartbeat(ci_type, unique): if not unique or not ci_type: return jsonify(message="error") # return jsonify(message="ok") return jsonify(message=CIManager().add_heartbeat(ci_type, unique))
def delete_ci(ci_id=None): manager = CIManager() manager.delete(ci_id) return jsonify(message="ok")
def update_ci_unique(ci_id): m = CIManager() m.update_unique_value(ci_id, request.values) return jsonify(ci_id=ci_id)