Skip to content

Commit 5da83d6

Browse files
committed
WebStat: register custom events on es
1 parent 833cd87 commit 5da83d6

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

modules/webstat/lib/webstat.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
import calendar
2727
from datetime import timedelta
2828
from urllib import quote
29+
from logging import getLogger
2930

3031
from invenio import template
3132
from invenio.config import \
3233
CFG_WEBDIR, \
3334
CFG_TMPDIR, \
3435
CFG_SITE_URL, \
3536
CFG_SITE_LANG, \
36-
CFG_WEBSTAT_BIBCIRCULATION_START_YEAR
37+
CFG_WEBSTAT_BIBCIRCULATION_START_YEAR,
38+
CFG_ELASTICSEARCH_LOGGING
3739
from invenio.webstat_config import CFG_WEBSTAT_CONFIG_PATH
3840
from invenio.bibindex_engine_utils import get_all_indexes
3941
from invenio.bibindex_tokenizers.BibIndexJournalTokenizer import CFG_JOURNAL_TAG
@@ -724,6 +726,7 @@ def destroy_customevents():
724726
msg += destroy_customevent(event[0])
725727
return msg
726728

729+
727730
def register_customevent(event_id, *arguments):
728731
"""
729732
Registers a custom event. Will add to the database's event tables
@@ -739,37 +742,59 @@ def register_customevent(event_id, *arguments):
739742
@param *arguments: The rest of the parameters of the function call
740743
@type *arguments: [params]
741744
"""
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
744749
if not res:
745-
return # the id does not exist
750+
return
746751
tbl_name = res[0][0]
747752
if res[0][1]:
748753
col_titles = cPickle.loads(res[0][1])
749754
else:
750755
col_titles = []
751756
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
753759

754760
# Make sql query
755761
if len(arguments[0]) != 0:
756762
sql_param = []
757763
sql_query = ["INSERT INTO %s (" % wash_table_column_name(tbl_name)]
758764
for title in col_titles:
765+
# update elastic serach keys
759766
sql_query.append("`%s`" % title)
760767
sql_query.append(",")
761-
sql_query.pop() # del the last ','
768+
# del the last ','
769+
sql_query.pop()
762770
sql_query.append(") VALUES (")
763771
for argument in arguments[0]:
772+
# update elastic search values
764773
sql_query.append("%s")
765774
sql_query.append(",")
766775
sql_param.append(argument)
767-
sql_query.pop() # del the last ','
776+
# del the last ','
777+
sql_query.pop()
768778
sql_query.append(")")
769779
sql_str = ''.join(sql_query)
770780
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)
771793
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+
)
773798

774799

775800
def cache_keyevent_trend(ids=[]):

modules/webstat/lib/webstat_config.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@
2121

2222
__revision__ = "$Id$"
2323

24-
from invenio.config import CFG_ETCDIR
24+
from invenio.config import CFG_ETCDIR, CFG_ELASTICSEARCH_LOGGING, CFG_CERN_SITE
2525

2626
CFG_WEBSTAT_CONFIG_PATH = CFG_ETCDIR + "/webstat/webstat.cfg"
27+
28+
if CFG_CERN_SITE:
29+
CFG_ELASTICSEARCH_EVENTS_MAP = {
30+
"loanrequest": {
31+
"_source": {
32+
"enabled": True
33+
},
34+
"properties": {
35+
"request_id": {
36+
"type": "integer"
37+
},
38+
"load_id": {
39+
"type": "integer"
40+
}
41+
}
42+
}
43+
}
44+
else:
45+
CFG_ELASTICSEARCH_CUSTOM_EVENT_MAP = {}
46+
47+
if CFG_ELASTICSEARCH_LOGGING:
48+
from invenio.elasticsearch_logging import register_schema
49+
for customevent_name, arguments in CFG_ELASTICSEARCH_EVENTS_MAP.items():
50+
register_schema(name, arguments)

0 commit comments

Comments
 (0)