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
+ CFG_WEBSTAT_BIBCIRCULATION_START_YEAR ,
38
+ CFG_ELASTICSEARCH_LOGGING
37
39
from invenio .webstat_config import CFG_WEBSTAT_CONFIG_PATH
38
40
from invenio .bibindex_engine_utils import get_all_indexes
39
41
from invenio .bibindex_tokenizers .BibIndexJournalTokenizer import CFG_JOURNAL_TAG
@@ -724,6 +726,7 @@ def destroy_customevents():
724
726
msg += destroy_customevent (event [0 ])
725
727
return msg
726
728
729
+
727
730
def register_customevent (event_id , * arguments ):
728
731
"""
729
732
Registers a custom event. Will add to the database's event tables
@@ -739,37 +742,59 @@ def register_customevent(event_id, *arguments):
739
742
@param *arguments: The rest of the parameters of the function call
740
743
@type *arguments: [params]
741
744
"""
742
- res = run_sql ("SELECT CONCAT('staEVENT', number),cols " + \
743
- "FROM staEVENT WHERE id = %s" , (event_id , ))
745
+ query = ("SELECT CONCAT('staEVENT', number),cols FROM staEVENT WHERE"
746
+ " id = {0}" ).format (event_id )
747
+ res = run_sql (query )
748
+ # the id does not exist
744
749
if not res :
745
- return # the id does not exist
750
+ return
746
751
tbl_name = res [0 ][0 ]
747
752
if res [0 ][1 ]:
748
753
col_titles = cPickle .loads (res [0 ][1 ])
749
754
else :
750
755
col_titles = []
751
756
if len (col_titles ) != len (arguments [0 ]):
752
- return # there is different number of arguments than cols
757
+ # there is different number of arguments than cols
758
+ return
753
759
754
760
# Make sql query
755
761
if len (arguments [0 ]) != 0 :
756
762
sql_param = []
757
763
sql_query = ["INSERT INTO %s (" % wash_table_column_name (tbl_name )]
758
764
for title in col_titles :
765
+ # update elastic serach keys
759
766
sql_query .append ("`%s`" % title )
760
767
sql_query .append ("," )
761
- sql_query .pop () # del the last ','
768
+ # del the last ','
769
+ sql_query .pop ()
762
770
sql_query .append (") VALUES (" )
763
771
for argument in arguments [0 ]:
772
+ # update elastic search values
764
773
sql_query .append ("%s" )
765
774
sql_query .append ("," )
766
775
sql_param .append (argument )
767
- sql_query .pop () # del the last ','
776
+ # del the last ','
777
+ sql_query .pop ()
768
778
sql_query .append (")" )
769
779
sql_str = '' .join (sql_query )
770
780
run_sql (sql_str , tuple (sql_param ))
781
+
782
+ # Register the event on
783
+ if CFG_ELASTICSEARCH_LOGGING and event_id in \
784
+ CFG_ELASTICSEARCH_EVENTS_MAP .keys ():
785
+ # Initialize elastic search handler
786
+ elastic_search_parameters = zip (col_titles , arguments [0 ])
787
+ event_logger_name = "events.{0}" .fomrat (event_id )
788
+ logger = getLogger (event_logger_name )
789
+ log_event = {}
790
+ for key , value in elastic_search_parameters :
791
+ log_event [key ] = value
792
+ logger .info (log_event )
771
793
else :
772
- run_sql ("INSERT INTO %s () VALUES ()" % wash_table_column_name (tbl_name )) # kwalitee: disable=sql
794
+ # kwalitee: disable=sql
795
+ run_sql (
796
+ "INSERT INTO %s () VALUES ()" % wash_table_column_name (tbl_name )
797
+ )
773
798
774
799
775
800
def cache_keyevent_trend (ids = []):
0 commit comments