Skip to content

Commit 13a0e37

Browse files
committed
Merge pull request #88 from drjova/custom-events
WebStat: register custom events on es
2 parents 49d9931 + 6bdc02c commit 13a0e37

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

modules/webstat/lib/webstat.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@
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-
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
3842
from invenio.bibindex_engine_utils import get_all_indexes
3943
from invenio.bibindex_tokenizers.BibIndexJournalTokenizer import CFG_JOURNAL_TAG
4044
from 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+
727732
def 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

775805
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+
"events.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_EVENTS_MAP = {}
46+
47+
if CFG_ELASTICSEARCH_LOGGING:
48+
from invenio.elasticsearch_logging import register_schema
49+
for name, arguments in CFG_ELASTICSEARCH_EVENTS_MAP.items():
50+
register_schema(name, arguments)

0 commit comments

Comments
 (0)