Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): detach console statistics admission (#44411)
非法操作e3ef66b8b994
diff --git a/api/controllers/console/app/statistic.py b/api/controllers/console/app/statistic.pyindex 195af05e5fa..2a61e2ff77f 100644--- a/api/controllers/console/app/statistic.py+++ b/api/controllers/console/app/statistic.py@@ -1,23 +1,27 @@from datetime import datetimefrom decimal import Decimal+from http import HTTPStatus+from uuid import UUIDfrom flask_restx import Resourcefrom pydantic import BaseModel, Field, field_validator-from werkzeug.exceptions import BadRequest+from controllers.common.errors import InvalidArgumentErrorfrom controllers.common.rbac import PlainApp, RBACCheckfrom controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_modelsfrom controllers.console import console_ns-from controllers.console.app.wraps import get_app_model+from controllers.console.app.error import AppNotFoundErrorfrom controllers.console.flask_admission import console_account_admissionfrom controllers.console.wraps import RBACPermission, model_validatefrom extensions.ext_application_services import application_servicesfrom fields.base import ResponseModelfrom libs.datetime_utils import parse_time_range+from libs.external_api import error_responsesfrom libs.helper import dump_responsefrom libs.login import current_account_with_tenantfrom machinery.context import RequestContext-from models.model import App, AppMode+from models.model import AppMode+from services.app.console_service import ConsoleAppNotFoundErrorclass StatisticTimeRangeQuery(BaseModel):@@ -143,7 +147,7 @@ def _resolve_statistic_time_range(try:start_date, end_date = parse_time_range(req_data.start, req_data.end, timezone)except ValueError as error:- raise BadRequest(str(error)) from error+ raise InvalidArgumentError(str(error)) from errorreturn start_date, end_date, timezone@@ -154,19 +158,25 @@ class DailyMessageStatistic(Resource):@console_ns.doc(description="Get daily message statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Daily message statistics retrieved successfully",console_ns.models[DailyMessageStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_daily_messages(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -181,19 +191,25 @@ class DailyConversationStatistic(Resource):@console_ns.doc(description="Get daily conversation statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Daily conversation statistics retrieved successfully",console_ns.models[DailyConversationStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_daily_conversations(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -208,19 +224,25 @@ class DailyTerminalsStatistic(Resource):@console_ns.doc(description="Get daily terminal/end-user statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Daily terminal statistics retrieved successfully",console_ns.models[DailyTerminalStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_daily_terminals(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -235,19 +257,25 @@ class DailyTokenCostStatistic(Resource):@console_ns.doc(description="Get daily token cost statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Daily token cost statistics retrieved successfully",console_ns.models[DailyTokenCostStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_daily_token_costs(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -262,19 +290,29 @@ class AverageSessionInteractionStatistic(Resource):@console_ns.doc(description="Get average session interaction statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Average session interaction statistics retrieved successfully",console_ns.models[AverageSessionInteractionStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from error+ supported_modes = (AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT)+ if app.mode not in supported_modes:+ mode_values = {mode.value for mode in supported_modes}+ raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")start_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_average_session_interactions(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -289,19 +327,25 @@ class UserSatisfactionRateStatistic(Resource):@console_ns.doc(description="Get user satisfaction rate statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"User satisfaction rate statistics retrieved successfully",console_ns.models[UserSatisfactionRateStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_user_satisfaction_rates(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -316,19 +360,29 @@ class AverageResponseTimeStatistic(Resource):@console_ns.doc(description="Get average response time statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Average response time statistics retrieved successfully",console_ns.models[AverageResponseTimeStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model(mode=AppMode.COMPLETION)@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from error+ supported_modes = (AppMode.COMPLETION,)+ if app.mode not in supported_modes:+ mode_values = {mode.value for mode in supported_modes}+ raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")start_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_average_response_times(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -343,19 +397,25 @@ class TokensPerSecondStatistic(Resource):@console_ns.doc(description="Get tokens per second statistics for an application")@console_ns.doc(params={"app_id": "Application ID", **query_params_from_model(StatisticTimeRangeQuery)})@console_ns.response(- 200,+ HTTPStatus.OK,"Tokens per second statistics retrieved successfully",console_ns.models[TokensPerSecondStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(StatisticTimeRangeQuery)- def get(self, req_data: StatisticTimeRangeQuery, _request_context: RequestContext, app_model: App):+ def get(+ self, req_data: StatisticTimeRangeQuery, request_context: RequestContext, app_id: UUID+ ) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().app_statistics.get_tokens_per_second(- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,diff --git a/api/controllers/console/app/workflow_statistic.py b/api/controllers/console/app/workflow_statistic.pyindex af5f057dccf..38d3b1dfaea 100644--- a/api/controllers/console/app/workflow_statistic.py+++ b/api/controllers/console/app/workflow_statistic.py@@ -1,13 +1,15 @@from datetime import datetime+from http import HTTPStatus+from uuid import UUIDfrom flask_restx import Resourcefrom pydantic import BaseModel, Field, field_validator-from werkzeug.exceptions import BadRequest+from controllers.common.errors import InvalidArgumentErrorfrom controllers.common.rbac import PlainApp, RBACCheckfrom controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_modelsfrom controllers.console import console_ns-from controllers.console.app.wraps import get_app_model+from controllers.console.app.error import AppNotFoundErrorfrom controllers.console.flask_admission import console_account_admissionfrom controllers.console.wraps import (RBACPermission,@@ -16,10 +18,12 @@ from controllers.console.wraps import (from extensions.ext_application_services import application_servicesfrom fields.base import ResponseModelfrom libs.datetime_utils import parse_time_range+from libs.external_api import error_responsesfrom libs.helper import dump_responsefrom libs.login import current_account_with_tenantfrom machinery.context import RequestContext-from models.model import App, AppMode+from models.model import AppMode+from services.app.console_service import ConsoleAppNotFoundErrorclass WorkflowStatisticQuery(BaseModel):@@ -89,7 +93,7 @@ def _resolve_statistic_time_range(try:start_date, end_date = parse_time_range(req_data.start, req_data.end, timezone)except ValueError as error:- raise BadRequest(str(error)) from error+ raise InvalidArgumentError(str(error)) from errorreturn start_date, end_date, timezone@@ -101,20 +105,24 @@ class WorkflowDailyRunsStatistic(Resource):@console_ns.doc(params={"app_id": "Application ID"})@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))@console_ns.response(- 200,+ HTTPStatus.OK,"Daily runs statistics retrieved successfully",console_ns.models[WorkflowDailyRunsStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(WorkflowStatisticQuery)- def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_model: App):+ def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().workflow_statistics.get_daily_runs(request_context,- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -130,20 +138,24 @@ class WorkflowDailyTerminalsStatistic(Resource):@console_ns.doc(params={"app_id": "Application ID"})@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))@console_ns.response(- 200,+ HTTPStatus.OK,"Daily terminals statistics retrieved successfully",console_ns.models[WorkflowDailyTerminalsStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(WorkflowStatisticQuery)- def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_model: App):+ def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().workflow_statistics.get_daily_terminals(request_context,- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -159,20 +171,24 @@ class WorkflowDailyTokenCostStatistic(Resource):@console_ns.doc(params={"app_id": "Application ID"})@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))@console_ns.response(- 200,+ HTTPStatus.OK,"Daily token cost statistics retrieved successfully",console_ns.models[WorkflowDailyTokenCostStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model@model_validate(WorkflowStatisticQuery)- def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_model: App):+ def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from errorstart_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().workflow_statistics.get_daily_token_costs(request_context,- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,@@ -188,20 +204,28 @@ class WorkflowAverageAppInteractionStatistic(Resource):@console_ns.doc(params={"app_id": "Application ID"})@console_ns.doc(params=query_params_from_model(WorkflowStatisticQuery))@console_ns.response(- 200,+ HTTPStatus.OK,"Average app interaction statistics retrieved successfully",console_ns.models[WorkflowAverageAppInteractionStatisticResponse.__name__],)+ @error_responses(AppNotFoundError)@console_account_admission(rbac_checks=[RBACCheck(RBACPermission.APP_MONITOR, PlainApp())],)- @get_app_model(mode=[AppMode.WORKFLOW])@model_validate(WorkflowStatisticQuery)- def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_model: App):+ def get(self, req_data: WorkflowStatisticQuery, request_context: RequestContext, app_id: UUID) -> dict[str, object]:+ try:+ app = application_services().apps.console.get_reference(request_context, str(app_id))+ except ConsoleAppNotFoundError as error:+ raise AppNotFoundError() from error+ supported_modes = (AppMode.WORKFLOW,)+ if app.mode not in supported_modes:+ mode_values = {mode.value for mode in supported_modes}+ raise AppNotFoundError(f"App mode is not in the supported list: {mode_values}")start_date, end_date, timezone = _resolve_statistic_time_range(req_data)response_data = application_services().workflow_statistics.get_average_app_interactions(request_context,- app_id=app_model.id,+ app_id=app.id,start_date=start_date,end_date=end_date,timezone=timezone,diff --git a/api/tests/e2e_tests/containers/controllers/console/app/test_statistic.py b/api/tests/e2e_tests/containers/controllers/console/app/test_statistic.pyindex 841d25f02bb..62efd5ad157 100644--- a/api/tests/e2e_tests/containers/controllers/console/app/test_statistic.py+++ b/api/tests/e2e_tests/containers/controllers/console/app/test_statistic.py@@ -1,8 +1,7 @@"""Controller integration tests for console statistic routes."""-from datetime import timedelta+from datetime import datetime, timedeltafrom decimal import Decimal-from unittest.mock import patchfrom uuid import uuid4from flask.testing import FlaskClient@@ -302,32 +301,49 @@ def test_invalid_time_range(account, tenant = create_console_account_and_tenant(db_session_with_containers)app = create_console_app(db_session_with_containers, tenant.id, account.id, AppMode.CHAT)- with patch("controllers.console.app.statistic.parse_time_range", side_effect=ValueError("Invalid time")):- response = test_client_with_containers.get(- f"/console/api/apps/{app.id}/statistics/daily-messages?start=invalid&end=invalid",- headers=authenticate_console_client(test_client_with_containers, account),- )+ response = test_client_with_containers.get(+ f"/console/api/apps/{app.id}/statistics/daily-messages",+ query_string={"start": "invalid", "end": "invalid"},+ headers=authenticate_console_client(test_client_with_containers, account),+ )assert response.status_code == 400- assert response.get_json()["message"] == "Invalid time"+ assert response.get_json()["code"] == "invalid_param"+ assert response.get_json()["message"].startswith("Invalid start time format:")-def test_time_range_params_passed(+def test_daily_messages_use_account_timezone_and_exclude_other_apps(db_session_with_containers: Session,test_client_with_containers: FlaskClient,) -> None:- import datetime-account, tenant = create_console_account_and_tenant(db_session_with_containers)+ account.timezone = "Asia/Shanghai"+ db_session_with_containers.commit()app = create_console_app(db_session_with_containers, tenant.id, account.id, AppMode.CHAT)- start = datetime.datetime.now()- end = datetime.datetime.now()+ conversation = _create_conversation(db_session_with_containers, app.id, account.id, mode=app.mode)+ for created_at in (+ datetime(2026, 9, 30, 15, 59), # Before the local start date.+ datetime(2026, 9, 30, 16), # Local midnight: included.+ datetime(2026, 10, 1, 15, 59),+ datetime(2026, 10, 1, 16), # Local end date: excluded.+ ):+ message = _create_message(db_session_with_containers, app.id, conversation.id, from_account_id=account.id)+ message.created_at = created_at+ db_session_with_containers.commit()++ other_app = create_console_app(db_session_with_containers, tenant.id, account.id, AppMode.CHAT)+ other_conversation = _create_conversation(db_session_with_containers, other_app.id, account.id, mode=other_app.mode)+ other_message = _create_message(+ db_session_with_containers, other_app.id, other_conversation.id, from_account_id=account.id+ )+ other_message.created_at = datetime(2026, 10, 1, 8)+ db_session_with_containers.commit()- with patch("controllers.console.app.statistic.parse_time_range", return_value=(start, end)) as mock_parse:- response = test_client_with_containers.get(- f"/console/api/apps/{app.id}/statistics/daily-messages?start=something&end=something",- headers=authenticate_console_client(test_client_with_containers, account),- )+ response = test_client_with_containers.get(+ f"/console/api/apps/{app.id}/statistics/daily-messages",+ query_string={"start": "2026-10-01 00:00", "end": "2026-10-02 00:00"},+ headers=authenticate_console_client(test_client_with_containers, account),+ )assert response.status_code == 200- mock_parse.assert_called_once_with("something", "something", "UTC")+ assert response.get_json() == {"data": [{"date": "2026-10-01", "message_count": 2}]}diff --git a/api/tests/e2e_tests/containers/controllers/console/app/test_workflow_statistic.py b/api/tests/e2e_tests/containers/controllers/console/app/test_workflow_statistic.pynew file mode 100644index 00000000000..5538ffe173f--- /dev/null+++ b/api/tests/e2e_tests/containers/controllers/console/app/test_workflow_statistic.py@@ -0,0 +1,112 @@+"""Exercise workflow statistics through console admission and PostgreSQL."""++from datetime import datetime+from uuid import uuid4++import pytest+from flask.testing import FlaskClient+from sqlalchemy.orm import Session++from graphon.enums import WorkflowExecutionStatus+from models.enums import CreatorUserRole, WorkflowRunTriggeredFrom+from models.model import AppMode+from models.workflow import WorkflowRun, WorkflowType+from tests.e2e_tests.containers.controllers.console.helpers import (+ authenticate_console_client,+ create_console_account_and_tenant,+ create_console_app,+)+++def _create_workflow_run(+ session: Session,+ *,+ tenant_id: str,+ app_id: str,+ created_by: str,+ created_at: datetime,+ total_tokens: int,+ triggered_from: WorkflowRunTriggeredFrom = WorkflowRunTriggeredFrom.APP_RUN,+) -> None:+ run = WorkflowRun(+ tenant_id=tenant_id,+ app_id=app_id,+ workflow_id=str(uuid4()),+ type=WorkflowType.WORKFLOW,+ triggered_from=triggered_from,+ version="1",+ graph="{}",+ inputs="{}",+ status=WorkflowExecutionStatus.SUCCEEDED,+ total_tokens=total_tokens,+ created_by_role=CreatorUserRole.ACCOUNT,+ created_by=created_by,+ )+ run.created_at = created_at+ session.add(run)+++ ("endpoint", "metric", "expected"),+ [+ ("daily-conversations", "runs", 3),+ ("daily-terminals", "terminal_count", 2),+ ("token-costs", "token_count", 60),+ ("average-app-interactions", "interactions", 1.5),+ ],+)+def test_workflow_statistics_scope_and_local_date_range(+ db_session_with_containers: Session,+ test_client_with_containers: FlaskClient,+ endpoint: str,+ metric: str,+ expected: int | float,+) -> None:+ account, tenant = create_console_account_and_tenant(db_session_with_containers)+ account.timezone = "Asia/Shanghai"+ db_session_with_containers.commit()+ app = create_console_app(db_session_with_containers, tenant.id, account.id, AppMode.WORKFLOW)+ other_app = create_console_app(db_session_with_containers, tenant.id, account.id, AppMode.WORKFLOW)+ other_account, other_tenant = create_console_account_and_tenant(db_session_with_containers)++ for created_by, created_at, tokens in (+ (account.id, datetime(2026, 9, 30, 16), 10),+ (account.id, datetime(2026, 10, 1, 8), 20),+ (other_account.id, datetime(2026, 10, 1, 15, 59), 30),+ ):+ _create_workflow_run(+ db_session_with_containers,+ tenant_id=tenant.id,+ app_id=app.id,+ created_by=created_by,+ created_at=created_at,+ total_tokens=tokens,+ )++ # Each decoy is independently excluded by the repository's scope or range.+ for tenant_id, app_id, triggered_from, created_at in (+ (tenant.id, other_app.id, WorkflowRunTriggeredFrom.APP_RUN, datetime(2026, 10, 1, 8)),+ (other_tenant.id, app.id, WorkflowRunTriggeredFrom.APP_RUN, datetime(2026, 10, 1, 8)),+ (tenant.id, app.id, WorkflowRunTriggeredFrom.DEBUGGING, datetime(2026, 10, 1, 8)),+ (tenant.id, app.id, WorkflowRunTriggeredFrom.APP_RUN, datetime(2026, 9, 30, 15, 59)),+ (tenant.id, app.id, WorkflowRunTriggeredFrom.APP_RUN, datetime(2026, 10, 1, 16)),+ ):+ _create_workflow_run(+ db_session_with_containers,+ tenant_id=tenant_id,+ app_id=app_id,+ created_by=account.id,+ created_at=created_at,+ total_tokens=100,+ triggered_from=triggered_from,+ )+ db_session_with_containers.commit()++ response = test_client_with_containers.get(+ f"/console/api/apps/{app.id}/workflow/statistics/{endpoint}",+ query_string={"start": "2026-10-01 00:00", "end": "2026-10-02 00:00"},+ headers=authenticate_console_client(test_client_with_containers, account),+ )++ assert response.status_code == 200+ assert response.get_json() == {"data": [{"date": "2026-10-01", metric: expected}]}diff --git a/api/tests/unit_tests/controllers/console/app/test_app.py b/api/tests/unit_tests/controllers/console/app/test_app.pyindex 0436472cd8c..f607a903fe3 100644--- a/api/tests/unit_tests/controllers/console/app/test_app.py+++ b/api/tests/unit_tests/controllers/console/app/test_app.py@@ -54,9 +54,6 @@ from controllers.console.app import (from controllers.console.app import (workflow_draft_variable as workflow_draft_variable_module,)-from controllers.console.app import (- workflow_statistic as workflow_statistic_module,-)from controllers.console.app import (wraps as wraps_module,)@@ -77,7 +74,6 @@ from controllers.console.app.workflow_draft_variable import (WorkflowDraftVariableListQuery,WorkflowDraftVariableUpdatePayload,)-from controllers.console.app.workflow_statistic import WorkflowStatisticQueryfrom fields import app_fieldsfrom fields.app_model_config_response import AppModelConfigResponsefrom libs.login import AccountWithTenant@@ -648,180 +644,6 @@ class TestAppApisCases:assert captured["account"].id == USER_IDassert captured["session"].get_bind() is db.engine- class TestWorkflowStatisticEndpoints:- def test_workflow_statistic_time_range(self):- query = WorkflowStatisticQuery(start="2024-01-01", end="2024-12-31")- assert query.start == "2024-01-01"-- def test_workflow_statistic_blank_to_none(self):- query = WorkflowStatisticQuery(start="", end="")- assert query.start is None- assert query.end is None-- def test_workflow_daily_runs_statistic(self, database_app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- service = MagicMock()- service.get_daily_runs.return_value = [{"date": "2024-01-01", "runs": 2}]- monkeypatch.setattr(- workflow_statistic_module,- "application_services",- lambda: SimpleNamespace(workflow_statistics=service),- )- self._patch_statistic_time_range(monkeypatch)-- api = workflow_statistic_module.WorkflowDailyRunsStatistic()- method = unwrap(api.get)- request_context = _make_request_context()-- with database_app.test_request_context("/"):- response = method(- api,- WorkflowStatisticQuery(),- request_context,- app_model=_make_app("app-1", tenant_id="t1"),- )-- assert response == {"data": [{"date": "2024-01-01", "runs": 2}]}- service.get_daily_runs.assert_called_once_with(- request_context,- app_id="app-1",- start_date=None,- end_date=None,- timezone="UTC",- )-- def test_workflow_daily_terminals_statistic(self, database_app: Flask, monkeypatch: pytest.MonkeyPatch) -> None:- service = MagicMock()- service.get_daily_terminals.return_value = [{"date": "2024-01-02", "terminal_count": 3}]- monkeypatch.setattr(- workflow_statistic_module,- "application_services",- lambda: SimpleNamespace(workflow_statistics=service),- )- self._patch_statistic_time_range(monkeypatch)-- api = workflow_statistic_module.WorkflowDailyTerminalsStatistic()- method = unwrap(api.get)- request_context = _make_request_context()-- with database_app.test_request_context("/"):- response = method(- api,- WorkflowStatisticQuery(),- request_context,- app_model=_make_app("app-1", tenant_id="t1"),- )-- assert response == {"data": [{"date": "2024-01-02", "terminal_count": 3}]}- service.get_daily_terminals.assert_called_once_with(- request_context,- app_id="app-1",- start_date=None,- end_date=None,- timezone="UTC",- )-- def test_workflow_daily_token_cost_statistic(- self,- database_app: Flask,- monkeypatch: pytest.MonkeyPatch,- ) -> None:- service = MagicMock()- service.get_daily_token_costs.return_value = [{"date": "2024-01-03", "token_count": 4}]- monkeypatch.setattr(- workflow_statistic_module,- "application_services",- lambda: SimpleNamespace(workflow_statistics=service),- )- self._patch_statistic_time_range(monkeypatch)-- api = workflow_statistic_module.WorkflowDailyTokenCostStatistic()- method = unwrap(api.get)- request_context = _make_request_context()-- with database_app.test_request_context("/"):- response = method(- api,- WorkflowStatisticQuery(),- request_context,- app_model=_make_app("app-1", tenant_id="t1"),- )-- assert response == {"data": [{"date": "2024-01-03", "token_count": 4}]}- service.get_daily_token_costs.assert_called_once_with(- request_context,- app_id="app-1",- start_date=None,- end_date=None,- timezone="UTC",- )-- def test_workflow_average_app_interaction_statistic(- self,- database_app: Flask,- monkeypatch: pytest.MonkeyPatch,- ) -> None:- service = MagicMock()- service.get_average_app_interactions.return_value = [{"date": "2024-01-04", "interactions": 2.5}]- monkeypatch.setattr(- workflow_statistic_module,- "application_services",- lambda: SimpleNamespace(workflow_statistics=service),- )- self._patch_statistic_time_range(monkeypatch)-- api = workflow_statistic_module.WorkflowAverageAppInteractionStatistic()- method = unwrap(api.get)- request_context = _make_request_context()-- with database_app.test_request_context("/"):- response = method(- api,- WorkflowStatisticQuery(),- request_context,- app_model=_make_app("app-1", tenant_id="t1"),- )-- assert response == {"data": [{"date": "2024-01-04", "interactions": 2.5}]}- service.get_average_app_interactions.assert_called_once_with(- request_context,- app_id="app-1",- start_date=None,- end_date=None,- timezone="UTC",- )-- def test_workflow_statistic_invalid_time_range(self, monkeypatch: pytest.MonkeyPatch) -> None:- account = _make_account()- account.timezone = "UTC"- monkeypatch.setattr(- workflow_statistic_module,- "current_account_with_tenant",- lambda: SimpleNamespace(account=account),- )- monkeypatch.setattr(- workflow_statistic_module,- "parse_time_range",- MagicMock(side_effect=ValueError("invalid range")),- )-- with pytest.raises(BadRequest, match="invalid range"):- workflow_statistic_module._resolve_statistic_time_range(WorkflowStatisticQuery())-- @staticmethod- def _patch_statistic_time_range(monkeypatch: pytest.MonkeyPatch) -> None:- account = _make_account()- account.timezone = "UTC"- monkeypatch.setattr(- workflow_statistic_module,- "current_account_with_tenant",- lambda: SimpleNamespace(account=account),- )- monkeypatch.setattr(- workflow_statistic_module,- "parse_time_range",- lambda *_args, **_kwargs: (None, None),- )-class TestWrapsEndpoints:def test_get_app_model_context(self):assert hasattr(wraps_module, "get_app_model")diff --git a/api/tests/unit_tests/controllers/console/app/test_statistic.py b/api/tests/unit_tests/controllers/console/app/test_statistic.pyindex fe00ae7856c..7ec600970ef 100644--- a/api/tests/unit_tests/controllers/console/app/test_statistic.py+++ b/api/tests/unit_tests/controllers/console/app/test_statistic.py@@ -1,203 +1,319 @@-from __future__ import annotations+"""Real HTTP and SQLite coverage for Console statistics admission.-from datetime import UTC, datetime-from decimal import Decimal-from inspect import unwrap-from types import SimpleNamespace-from typing import Any-from unittest.mock import MagicMock+Metric aggregation uses PostgreSQL/MySQL functions, so successful query results+are exercised by the controller integration tests against PostgreSQL. These tests+exercise the real identity and app queries without substituting query services.+"""++from collections.abc import Iterator+from dataclasses import dataclass+from http import HTTPStatus+from uuid import uuid4import pytest-from flask import Flask-from pytest_mock import MockerFixture-from sqlalchemy.orm import Session, sessionmaker-from werkzeug.exceptions import BadRequest--from controllers.console.app import statistic as statistic_module-from machinery.context import RequestContext-from models.model import App-from repositories.app_statistic_query_repository import AppStatisticQueryRepository-from services.app_statistic_query import AppStatisticQuery---def _request_context() -> RequestContext:- return RequestContext(- request_id="request-1",- trace_id="trace-1",- account_id="account-1",- active_workspace_id="tenant-1",- )+from flask import Blueprint, Flask, Response+from flask.testing import FlaskClient+from flask_restx import Resource+from redis import Redis+from sqlalchemy import Connection, Engine, event, text+from sqlalchemy.orm import Session, SessionTransaction, sessionmaker+from werkzeug.test import TestResponse+from constants import HEADER_NAME_CSRF_TOKEN+from controllers.console.app import statistic, workflow_statistic+from controllers.console.wraps import _is_setup_completed+from enums import DeploymentEdition+from extensions.ext_application_services import build_application_services+from extensions.ext_login import DifyLoginManager, bind_account_loader, load_user_from_request, unauthorized_handler+from extensions.ext_redis import RedisClientWrapper+from libs.datetime_utils import naive_utc_now+from libs.external_api import ExternalApi+from libs.passport import PassportService+from libs.token import _real_cookie_name, generate_csrf_token+from models import Account, DifySetup, Tenant, TenantAccountJoin+from models.account import AccountStatus, TenantAccountRole+from models.agent import Agent, AgentScope, AgentSource+from models.engine import db+from models.model import App, AppMode+from tests.helpers.unit.config_override import config_overrides_context-def _app_model() -> App:- return App(id="app-1", tenant_id="tenant-1", name="Statistics App")+@dataclass(frozen=True)+class _Endpoint:+ path: str+ resource: type[Resource]+ mode: AppMode-def _install_dependencies(- monkeypatch: pytest.MonkeyPatch,- statistics: AppStatisticQuery,- *,- time_range: tuple[datetime | None, datetime | None] = (None, None),-) -> None:- monkeypatch.setattr(- statistic_module,- "application_services",- lambda: SimpleNamespace(app_statistics=statistics),- )- monkeypatch.setattr(- statistic_module,- "current_account_with_tenant",- lambda: SimpleNamespace(account=SimpleNamespace(timezone="UTC")),- )- monkeypatch.setattr(statistic_module, "parse_time_range", lambda *_args, **_kwargs: time_range)---def _invoke(- app: Flask,- resource_type: type,- *,- start: str | None = None,- end: str | None = None,-) -> dict[str, Any]:- resource = resource_type()- method = unwrap(resource.get)- with app.test_request_context("/console/api/apps/app-1/statistics", method="GET"):- response = method(- resource,- statistic_module.StatisticTimeRangeQuery(start=start, end=end),- _request_context(),- app_model=_app_model(),++_ENDPOINTS = (+ _Endpoint("statistics/daily-messages", statistic.DailyMessageStatistic, AppMode.CHAT),+ _Endpoint("statistics/daily-conversations", statistic.DailyConversationStatistic, AppMode.CHAT),+ _Endpoint("statistics/daily-end-users", statistic.DailyTerminalsStatistic, AppMode.CHAT),+ _Endpoint("statistics/token-costs", statistic.DailyTokenCostStatistic, AppMode.CHAT),+ _Endpoint("statistics/average-session-interactions", statistic.AverageSessionInteractionStatistic, AppMode.CHAT),+ _Endpoint("statistics/user-satisfaction-rate", statistic.UserSatisfactionRateStatistic, AppMode.CHAT),+ _Endpoint("statistics/average-response-time", statistic.AverageResponseTimeStatistic, AppMode.COMPLETION),+ _Endpoint("statistics/tokens-per-second", statistic.TokensPerSecondStatistic, AppMode.CHAT),+ _Endpoint(+ "workflow/statistics/daily-conversations", workflow_statistic.WorkflowDailyRunsStatistic, AppMode.WORKFLOW+ ),+ _Endpoint(+ "workflow/statistics/daily-terminals", workflow_statistic.WorkflowDailyTerminalsStatistic, AppMode.WORKFLOW+ ),+ _Endpoint("workflow/statistics/token-costs", workflow_statistic.WorkflowDailyTokenCostStatistic, AppMode.WORKFLOW),+ _Endpoint(+ "workflow/statistics/average-app-interactions",+ workflow_statistic.WorkflowAverageAppInteractionStatistic,+ AppMode.WORKFLOW,+ ),+)+++@dataclass(frozen=True)+class _Harness:+ client: FlaskClient+ session_factory: sessionmaker[Session]+ account_id: str+ tenant_id: str+ headers: dict[str, str]+ sessions: list[Session]++ def create_app(self, mode: AppMode, *, tenant_id: str | None = None) -> str:+ with self.session_factory.begin() as session:+ app = App(+ tenant_id=tenant_id or self.tenant_id,+ name="Statistics app",+ mode=mode,+ created_by=self.account_id,+ enable_api=True,+ enable_site=True,+ )+ session.add(app)+ session.flush()+ return app.id++ def get(self, endpoint: _Endpoint, app_id: str, *, query: dict[str, str] | None = None) -> TestResponse:+ self.sessions.clear()+ return self.client.get(f"/console/api/apps/{app_id}/{endpoint.path}", headers=self.headers, query_string=query)+++def harness(sqlite_engine: Engine, sqlite_session_factory: sessionmaker[Session]) -> Iterator[_Harness]:+ with (+ Redis() as redis_client,+ config_overrides_context(+ DEPLOYMENT_EDITION=DeploymentEdition.COMMUNITY,+ RBAC_ENABLED=False,+ LOGIN_DISABLED=False,+ INIT_PASSWORD="",+ SECRET_KEY="statistics-http-test-secret-at-least-32-bytes",+ ),+ ):+ app = Flask(__name__)+ app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI=str(sqlite_engine.url))+ db.init_app(app)+ redis = RedisClientWrapper()+ redis.initialize(redis_client)+ services = build_application_services(+ database_client=sqlite_session_factory,+ deployment_edition=DeploymentEdition.COMMUNITY,+ initialization_password="",+ redis=redis,)- return response if isinstance(response, dict) else response.get_json()+ app.extensions["application_services"] = services+ bind_account_loader(app, services.accounts.identity.load_user)+ login_manager = DifyLoginManager()+ login_manager.request_loader(load_user_from_request)+ login_manager.unauthorized_handler(unauthorized_handler)+ login_manager.init_app(app)+ blueprint = Blueprint("console", __name__, url_prefix="/console/api")+ api = ExternalApi(blueprint)+ for endpoint in _ENDPOINTS:+ api.add_resource(endpoint.resource, f"/apps/<uuid:app_id>/{endpoint.path}")+ app.register_blueprint(blueprint)++ now = naive_utc_now()+ with sqlite_session_factory.begin() as session:+ account = Account(+ email="[email protected]",+ name="Statistics viewer",+ status=AccountStatus.ACTIVE,+ initialized_at=now,+ timezone="America/New_York",+ )+ account.last_active_at = now+ tenant = Tenant(name="Statistics workspace")+ session.add_all([account, tenant, DifySetup(version="test")])+ session.flush()+ session.add(+ TenantAccountJoin(+ tenant_id=tenant.id,+ account_id=account.id,+ role=TenantAccountRole.OWNER,+ current=True,+ )+ )+ account_id, tenant_id = account.id, tenant.id++ # Complete the one-time setup check before observing request transactions.+ # The legacy setup cache still owns a Flask-scoped Session independently.+ with app.app_context():+ _is_setup_completed.reset_success()+ assert _is_setup_completed()+ db.session.remove()++ client = app.test_client()+ csrf_token = generate_csrf_token(account_id)+ client.set_cookie(_real_cookie_name("csrf_token"), csrf_token)+ headers = {+ "Authorization": f"Bearer {PassportService().issue({'user_id': account_id})}",+ HEADER_NAME_CSRF_TOKEN: csrf_token,+ }+ sessions: list[Session] = []++ def observe_session(session: Session, transaction: SessionTransaction, connection: Connection) -> None:+ sessions.append(session)++ @app.after_request+ def assert_request_sessions_closed(response: Response) -> Response:+ assert all(not session.in_transaction() and not session.identity_map for session in sessions)+ return response++ event.listen(Session, "after_begin", observe_session)+ try:+ yield _Harness(client, sqlite_session_factory, account_id, tenant_id, headers, sessions)+ finally:+ event.remove(Session, "after_begin", observe_session)+ _is_setup_completed.reset_success()+ with app.app_context():+ db.session.remove()+ db.engine.dispose()++[email protected]("endpoint", _ENDPOINTS, ids=lambda endpoint: endpoint.path)[email protected]("visibility", ["missing", "different-workspace", "non-normal", "workflow-only"])+def test_statistics_reject_inaccessible_apps(harness: _Harness, endpoint: _Endpoint, visibility: str) -> None:+ app_id = str(uuid4())+ if visibility != "missing":+ app_id = harness.create_app(+ endpoint.mode, tenant_id=str(uuid4()) if visibility == "different-workspace" else None+ )+ with harness.session_factory.begin() as session:+ app = session.get(App, app_id)+ assert app is not None+ if visibility == "non-normal":+ # Persist a historical status not accepted by the current enum.+ session.execute(+ text("UPDATE apps SET status = :status WHERE id = :app_id"),+ {"status": "archived", "app_id": app_id},+ )+ elif visibility == "workflow-only":+ app.mode = AppMode.AGENT+ session.add(+ Agent(+ tenant_id=harness.tenant_id,+ name="Hidden workflow agent",+ scope=AgentScope.WORKFLOW_ONLY,+ source=AgentSource.WORKFLOW,+ app_id=str(uuid4()),+ backing_app_id=app_id,+ )+ )++ response = harness.get(endpoint, app_id)++ assert response.status_code == HTTPStatus.NOT_FOUND+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "app_not_found"+ assert response.content_type == "application/json"+ assert harness.sessions++[email protected]("endpoint", _ENDPOINTS, ids=lambda endpoint: endpoint.path)[email protected]("field", ["start", "end"])+def test_statistics_reject_invalid_dates_after_app_admission(+ harness: _Harness, endpoint: _Endpoint, field: str+) -> None:+ app_id = harness.create_app(endpoint.mode)++ response = harness.get(endpoint, app_id, query={field: "not-a-date"})++ assert response.status_code == HTTPStatus.BAD_REQUEST+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "invalid_param"+ assert "not-a-date" in body["message"]+ assert harness.sessions@pytest.mark.parametrize(- ("resource_type", "query_method", "row", "expected"),+ ("endpoint", "unsupported_mode"),[- pytest.param(- statistic_module.DailyMessageStatistic,- "get_daily_messages",- {"date": "2024-01-01", "message_count": 3},- {"date": "2024-01-01", "message_count": 3},- id="daily-messages",- ),- pytest.param(- statistic_module.DailyConversationStatistic,- "get_daily_conversations",- {"date": "2024-01-02", "conversation_count": 5},- {"date": "2024-01-02", "conversation_count": 5},- id="daily-conversations",- ),- pytest.param(- statistic_module.DailyTerminalsStatistic,- "get_daily_terminals",- {"date": "2024-01-03", "terminal_count": 7},- {"date": "2024-01-03", "terminal_count": 7},- id="daily-terminals",- ),- pytest.param(- statistic_module.DailyTokenCostStatistic,- "get_daily_token_costs",- {- "date": "2024-01-04",- "token_count": 10,- "total_price": Decimal("0.25"),- },- {"date": "2024-01-04", "token_count": 10, "total_price": "0.25", "currency": "USD"},- id="daily-token-costs",- ),- pytest.param(- statistic_module.AverageSessionInteractionStatistic,- "get_average_session_interactions",- {"date": "2024-01-05", "interactions": Decimal("2.5")},- {"date": "2024-01-05", "interactions": 2.5},- id="average-session-interactions",- ),- pytest.param(- statistic_module.UserSatisfactionRateStatistic,- "get_user_satisfaction_rates",- {"date": "2024-01-06", "feedback_count": 1, "message_count": 10},- {"date": "2024-01-06", "rate": 100.0},- id="user-satisfaction-rate",- ),- pytest.param(- statistic_module.AverageResponseTimeStatistic,- "get_average_response_times",- {"date": "2024-01-07", "latency": 1.234},- {"date": "2024-01-07", "latency": 1234.0},- id="average-response-time",- ),- pytest.param(- statistic_module.TokensPerSecondStatistic,- "get_tokens_per_second",- {"date": "2024-01-08", "tokens_per_second": 15.5},- {"date": "2024-01-08", "tps": 15.5},- id="tokens-per-second",- ),+ (_ENDPOINTS[4], AppMode.COMPLETION),+ (_ENDPOINTS[6], AppMode.CHAT),+ (_ENDPOINTS[11], AppMode.ADVANCED_CHAT),],+ ids=["session-interactions", "response-time", "workflow-interactions"],)-def test_statistic_endpoint_delegates_to_statistic_query(- app: Flask,- monkeypatch: pytest.MonkeyPatch,- resource_type: type,- query_method: str,- row: dict[str, object],- mocker: MockerFixture,- sqlite_session_factory: sessionmaker[Session],- expected: dict[str, Any],+def test_statistics_preserve_app_mode_restrictions(+ harness: _Harness, endpoint: _Endpoint, unsupported_mode: AppMode) -> None:- statistics = AppStatisticQueryRepository(session_factory=sqlite_session_factory)- mocker.patch.object(statistics, "_execute", return_value=(row,))- query_call = mocker.spy(statistics, query_method)- _install_dependencies(monkeypatch, statistics)-- assert _invoke(app, resource_type) == {"data": [expected]}- query_call.assert_called_once_with(- app_id="app-1",- start_date=None,- end_date=None,- timezone="UTC",- )+ app_id = harness.create_app(unsupported_mode)+ response = harness.get(endpoint, app_id)-def test_statistic_endpoint_passes_time_range(- app: Flask,- monkeypatch: pytest.MonkeyPatch,- mocker: MockerFixture,- sqlite_session_factory: sessionmaker[Session],-) -> None:- statistics = AppStatisticQueryRepository(session_factory=sqlite_session_factory)- query_call = mocker.spy(statistics, "get_daily_messages")- mocker.patch.object(statistics, "_execute", return_value=())- start_date = datetime(2024, 1, 1, tzinfo=UTC)- end_date = datetime(2024, 1, 2, tzinfo=UTC)- _install_dependencies(monkeypatch, statistics, time_range=(start_date, end_date))-- assert _invoke(app, statistic_module.DailyMessageStatistic, start="start", end="end") == {"data": []}- query_call.assert_called_once_with(- app_id="app-1",- start_date=start_date,- end_date=end_date,- timezone="UTC",- )+ assert response.status_code == HTTPStatus.NOT_FOUND+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "app_not_found"+ assert "App mode is not in the supported list" in body["message"]-def test_statistic_endpoint_rejects_invalid_time_range(- app: Flask,- monkeypatch: pytest.MonkeyPatch,- mocker: MockerFixture,- sqlite_session_factory: sessionmaker[Session],[email protected]("mode", [AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT, AppMode.AGENT])+def test_session_interactions_admits_supported_chat_modes_before_date_validation(+ harness: _Harness, mode: AppMode) -> None:- statistics = AppStatisticQueryRepository(session_factory=sqlite_session_factory)- query_call = mocker.spy(statistics, "get_daily_messages")- _install_dependencies(monkeypatch, statistics)- monkeypatch.setattr(- statistic_module,- "parse_time_range",- MagicMock(side_effect=ValueError("Invalid time range")),+ app_id = harness.create_app(mode)++ response = harness.get(_ENDPOINTS[4], app_id, query={"start": "invalid-date"})++ assert response.status_code == HTTPStatus.BAD_REQUEST+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "invalid_param"+++def test_statistics_require_console_authentication(harness: _Harness) -> None:+ app_id = harness.create_app(AppMode.CHAT)++ response = harness.client.get(f"/console/api/apps/{app_id}/statistics/daily-messages")++ assert response.status_code == HTTPStatus.UNAUTHORIZED+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "unauthorized"+++def test_statistics_require_valid_csrf_token(harness: _Harness) -> None:+ app_id = harness.create_app(AppMode.CHAT)++ response = harness.client.get(+ f"/console/api/apps/{app_id}/statistics/daily-messages",+ headers={"Authorization": harness.headers["Authorization"]},)- with pytest.raises(BadRequest, match="Invalid time range"):- _invoke(app, statistic_module.DailyMessageStatistic)+ assert response.status_code == HTTPStatus.UNAUTHORIZED+ body = response.get_json()+ assert isinstance(body, dict)+ assert body["code"] == "unauthorized"+ assert body["message"] == "CSRF token is missing or invalid."+- query_call.assert_not_called()[email protected]("query_type", [statistic.StatisticTimeRangeQuery, workflow_statistic.WorkflowStatisticQuery])+def test_statistics_treat_blank_date_bounds_as_absent(+ query_type: type[statistic.StatisticTimeRangeQuery] | type[workflow_statistic.WorkflowStatisticQuery],+) -> None:+ query = query_type(start="", end="")+ assert query.start is None+ assert query.end is None