Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions modules/invenio-stats/invenio_stats/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,22 @@ def build_query(self, interval, start_date, end_date, **kwargs):
for modifier in self.query_modifiers:
agg_query = modifier(agg_query, **kwargs)

base_agg = agg_query.aggs.bucket(
'histogram',
'date_histogram',
field=self.time_field,
interval=interval,
time_zone=current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']
)
try:
base_agg = agg_query.aggs.bucket(
'histogram',
'date_histogram',
field=self.time_field,
interval=interval,
time_zone=str(current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']())
)
except AttributeError:
base_agg = agg_query.aggs.bucket(
'histogram',
'date_histogram',
field=self.time_field,
interval=interval,
time_zone=current_app.config.get('BABEL_DEFAULT_TIMEZONE', 'Asia/Tokyo')
)

for destination, (metric, field, opts) in self.metric_fields.items():
base_agg.metric(destination, metric, field=field, **opts)
Expand Down Expand Up @@ -251,7 +260,11 @@ def build_query(self, start_date, end_date, **kwargs):
time_range['gte'] = start_date.isoformat()
if end_date:
time_range['lte'] = end_date.isoformat()
time_range['time_zone'] = current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']
try:
time_range['time_zone'] = str(
current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']())
except AttributeError:
time_range['time_zone'] = current_app.config.get('BABEL_DEFAULT_TIMEZONE', 'Asia/Tokyo')
agg_query = agg_query.filter(
'range',
**{self.time_field: time_range})
Expand Down Expand Up @@ -430,7 +443,11 @@ def build_query(self, start_date, end_date, **kwargs):
time_range['gte'] = start_date.isoformat()
if end_date:
time_range['lte'] = end_date.isoformat()
time_range['time_zone'] = current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']
try:
time_range['time_zone'] = str(
current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']())
except AttributeError:
time_range['time_zone'] = current_app.config.get('BABEL_DEFAULT_TIMEZONE', 'Asia/Tokyo')
agg_query = agg_query.filter(
'range',
**{self.time_field: time_range})
Expand Down Expand Up @@ -489,8 +506,12 @@ def build_query(self, start_date, end_date, **kwargs):
time_range['gte'] = start_date.isoformat()
if end_date is not None:
time_range['lte'] = end_date.isoformat()
time_range['time_zone'] = current_app.config[
'STATS_WEKO_DEFAULT_TIMEZONE']
try:
time_range['time_zone'] = str(
current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']())
except AttributeError:
time_range['time_zone'] = current_app.config.get('BABEL_DEFAULT_TIMEZONE', 'Asia/Tokyo')

agg_query = agg_query.filter(
'range',
**{self.time_field: time_range})
Expand Down Expand Up @@ -574,9 +595,14 @@ def build_query(self, **kwargs):
query_q = query_q.replace(
"@{}".format(_field), kwargs.get(_field, ""))

query_q = query_q.replace(
"@time_zone", current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']
)
try:
query_q = query_q.replace(
"@time_zone", str(current_app.config['STATS_WEKO_DEFAULT_TIMEZONE']())
)
except AttributeError:
query_q = query_q.replace(
"@time_zone", current_app.config.get('BABEL_DEFAULT_TIMEZONE', 'Asia/Tokyo')
)
query_q = orjson.loads(query_q)
if kwargs.get("must_not"):
query_q['query']['bool']['must_not'] = orjson.loads(kwargs.get('must_not'))
Expand All @@ -600,5 +626,5 @@ def run(self, start_date=None, end_date=None, **kwargs):
agg_query = self.build_query(**kwargs)

query_result = agg_query.execute().to_dict()
return query_result

return query_result
13 changes: 8 additions & 5 deletions modules/invenio-stats/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from flask import Flask, appcontext_pushed, g
from flask.cli import ScriptInfo
from flask_celeryext import FlaskCeleryExt
from flask_babelex import Babel

from invenio_access import InvenioAccess
from invenio_accounts import InvenioAccounts, InvenioAccountsREST
Expand Down Expand Up @@ -281,6 +282,7 @@ def base_app(instance_path, mock_gethostbyaddr, search_class):
CACHE_REDIS_URL="redis://redis:6379/0",
CACHE_REDIS_DB=0,
CACHE_REDIS_HOST="redis",
BABEL_DEFAULT_TIMEZONE='Asia/Tokyo',
QUEUES_BROKER_URL="amqp://guest:guest@rabbitmq:5672//",
# SQLALCHEMY_DATABASE_URI=os.environ.get(
# 'SQLALCHEMY_DATABASE_URI', 'sqlite://'),
Expand Down Expand Up @@ -357,6 +359,7 @@ def app(base_app):

@pytest.yield_fixture()
def i18n_app(app):
Babel(app)
InvenioI18N(app)
with app.test_request_context(
headers=[('Accept-Language','ja')]):
Expand Down Expand Up @@ -499,7 +502,7 @@ def role_users(app, db):
ds.add_role_to_user(originalroleuser, originalrole)
ds.add_role_to_user(originalroleuser2, originalrole)
ds.add_role_to_user(originalroleuser2, repoadmin_role)


return [
{"email": contributor.email, "id": contributor.id, "obj": contributor},
Expand Down Expand Up @@ -563,7 +566,7 @@ def exists(self, index, **kwargs):
return False
def flush(self,index):
pass

def search(self,index,doc_type,body,**kwargs):
pass

Expand Down Expand Up @@ -1019,7 +1022,7 @@ def base_event(id, event_type):
source=json.dumps({'test': 'test'}),
date=datetime.datetime(2023, 1, 1, 1, 0, 0)
)

try:
with db.session.begin_nested():
db.session.add(base_event(1, 'top-view'))
Expand Down Expand Up @@ -1190,7 +1193,7 @@ def index(app, db):
"parent": 0,
"value": "index_{}".format(i)
})

return Index.query.all()

@pytest.fixture()
Expand Down Expand Up @@ -1263,4 +1266,4 @@ def index_issn(app,db):
db.session.add(index_metadata5)
db.session.add(index_metadata6)

db.session.commit()
db.session.commit()
Loading