26
26
import calendar
27
27
from datetime import timedelta
28
28
from urllib import quote
29
+ from logging import getLogger
29
30
30
31
from invenio import template
31
32
from invenio .config import \
32
33
CFG_WEBDIR , \
33
34
CFG_TMPDIR , \
34
35
CFG_SITE_URL , \
35
36
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
38
42
from invenio .bibindex_engine_utils import get_all_indexes
39
43
from invenio .bibindex_tokenizers .BibIndexJournalTokenizer import CFG_JOURNAL_TAG
40
44
from invenio .search_engine import get_coll_i18nname , \
@@ -724,6 +728,7 @@ def destroy_customevents():
724
728
msg += destroy_customevent (event [0 ])
725
729
return msg
726
730
731
+
727
732
def register_customevent (event_id , * arguments ):
728
733
"""
729
734
Registers a custom event. Will add to the database's event tables
@@ -739,17 +744,25 @@ def register_customevent(event_id, *arguments):
739
744
@param *arguments: The rest of the parameters of the function call
740
745
@type *arguments: [params]
741
746
"""
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
744
756
if not res :
745
- return # the id does not exist
757
+ return
746
758
tbl_name = res [0 ][0 ]
747
759
if res [0 ][1 ]:
748
760
col_titles = cPickle .loads (res [0 ][1 ])
749
761
else :
750
762
col_titles = []
751
763
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
753
766
754
767
# Make sql query
755
768
if len (arguments [0 ]) != 0 :
@@ -758,18 +771,35 @@ def register_customevent(event_id, *arguments):
758
771
for title in col_titles :
759
772
sql_query .append ("`%s`" % title )
760
773
sql_query .append ("," )
761
- sql_query .pop () # del the last ','
774
+ # del the last ','
775
+ sql_query .pop ()
762
776
sql_query .append (") VALUES (" )
763
777
for argument in arguments [0 ]:
764
778
sql_query .append ("%s" )
765
779
sql_query .append ("," )
766
780
sql_param .append (argument )
767
- sql_query .pop () # del the last ','
781
+ # del the last ','
782
+ sql_query .pop ()
768
783
sql_query .append (")" )
769
784
sql_str = '' .join (sql_query )
770
785
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 )
771
798
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
+ )
773
803
774
804
775
805
def cache_keyevent_trend (ids = []):
0 commit comments