def ReadHuntCounters(self, hunt_id): """Reads hunt counters.""" num_clients = self.CountHuntFlows(hunt_id) num_successful_clients = self.CountHuntFlows( hunt_id, filter_condition=db.HuntFlowsCondition.SUCCEEDED_FLOWS_ONLY) num_failed_clients = self.CountHuntFlows( hunt_id, filter_condition=db.HuntFlowsCondition.FAILED_FLOWS_ONLY) num_clients_with_results = len( set(r[0].client_id for r in self.flow_results.values() if r and r[0].hunt_id == hunt_id)) num_crashed_clients = self.CountHuntFlows( hunt_id, filter_condition=db.HuntFlowsCondition.CRASHED_FLOWS_ONLY) num_results = self.CountHuntResults(hunt_id) total_cpu_seconds = 0 total_network_bytes_sent = 0 for f in self.ReadHuntFlows(hunt_id, 0, sys.maxsize): total_cpu_seconds += (f.cpu_time_used.user_cpu_time + f.cpu_time_used.system_cpu_time) total_network_bytes_sent += f.network_bytes_sent return db.HuntCounters( num_clients=num_clients, num_successful_clients=num_successful_clients, num_failed_clients=num_failed_clients, num_clients_with_results=num_clients_with_results, num_crashed_clients=num_crashed_clients, num_results=num_results, total_cpu_seconds=total_cpu_seconds, total_network_bytes_sent=total_network_bytes_sent)
def ReadHuntCounters(self, hunt_id, cursor=None): """Reads hunt counters.""" hunt_id_int = db_utils.HuntIDToInt(hunt_id) query = ("SELECT flow_state, COUNT(*) " "FROM flows " "FORCE INDEX(flows_by_hunt) " "WHERE parent_hunt_id = %s AND parent_flow_id IS NULL " "GROUP BY flow_state") cursor.execute(query, [hunt_id_int]) counts_by_state = dict(cursor.fetchall()) num_successful_clients = counts_by_state.get( int(rdf_flow_objects.Flow.FlowState.FINISHED), 0) num_failed_clients = counts_by_state.get( int(rdf_flow_objects.Flow.FlowState.ERROR), 0) num_crashed_clients = counts_by_state.get( int(rdf_flow_objects.Flow.FlowState.CRASHED), 0) num_running_clients = counts_by_state.get( int(rdf_flow_objects.Flow.FlowState.RUNNING), 0) num_clients = sum(counts_by_state.values()) query = """ SELECT * FROM ( SELECT COUNT(client_id) FROM flows FORCE INDEX(flows_by_hunt) WHERE parent_hunt_id = %s AND parent_flow_id IS NULL AND num_replies_sent > 0) counters, ( SELECT SUM(user_cpu_time_used_micros + system_cpu_time_used_micros), SUM(network_bytes_sent), SUM(num_replies_sent) FROM flows FORCE INDEX(flows_by_hunt) WHERE parent_hunt_id = %s AND parent_flow_id IS NULL) resources """ cursor.execute(query, [hunt_id_int, hunt_id_int]) ( num_clients_with_results, total_cpu_seconds, total_network_bytes_sent, num_results, ) = cursor.fetchone() return db.HuntCounters( num_clients=num_clients, num_successful_clients=num_successful_clients, num_failed_clients=num_failed_clients, num_clients_with_results=num_clients_with_results, num_crashed_clients=num_crashed_clients, num_running_clients=num_running_clients, num_results=int(num_results or 0), total_cpu_seconds=db_utils.MicrosToSeconds(int(total_cpu_seconds or 0)), total_network_bytes_sent=int(total_network_bytes_sent or 0))