diff --git a/modules/invenio-stats/invenio_stats/queries.py b/modules/invenio-stats/invenio_stats/queries.py index 54ceaf8871..959ad22c64 100644 --- a/modules/invenio-stats/invenio_stats/queries.py +++ b/modules/invenio-stats/invenio_stats/queries.py @@ -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) @@ -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}) @@ -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}) @@ -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}) @@ -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')) @@ -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 \ No newline at end of file + + return query_result diff --git a/modules/invenio-stats/tests/conftest.py b/modules/invenio-stats/tests/conftest.py index dac1d3b273..6bedc74341 100644 --- a/modules/invenio-stats/tests/conftest.py +++ b/modules/invenio-stats/tests/conftest.py @@ -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 @@ -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://'), @@ -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')]): @@ -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}, @@ -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 @@ -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')) @@ -1190,7 +1193,7 @@ def index(app, db): "parent": 0, "value": "index_{}".format(i) }) - + return Index.query.all() @pytest.fixture() @@ -1263,4 +1266,4 @@ def index_issn(app,db): db.session.add(index_metadata5) db.session.add(index_metadata6) - db.session.commit() \ No newline at end of file + db.session.commit()