2626import calendar
2727from datetime import timedelta
2828from urllib import quote
29+ from logging import getLogger
2930
3031from invenio import template
3132from invenio .config import \
3233 CFG_WEBDIR , \
3334 CFG_TMPDIR , \
3435 CFG_SITE_URL , \
3536 CFG_SITE_LANG , \
36- CFG_WEBSTAT_BIBCIRCULATION_START_YEAR
37- from invenio .webstat_config import CFG_WEBSTAT_CONFIG_PATH
37+ CFG_WEBSTAT_BIBCIRCULATION_START_YEAR , \
38+ CFG_ELASTICSEARCH_LOGGING
39+ from invenio .webstat_config import \
40+ CFG_WEBSTAT_CONFIG_PATH , \
41+ CFG_ELASTICSEARCH_EVENTS_MAP
3842from invenio .bibindex_engine_utils import get_all_indexes
3943from invenio .bibindex_tokenizers .BibIndexJournalTokenizer import CFG_JOURNAL_TAG
4044from invenio .search_engine import get_coll_i18nname , \
@@ -724,6 +728,7 @@ def destroy_customevents():
724728 msg += destroy_customevent (event [0 ])
725729 return msg
726730
731+
727732def register_customevent (event_id , * arguments ):
728733 """
729734 Registers a custom event. Will add to the database's event tables
@@ -739,17 +744,25 @@ def register_customevent(event_id, *arguments):
739744 @param *arguments: The rest of the parameters of the function call
740745 @type *arguments: [params]
741746 """
742- res = run_sql ("SELECT CONCAT('staEVENT', number),cols " + \
743- "FROM staEVENT WHERE id = %s" , (event_id , ))
747+ query = """
748+ SELECT CONCAT('staEVENT', number),
749+ cols
750+ FROM staEVENT
751+ WHERE id = %s
752+ """
753+ params = (event_id ,)
754+ res = run_sql (query , params )
755+ # the id does not exist
744756 if not res :
745- return # the id does not exist
757+ return
746758 tbl_name = res [0 ][0 ]
747759 if res [0 ][1 ]:
748760 col_titles = cPickle .loads (res [0 ][1 ])
749761 else :
750762 col_titles = []
751763 if len (col_titles ) != len (arguments [0 ]):
752- return # there is different number of arguments than cols
764+ # there is different number of arguments than cols
765+ return
753766
754767 # Make sql query
755768 if len (arguments [0 ]) != 0 :
@@ -758,18 +771,35 @@ def register_customevent(event_id, *arguments):
758771 for title in col_titles :
759772 sql_query .append ("`%s`" % title )
760773 sql_query .append ("," )
761- sql_query .pop () # del the last ','
774+ # del the last ','
775+ sql_query .pop ()
762776 sql_query .append (") VALUES (" )
763777 for argument in arguments [0 ]:
764778 sql_query .append ("%s" )
765779 sql_query .append ("," )
766780 sql_param .append (argument )
767- sql_query .pop () # del the last ','
781+ # del the last ','
782+ sql_query .pop ()
768783 sql_query .append (")" )
769784 sql_str = '' .join (sql_query )
770785 run_sql (sql_str , tuple (sql_param ))
786+
787+ # Register the event on elastic search
788+ if CFG_ELASTICSEARCH_LOGGING and event_id in \
789+ CFG_ELASTICSEARCH_EVENTS_MAP .keys ():
790+ # Initialize elastic search handler
791+ elastic_search_parameters = zip (col_titles , arguments [0 ])
792+ event_logger_name = "events.{0}" .format (event_id )
793+ logger = getLogger (event_logger_name )
794+ log_event = {}
795+ for key , value in elastic_search_parameters :
796+ log_event [key ] = value
797+ logger .info (log_event )
771798 else :
772- run_sql ("INSERT INTO %s () VALUES ()" % wash_table_column_name (tbl_name )) # kwalitee: disable=sql
799+ # kwalitee: disable=sql
800+ run_sql (
801+ "INSERT INTO %s () VALUES ()" % wash_table_column_name (tbl_name )
802+ )
773803
774804
775805def cache_keyevent_trend (ids = []):
0 commit comments