def get_all(self, name=None): if name and isinstance(name, dict) and '$regex' in name: # {'$regex': 'localhost:.*'} name = name['$regex'].partition(':')[0] all_names = list(Scenario.get_all(self)) return [self.get(x.get('name')) for x in all_names if name in x.get('name')] else: return Scenario.get_all(self, name=name)
def list_scenarios(host): response = {'version' : version} scenario_db = Scenario() if host == 'all': scenarios = [x['name'] for x in scenario_db.get_all()] else: # get all scenarios for host scenarios = [x['name'] for x in scenario_db.get_all( {'$regex': '{0}:.*'.format(host)})] response['data'] = dict(host=host, scenarios=scenarios) return response
def list_scenarios(host): response = {'version': version} scenario_db = Scenario() if host == 'all': scenarios = [x['name'] for x in scenario_db.get_all()] else: # get all scenarios for host scenarios = [x['name'] for x in scenario_db.get_all( {'$regex': '{0}:.*'.format(host)})] response['data'] = dict(host=host, scenarios=scenarios) return response
def delete_stubs(handler, scenario_name=None, host=None, force=False): """delete all data relating to one named scenario or host/s.""" log.debug('delete_stubs') response = {'version': version} scenario_db = Scenario() static_dir = handler.settings['static_path'] def delete_scenario(scenario_name_key, force): log.debug(u'delete_scenario: {0}'.format(scenario_name_key)) host, scenario_name = scenario_name_key.split(':') cache = Cache(host) if not force: active_sessions = cache.get_active_sessions(scenario_name, local=False) if active_sessions: raise exception_response( 400, title='Sessons in playback/record, can not delete. Found th' 'e following active sessions: {0} for scenario: {1}'. format(active_sessions, scenario_name)) scenario_db.remove_all(scenario_name_key) cache.delete_caches(scenario_name) scenarios = [] if scenario_name: # if scenario_name exists it takes priority handler.track.scenario = scenario_name hostname = host or get_hostname(handler.request) scenarios.append(':'.join([hostname, scenario_name])) elif host: if host == 'all': scenarios = [x['name'] for x in scenario_db.get_all()] export_dir = os.path.join(static_dir, 'exports') if os.path.exists(export_dir): log.info('delete export dir') shutil.rmtree(export_dir) else: # get all scenarios for host scenarios = [ x['name'] for x in scenario_db.get_all({'$regex': '{0}:.*'.format(host)}) ] else: raise exception_response(400, title='scenario or host argument required') for scenario_name_key in scenarios: delete_scenario(scenario_name_key, force) response['data'] = dict(message='stubs deleted.', scenarios=scenarios) return response
def delete_stubs(handler, scenario_name=None, host=None, force=False): """delete all data relating to one named scenario or host/s.""" log.debug('delete_stubs') response = { 'version': version } scenario_db = Scenario() static_dir = handler.settings['static_path'] def delete_scenario(sce_name_key, frc): log.debug(u'delete_scenario: {0}'.format(sce_name_key)) # getting host and scenario names hst, sce_name = sce_name_key.split(':') cache = Cache(hst) if not frc: active_sessions = cache.get_active_sessions(sce_name, local=False) if active_sessions: raise exception_response(400, title='Sessons in playback/record, can not delete. ' 'Found the following active sessions: {0} ' 'for scenario: {1}'.format(active_sessions, sce_name)) scenario_db.remove_all(sce_name_key) cache.delete_caches(sce_name) scenarios = [] if scenario_name: # if scenario_name exists it takes priority handler.track.scenario = scenario_name hostname = host or get_hostname(handler.request) scenarios.append(':'.join([hostname, scenario_name])) elif host: if host == 'all': scenarios = [x['name'] for x in scenario_db.get_all()] export_dir = os.path.join(static_dir, 'exports') if os.path.exists(export_dir): log.info('delete export dir') shutil.rmtree(export_dir) else: # get all scenarios for host scenarios = [x['name'] for x in scenario_db.get_all( {'$regex': '{0}:.*'.format(host)})] else: raise exception_response(400, title='scenario or host argument required') for scenario_name_key in scenarios: delete_scenario(scenario_name_key, force) response['data'] = dict(message='stubs deleted.', scenarios=scenarios) return response
def get_session_status(handler, all_hosts=True): scenario = Scenario() host_scenarios = defaultdict() # getting a dictionary with sizes for all scenarios scenario_sizes = scenario.size() scenarios_recorded = scenario.recorded() for s in scenario.get_all(): host, scenario_name = s['name'].split(':') if not all_hosts and get_hostname(handler.request) != host: continue if host not in host_scenarios: host_scenarios[host] = {} # getting session data sessions = [] cache = Cache(host) for session_name, session in cache.get_sessions(scenario_name): # try and get the last_used from the last tracker get/response # else when the begin/session playback was called last_used = session_last_used(s['name'], session_name, 'playback') if last_used: last_used = last_used['start_time'].strftime('%Y-%m-%d %H:%M:%S') else: # session has never been used for playback last_used = session.get('last_used', '-') session['last_used'] = last_used # removing stub information since we aren't using it anyway and it can consume a lot of memory session.pop('stubs', None) # creating sessions list sessions.append(session) # getting stub count stub_counts = stub_count(host, scenario_name)['data']['count'] recorded = '-' # adding session information if sessions: if stub_counts: # getting scenario size and recorded values scenario_size = 0 try: scenario_size = scenario_sizes[s['name']] recorded = scenarios_recorded[s['name']] except KeyError: log.debug("Could not get scenario size for: %s" % s['name']) except Exception as ex: log.warn("Failed to get scenario size for: %s, got error: %s" % (s['name'], ex)) # creating a dict with scenario information host_scenarios[host][scenario_name] = (sessions, stub_counts, recorded, round(scenario_size, 0)) else: host_scenarios[host][scenario_name] = (sessions, 0, '-', 0) return host_scenarios
def get_session_status(handler, all_hosts=True): scenario = Scenario() host_scenarios = {} for s in scenario.get_all(): host, scenario_name = s['name'].split(':') if not all_hosts and get_hostname(handler.request) != host: continue if host not in host_scenarios: host_scenarios[host] = {} sessions = [] cache = Cache(host) for session_name, session in cache.get_sessions(scenario_name): # try and get the last_used from the last tracker get/response # else when the begin/session playback was called last_used = session_last_used(s['name'], session_name) if last_used: last_used = last_used['start_time'].strftime( '%Y-%m-%d %H:%M:%S') else: # session has never been used for playback last_used = session.get('last_used', '-') session['last_used'] = last_used sessions.append(session) stub_counts = stub_count(host, scenario_name)['data']['count'] recorded = '-' space_used = 0 if sessions: if stub_counts: stubs = list(get_stubs(host, scenario_name)) recorded = max(x['stub'].get('recorded') for x in stubs) for stub in stubs: stub = Stub(stub['stub'], s['name']) for matcher in stub.contains_matchers(): space_used += len(matcher) space_used += stub.space_used() host_scenarios[host][scenario_name] = (sessions, stub_counts, recorded, human_size(space_used)) else: host_scenarios[host][scenario_name] = (sessions, 0, '-', 0) return host_scenarios
def get_session_status(handler, all_hosts=True): scenario = Scenario() host_scenarios = {} for s in scenario.get_all(): host, scenario_name = s['name'].split(':') if not all_hosts and get_hostname(handler.request) != host: continue if host not in host_scenarios: host_scenarios[host] = {} sessions = [] cache = Cache(host) for session_name, session in cache.get_sessions(scenario_name): # try and get the last_used from the last tracker get/response # else when the begin/session playback was called last_used = session_last_used(s['name'], session_name, 'playback') if last_used: last_used = last_used['start_time'].strftime('%Y-%m-%d %H:%M:%S') else: # session has never been used for playback last_used = session.get('last_used', '-') session['last_used'] = last_used sessions.append(session) stub_counts = stub_count(host, scenario_name)['data']['count'] recorded = '-' space_used = 0 if sessions: if stub_counts: stubs = list(get_stubs(host, scenario_name)) recorded = max(x['stub'].get('recorded') for x in stubs) for stub in stubs: stub = Stub(stub['stub'], s['name']) space_used += stub.space_used() host_scenarios[host][scenario_name] = (sessions, stub_counts, recorded, human_size(space_used)) else: host_scenarios[host][scenario_name] = (sessions, 0, '-', 0) return host_scenarios
def get_session_status(handler, all_hosts=True): scenario = Scenario() host_scenarios = defaultdict() # getting a dictionary with sizes for all scenarios scenario_sizes = scenario.size() scenarios_recorded = scenario.recorded() for s in scenario.get_all(): host, scenario_name = s['name'].split(':') if not all_hosts and get_hostname(handler.request) != host: continue if host not in host_scenarios: host_scenarios[host] = {} # getting session data sessions = [] cache = Cache(host) for session_name, session in cache.get_sessions(scenario_name): # try and get the last_used from the last tracker get/response # else when the begin/session playback was called last_used = session_last_used(s['name'], session_name, 'playback') if last_used: last_used = last_used['start_time'].strftime( '%Y-%m-%d %H:%M:%S') else: # session has never been used for playback last_used = session.get('last_used', '-') session['last_used'] = last_used # removing stub information since we aren't using it anyway and it can consume a lot of memory session.pop('stubs', None) # creating sessions list sessions.append(session) # getting stub count stub_counts = stub_count(host, scenario_name)['data']['count'] recorded = '-' # adding session information if sessions: if stub_counts: # getting scenario size and recorded values scenario_size = 0 try: scenario_size = scenario_sizes[s['name']] recorded = scenarios_recorded[s['name']] except KeyError: log.debug("Could not get scenario size for: %s" % s['name']) except Exception as ex: log.warn( "Failed to get scenario size for: %s, got error: %s" % (s['name'], ex)) # creating a dict with scenario information host_scenarios[host][scenario_name] = (sessions, stub_counts, recorded, round(scenario_size, 0)) else: host_scenarios[host][scenario_name] = (sessions, 0, '-', 0) return host_scenarios