def get_data(self, uri=None, method=None, conn=None): try: agent_data = (r.table(AgentsCollection).get( self.agent_id).run(conn)) agent_data[AgentKey.LastAgentUpdate] = (int( agent_data[AgentKey.LastAgentUpdate].strftime('%s'))) if agent_data: agent_data['tags'] = get_tags_by_agent_id( agent_id=self.agent_id) agent_data[BASIC_RV_STATS] = (get_all_app_stats_by_agentid( self.username, self.customer_name, uri, method, self.agent_id)['data']) status = (GenericResults(self.username, uri, method).information_retrieved( agent_data, 1)) logger.info(status['message']) else: status = (GenericResults(self.username, uri, method).invalid_id( self.agent_id, 'agent_id')) logger.info(status['message']) except Exception as e: agent_data = None status = (GenericResults(self.username, uri, method).something_broke( self.agent_id, 'agents', e)) logger.error(status['message']) return (status)
def query_agents_by_name(self, query, conn=None): try: count = ( r .table(AgentsCollection) .get_all(self.customer_name, index=AgentKey.CustomerName) .filter( (r.row[AgentKey.ComputerName].match("(?i)"+query)) | (r.row[AgentKey.DisplayName].match("(?i)"+query)) ) .count() .run(conn) ) data = list( r .table(AgentsCollection) .filter( (r.row[AgentKey.ComputerName].match("(?i)"+query)) | (r.row[AgentKey.DisplayName].match("(?i)"+query)) ) .pluck(self.keys_to_pluck) .order_by(self.sort(self.sort_key)) .skip(self.offset) .limit(self.count) .run(conn) ) if data: for agent in data: agent[BASIC_RV_STATS] = ( get_all_app_stats_by_agentid( self.username, self.customer_name, self.uri, self.method, agent[AgentKey.AgentId] )['data'] ) status = ( GenericResults( self.username, self.uri, self.method ).information_retrieved(data, count) ) logger.info(status['message']) except Exception as e: status = ( GenericResults( self.username, self.uri, self.method ).something_broke('agent_query', 'agent', e) ) logger.exception(status['message']) return(status)
def filter_by(self, fkey, fval, conn=None): try: if fkey in self.valid_keys_to_filter_by: count = ( r .table(AgentsCollection) .get_all(self.customer_name, index=AgentKey.CustomerName) .filter({fkey: fval}) .count() .run(conn) ) data = list( r .table(AgentsCollection) .get_all(self.customer_name, index=AgentKey.CustomerName) .filter({fkey: fval}) .pluck(self.keys_to_pluck) .order_by(self.sort(self.sort_key)) .skip(self.offset) .limit(self.count) .run(conn) ) if data: for agent in data: agent[BASIC_RV_STATS] = ( get_all_app_stats_by_agentid( self.username, self.customer_name, self.uri, self.method, agent[AgentKey.AgentId] )['data'] ) status = ( GenericResults( self.username, self.uri, self.method ).information_retrieved(data, count) ) else: status = ( GenericResults( self.username, self.uri, self.method ).invalid_filter(fkey) ) except Exception as e: status = ( GenericResults( self.username, self.uri, self.method ).something_broke('filter', 'agent', e) ) return(status)
def get_data(self, uri=None, method=None, conn=None): try: agent_data = ( r .table(AgentsCollection) .get(self.agent_id) .run(conn) ) agent_data[AgentKey.LastAgentUpdate] = ( int(agent_data[AgentKey.LastAgentUpdate].strftime('%s')) ) if agent_data: agent_data['tags'] = get_tags_by_agent_id(agent_id=self.agent_id) agent_data[BASIC_RV_STATS] = ( get_all_app_stats_by_agentid( self.username, self.customer_name, uri, method, self.agent_id )['data'] ) status = ( GenericResults( self.username, uri, method ).information_retrieved(agent_data, 1) ) logger.info(status['message']) else: status = ( GenericResults( self.username, uri, method ).invalid_id(self.agent_id, 'agent_id') ) logger.info(status['message']) except Exception as e: agent_data = None status = ( GenericResults( self.username, uri, method ).something_broke(self.agent_id, 'agents', e) ) logger.error(status['message']) return(status)
def query_agents_by_mac_and_filter(self, query, fkey, fval, conn=None): try: count = ( r .table(HardwarePerAgentCollection) .get_all( HardwarePerAgentKey.Nic, index=HardwarePerAgentIndexes.Type ) .filter({fkey: fval}) .filter(r.row[HardwarePerAgentKey.Mac].match("(?i)"+query)) .pluck(self.keys_to_pluck) .distinct() .count() .run(conn) ) data = list( r .table(HardwarePerAgentCollection) .get_all( HardwarePerAgentKey.Nic, index=HardwarePerAgentIndexes.Type ) .eq_join(HardwarePerAgentKey.AgentId, r.table(AgentsCollection)) .zip() .filter({fkey: fval}) .filter(r.row[HardwarePerAgentKey.Mac].match("(?i)"+query)) .pluck(self.keys_to_pluck) .distinct() .order_by(self.sort(self.sort_key)) .skip(self.offset) .limit(self.count) .run(conn) ) if data: for agent in data: agent[BASIC_RV_STATS] = ( get_all_app_stats_by_agentid( self.username, self.customer_name, self.uri, self.method, agent[AgentKey.AgentId] )['data'] ) status = ( GenericResults( self.username, self.uri, self.method ).information_retrieved(data, count) ) logger.info(status['message']) except Exception as e: status = ( GenericResults( self.username, self.uri, self.method ).something_broke('agent_query', 'agent', e) ) logger.exception(status['message']) return(status)