Index: /trunk/Monitoring/Analyze/analyze
===================================================================
--- /trunk/Monitoring/Analyze/analyze	(revision 878)
+++ /trunk/Monitoring/Analyze/analyze	(revision 879)
@@ -18,6 +18,6 @@
 import smtplib
 from email.mime.text import MIMEText
-import time;
-from datetime import datetime
+import time
+import datetime
 
 
@@ -31,21 +31,23 @@
 
 class CheckList():
-	max_time_between_msg=10 # unit => seconds
-
-	@classmethod
-	def datetime_to_epoch(cls,datetime):
-		epoch = time.mktime(time.strptime(datetime, "%d.%m.%Y %H:%M:%S")).time(); # assuming datetime format is "29.08.2011 11:05:02"
-		return epoch
+	max_time_between_msg=20 # unit => seconds
 
 	@classmethod
 	def msg_timeout(cls,message):
 
-		msg_time=cls.datetime_to_epoch(message.timestamp)
-		current_time=time.time()
+		# get current epoch
+		current_epoch=time.time()
 
-		diff=current_time-msg_time
+		# get msg epoch
+		msg_time=time.strptime(str(message.crea_date), "%Y-%m-%d %H:%M:%S.%f")
+		msg_epoch=time.mktime(msg_time)
+
+		diff =  current_epoch - msg_epoch
 
 		# debug
-		print "diff=%s"%diff
+		#print "cur=%i,ms=%s"%(current_epoch,message.crea_date)
+
+		# debug
+		#print "diff=%i"%int(diff)
 
 		if diff>cls.max_time_between_msg:
@@ -68,16 +70,23 @@
 		for simulation in repo_io.get_running_simulations():
 
-			print "checking heartbeat for '%s'"%simulation.name
+			print "hhh"
+
+			print "checking heartbeat ('%s')"%simulation.name
 
 			try:
-
 				message=repo_io.retrieve_last_message(simulation)
 
+				# debug
+				#print "found"
+
 			except types.MessageNotFoundException, e:
+				# when we are here, it mean we are in the interval when a new simulation have just been inserted but the corresponding message have not been inserted yet
+
+				print "no message found for simulation ('%s')"%simulation.name
 
 				continue
 
 
-			if msg_timeout(message):
+			if cls.msg_timeout(message):
 
 				simulation.status="error"
@@ -85,9 +94,9 @@
 				repo_io.update_simulation_status(simulation)
 
-				print "heartbeat NOK (simulation status set to 'error')"%simulation.name
+				print "heartbeat NOK - simulation status set to 'error' (%s)"%simulation.name
 
 
 			else:
-				print "heartbeat OK"%simulation.name
+				print "heartbeat OK (%s)"%simulation.name
 
 class Analyzer():
@@ -128,5 +137,5 @@
 
 
-			time.sleep(1)
+			time.sleep(3)
 
 		"""
Index: /trunk/Monitoring/Watch/watch
===================================================================
--- /trunk/Monitoring/Watch/watch	(revision 878)
+++ /trunk/Monitoring/Watch/watch	(revision 879)
@@ -21,5 +21,5 @@
 import smtplib
 from email.mime.text import MIMEText
-from datetime import datetime
+import datetime
 
 # line below is to include "smon" package in the search path
@@ -58,8 +58,16 @@
 	def store_msg(cls,message):
 
-		# the simu exists when we are here (see TAG0001 tag)
-		s=repo_io.retrieve_simulation(message.simuid)
-
-		repo_io.create_message(message,s)
+		try:
+
+			# the simu exists when we are here (see TAG0001 tag)
+			s=repo_io.retrieve_simulation(message.simuid)
+
+			repo_io.create_message(message,s)
+
+			sys.exit()
+
+		except:
+			traceback.print_exc()
+			raise
 
 	@classmethod
@@ -70,9 +78,15 @@
 	def set_sim_status_to_error(cls,message):
 
-		s=repo_io.retrieve_simulation(message.simuid)
-
-		s.status="error"
-
-		repo_io.update_simulation_status(s)
+		try:
+
+			s=repo_io.retrieve_simulation(message.simuid)
+
+			s.status="error"
+
+			repo_io.update_simulation_status(s)
+
+		except:
+			traceback.print_exc()
+			raise
 
 	@classmethod
@@ -103,13 +117,17 @@
 		# used for debug
 
+		"""
 		if message.file is not None:
 			print "%s %s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp,message.file)
 		else:
 			print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp)
