def add_del_all(user_id, access): parameters = {"group_id": g.user.group_id, "user_id": user_id} # set_id = raw_db.query(user_extend_sqls.get_entity_set_id, parameters).to_dict() set_id = asset_services.get_default_set() parameters['set_id'] = set_id.id if access == 1: raw_db.query(user_extend_sqls.delete_user_entity_all, parameters) return structure_ids = raw_db.query(user_extend_sqls.get_structure_id, parameters).to_dict() add_entity = {} add_entity['set_id'] = set_id.id for structure_id in structure_ids: add_entity['entity_id'] = structure_id.get('entityId') add_entity['user_id'] = user_id user_entity = UserEntity.from_dict(add_entity) db.session.add(user_entity) db.session.commit()
def get(self): # to get the latest date page = request.args.get('page', default = 1, type = int) page_size = request.args.get('page_size', default = 10, type = int) default_set = asset_services.get_default_set() return alert_services.get_alerts_by_page(default_set, page, page_size,[])
def get_entity_ids_by_level(level): set_id = asset_services.get_default_set() strutures = raw_db.query(asset_sqls.select_structure_by_level, {"level": level, "set_id": set_id.id}).all() entity_ids = [] for struture in strutures: entity_ids.append(struture.entity_id) return entity_ids
def get(self): default_set = asset_services.get_default_set() res = {} res['userName'] = g.user.name res['statistics'] = asset_services.m_assets_statistics(default_set) return res
def get(self, level): res = {} default_set = asset_services.get_default_set() page = request.args.get('page', default=1, type=int) page_size = request.args.get('page_size', default=7, type=int) details, level_name, counts = asset_services.m_assets_level_details( level, default_set, page, page_size) res = { 'level_name': level_name, 'count': counts, 'details': details.to_dict() } return res
def get(self, entity_id = None): page = request.args.get('page', default = 1, type = int) page_size = request.args.get('page_size', default = 9, type = int) entity_id = request.args.get('entity', default = None, type = int) level = request.args.get('level', type = int, default = None) date = request.args.get('as_of_date', type = float, default = datetime_to_timestamp(datetime.datetime.utcnow())) as_of_date = datetime.datetime.fromtimestamp(date / 1000) entity_ids = [] if level: entity_ids = alert_services.get_entity_ids_by_level(level) default_set = asset_services.get_default_set(as_of_date) if entity_id: res = alert_services.get_alerts_by_page_and_entity(default_set, entity_id, page, page_size) else: res = alert_services.get_alerts_by_page(default_set, page, page_size, entity_ids) return res
def get_access_entity_ids(): set_id = asset_services.get_default_set() user_entity_ids = raw_db.query(user_extend_sqls.get_user_entity, { "user_id": g.user.id, "set_id": set_id.id }).to_dict() child_tree = set([]) for user_entity_id in user_entity_ids: child_tree.add(user_entity_id.get('entityId')) structures = raw_db.query( user_extend_sqls.select_structure_by_parent_id, { "set_id": set_id.id, "entity_id": user_entity_id.get('entityId') }).all() # print("*** ",structures ," ***") if structures: for structure in structures: get_structure(structure.id, set_id.id, child_tree) # print("*** ", child_tree, " ***") return child_tree
def get(self): entity_id = request.args.get('entity', type = int, default = None) metric_id = request.args.get('metric', type = int, default = 1) page = request.args.get('page', type = int, default = 1) date = request.args.get('as_of_date', type = float, default = datetime_to_timestamp(datetime.datetime.utcnow())) as_of_date = datetime.datetime.fromtimestamp(date / 1000) level = request.args.get('level', type = int, default = None) entity_ids = [] if level: entity_ids = alert_services.get_entity_ids_by_level(level) res = {} # to get the latest date default_set = asset_services.get_default_set(as_of_date) sankey_data = asset_services.generate_sankey_for_entity(default_set, entity_id, metric_id) if entity_id: sankey_data = asset_services.generate_sankey_for_entity(default_set, entity_id, metric_id) res['sankeyData'] = alert_services.set_alert_flag_on_map(sankey_data, default_set) res['alerts'] = alert_services.get_alerts_by_page_and_entity(default_set, entity_id, page, 9) else: sankey_data = asset_services.generate_sankey_for_whole_set(default_set, metric_id) res['sankeyData'] = alert_services.set_alert_flag_on_map(sankey_data, default_set) res['alerts'] = alert_services.get_alerts_by_page(default_set, page, 9, entity_ids) access_entity_ids = user_services.get_access_entity_ids() for item in res.get('sankeyData').get('nodes'): if item.get('id') in access_entity_ids: item['access'] = 1 else: item['access'] = 1 res['sankeyData']['title'] = 'Alerts Map' res['effectiveDate'] = datetime_to_timestamp(default_set.date_key) res['availableStartDate'] = datetime_to_timestamp(chart_services.get_available_start_date()) return res
def query_entity(user_id, page, page_size, search): parameters = { "group_id": g.user.group_id, "user_id": user_id, 'offset': (page - 1) * page_size, 'limit': page_size } # set_id = raw_db.query(user_extend_sqls.get_entity_set_id, parameters).to_dict() set_id = asset_services.get_default_set() parameters['set_id'] = set_id.id user_entity_ids = raw_db.query(user_extend_sqls.get_user_entity, parameters).to_dict() structure_ids = raw_db.query(user_extend_sqls.get_structure_id, parameters).to_dict() structure = [] user_entity = [] for structure_id in structure_ids: structure.append(structure_id.get('entityId')) structure = set(structure) if user_entity_ids: for user_entity_id in user_entity_ids: user_entity.append(user_entity_id.get('entityId')) user_entity = set(user_entity) activated = len(user_entity) else: user_entity.append(-1) user_entity = set(user_entity) activated = 0 # print("before ---------- ", user_entity) # child_tree = set([]) # for user_entity_id in user_entity: # get_structure(user_entity_id, set_id.id, child_tree) # print("after ---------- ", child_tree) # user_entity = user_entity.union(child_tree) # activated = len(user_entity) dif = structure.difference(user_entity) total = len(dif) if total == 0: dif.add(-1) # print(user_entity, '------', structure) parameters['list1'] = tuple(user_entity) parameters['list2'] = tuple(dif) results = {} if search: parameters['search1'] = '%{}%'.format(search.lower()) parameters['search2'] = '%{}%'.format(search.upper()) result = raw_db.query(user_extend_sqls.get_entity_search, parameters).to_dict() search = raw_db.query(user_extend_sqls.get_entity_search_count, parameters).first() results['search'] = search.count else: result = raw_db.query(user_extend_sqls.get_entity, parameters).to_dict() results['entity'] = result results['activated'] = activated results['total'] = len(structure) results['set_id'] = parameters.get('set_id') # print(results) return results