Skip to content

Commit

Permalink
Merge pull request #88 from drjova/custom-events
Browse files Browse the repository at this point in the history
WebStat: register custom events on es
  • Loading branch information
egabancho committed Feb 19, 2015
2 parents 49d9931 + 6bdc02c commit 13a0e37
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
48 changes: 39 additions & 9 deletions modules/webstat/lib/webstat.py
Expand Up @@ -26,15 +26,19 @@
import calendar
from datetime import timedelta
from urllib import quote
from logging import getLogger

from invenio import template
from invenio.config import \
CFG_WEBDIR, \
CFG_TMPDIR, \
CFG_SITE_URL, \
CFG_SITE_LANG, \
CFG_WEBSTAT_BIBCIRCULATION_START_YEAR
from invenio.webstat_config import CFG_WEBSTAT_CONFIG_PATH
CFG_WEBSTAT_BIBCIRCULATION_START_YEAR, \
CFG_ELASTICSEARCH_LOGGING
from invenio.webstat_config import \
CFG_WEBSTAT_CONFIG_PATH, \
CFG_ELASTICSEARCH_EVENTS_MAP
from invenio.bibindex_engine_utils import get_all_indexes
from invenio.bibindex_tokenizers.BibIndexJournalTokenizer import CFG_JOURNAL_TAG
from invenio.search_engine import get_coll_i18nname, \
Expand Down Expand Up @@ -724,6 +728,7 @@ def destroy_customevents():
msg += destroy_customevent(event[0])
return msg


def register_customevent(event_id, *arguments):
"""
Registers a custom event. Will add to the database's event tables
Expand All @@ -739,17 +744,25 @@ def register_customevent(event_id, *arguments):
@param *arguments: The rest of the parameters of the function call
@type *arguments: [params]
"""
res = run_sql("SELECT CONCAT('staEVENT', number),cols " + \
"FROM staEVENT WHERE id = %s", (event_id, ))
query = """
SELECT CONCAT('staEVENT', number),
cols
FROM staEVENT
WHERE id = %s
"""
params = (event_id,)
res = run_sql(query, params)
# the id does not exist
if not res:
return # the id does not exist
return
tbl_name = res[0][0]
if res[0][1]:
col_titles = cPickle.loads(res[0][1])
else:
col_titles = []
if len(col_titles) != len(arguments[0]):
return # there is different number of arguments than cols
# there is different number of arguments than cols
return

# Make sql query
if len(arguments[0]) != 0:
Expand All @@ -758,18 +771,35 @@ def register_customevent(event_id, *arguments):
for title in col_titles:
sql_query.append("`%s`" % title)
sql_query.append(",")
sql_query.pop() # del the last ','
# del the last ','
sql_query.pop()
sql_query.append(") VALUES (")
for argument in arguments[0]:
sql_query.append("%s")
sql_query.append(",")
sql_param.append(argument)
sql_query.pop() # del the last ','
# del the last ','
sql_query.pop()
sql_query.append(")")
sql_str = ''.join(sql_query)
run_sql(sql_str, tuple(sql_param))

# Register the event on elastic search
if CFG_ELASTICSEARCH_LOGGING and event_id in \
CFG_ELASTICSEARCH_EVENTS_MAP.keys():
# Initialize elastic search handler
elastic_search_parameters = zip(col_titles, arguments[0])
event_logger_name = "events.{0}".format(event_id)
logger = getLogger(event_logger_name)
log_event = {}
for key, value in elastic_search_parameters:
log_event[key] = value
logger.info(log_event)
else:
run_sql("INSERT INTO %s () VALUES ()" % wash_table_column_name(tbl_name)) # kwalitee: disable=sql
# kwalitee: disable=sql
run_sql(
"INSERT INTO %s () VALUES ()" % wash_table_column_name(tbl_name)
)


def cache_keyevent_trend(ids=[]):
Expand Down
26 changes: 25 additions & 1 deletion modules/webstat/lib/webstat_config.py
Expand Up @@ -21,6 +21,30 @@

__revision__ = "$Id$"

from invenio.config import CFG_ETCDIR
from invenio.config import CFG_ETCDIR, CFG_ELASTICSEARCH_LOGGING, CFG_CERN_SITE

CFG_WEBSTAT_CONFIG_PATH = CFG_ETCDIR + "/webstat/webstat.cfg"

if CFG_CERN_SITE:
CFG_ELASTICSEARCH_EVENTS_MAP = {
"events.loanrequest": {
"_source": {
"enabled": True
},
"properties": {
"request_id": {
"type": "integer"
},
"load_id": {
"type": "integer"
}
}
}
}
else:
CFG_ELASTICSEARCH_EVENTS_MAP = {}

if CFG_ELASTICSEARCH_LOGGING:
from invenio.elasticsearch_logging import register_schema
for name, arguments in CFG_ELASTICSEARCH_EVENTS_MAP.items():
register_schema(name, arguments)

0 comments on commit 13a0e37

Please sign in to comment.