mirror of
https://github.com/YFGaia/dify-plus.git
synced 2026-06-14 20:41:21 +08:00
f26fe2f4d2
# Conflicts:
# .gitignore
# README.md
# api/.env.example
# api/Dockerfile
# api/commands.py
# api/configs/app_config.py
# api/controllers/console/__init__.py
# api/controllers/console/apikey.py
# api/controllers/console/app/statistic.py
# api/controllers/service_api/app/app.py
# api/controllers/service_api/app/audio.py
# api/controllers/service_api/app/completion.py
# api/controllers/service_api/app/conversation.py
# api/controllers/service_api/app/file.py
# api/controllers/service_api/app/message.py
# api/controllers/service_api/app/workflow.py
# api/controllers/service_api/wraps.py
# api/controllers/web/completion.py
# api/core/app/apps/advanced_chat/app_generator.py
# api/core/app/apps/advanced_chat/generate_task_pipeline.py
# api/core/app/apps/agent_chat/app_generator.py
# api/core/app/apps/workflow/app_generator.py
# api/core/app/apps/workflow/generate_task_pipeline.py
# api/core/app/task_pipeline/workflow_cycle_manage.py
# api/core/helper/code_executor/code_executor.py
# api/core/tools/builtin_tool/providers/code/tools/simple_code.py
# api/core/workflow/nodes/code/code_node.py
# api/docker/entrypoint.sh
# api/events/event_handlers/__init__.py
# api/extensions/ext_celery.py
# api/extensions/ext_commands.py
# api/models/model.py
# api/models/workflow.py
# api/poetry.lock
# api/pyproject.toml
# api/services/app_service.py
# api/services/feature_service.py
# api/services/workspace_service.py
# web/.env.example
# web/Dockerfile
# web/app/(commonLayout)/apps/Apps.tsx
# web/app/components/apps/app-card.tsx
# web/app/components/base/chat/embedded-chatbot/index.tsx
# web/app/components/base/mermaid/index.tsx
# web/app/components/develop/index.tsx
# web/app/components/develop/secret-key/secret-key-modal.tsx
# web/app/components/develop/secret-key/style.module.css
# web/app/components/develop/template/template.zh.mdx
# web/app/components/explore/app-list/index.tsx
# web/app/components/explore/category.tsx
# web/app/components/explore/sidebar/index.tsx
# web/app/components/header/account-dropdown/index.tsx
# web/app/components/header/index.tsx
# web/app/components/share/utils.ts
# web/app/layout.tsx
# web/app/signin/components/mail-and-password-auth.tsx
# web/app/signin/normal-form.tsx
# web/app/signin/page.module.css
# web/context/app-context.tsx
# web/i18n/i18next-config.ts
# web/i18n/ja-JP/login.ts
# web/i18n/ko-KR/login.ts
#
if dify_config.WORKFLOW_LOG_CLEANUP_ENABLED:
# 2:00 AM every day
imports.append("schedule.clean_workflow_runlogs_precise")
beat_schedule["clean_workflow_runlogs_precise"] = {
"task": "schedule.clean_workflow_runlogs_precise.clean_workflow_runlogs_precise",
"schedule": crontab(minute="0", hour="2"),
} web/package.json
# web/pnpm-lock.yaml
# web/types/feature.ts
584 lines
22 KiB
Python
584 lines
22 KiB
Python
from datetime import datetime
|
|
from decimal import Decimal
|
|
|
|
import pytz
|
|
import sqlalchemy as sa
|
|
from flask import jsonify
|
|
from flask_login import current_user
|
|
from flask_restx import Resource, reqparse
|
|
|
|
from controllers.console import api
|
|
from controllers.console.app.wraps import get_app_model
|
|
from controllers.console.wraps import account_initialization_required, setup_required
|
|
from core.app.entities.app_invoke_entities import InvokeFrom
|
|
from extensions.ext_database import db
|
|
from libs.helper import DatetimeString
|
|
from libs.login import login_required
|
|
from models import AppMode, Message
|
|
|
|
|
|
class DailyMessageStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
COUNT(*) AS message_count
|
|
FROM
|
|
messages
|
|
WHERE
|
|
app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append({"date": str(i.date), "message_count": i.message_count})
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class DailyConversationStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("account", type=bool, location="args") # Extend added a new app personal expenses page
|
|
args = parser.parse_args()
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
stmt = (
|
|
sa.select(
|
|
sa.func.date(
|
|
sa.func.date_trunc("day", sa.text("created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz"))
|
|
).label("date"),
|
|
sa.func.count(sa.distinct(Message.conversation_id)).label("conversation_count"),
|
|
)
|
|
.select_from(Message)
|
|
.where(Message.app_id == app_model.id, Message.invoke_from != InvokeFrom.DEBUGGER.value)
|
|
)
|
|
|
|
# Extend Start: added a new app personal expenses page
|
|
if args.account is not None and args.account:
|
|
stmt = (
|
|
select(
|
|
func.date(
|
|
func.date_trunc("day", text("created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz"))
|
|
).label("date"),
|
|
func.count(distinct(Message.conversation_id)).label("conversation_count")
|
|
)
|
|
.select_from(Message)
|
|
.where(
|
|
Message.app_id == app_model.id,
|
|
or_(
|
|
Message.from_account_id == account.id,
|
|
Message.from_end_user_id.in_(
|
|
select(EndUser.id)
|
|
.where(EndUser.external_user_id == account.id)
|
|
.distinct()
|
|
)
|
|
)
|
|
)
|
|
.group_by(
|
|
func.date(
|
|
func.date_trunc("day", text("created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz"))
|
|
)
|
|
)
|
|
.params(tz=account.timezone) # 绑定参数
|
|
)
|
|
# Extend Stop: added a new app personal expenses page
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
stmt = stmt.where(Message.created_at >= start_datetime_utc)
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
stmt = stmt.where(Message.created_at < end_datetime_utc)
|
|
|
|
stmt = stmt.group_by("date").order_by("date")
|
|
|
|
response_data = []
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(stmt, {"tz": account.timezone})
|
|
for row in rs:
|
|
response_data.append({"date": str(row.date), "conversation_count": row.conversation_count})
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class DailyTerminalsStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
COUNT(DISTINCT messages.from_end_user_id) AS terminal_count
|
|
FROM
|
|
messages
|
|
WHERE
|
|
app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append({"date": str(i.date), "terminal_count": i.terminal_count})
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class DailyTokenCostStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("account", type=bool, location="args") # Extend added a new app personal expenses page
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
(SUM(messages.message_tokens) + SUM(messages.answer_tokens)) AS token_count,
|
|
SUM(total_price) AS total_price
|
|
FROM
|
|
messages
|
|
WHERE
|
|
app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
# Extend Start: added a new app personal expenses page
|
|
if args.account is not None and args.account:
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
(SUM(messages.message_tokens) + SUM(messages.answer_tokens)) AS token_count,
|
|
SUM(total_price) AS total_price
|
|
FROM
|
|
messages
|
|
where
|
|
app_id = :app_id
|
|
AND (from_account_id = :user_id OR from_end_user_id IN (
|
|
SELECT DISTINCT(id) FROM end_users WHERE external_user_id = :user_id))
|
|
"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id, "user_id": account.id}
|
|
# Extend Stop: added a new app personal expenses page
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append(
|
|
{"date": str(i.date), "token_count": i.token_count, "total_price": i.total_price, "currency": "USD"}
|
|
)
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class AverageSessionInteractionStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT])
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("account", type=bool, location="args") # Extend added a new app personal expenses page
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', c.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
AVG(subquery.message_count) AS interactions
|
|
FROM
|
|
(
|
|
SELECT
|
|
m.conversation_id,
|
|
COUNT(m.id) AS message_count
|
|
FROM
|
|
conversations c
|
|
JOIN
|
|
messages m
|
|
ON c.id = m.conversation_id
|
|
WHERE
|
|
c.app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
# Extend Start: added a new app personal expenses page
|
|
if args.account is not None and args.account:
|
|
sql_query = """SELECT date(DATE_TRUNC('day', c.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
AVG(subquery.message_count) AS interactions FROM (SELECT m.conversation_id, COUNT(m.id) AS message_count
|
|
FROM conversations c JOIN messages m ON c.id = m.conversation_id
|
|
WHERE c.override_model_configs IS NULL AND c.app_id = :app_id AND (c.from_account_id = :user_id OR
|
|
c.from_end_user_id IN (SELECT DISTINCT(id) FROM end_users WHERE external_user_id = :user_id))"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id, "user_id": account.id}
|
|
# Extend Stop: added a new app personal expenses page
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND c.created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND c.created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += """
|
|
GROUP BY m.conversation_id
|
|
) subquery
|
|
LEFT JOIN
|
|
conversations c
|
|
ON c.id = subquery.conversation_id
|
|
GROUP BY
|
|
date
|
|
ORDER BY
|
|
date"""
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append(
|
|
{"date": str(i.date), "interactions": float(i.interactions.quantize(Decimal("0.01")))}
|
|
)
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class UserSatisfactionRateStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', m.created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
COUNT(m.id) AS message_count,
|
|
COUNT(mf.id) AS feedback_count
|
|
FROM
|
|
messages m
|
|
LEFT JOIN
|
|
message_feedbacks mf
|
|
ON mf.message_id=m.id AND mf.rating='like'
|
|
WHERE
|
|
m.app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND m.created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND m.created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append(
|
|
{
|
|
"date": str(i.date),
|
|
"rate": round((i.feedback_count * 1000 / i.message_count) if i.message_count > 0 else 0, 2),
|
|
}
|
|
)
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class AverageResponseTimeStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model(mode=AppMode.COMPLETION)
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("account", type=bool, location="args") # Extend added a new app personal expenses page
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
AVG(provider_response_latency) AS latency
|
|
FROM
|
|
messages
|
|
WHERE
|
|
app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
# Extend Start: added a new app personal expenses page
|
|
if args.account is not None and args.account:
|
|
sql_query = """
|
|
SELECT date(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
AVG(provider_response_latency) as latency FROM messages
|
|
WHERE app_id = :app_id AND (from_account_id = :user_id OR from_end_user_id IN (
|
|
SELECT DISTINCT(id) FROM end_users WHERE external_user_id = :user_id))
|
|
"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id, "user_id": account.id}
|
|
# Extend Stop: added a new app personal expenses page
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append({"date": str(i.date), "latency": round(i.latency * 1000, 4)})
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
class TokensPerSecondStatistic(Resource):
|
|
@setup_required
|
|
@login_required
|
|
@account_initialization_required
|
|
@get_app_model
|
|
def get(self, app_model):
|
|
account = current_user
|
|
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("start", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
args = parser.parse_args()
|
|
|
|
sql_query = """SELECT
|
|
DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date,
|
|
CASE
|
|
WHEN SUM(provider_response_latency) = 0 THEN 0
|
|
ELSE (SUM(answer_tokens) / SUM(provider_response_latency))
|
|
END as tokens_per_second
|
|
FROM
|
|
messages
|
|
WHERE
|
|
app_id = :app_id"""
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
utc_timezone = pytz.utc
|
|
|
|
if args["start"]:
|
|
start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M")
|
|
start_datetime = start_datetime.replace(second=0)
|
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at >= :start"
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
if args["end"]:
|
|
end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M")
|
|
end_datetime = end_datetime.replace(second=0)
|
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
sql_query += " AND created_at < :end"
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
response_data = []
|
|
|
|
with db.engine.begin() as conn:
|
|
rs = conn.execute(sa.text(sql_query), arg_dict)
|
|
for i in rs:
|
|
response_data.append({"date": str(i.date), "tps": round(i.tokens_per_second, 4)})
|
|
|
|
return jsonify({"data": response_data})
|
|
|
|
|
|
api.add_resource(DailyMessageStatistic, "/apps/<uuid:app_id>/statistics/daily-messages")
|
|
api.add_resource(DailyConversationStatistic, "/apps/<uuid:app_id>/statistics/daily-conversations")
|
|
api.add_resource(DailyTerminalsStatistic, "/apps/<uuid:app_id>/statistics/daily-end-users")
|
|
api.add_resource(DailyTokenCostStatistic, "/apps/<uuid:app_id>/statistics/token-costs")
|
|
api.add_resource(AverageSessionInteractionStatistic, "/apps/<uuid:app_id>/statistics/average-session-interactions")
|
|
api.add_resource(UserSatisfactionRateStatistic, "/apps/<uuid:app_id>/statistics/user-satisfaction-rate")
|
|
api.add_resource(AverageResponseTimeStatistic, "/apps/<uuid:app_id>/statistics/average-response-time")
|
|
api.add_resource(TokensPerSecondStatistic, "/apps/<uuid:app_id>/statistics/tokens-per-second")
|