+		"""
+
+		print "%s %s %s %s\n"%(message.code,message.jobid,message.command,message.timestamp)
 
 	@classmethod
 	def log(cls,message):
 		with open("/home/jripsl/supervisor/log/supervisor.log", "a") as log_file:
-			log_file.write("%s %s %s %s %s\n"%(datetime.now().strftime('%Y%m%d_%H%M%S'), message.code,message.jobid,message.timestamp,message.command))
+			log_file.write("%s %s %s %s %s\n"%(datetime.datetime.now().strftime('%Y%m%d_%H%M%S'), message.code,message.jobid,message.timestamp,message.command))
 
 	@classmethod
@@ -239,4 +257,10 @@
 				message=smon.types.Message(JSON_msg)      # all JSON object members will be available in smon.types.Message object
 
+				# non working
+				#print message.type
+
+				# working
+				#print message.code
+
 
 
@@ -249,5 +273,5 @@
 
 
-			# manage config-card file which is attached to the "0000" type message (this file is base64 encoded and need to be unencoded)
+			# manage config-card file which is attached to the "0000" code message (this file is base64 encoded and need to be unencoded)
 			#
 			if "file" in l__tmp_dic:
Index: /trunk/Monitoring/smon/local_repo.py
===================================================================
--- /trunk/Monitoring/smon/local_repo.py	(revision 878)
+++ /trunk/Monitoring/smon/local_repo.py	(revision 879)
@@ -59,5 +59,5 @@
 	_conn.execute("create unique index if not exists idx_simulation_1 on simulation (name)")
 
-	_conn.execute("create table if not exists message (id INTEGER PRIMARY KEY, simulation_id TEXT, body TEXT, crea_date TEXT)") # TODO: check how to use INT datatype for simulation_id column
+	_conn.execute("create table if not exists message (id INTEGER PRIMARY KEY, simulation_id TEXT, body TEXT, timestamp TEXT, crea_date TEXT)") # TODO: check how to use INT datatype for simulation_id column
 
 def cleanup():
@@ -102,5 +102,5 @@
 
 	if rs is None:
-		raise Exception()
+		raise Exception("name=%s"%name)
 
 	return types.Simulation(name=rs[0],id=rs[1],status=rs[2])
@@ -126,5 +126,5 @@
 	c=_conn.cursor()
 
-	_conn.execute("select id from message where simulation_id = ?",(simulation.id,))
+	c.execute("select id from message where simulation_id = ?",(simulation.id,))
 
 	rs=c.fetchone()
@@ -142,5 +142,5 @@
 def create_message(message,simulation):
 	
-	_conn.execute("insert into message (simulation_id,crea_date) values (?,?)",(simulation.id, message.timestamp))
+	_conn.execute("insert into message (simulation_id,timestamp,crea_date) values (?,?,datetime('now', 'localtime'))",(simulation.id, message.timestamp))
 
 	_conn.commit()
@@ -149,10 +149,29 @@
 	c=_conn.cursor()
 
-	_conn.execute("select id, simulation_id, body, crea_date from message where simulation_id=? order by crea_date desc limit 1",(simulation.id,))
+	# debug
+	#print "simulation_id=%d"%simulation.id
+
+	c.execute("select id, simulation_id, body, timestamp, crea_date from message where simulation_id=? order by crea_date desc limit 1",(simulation.id,))
 
 	rs=c.fetchone()
 
 	if rs is None:
+
+		# debug
+		#print "simulation not found (%d)"%simulation.id
+
 		raise types.MessageNotFoundException()
 
-	return types.Message(id=rs[0],simulation_id=rs[1],body=rs[2],timestamp=rs[3])
+	# HACK (we want the Message constructor to support BOTH **kw AND kw)
+	di={}
+	di["id"]=rs[0]
+	di["simulation_id"]=rs[1]
+	di["body"]=rs[2]
+	di["timestamp"]=id=rs[3]
+	di["crea_date"]=rs[4]
+
+	m=types.Message(di)
+
+	return m
+
+
Index: /trunk/Monitoring/smon/repo_io.py
===================================================================
--- /trunk/Monitoring/smon/repo_io.py	(revision 878)
+++ /trunk/Monitoring/smon/repo_io.py	(revision 879)
@@ -17,16 +17,11 @@
 
 import sys
+import datetime
 
 # line below is to include Prodiguer database I/O library in the search path
-sys.path.append("/home/jripsl/snapshot/src")
-
-# import Prodiguer database I/O library
-import elixir
-import prodiguer_shared
-
-
-
-
-
+sys.path.append("/home/jripsl/snapshot/src/prodiguer_shared/src")
+
+
+import types
 
 
@@ -37,12 +32,44 @@
 CSTE_MODE_REMOTE_REPO_STUB="remote_repo_stub"
 
+
+# HACK
+import prodiguer_shared.repo.session as repo_session
+import prodiguer_shared.models as models
+
+
+# Test constants.
+_SIM_ACTIVITY = 'IPSL'
+_SIM_COMPUTE_NODE = 'TGCC'
+_SIM_COMPUTE_NODE_LOGIN = 'p86denv'
+_SIM_COMPUTE_NODE_MACHINE = 'TGCC - Curie'
+_SIM_EXECUTION_START_DATE = datetime.datetime.now()
+_SIM_EXECUTION_STATE = models.EXECUTION_STATE_RUNNING
+_SIM_EXPERIMENT = '1pctCO2'
+_SIM_MODEL_ENGINE = 'IPSL-CM5A-LR'
+_SIM_SPACE = models.SIMULATION_SPACE_TEST
+_MSG_TYPE = "0000"
+_MSG_CONTENT1 = "12345690"
+_MSG_CONTENT2 = "12345690"
+
+
+
+
+
+
+
 # set mode
-mode=CSTE_MODE_LOCAL_REPO # CSTE_MODE_LOCAL_REPO, CSTE_MODE_REMOTE_REPO, CSTE_MODE_REMOTE_REPO_STUB
+mode=CSTE_MODE_REMOTE_REPO
+#mode=CSTE_MODE_LOCAL_REPO
 
 # set repository driver
 if mode==CSTE_MODE_REMOTE_REPO_STUB:
-	import prodiguer_shared.repo.mq.hooks_stub as repo
+	raise Exception()
+
 elif mode==CSTE_MODE_REMOTE_REPO:
-	import prodiguer_shared.repo.mq.hooks as repo
+
+	# import Prodiguer database I/O library
+	import prodiguer_shared.mq.hooks as repo
+
+
 elif mode==CSTE_MODE_LOCAL_REPO:
 	import local_repo as repo
@@ -61,6 +88,7 @@
 		repo.connect()
 	elif mode==CSTE_MODE_REMOTE_REPO:
-		_CONNECTION = "postgresql://postgres:Silence107!@localhost:5432/prodiguer"
-		prodiguer_shared.connect(_CONNECTION)
+		_CONNECTION = "postgresql://postgres:Silence107!@ib-pp-db-dev.ipslnet:5432/pdgr_1_0b5"
+		repo_session.start(_CONNECTION)
+
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
 		pass
@@ -72,7 +100,5 @@
 		repo.free()
 	elif mode==CSTE_MODE_REMOTE_REPO:
-
-		#prodiguer_shared.close()
-		pass
+		repo_session.end()
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
 		pass
@@ -91,5 +117,51 @@
 	used by get_running_simulations
 	"""
