"""
This grammar is for producing records sutiable for serialisation into
the format required by GAIA-C9-TN-ASDC-GG-001-01.  It is used
in conjunction with bin/make_gaia_log.
"""

import datetime

import GeoIP  # from python-geoip package

from gavo.grammars.customgrammar import CustomRowIterator


class Stats(object):
	def __init__(self):
		self.handlesSeen = set()
		self.nSync = 0
		self.nAsync = 0

	def add(self, row):
		# todo: figure out from row if it's "sync" or "async"
		self.handlesSeen.add(row["handle"])
		self.nSync += 1

	def asDict(self):
		return {
			"n_users": len(self.handlesSeen),
			"n_sync": self.nSync,
			"n_async": self.nAsync,}

	
class RowIterator(CustomRowIterator):
	_validMethods = set(["GET", "POST", "HEAD", "PUT", "COPY"])

	geoResolver = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)

	def _iterRows(self):
		accesses = {}
		upstream = self.grammar.rd.getById("nvgrammar")
		upstream._initUserGrammar()
		# Important: don't move logs out of the way, the actual logs analyser
		# still wants to see them:
		upstream.rowIterator.keepInputs = True

		for row in upstream.parse(self.sourceToken):
			row["country"] = self.geoResolver.country_code_by_addr(row["ip"])
			if not row["service"]:
				continue
			key = (row["service"], row["country"])

			if key not in accesses:
				accesses[key] = Stats()
			accesses[key].add(row)

		keyLabels = ("service_name", "country")
		today = datetime.date.today()

		for key, stats in accesses.items():
			res = dict((k,v) for k,v in zip(keyLabels, key))
			res.update({
				"flag_a_p": None,
				"for_date": today,
				"gaia_release": 1,})
			res.update(stats.asDict())
			yield res


if __name__=="__main__":
	from gavo import api
	rd = api.getRD("logs/logs")
	res = api.makeData(rd.getById("make_gaia_log"))
	print(res.getPrimaryTable().rows)

