<commit_before>import paasmaker

# From: http://stackoverflow.com/questions/5389507/iterating-over-every-two-elements-in-a-list
from itertools import izip
def pairwise(iterable):
	"s -> (s0,s1), (s2,s3), (s4, s5), ..."
	a = iter(iterable)
	return izip(a, a)

class RouterTableDump(object):
	def __init__(self, configuration, callback, error_callback):
		self.configuration = configuration
		self.callback = callback
		self.error_callback = error_callback
		self.table = []
		self.all_roots = []

	def dump(self):
		self.session = self.configuration.get_database_session()
		self.configuration.get_router_table_redis(self._got_redis, self.error_callback)

	def _got_redis(self, redis):
		self.redis = redis
		self.redis.keys('instances_*', self._got_instance_ids)

	def _got_instance_ids(self, instance_ids):
		self.all_roots = instance_ids
		pipeline = self.redis.pipeline(True)
		for instance_set in instance_ids:
			hostname = instance_set.replace("instances_", "")
			# Fetch the instance IDs for this.
			pipeline.smembers("instance_ids_%s" % hostname)
			# Also fetch the instance routes.
			pipeline.smembers(instance_set)
		pipeline.execute(self._got_set_members)

	def _got_set_members(self, members):
		self.all_roots.reverse()
		for instance_set, instance_routes in pairwise(members):
			# Infer the hostname from the key name.
			hostname = self.all_roots.pop().replace("instance_ids_", "")
			# Fetch out the instances. NOTE: This is quite intensive.
			instances = self.session.query(
				paasmaker.model.ApplicationInstance
			).filter(
				paasmaker.model.ApplicationInstance.instance_id.in_(instance_set)
			).all()

			# Fetch out the application IDs. We then reduce this to one.
			# This is purely for sorting the table. TODO: This is really intensive.
			application_ids = map(lambda x: x.application_instance_type.application_version.application_id, instances)
			if len(application_ids) > 0:
				application_id = max(application_ids)
			else:
				application_id = 0

			sorted_routes = list(instance_routes)
			sorted_routes.sort()<commit_msg>In the routing table dump, return the correct hostname.<commit_after>import paasmaker

# From: http://stackoverflow.com/questions/5389507/iterating-over-every-two-elements-in-a-list
from itertools import izip
def pairwise(iterable):
	"s -> (s0,s1), (s2,s3), (s4, s5), ..."
	a = iter(iterable)
	return izip(a, a)

class RouterTableDump(object):
	def __init__(self, configuration, callback, error_callback):
		self.configuration = configuration
		self.callback = callback
		self.error_callback = error_callback
		self.table = []
		self.all_roots = []

	def dump(self):
		self.session = self.configuration.get_database_session()
		self.configuration.get_router_table_redis(self._got_redis, self.error_callback)

	def _got_redis(self, redis):
		self.redis = redis
		self.redis.keys('instances_*', self._got_instance_ids)

	def _got_instance_ids(self, instance_ids):
		self.all_roots = instance_ids
		pipeline = self.redis.pipeline(True)
		for instance_set in instance_ids:
			hostname = instance_set.replace("instances_", "")
			# Fetch the instance IDs for this.
			pipeline.smembers("instance_ids_%s" % hostname)
			# Also fetch the instance routes.
			pipeline.smembers(instance_set)
		pipeline.execute(self._got_set_members)

	def _got_set_members(self, members):
		self.all_roots.reverse()
		for instance_set, instance_routes in pairwise(members):
			# Infer the hostname from the key name.
			hostname = self.all_roots.pop().replace("instances_", "")
			# Fetch out the instances. NOTE: This is quite intensive.
			instances = self.session.query(
				paasmaker.model.ApplicationInstance
			).filter(
				paasmaker.model.ApplicationInstance.instance_id.in_(instance_set)
			).all()

			# Fetch out the application IDs. We then reduce this to one.
			# This is purely for sorting the table. TODO: This is really intensive.
			application_ids = map(lambda x: x.application_instance_type.application_version.application_id, instances)
			if len(application_ids) > 0:
				application_id = max(application_ids)
			else:
				application_id = 0

			sorted_routes = list(instance_routes)
			sorted_routes.sort()
{'fraction_non_alphanumeric': 0.10602062844806909, 'fraction_numerical': 0.007195970256656272, 'mean_word_length': 4.650406504065041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7123774', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1355, 'n_words': 303}