-	return repo.retrieve_simulations()
+	simulations=None
+
+	# debug
+	#print "bla"
+
+
+	if mode==CSTE_MODE_LOCAL_REPO:
+		simulations=repo.retrieve_simulations()
+	elif mode==CSTE_MODE_REMOTE_REPO:
+
+		# prepare
+		simulations=[]
+
+		# execute
+		sims=repo.retrieve_simulations()
+
+		# process return values
+
+		for s in sims:
+
+			status=get_repo_execution_state(s.ExecutionState_ID)
+
+			# HACK
+			if status=="queued":
+				status="waiting"
+
+			
+
+			simulations.append(types.Simulation(id=s.ID,name=s.Name,status=status.lower()))
+
+
+	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
+		pass
+	else:
+		raise Exception("ERR115 - incorrect mode")
+
+
+	# debug
+	"""
+	if len(simulations)<1:
+		raise Exception("ERR915 - debug")
+	else:
+		print "%d"%len(simulations)
+	"""
+
+
+	return simulations
 
 def test():
@@ -111,5 +183,21 @@
 		repo.cleanup()
 	elif mode==CSTE_MODE_REMOTE_REPO:
-		raise Exception("ERR707")
+
+
+		simulations_to_delete=["BIGBRO.clim.TEST.LMDZOR.p86denv.TGCC.CURIE",
+								"BIGBRO.clim.TEST.LMDZOR.p86denv.TGCC.CURIE",
+								"v5cf.amipMR1.amip.PROD.LMDZOR.p86denv.TGCC.CURIE",
+								"v5cf.amipMR2.amip.PROD.LMDZOR.p86denv.TGCC.CURIE",
+								"v5cf.amipMR3.amip.PROD.LMDZOR.p86denv.TGCC.CURIE"]
+
+
+		for s in simulations_to_delete:
+			repo.delete_messages(s)
+			repo.delete_simulation(s)
+
+			commit()
+
+
+
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
 		pass
