|
@@ -29,10 +29,13 @@ class DailyMessageStatistic(Resource):
|
|
|
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
|
|
|
- """
|
|
|
+ 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)
|
|
@@ -45,7 +48,7 @@ class DailyMessageStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -55,10 +58,10 @@ class DailyMessageStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -83,10 +86,13 @@ class DailyConversationStatistic(Resource):
|
|
|
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.conversation_id) AS conversation_count
|
|
|
- FROM messages where app_id = :app_id
|
|
|
- """
|
|
|
+ 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"""
|
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
@@ -99,7 +105,7 @@ class DailyConversationStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -109,10 +115,10 @@ class DailyConversationStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -137,10 +143,13 @@ class DailyTerminalsStatistic(Resource):
|
|
|
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
|
|
|
- """
|
|
|
+ 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)
|
|
@@ -153,7 +162,7 @@ class DailyTerminalsStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -163,10 +172,10 @@ class DailyTerminalsStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -191,12 +200,14 @@ class DailyTokenCostStatistic(Resource):
|
|
|
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,
|
|
|
- (sum(messages.message_tokens) + sum(messages.answer_tokens)) as token_count,
|
|
|
- sum(total_price) as total_price
|
|
|
- FROM messages where app_id = :app_id
|
|
|
- """
|
|
|
+ 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}
|
|
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
@@ -209,7 +220,7 @@ class DailyTokenCostStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -219,10 +230,10 @@ class DailyTokenCostStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -249,12 +260,22 @@ class AverageSessionInteractionStatistic(Resource):
|
|
|
parser.add_argument("end", type=DatetimeString("%Y-%m-%d %H:%M"), location="args")
|
|
|
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.override_model_configs IS NULL AND c.app_id = :app_id"""
|
|
|
+ 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"""
|
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
@@ -267,7 +288,7 @@ FROM (SELECT m.conversation_id, COUNT(m.id) AS message_count
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and c.created_at >= :start"
|
|
|
+ sql_query += " AND c.created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -277,14 +298,19 @@ FROM (SELECT m.conversation_id, COUNT(m.id) AS message_count
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and c.created_at < :end"
|
|
|
+ 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"""
|
|
|
+ GROUP BY m.conversation_id
|
|
|
+ ) subquery
|
|
|
+LEFT JOIN
|
|
|
+ conversations c
|
|
|
+ ON c.id = subquery.conversation_id
|
|
|
+GROUP BY
|
|
|
+ date
|
|
|
+ORDER BY
|
|
|
+ date"""
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -311,13 +337,17 @@ class UserSatisfactionRateStatistic(Resource):
|
|
|
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
|
|
|
- """
|
|
|
+ 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)
|
|
@@ -330,7 +360,7 @@ class UserSatisfactionRateStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and m.created_at >= :start"
|
|
|
+ sql_query += " AND m.created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -340,10 +370,10 @@ class UserSatisfactionRateStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and m.created_at < :end"
|
|
|
+ sql_query += " AND m.created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -373,12 +403,13 @@ class AverageResponseTimeStatistic(Resource):
|
|
|
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,
|
|
|
- AVG(provider_response_latency) as latency
|
|
|
- FROM messages
|
|
|
- WHERE app_id = :app_id
|
|
|
- """
|
|
|
+ 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}
|
|
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
@@ -391,7 +422,7 @@ class AverageResponseTimeStatistic(Resource):
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -401,10 +432,10 @@ class AverageResponseTimeStatistic(Resource):
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|
|
@@ -429,13 +460,16 @@ class TokensPerSecondStatistic(Resource):
|
|
|
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
|
|
|
+ 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"""
|
|
|
+FROM
|
|
|
+ messages
|
|
|
+WHERE
|
|
|
+ app_id = :app_id"""
|
|
|
arg_dict = {"tz": account.timezone, "app_id": app_model.id}
|
|
|
|
|
|
timezone = pytz.timezone(account.timezone)
|
|
@@ -448,7 +482,7 @@ WHERE app_id = :app_id"""
|
|
|
start_datetime_timezone = timezone.localize(start_datetime)
|
|
|
start_datetime_utc = start_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at >= :start"
|
|
|
+ sql_query += " AND created_at >= :start"
|
|
|
arg_dict["start"] = start_datetime_utc
|
|
|
|
|
|
if args["end"]:
|
|
@@ -458,10 +492,10 @@ WHERE app_id = :app_id"""
|
|
|
end_datetime_timezone = timezone.localize(end_datetime)
|
|
|
end_datetime_utc = end_datetime_timezone.astimezone(utc_timezone)
|
|
|
|
|
|
- sql_query += " and created_at < :end"
|
|
|
+ sql_query += " AND created_at < :end"
|
|
|
arg_dict["end"] = end_datetime_utc
|
|
|
|
|
|
- sql_query += " GROUP BY date order by date"
|
|
|
+ sql_query += " GROUP BY date ORDER BY date"
|
|
|
|
|
|
response_data = []
|
|
|
|