| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from decimal import Decimal | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import pytz | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask import jsonify | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from flask_login import current_user  # type: ignore | 
					
						
							|  |  |  | from flask_restful import Resource, reqparse  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from controllers.console import api | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from controllers.console.app.wraps import get_app_model | 
					
						
							| 
									
										
										
										
											2024-11-01 15:51:22 +08:00
										 |  |  | from controllers.console.wraps import account_initialization_required, setup_required | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  | from libs.helper import DatetimeString | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from models.model import AppMode | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  | class DailyMessageStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							|  |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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"""
 | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  |         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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  |             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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         response_data = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.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}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | class DailyConversationStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query = """SELECT
 | 
					
						
							|  |  |  |     DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date, | 
					
						
							|  |  |  |     COUNT(DISTINCT messages.conversation_id) AS conversation_count | 
					
						
							|  |  |  | FROM | 
					
						
							|  |  |  |     messages | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  |     app_id = :app_id"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         response_data = [] | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							|  |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append({"date": str(i.date), "conversation_count": i.conversation_count}) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DailyTerminalsStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         response_data = [] | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append({"date": str(i.date), "terminal_count": i.terminal_count}) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DailyTokenCostStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         response_data = [] | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							|  |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append( | 
					
						
							|  |  |  |                     {"date": str(i.date), "token_count": i.token_count, "total_price": i.total_price, "currency": "USD"} | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AverageSessionInteractionStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model(mode=[AppMode.CHAT, AppMode.AGENT_CHAT, AppMode.ADVANCED_CHAT]) | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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 | 
					
						
							| 
									
										
										
										
											2025-01-02 10:02:56 +08:00
										 |  |  |             c.app_id = :app_id"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND c.created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND c.created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         sql_query += """
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         GROUP BY m.conversation_id | 
					
						
							|  |  |  |     ) subquery | 
					
						
							|  |  |  | LEFT JOIN | 
					
						
							|  |  |  |     conversations c | 
					
						
							|  |  |  |     ON c.id = subquery.conversation_id | 
					
						
							|  |  |  | GROUP BY | 
					
						
							|  |  |  |     date | 
					
						
							|  |  |  | ORDER BY | 
					
						
							|  |  |  |     date"""
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         response_data = [] | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append( | 
					
						
							|  |  |  |                     {"date": str(i.date), "interactions": float(i.interactions.quantize(Decimal("0.01")))} | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UserSatisfactionRateStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND m.created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND m.created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         response_data = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							|  |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "date": str(i.date), | 
					
						
							|  |  |  |                         "rate": round((i.feedback_count * 1000 / i.message_count) if i.message_count > 0 else 0, 2), | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AverageResponseTimeStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model(mode=AppMode.COMPLETION) | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         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"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         response_data = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append({"date": str(i.date), "latency": round(i.latency * 1000, 4)}) | 
					
						
							| 
									
										
										
										
											2023-05-31 11:20:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         return jsonify({"data": response_data}) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  | class TokensPerSecondStatistic(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     @get_app_model | 
					
						
							|  |  |  |     def get(self, app_model): | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  |         account = current_user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser = reqparse.RequestParser() | 
					
						
							| 
									
										
										
										
											2024-09-11 16:40:52 +08:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  |         args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query = """SELECT
 | 
					
						
							|  |  |  |     DATE(DATE_TRUNC('day', created_at AT TIME ZONE 'UTC' AT TIME ZONE :tz )) AS date, | 
					
						
							|  |  |  |     CASE | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  |         WHEN SUM(provider_response_latency) = 0 THEN 0 | 
					
						
							|  |  |  |         ELSE (SUM(answer_tokens) / SUM(provider_response_latency)) | 
					
						
							|  |  |  |     END as tokens_per_second | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  | FROM | 
					
						
							|  |  |  |     messages | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  |     app_id = :app_id"""
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         arg_dict = {"tz": account.timezone, "app_id": app_model.id} | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         timezone = pytz.timezone(account.timezone) | 
					
						
							|  |  |  |         utc_timezone = pytz.utc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["start"]: | 
					
						
							|  |  |  |             start_datetime = datetime.strptime(args["start"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  |             start_datetime = start_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             start_datetime_timezone = timezone.localize(start_datetime) | 
					
						
							|  |  |  |             start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at >= :start" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["start"] = start_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |         if args["end"]: | 
					
						
							|  |  |  |             end_datetime = datetime.strptime(args["end"], "%Y-%m-%d %H:%M") | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  |             end_datetime = end_datetime.replace(second=0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             end_datetime_timezone = timezone.localize(end_datetime) | 
					
						
							|  |  |  |             end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |             sql_query += " AND created_at < :end" | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |             arg_dict["end"] = end_datetime_utc | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-12 14:00:36 +08:00
										 |  |  |         sql_query += " GROUP BY date ORDER BY date" | 
					
						
							| 
									
										
										
										
											2023-08-01 15:17:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         response_data = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 14:28:21 +08:00
										 |  |  |         with db.engine.begin() as conn: | 
					
						
							|  |  |  |             rs = conn.execute(db.text(sql_query), arg_dict) | 
					
						
							|  |  |  |             for i in rs: | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  |                 response_data.append({"date": str(i.date), "tps": round(i.tokens_per_second, 4)}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return jsonify({"data": response_data}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-27 12:53:27 +08:00
										 |  |  | api.add_resource(DailyMessageStatistic, "/apps/<uuid:app_id>/statistics/daily-messages") | 
					
						
							| 
									
										
										
										
											2024-08-26 15:29:10 +08:00
										 |  |  | 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") |