def get(self): """@params: q: query statement fl: filter by column count/page_size: the number of ci ret_key: id, name, alias facet: statistic """ page = get_page(request.values.get("page", 1)) count = get_page_size(request.values.get("count") or request.values.get("page_size")) query = request.values.get('q', "") fl = handle_arg_list(request.values.get('fl', "")) ret_key = request.values.get('ret_key', RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME facet = handle_arg_list(request.values.get("facet", "")) sort = request.values.get("sort") start = time.time() if current_app.config.get("USE_ES"): s = SearchFromES(query, fl, facet, page, ret_key, count, sort) else: s = SearchFromDB(query, fl, facet, page, ret_key, count, sort) try: response, counter, total, page, numfound, facet = s.search() except SearchError as e: return abort(400, str(e)) current_app.logger.debug("search time is :{0}".format(time.time() - start)) return self.jsonify(numfound=numfound, total=total, page=page, facet=facet, counter=counter, result=response)
def get(self): """@params: q: query statement fl: filter by column count: the number of ci root_id: ci id level: default is 1 facet: statistic """ page = get_page(request.values.get("page", 1)) count = get_page_size(request.values.get("count") or request.values.get("page_size")) root_id = request.values.get('root_id') level = list(map(int, handle_arg_list(request.values.get('level', '1')))) query = request.values.get('q', "") fl = handle_arg_list(request.values.get('fl', "")) facet = handle_arg_list(request.values.get("facet", "")) sort = request.values.get("sort") start = time.time() s = Search(root_id, level, query, fl, facet, page, count, sort) try: response, counter, total, page, numfound, facet = s.search() except SearchError as e: return abort(400, str(e)) current_app.logger.debug("search time is :{0}".format(time.time() - start)) return self.jsonify(numfound=numfound, total=total, page=page, facet=facet, counter=counter, result=response)
def get(self): root_ids = list(map(int, handle_arg_list(request.values.get('root_ids')))) level = request.values.get('level', 1) type_ids = set(map(int, handle_arg_list(request.values.get('type_ids', [])))) start = time.time() s = Search(root_ids, level) try: result = s.statistics(type_ids) except SearchError as e: return abort(400, str(e)) current_app.logger.debug("search time is :{0}".format(time.time() - start)) return self.jsonify(result)
def post(self, type_id=None): attr_id_list = handle_arg_list(request.values.get("attr_id")) params = request.values params.pop("attr_id", "") CITypeAttributeManager.add(type_id, attr_id_list, **params) return self.jsonify(attributes=attr_id_list)
def put(self, attr_id): choice_value = handle_arg_list(request.values.get("choice_value")) params = request.values params["choice_value"] = choice_value current_app.logger.debug(params) AttributeManager().update(attr_id, **params) return self.jsonify(attr_id=attr_id)
def create_or_update_attr_value(self, key, value, ci_id, _no_attribute_policy=ExistPolicy.IGNORE): """ add or update attribute value, then write history :param key: id, name or alias :param value: :param ci_id: :param _no_attribute_policy: ignore or reject :return: """ attr = self._get_attr(key) if attr is None: if _no_attribute_policy == ExistPolicy.IGNORE: return if _no_attribute_policy == ExistPolicy.REJECT: return abort(400, 'attribute {0} does not exist'.format(key)) value_table = TableMap(attr_name=attr.name).table existed_attr = value_table.get_by(attr_id=attr.id, ci_id=ci_id, first=True, to_dict=False) existed_value = existed_attr and existed_attr.value operate_type = OperateType.ADD if existed_attr is None else OperateType.UPDATE value_list = handle_arg_list(value) if attr.is_list else [value] for v in value_list: v = self._validate(attr, v, value_table, ci_id) if operate_type == OperateType.ADD: value_table.create(ci_id=ci_id, attr_id=attr.id, value=v) self._write_change(ci_id, attr.id, operate_type, None, v) elif existed_attr.value != v: existed_attr.update(value=v) self._write_change(ci_id, attr.id, operate_type, existed_value, v)
def post(self, id_or_name): id_or_name = int(id_or_name) attr_list = handle_arg_list(request.values.get("attr", "")) orders = list(range(len(attr_list))) PreferenceManager.create_or_update_show_attributes(id_or_name, list(zip(attr_list, orders))) return self.jsonify(type_id=id_or_name, attr_order=list(zip(attr_list, orders)))
def create_or_update_attr_value(self, key, value, ci, _no_attribute_policy=ExistPolicy.IGNORE): """ add or update attribute value, then write history :param key: id, name or alias :param value: :param ci: instance object :param _no_attribute_policy: ignore or reject :return: """ attr = self._get_attr(key) if attr is None: if _no_attribute_policy == ExistPolicy.IGNORE: return if _no_attribute_policy == ExistPolicy.REJECT: return abort(400, 'attribute {0} does not exist'.format(key)) value_table = TableMap(attr_name=attr.name).table if attr.is_list: value_list = [ self._validate(attr, i, value_table, ci) for i in handle_arg_list(value) ] if not value_list: self.__check_is_required(ci.type_id, attr, '') existed_attrs = value_table.get_by(attr_id=attr.id, ci_id=ci.id, to_dict=False) existed_values = [i.value for i in existed_attrs] added = set(value_list) - set(existed_values) deleted = set(existed_values) - set(value_list) for v in added: value_table.create(ci_id=ci.id, attr_id=attr.id, value=v) self._write_change(ci.id, attr.id, OperateType.ADD, None, v) for v in deleted: existed_attr = existed_attrs[existed_values.index(v)] existed_attr.delete() self._write_change(ci.id, attr.id, OperateType.DELETE, v, None) else: value = self._validate(attr, value, value_table, ci) existed_attr = value_table.get_by(attr_id=attr.id, ci_id=ci.id, first=True, to_dict=False) existed_value = existed_attr and existed_attr.value if existed_value is None: value_table.create(ci_id=ci.id, attr_id=attr.id, value=value) self._write_change(ci.id, attr.id, OperateType.ADD, None, value) else: existed_attr.update(value=value) self._write_change(ci.id, attr.id, OperateType.UPDATE, existed_value, value)
def post(self): choice_value = handle_arg_list(request.values.get("choice_value")) params = request.values params["choice_value"] = choice_value current_app.logger.debug(params) attr_id = AttributeManager.add(**params) return self.jsonify(attr_id=attr_id)
def put(self, group_id): items = handle_arg_list(request.values.get("items")) ResourceGroupCRUD.update(group_id, items) items = ResourceGroupCRUD.get_items(group_id) return self.jsonify(items)
def put(self, group_id): name = request.values.get("name") order = request.values.get("order") or 0 attrs = handle_arg_list(request.values.get("attributes", "")) orders = list(range(len(attrs))) attr_order = list(zip(attrs, orders)) CITypeAttributeGroupManager.update(group_id, name, attr_order, order) return self.jsonify(group_id=group_id)
def post(self, type_id): name = request.values.get("name").strip() order = request.values.get("order") or 0 attrs = handle_arg_list(request.values.get("attributes", "")) orders = list(range(len(attrs))) attr_order = list(zip(attrs, orders)) group = CITypeAttributeGroupManager.create_or_update(type_id, name, attr_order, order) current_app.logger.warning(group.id) return self.jsonify(group_id=group.id)
def get(self, ci_id): fields = handle_arg_list(request.values.get("fields", "")) ret_key = request.values.get("ret_key", RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME manager = CIManager() ci = manager.get_ci_by_id_from_db(ci_id, ret_key=ret_key, fields=fields) return self.jsonify(ci_id=ci_id, ci=ci)
def delete(self, type_id=None): """ Form request: attr_id is a string, separated by commas Raw data request: attr_id is a list :param type_id: :return: """ attr_id_list = handle_arg_list(request.values.get("attr_id", "")) CITypeAttributeManager.delete(type_id, attr_id_list) return self.jsonify(attributes=attr_id_list)
def get(self): """@params: q: query statement fl: filter by column count: the number of ci ret_key: id, name, alias facet: statistic """ page = get_page(request.values.get("page", 1)) count = get_page_size(request.values.get("count")) query = request.values.get('q', "") fl = handle_arg_list(request.values.get('fl', "")) ret_key = request.values.get('ret_key', RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME facet = handle_arg_list(request.values.get("facet", "")) fl = list(filter(lambda x: x != "", fl)) facet = list(filter(lambda x: x != "", facet)) sort = request.values.get("sort") start = time.time() s = Search(query, fl, facet, page, ret_key, count, sort) try: response, counter, total, page, numfound, facet = s.search() except SearchError as e: return abort(400, str(e)) except Exception as e: current_app.logger.error(str(e)) return abort(500, "search unknown error") current_app.logger.debug("search time is :{0}".format(time.time() - start)) return self.jsonify(numfound=numfound, total=total, page=page, facet=facet, counter=counter, result=response)
def _query_build_raw(self): queries = handle_arg_list(self.orig_query) queries = self.__confirm_type_first(queries) current_app.logger.debug(queries) ret, query_sql = self.__query_build_by_field(queries) if ret is not None: return ret, query_sql s = time.time() if query_sql: self.query_sql = query_sql current_app.logger.debug(query_sql) numfound, res = self._execute_sql(query_sql) current_app.logger.debug("query ci ids is: {0}".format(time.time() - s)) return numfound, [_res[0] for _res in res] return 0, []
def _query_build_raw(self): queries = handle_arg_list(self.orig_query) current_app.logger.debug(queries) self.__query_build_by_field(queries) self._paginate_build() filter_path = self._fl_build() self._sort_build() self._facet_build() self._filter_ids() return es.read(self.query, filter_path=filter_path)
def get_heartbeat(cls, **kwargs): query = db.session.query(CI.id, CI.heartbeat).filter(CI.deleted.is_(False)) expire = datetime.datetime.now() - datetime.timedelta(minutes=72) type_ids = handle_arg_list(kwargs["type_id"]) query = query.filter(CI.type_id.in_(type_ids)) page = kwargs.get("page") agent_status = kwargs.get("agent_status") if agent_status == -1: query = query.filter(CI.heartbeat.is_(None)) elif agent_status == 0: query = query.filter(CI.heartbeat <= expire) elif agent_status == 1: query = query.filter(CI.heartbeat > expire) numfound = query.count() per_page_count = current_app.config.get("DEFAULT_PAGE_COUNT") cis = query.offset( (page - 1) * per_page_count).limit(per_page_count).all() ci_ids = [ci.id for ci in cis] heartbeat_dict = {} for ci in cis: if agent_status is not None: heartbeat_dict[ci.id] = agent_status else: if ci.heartbeat is None: heartbeat_dict[ci.id] = -1 elif ci.heartbeat <= expire: heartbeat_dict[ci.id] = 0 else: heartbeat_dict[ci.id] = 1 current_app.logger.debug(heartbeat_dict) ci_ids = list(map(str, ci_ids)) res = cls.get_cis_by_ids(ci_ids, fields=["hostname", "private_ip"]) result = [(i.get("hostname"), i.get("private_ip")[0], i.get("ci_type"), heartbeat_dict.get(i.get("_id"))) for i in res if i.get("private_ip")] return numfound, result
def get(self, type_id): fields = handle_arg_list(request.values.get("fields", "")) ret_key = request.values.get("ret_key", RetKey.NAME) if ret_key not in (RetKey.NAME, RetKey.ALIAS, RetKey.ID): ret_key = RetKey.NAME page = get_page(request.values.get("page", 1)) count = get_page_size(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 self.jsonify(type_id=type_id, numfound=res[0], total=len(res[2]), page=res[1], cis=res[2])
def post(self): type_id = request.values.get("type_id") levels = handle_arg_list(request.values.get("levels")) res = PreferenceManager.create_or_update_tree_view(type_id, levels) return self.jsonify(res and res.to_dict() or {})
def post(self, rid, resource_id=None, group_id=None): perms = handle_arg_list(request.values.get("perms")) PermissionCRUD.revoke(rid, perms, resource_id=resource_id, group_id=group_id) return self.jsonify(rid=rid, resource_id=resource_id, group_id=group_id, perms=perms)