#!/home/conda/feedstock_root/build_artifacts/gstlal-inspiral_1694401452756/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_place/bin/python

# Copyright (C) 2019  Patrick Godwin
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__usage__ = "gstlal_ll_inspiral_event_uploader [--options]"
__description__ = "an executable to aggregate and upload GraceDB events from gstlal_inspiral jobs"
__author__ = "Patrick Godwin (patrick.godwin@ligo.org)"

#-------------------------------------------------
#				   Preamble
#-------------------------------------------------

from collections import deque, OrderedDict
import http.client as httplib
import gzip
import json
import logging
import math
from optparse import OptionParser
from io import BytesIO
import time
import yaml

import numpy
from numpy import random

from ligo.segments import segment
from ligo.gracedb.rest import GraceDb, HTTPError
from ligo.gracedb.rest import DEFAULT_SERVICE_URL as DEFAULT_GRACEDB_URL
from ligo.scald import utils
from ligo.scald.io import influx

from lal import LIGOTimeGPS

from gstlal import events
from gstlal import inspiral

from ligo.lw import ligolw
from ligo.lw import lsctables
from ligo.lw import utils as ligolw_utils

class LIGOLWContentHandler(ligolw.LIGOLWContentHandler):
    pass

lsctables.use_in(LIGOLWContentHandler)

#-------------------------------------------------
#				   Functions
#-------------------------------------------------

def parse_command_line():

	parser = OptionParser(usage=__usage__, description=__description__)
	parser.add_option("-v", "--verbose", default=False, action="store_true", help = "Be verbose.")
	parser.add_option("--rootdir", metavar = "path", default = ".", help = "Sets the root directory where logs and metadata are stored.")
	parser.add_option("--num-jobs", type = int, default = 10000, help="number of jobs to listen to")
	parser.add_option("--tag", metavar = "string", default = "test", help = "Sets the name of the tag used. Default = 'test'")
	parser.add_option("--max-event-time", type = "int", default = 7200, help = "Maximum time to keep around an event. Default = 2 hours.")
	parser.add_option("--upload-cadence-type", metavar = "string", default = "geometric", help = "Choose the method [geometric|linear] in which the cadence of subsequent uploads are done. Default = geometric.")
	parser.add_option("--upload-cadence-factor", type = "float", default = 4, help = "Cadence factor T for sending out subsequent events for the same event window. For geometric cadence, first event gets sent T seconds later, second event gets sent T^2 seconds later, etc. For linear cadence, subsequent events get sent T seconds later. Default = 4.0.")
	parser.add_option("--selection-criteria", type = "str", default = "MAXSNR", help = "Choose method for determining favored event. Select one of the following: " +
		"1. MAXSNR - upload highest SNR candidate *below* public alert threshold (default). " +
		"2. MINFAR - upload lowest FAR candidate. " +
		"3. COMPOSITE - upload composite event by assigning the minimum FAR to the highest SNR candidate.")
	parser.add_option("--far-threshold", type = "float", default = 3.84e-07, help = "FAR threshold considered for an event to be public, not including a trials factor. Default = 1 / month")
	parser.add_option("--far-trials-factor", type = "int", default = 1, help = "Trials factor for number of CBC pipelines uploading events to GraceDB. Default = 1.")
	parser.add_option("--processing-cadence", type = "float", default = 0.1, help = "Rate at which the event uploader acquires and processes data. Default = 0.1 seconds.")
	parser.add_option("--request-timeout", type = "float", default = 0.2, help = "Timeout for requesting messages from a topic. Default = 0.2 seconds.")
	parser.add_option("--kafka-server", metavar = "string", help = "Sets the server url that the kafka topic is hosted on. Required.")
	parser.add_option("--input-topic", metavar = "string", help = "Sets the input kafka topic. Required.")
	parser.add_option("--gracedb-group", metavar = "name", default = "Test", help = "Gracedb group to which to upload events (default is Test).")
	parser.add_option("--gracedb-pipeline", metavar = "name", default = "gstlal", help = "Name of pipeline to provide in GracedB uploads (default is gstlal).")
	parser.add_option("--gracedb-search", metavar = "name", default = "LowMass", help = "Name of search to provide in GracedB uploads (default is LowMass).")
	parser.add_option("--gracedb-service-url", metavar = "url", default = DEFAULT_GRACEDB_URL, help = "Override default GracedB service url (optional, default is {}).".format(DEFAULT_GRACEDB_URL))
	parser.add_option("--scald-config", metavar = "path", help = "sets ligo-scald options based on yaml configuration.")

	options, args = parser.parse_args()

	return options, args