@@ -121,5 +209,5 @@
 		repo.commit()
 	elif mode==CSTE_MODE_REMOTE_REPO:
-		elixir.session.commit()
+		repo_session.commit()
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
 		pass
@@ -131,9 +219,15 @@
 		repo.rollback()
 	elif mode==CSTE_MODE_REMOTE_REPO:
-		elixir.session.rollback()
+		repo_session.commit()
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
 		pass
 	else:
 		raise Exception("ERR003 - incorrect mode")
+
+def get_repo_execution_state(state_id):
+    for key, value in models.EXECUTION_STATE_ID_SET.items():
+        if value == state_id:
+            return key
+    return None
 
 def retrieve_simulation(name):
@@ -141,5 +235,10 @@
 
 	if mode==CSTE_MODE_LOCAL_REPO:
-		simulation=repo.retrieve_simulation(name)
+
+		try:
+			simulation=repo.retrieve_simulation(name)
+		except:
+			traceback.print_exc()
+
 	elif mode==CSTE_MODE_REMOTE_REPO:
 
@@ -150,6 +249,20 @@
 		s=repo.retrieve_simulation(name)
 
-		# process return values
-		simulation=smon.types.Simulation(exec_start_date=s.ExecutionStartDate,exec_end_date=s.ExecutionEndDate,status=s.ExecutionState) # ExecutionState example: EXECUTION_STATE_RUNNING, EXECUTION_STATE_SET..
+		if s is None:
+			raise Exception("RG543534")
+
+
+
+		# process return values
+
+		status=get_repo_execution_state(s.ExecutionState_ID)
+
+		# HACK
+		if status=="queued":
+			status="waiting"
+
+		simulation=types.Simulation(id=s.ID,name=s.Name,status=status) 
+
+
 
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
@@ -185,8 +298,40 @@
 
 		# prepare args