#-------------------------------------------------
#					Classes
#-------------------------------------------------

class EventUploader(events.EventProcessor):
	"""
	manages handling of incoming events, selecting the best and uploading to GraceDB.
	"""
	_name = 'event_uploader'

	def __init__(self, options):
		logging.info('setting up event uploader...')

		is_injection_job = (options.input_topic == 'inj_events')
		heartbeat_topic = f"gstlal.{options.tag}.event_uploader_heartbeat" if not is_injection_job else f"gstlal.{options.tag}.inj_event_uploader_heartbeat"

		events.EventProcessor.__init__(
			self,
			process_cadence=options.processing_cadence,
			request_timeout=options.request_timeout,
			num_messages=options.num_jobs,
			kafka_server=options.kafka_server,
			input_topic=f"gstlal.{options.tag}.{options.input_topic}",
			tag=options.tag,
			send_heartbeats = True,
			heartbeat_cadence = 60.,
			heartbeat_topic = heartbeat_topic,
		)

		### initialize timing options
		self.max_event_time = options.max_event_time
		self.retries = 5
		self.retry_delay = 1

		### initialize gracedb client
		if options.gracedb_service_url.startswith("file"):
			self.client = inspiral.FakeGracedbClient(options.gracedb_service_url)
		else:
			self.client = GraceDb(options.gracedb_service_url)

		### gracedb settings
		self.gracedb_group = options.gracedb_group
		self.gracedb_pipeline = options.gracedb_pipeline
		self.gracedb_search = options.gracedb_search

		### upload cadence settings
		self.upload_cadence_type = options.upload_cadence_type
		self.upload_cadence_factor = options.upload_cadence_factor

		### initialize event store
		self.events = OrderedDict()

		### favored event settings
		if options.selection_criteria == "MAXSNR":
			self.favored_function = self.select_maxsnr_candidate
		elif options.selection_criteria == "MINFAR":
			self.favored_function = self.select_minfar_candidate
		else:
			self.favored_function = self.construct_composite_candidate

		topic_prefix = "" if options.input_topic == "events" else "inj_"
		self.public_far_threshold = options.far_threshold / options.far_trials_factor
		self.favored_event_topic = f'gstlal.{self.tag}.{topic_prefix}favored_events'
		self.upload_topic = f'gstlal.{self.tag}.{topic_prefix}uploads'

		### heartbeat settings
		self.last_inspiral_heartbeat = 0.0
		self.heartbeat_write = inspiral.now()

		### set up aggregator sink
		with open(options.scald_config, 'r') as f:
			agg_config = yaml.safe_load(f)
		self.agg_sink = influx.Aggregator(**agg_config["backends"]["default"])

		# register measurement schemas for aggregators
		self.agg_sink.load(path=options.scald_config)

	def ingest(self, message):
		"""
		parse a message containing a candidate event
		"""
		### process heartbeat messages from inspiral jobs
		if message.key() and 'heartbeat' == message.key().decode('UTF-8'):
			heartbeat = json.loads(message.value())
			if heartbeat['time'] > self.last_inspiral_heartbeat:
				self.last_inspiral_heartbeat = heartbeat['time']

		### process candidate event
		else:
			candidate = json.loads(message.value())
			candidate['time'] = LIGOTimeGPS(candidate['time'], candidate.pop('time_ns'))
			candidate.update(self.trigger_info(candidate))
			self.process_candidate(candidate)



	def process_candidate(self, candidate):
		"""
		handles the processing of a candidate, creating
		a new event if necessary
		"""
		key = self.event_window(candidate['time'])
		if key in self.events:
			logging.info('adding new candidate for event: [{:.1f}, {:.1f}]'.format(*key))
			self.events[key]['candidates'].append(candidate)
			self.update_trigger_history(key, candidate)
		else:
			new_event = True
			for seg, event in self.events.items():
				if segment(candidate['time'], candidate['time']) in seg:
					logging.info('adding new candidate for time window: [{:.1f}, {:.1f}]'.format(*seg))
					event['candidates'].append(candidate)
					self.update_trigger_history(seg, candidate)
					new_event = False

			### event not found, create a new event
			if new_event:
				logging.info('found new event: [{:.1f}, {:.1f}]'.format(*key))
				self.events[key] = self.new_event()
				self.events[key]['candidates'].append(candidate)
				self.update_trigger_history(key, candidate)

	def update_trigger_history(self, key, candidate):
		"""
		update trigger history dict for each candidate
		"""
		self.events[key]['trigger_history'].update(candidate['trigger_info'])

	def trigger_info(self, candidate):
		"""
		gather trigger information for each candidate,
		used for rtpe
		"""
		### parse candidate for SVD bin, masses, LR, and SNR
		svdbin, type = self.parse_job_tag(candidate["job_tag"])

		coinc = self.load_xmlobj(candidate["coinc"])
		coinc_row = lsctables.CoincTable.get_table(coinc)[0]
		snglinspiral_row = lsctables.SnglInspiralTable.get_table(coinc)[0]

		return {'trigger_info': {svdbin: {
				'snr': candidate["snr"],
				'likelihood': coinc_row.likelihood,
				'mass1': snglinspiral_row.mass1,
				'mass2': snglinspiral_row.mass2,
				'spin1z': snglinspiral_row.spin1z,
				'spin2z': snglinspiral_row.spin2z,
				'Gamma0': snglinspiral_row.Gamma0
				}
			}}

	def parse_job_tag(self, tag):
		"""
		get svd bin and job type from job tag
		"""
		svdbin = tag.split('_')[0]
		name = '_'.join(tag.split('_')[1:])
		return svdbin, tag

	def load_xmlobj(self, xmlobj):
		"""
		returns the coinc xml object from the kafka message
		"""
		if isinstance(xmlobj, str):
			xmlobj = BytesIO(xmlobj.encode("utf-8"))
		return ligolw_utils.load_fileobj(xmlobj, contenthandler = LIGOLWContentHandler)

	def event_window(self, t):
		"""
		returns the event window representing the event
		"""
		dt = 0.2
		return segment(utils.floor_div(t - dt, 0.5), utils.floor_div(t + dt, 0.5) + 0.5)


	def new_event(self):
		"""
		returns the structure that defines an event
		"""
		return {
			'num_sent': 0,
			'time_sent': None,
			'favored': None,
			'gid': None,
			'candidates': deque(maxlen = self.num_messages),
			'trigger_history': {}
		}


	def handle(self):
		"""
		handle events stored, selecting the best candidate.
		upload if a new favored event is found
		"""
		for key, event in sorted(self.events.items(), reverse=True):
			if event['num_sent'] == 0 or event['candidates'] and ((utils.gps_now() >= self.next_event_upload(event)) or (
					event['favored']['far'] > self.public_far_threshold and any([candidate['far'] <= self.public_far_threshold for candidate in event['candidates']]))):
				self.process_event(event, key)

		# clean out old events
		current_time = utils.gps_now()
		for key in list(self.events.keys()):
			if current_time - key[0] >= self.max_event_time:
				logging.info('sending final trigger history for event [{:.1f}, {:.1f}]'.format(*key))
				self.upload_file(
					"Trigger history file for RTPE",
					"trigger_history.json",
					"trigger_history",
					json.dumps(self.events[key]["trigger_history"]),
					self.events[key]['gid']
				)
				logging.info('removing stale event [{:.1f}, {:.1f}]'.format(*key))
				self.events.pop(key)

		# has it been more than 15 minutes since last inspiral heartbeat
		# jobs should send heartbeats every 10 minutes
		now = inspiral.now()
		if now - self.heartbeat_write > 15*60:
			state = 0 if now - self.last_inspiral_heartbeat > 15*60 else 1
			data = {'heartbeat': {'heartbeat_tag': {'time': [int(now)], 'fields': {'data': [state]}}}}

			logging.debug(f'Storing heartbeat state {state} to influx...')
			self.agg_sink.store_columns('heartbeat', data['heartbeat'], aggregate=None)
			self.heartbeat_write = now


	def process_event(self, event, window):
		"""
		handle a single event, selecting the best candidate.
		upload if a new favored event is found
		"""
		updated, event = self.process_candidates(event)
		if event['num_sent'] == 0:
			assert updated
		if updated:
			logging.info(
				'uploading {} candidate with FAR = {:.3E}, '
				'SNR = {:2.1f} for event: [{:.1f}, {:.1f}]'.format(
					self.to_ordinal(event['num_sent'] + 1),
					event['favored']['far'],
					event['favored']['snr'],
					window[0], window[1]
				)
			)
			self.send_favored_event(event, window)
			gid = self.upload_event(event)
			if gid:
				event['num_sent'] += 1
				event['gid'] = gid
				self.send_uploaded(event, gid)


	def process_candidates(self, event):
		"""
		process candidates and update the favored (maxsnr) event
		if needed

		returns event and whether the favored (maxsnr) event was updated
		"""
		updated, this_favored = self.favored_function(event['candidates'], event['favored'])

		if updated:
			event['favored'] = this_favored

		event['candidates'].clear()

		return updated, event


	def construct_composite_candidate(self, candidates, favored):
		"""
		Construct composite event. Replace far and likelihood 
		in maxsnr candidate with those in the minfar candidate.
		"""

		# add previous favored event to list so we can get
		# the overall min FAR and max SNR instead of just
		# over the new candidates
		if favored:
			candidates.append(favored)

		maxsnr_candidate = max(candidates, key=self.rank_snr)
		maxsnr = maxsnr_candidate['snr']

		minfar_candidate = min(candidates, key=self.rank_far)
		minfar = minfar_candidate['far']

		# if neither the FAR nor SNR have improved compared to
		# the previous favored, no update. Otherwise, make
		# composite event and send an update
		if favored and maxsnr <= favored['snr'] and minfar >= favored['far']:
			return False, favored
		else:
			### construct composite event
			if maxsnr_candidate['far'] != minfar:
				logging.info(
					'construct new composite event with FAR: {:.3E}, '
					'SNR: {:2.3f}'.format(minfar, maxsnr_candidate['snr'])
				)

				### replace far
				maxsnr_candidate['far'] = minfar
				
				### load coinc file
				maxsnr_coinc_row, maxsnr_coinc_file = self.get_coinc_row(maxsnr_candidate)
				minfar_coinc_row, minfar_coinc_file = self.get_coinc_row(minfar_candidate)

				### update likelihood and far
				maxsnr_coinc_row.likelihood = minfar_coinc_row.likelihood
				maxsnr_coinc_row.combined_far = minfar

				### save coinc file
				coinc_obj = BytesIO()
				ligolw_utils.write_fileobj(maxsnr_coinc_file, coinc_obj)
				maxsnr_candidate["coinc"] = coinc_obj.getvalue().decode("utf-8")

			if favored:
				assert maxsnr_candidate['far'] <= favored['far'], "composite event FAR should be smaller than previous favored FAR"
				assert maxsnr_candidate['snr'] >= favored['snr'], "composite event SNR should be larger than previous favored SNR"

			return True, maxsnr_candidate


	def select_maxsnr_candidate(self, candidates, favored = None):
		"""
		select max snr candidate from candidates below
		public alert threshold:
		"""
		# select the best candidate
		new_favored = max(candidates, key=self.rank_candidate)
		if not favored:
			return True, new_favored
		elif self.rank_candidate(new_favored) > self.rank_candidate(favored):
			return True, new_favored
		else:
			return False, favored


	def select_minfar_candidate(self, candidates, favored = None):
		"""
		select the min far candidate out of the candidates
		"""
		minfar_candidate = min(candidates, key=self.rank_far)
		if favored and minfar_candidate['far'] >= favored['far']:
			return False, minfar_candidate
		else:
			return True, minfar_candidate


	def rank_candidate(self, candidate):
		"""
		rank a candidate based on the following criterion:
		* FAR >  public threshold, choose lowest FAR
		* FAR <= public threshold, choose highest SNR
		"""
		if candidate['far'] <= self.public_far_threshold:
			return True, candidate['snr'], 1. / candidate['far']
		else:
			return False, 1. / candidate['far'], candidate['snr']


	@staticmethod
	def rank_snr(candidate): 
		return candidate['snr']


	@staticmethod
	def rank_far(candidate): 
		return candidate['far']


	def send_favored_event(self, event, event_window):
		"""
		send a favored event via Kafka
		"""
		favored_event = {
			'event_window': list(event_window),
			'time': event['favored']['time'].gpsSeconds,
			'time_ns': event['favored']['time'].gpsNanoSeconds,
			'snr': event['favored']['snr'],
			'far': event['favored']['far'],
			'coinc': event['favored']['coinc']
		}
		self.producer.produce(topic=self.favored_event_topic, value=json.dumps(favored_event))
		self.producer.poll(0)


	def send_uploaded(self, event, gid):
		"""
		send an uploaded event via Kafka
		"""
		uploaded = {
			'gid': gid,
			'time': event['favored']['time'].gpsSeconds,
			'time_ns': event['favored']['time'].gpsNanoSeconds,
			'coinc': event['favored']['coinc']
		}
		self.producer.produce(topic=self.upload_topic, value=json.dumps(uploaded))
		self.producer.poll(0)


	def upload_event(self, event):
		"""
		upload a new event + auxiliary files
		"""
		# upload event
		if self.gracedb_search == "EarlyWarning":
			label = "EARLY_WARNING"
		else:
			label = None
		for attempt in range(1, self.retries + 1):
			try:
				resp = self.client.createEvent(
					self.gracedb_group,
					self.gracedb_pipeline,
					'coinc.xml',
					filecontents = event['favored']['coinc'],
					search = self.gracedb_search,
					labels = label
				)
			except HTTPError as resp:
				logging.warning(resp)
			else:
				resp_json = resp.json()
				if resp.status == httplib.CREATED:
					graceid = resp_json['graceid']
					logging.info("event assigned grace ID {}".format(graceid))
					if not event['time_sent']:
						event['time_sent'] = utils.gps_now()
					break
			logging.warning(
				"gracedb upload of {} "
				"failed on attempt {:d}/{:d}".format('coinc.xml', attempt, self.retries)
			)
			time.sleep(numpy.random.lognormal(math.log(self.retry_delay), .5))
		else:
			logging.warning("gracedb upload of {} failed".format('coinc.xml'))
			return None

		self.upload_file(
			"Trigger history file for RTPE",
			"trigger_history.json",
			"trigger_history",
			json.dumps(event["trigger_history"]),
			graceid
		)

		return graceid


	def upload_file(self, message, filename, tag, contents, graceid):
		"""
		upload a file to gracedb
		"""
		logging.info("posting '{}' to gracedb ID {}".format(filename, graceid))
		for attempt in range(1, self.retries + 1):
			try:
				resp = self.client.writeLog(
					graceid,
					message,
					filename = filename,
					filecontents = contents,
					tagname = tag
				)
			except HTTPError as resp:
				logging.warning(resp)
			else:
				if resp.status == httplib.CREATED:
					break
			logging.info(
				"gracedb upload of {} for ID {} "
				"failed on attempt {:d}/{:d}".format(filename, graceid, attempt, self.retries)
			)
			time.sleep(numpy.random.lognormal(math.log(self.retry_delay), .5))
		else:
			logging.warning("gracedb upload of {} for ID {} failed".format(filename, graceid))

			return False



	def next_event_upload(self, event):
		"""
		check whether enough time has elapsed to send an updated event
		"""
		if self.upload_cadence_type == 'geometric':
			return event['time_sent'] + numpy.power(self.upload_cadence_factor, event['num_sent'])
		elif self.upload_cadence_type == 'linear':
			return event['time_sent'] + self.upload_cadence_factor * event['num_sent']

	def finish(self):
		"""
		send remaining events before shutting down
		"""
		for key, event in sorted(self.events.items(), reverse=True):
			if event['candidates']:
				self.process_event(event, key)


	@staticmethod
	def to_ordinal(n):
		"""
		given an integer, returns the ordinal number
		representation.

		this black magic is taken from
		https://stackoverflow.com/a/20007730
		"""
		return "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])


	@staticmethod
	def get_coinc_row(event):
		coinc_file = self.load_xmlobj(event["coinc"])
		return lsctables.CoincTable.get_table(coinc_file)[0], coinc_file


#-------------------------------------------------
#					 Main
#-------------------------------------------------

if __name__ == '__main__':
	# parse arguments
	options, args = parse_command_line()

	# check input topic
	if not options.input_topic in ("events", "inj_events"):
		raise Exception("Input topic should be either events or inj_events.")

	# check favored method
	if not options.selection_criteria in ("MAXSNR", "MINFAR", "COMPOSITE"):
		raise ValueError("Favored event method must be either MAXSNR, MINFAR, or COMPOSITE.")

	# set up logging
	log_level = logging.DEBUG if options.verbose else logging.INFO
	logging.basicConfig(format = '%(asctime)s | event_uploader : %(levelname)s : %(message)s')
	logging.getLogger().setLevel(log_level)

	# create event uploader instance
	event_uploader = EventUploader(options)

	# start up
	event_uploader.start()