-		# ..
-
-		# execute
-		repo.create_simulation(activity)
+		model_engine=None
+		space=None
+		exp=None
+
+		if "BIGBRO" in simulation.name:
+
+			model_engine="IPSL-CM5A-LR"
+			space="TEST"
+			exp="sstClim"
+
+		elif "v5cf.amipMR" in simulation.name:
+
+			model_engine="IPSL-CM5A-MR"
+			space="PROD"
+			exp="amip"
+
+		else:
+
+			model_engine=_SIM_MODEL_ENGINE
+			space=_SIM_SPACE
+			exp=_SIM_EXPERIMENT
+
+
+		# execute
+		repo.create_simulation(_SIM_ACTIVITY,
+								_SIM_COMPUTE_NODE,
+								_SIM_COMPUTE_NODE_LOGIN,
+								_SIM_COMPUTE_NODE_MACHINE,
+								_SIM_EXECUTION_START_DATE,
+								_SIM_EXECUTION_STATE,
+								exp,
+								model_engine,
+								simulation.name,
+								space,
+								parent_simulation_name=None)
+
 
 		# process return values
@@ -200,15 +345,29 @@
 def update_simulation_status(simulation):
 	if mode==CSTE_MODE_LOCAL_REPO:
-		repo.update_simulation_status(simulation)
-	elif mode==CSTE_MODE_REMOTE_REPO:
-
-		# prepare args
-		# ..
-
-		# execute
-		repo.update_simulation_status(name)
-
-		# process return values
-		# ..
+
+		try:
+			repo.update_simulation_status(simulation)
+		except:
+			traceback.print_exc()
+
+	elif mode==CSTE_MODE_REMOTE_REPO:
+
+		# prepare args
+		# ..
+
+
+		# HACK
+		prodiguer_status=simulation.status
+		if simulation.status == "waiting":
+			prodiguer_status="queued"
+
+
+		# execute
+		repo.update_simulation_status(simulation.name, prodiguer_status.upper())
+
+		# process return values
+		# ..
+
+		commit()
 
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
@@ -267,6 +426,14 @@
 		# ..
 
-		# execute
-		repo.create_message()
+		print "%s %s %s"%(simulation.name,message.code,"message.body")
+
+		# execute
+		try:
+			repo.create_message(simulation.name,message.code,"message.body")
+		except ValueError as i:
+			print "bla (%s)"%str(i)
+
+		commit()
+
 
 		# process return values
@@ -277,4 +444,7 @@
 	else:
 		raise Exception("ERR020 - incorrect mode")
+
+	commit()
+
 
 def retrieve_last_message(simulation):
@@ -285,12 +455,14 @@
 	elif mode==CSTE_MODE_REMOTE_REPO:
 
-		# prepare args
-		# ..
-
-		# execute
-		repo.retrieve_last_message(simulation)
-
-		# process return values
-		# ..
+		# execute
+		message=repo.retrieve_last_message(simulation.name)
+
+		if message is None:
+			raise Exception("ERR221 - null value")
+
+		# process return values
+		di={}
+		di["crea_date"]=message.CreateDate
+		message=types.Message(di)
 
 	elif mode==CSTE_MODE_REMOTE_REPO_STUB:
@@ -308,4 +480,5 @@
 
 	for s in retrieve_simulations():
+
 		if s.status=="running":
 			running_simulation.append(s)
Index: /trunk/Monitoring/smon/types.py
===================================================================
--- /trunk/Monitoring/smon/types.py	(revision 878)
+++ /trunk/Monitoring/smon/types.py	(revision 879)
@@ -52,9 +52,12 @@
 
 class Message():
+	type=None
 	file=None
 	simuid=None
 	jobid=None
 	timestamp=None
+	crea_date=None
 	command=None
+	body=None
 
 	def __init__(self,JSON_KW):
