-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:1Panel-dev/MaxKB into pr@main@fix_bugs
- Loading branch information
Showing
24 changed files
with
1,012 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.1.13 on 2024-03-28 13:59 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('application', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='chat', | ||
name='client_id', | ||
field=models.UUIDField(default=None, null=True, verbose_name='客户端id'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
apps/application/serializers/application_statistics_serializers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: maxkb | ||
@Author:虎 | ||
@file: application_statistics_serializers.py | ||
@date:2024/3/27 10:55 | ||
@desc: | ||
""" | ||
import datetime | ||
import os | ||
from typing import List, Dict | ||
|
||
from django.db import models | ||
from django.db.models.query import QuerySet | ||
from rest_framework import serializers | ||
|
||
from application.models.api_key_model import ApplicationPublicAccessClient | ||
from common.db.search import native_search, get_dynamics_model | ||
from common.util.field_message import ErrMessage | ||
from common.util.file_util import get_file_content | ||
from smartdoc.conf import PROJECT_DIR | ||
|
||
|
||
class ApplicationStatisticsSerializer(serializers.Serializer): | ||
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.char("应用id")) | ||
start_time = serializers.DateField(format='%Y-%m-%d', error_messages=ErrMessage.date("开始时间")) | ||
end_time = serializers.DateField(format='%Y-%m-%d', error_messages=ErrMessage.date("结束时间")) | ||
|
||
def get_end_time(self): | ||
return datetime.datetime.combine( | ||
datetime.datetime.strptime(self.data.get('end_time'), '%Y-%m-%d'), | ||
datetime.datetime.max.time()) | ||
|
||
def get_start_time(self): | ||
return self.data.get('start_time') | ||
|
||
def get_customer_count(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
start_time = self.get_start_time() | ||
end_time = self.get_end_time() | ||
return native_search( | ||
QuerySet(ApplicationPublicAccessClient).filter(application_id=self.data.get('application_id'), | ||
create_time__gte=start_time, | ||
create_time__lte=end_time), | ||
select_string=get_file_content( | ||
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'customer_count.sql')), | ||
with_search_one=True) | ||
|
||
def get_customer_count_trend(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
start_time = self.get_start_time() | ||
end_time = self.get_end_time() | ||
return native_search( | ||
{'default_sql': QuerySet(ApplicationPublicAccessClient).filter( | ||
application_id=self.data.get('application_id'), | ||
create_time__gte=start_time, | ||
create_time__lte=end_time)}, | ||
select_string=get_file_content( | ||
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'customer_count_trend.sql'))) | ||
|
||
def get_chat_record_aggregate(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
start_time = self.get_start_time() | ||
end_time = self.get_end_time() | ||
chat_record_aggregate = native_search( | ||
QuerySet(model=get_dynamics_model( | ||
{'application_chat.application_id': models.UUIDField(), | ||
'application_chat_record.create_time': models.DateTimeField()})).filter( | ||
**{'application_chat.application_id': self.data.get('application_id'), | ||
'application_chat_record.create_time__gte': start_time, | ||
'application_chat_record.create_time__lte': end_time} | ||
), | ||
select_string=get_file_content( | ||
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'chat_record_count.sql')), | ||
with_search_one=True) | ||
customer = self.get_customer_count(with_valid=False) | ||
return {**chat_record_aggregate, **customer} | ||
|
||
def get_chat_record_aggregate_trend(self, with_valid=True): | ||
if with_valid: | ||
self.is_valid(raise_exception=True) | ||
start_time = self.get_start_time() | ||
end_time = self.get_end_time() | ||
chat_record_aggregate_trend = native_search( | ||
{'default_sql': QuerySet(model=get_dynamics_model( | ||
{'application_chat.application_id': models.UUIDField(), | ||
'application_chat_record.create_time': models.DateTimeField()})).filter( | ||
**{'application_chat.application_id': self.data.get('application_id'), | ||
'application_chat_record.create_time__gte': start_time, | ||
'application_chat_record.create_time__lte': end_time} | ||
)}, | ||
select_string=get_file_content( | ||
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'chat_record_count_trend.sql'))) | ||
customer_count_trend = self.get_customer_count_trend(with_valid=False) | ||
return self.merge_customer_chat_record(chat_record_aggregate_trend, customer_count_trend) | ||
|
||
def merge_customer_chat_record(self, chat_record_aggregate_trend: List[Dict], customer_count_trend: List[Dict]): | ||
|
||
return [{**self.find(chat_record_aggregate_trend, lambda c: c.get('day').strftime('%Y-%m-%d') == day, | ||
{'star_num': 0, 'trample_num': 0, 'tokens_num': 0, 'chat_record_count': 0, | ||
'customer_num': 0, | ||
'day': day}), | ||
**self.find(customer_count_trend, lambda c: c.get('day').strftime('%Y-%m-%d') == day, | ||
{'customer_added_count': 0})} | ||
for | ||
day in | ||
self.get_days_between_dates(self.data.get('start_time'), self.data.get('end_time'))] | ||
|
||
@staticmethod | ||
def find(source_list, condition, default): | ||
value_list = [row for row in source_list if condition(row)] | ||
if len(value_list) > 0: | ||
return value_list[0] | ||
return default | ||
|
||
@staticmethod | ||
def get_days_between_dates(start_date, end_date): | ||
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') | ||
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') | ||
days = [] | ||
current_date = start_date | ||
while current_date <= end_date: | ||
days.append(current_date.strftime('%Y-%m-%d')) | ||
current_date += datetime.timedelta(days=1) | ||
return days |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT SUM | ||
( CASE WHEN application_chat_record.vote_status = '0' THEN 1 ELSE 0 END ) AS "star_num", | ||
SUM ( CASE WHEN application_chat_record.vote_status = '1' THEN 1 ELSE 0 END ) AS "trample_num", | ||
SUM ( application_chat_record.message_tokens + application_chat_record.answer_tokens ) as "tokens_num", | ||
"count"(DISTINCT application_chat.client_id) customer_num, | ||
"count"(application_chat_record."id") as chat_record_count | ||
FROM | ||
application_chat_record application_chat_record | ||
LEFT JOIN application_chat application_chat ON application_chat."id" = application_chat_record.chat_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
SELECT SUM | ||
( CASE WHEN application_chat_record.vote_status = '0' THEN 1 ELSE 0 END ) AS "star_num", | ||
SUM ( CASE WHEN application_chat_record.vote_status = '1' THEN 1 ELSE 0 END ) AS "trample_num", | ||
SUM ( application_chat_record.message_tokens + application_chat_record.answer_tokens ) as "tokens_num", | ||
"count"(application_chat_record."id") as chat_record_count, | ||
"count"(DISTINCT application_chat.client_id) customer_num, | ||
application_chat_record.create_time :: DATE as "day" | ||
FROM | ||
application_chat_record application_chat_record | ||
LEFT JOIN application_chat application_chat ON application_chat."id" = application_chat_record.chat_id | ||
${default_sql} | ||
GROUP BY "day" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
SELECT | ||
( SUM ( CASE WHEN create_time :: DATE = CURRENT_DATE THEN 1 ELSE 0 END ) ) AS "customer_today_added_count", | ||
COUNT ( "application_public_access_client"."id" ) AS "customer_added_count" | ||
FROM | ||
"application_public_access_client" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
SELECT | ||
COUNT ( "application_public_access_client"."id" ) AS "customer_added_count", | ||
create_time :: DATE as "day" | ||
FROM | ||
"application_public_access_client" | ||
${default_sql} | ||
GROUP BY "day" |
86 changes: 86 additions & 0 deletions
86
apps/application/swagger_api/application_statistics_api.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: maxkb | ||
@Author:虎 | ||
@file: application_statistics_api.py | ||
@date:2024/3/27 15:09 | ||
@desc: | ||
""" | ||
from drf_yasg import openapi | ||
|
||
from common.mixins.api_mixin import ApiMixin | ||
|
||
|
||
class ApplicationStatisticsApi(ApiMixin): | ||
@staticmethod | ||
def get_request_params_api(): | ||
return [openapi.Parameter(name='application_id', | ||
in_=openapi.IN_PATH, | ||
type=openapi.TYPE_STRING, | ||
required=True, | ||
description='应用id'), | ||
openapi.Parameter(name='start_time', | ||
in_=openapi.IN_QUERY, | ||
type=openapi.TYPE_STRING, | ||
required=True, | ||
description='开始时间'), | ||
openapi.Parameter(name='end_time', | ||
in_=openapi.IN_QUERY, | ||
type=openapi.TYPE_STRING, | ||
required=True, | ||
description='结束时间'), | ||
] | ||
|
||
class ChatRecordAggregate(ApiMixin): | ||
@staticmethod | ||
def get_response_body_api(): | ||
return openapi.Schema( | ||
type=openapi.TYPE_OBJECT, | ||
required=['star_num', 'trample_num', 'tokens_num', 'chat_record_count'], | ||
properties={ | ||
'star_num': openapi.Schema(type=openapi.TYPE_NUMBER, title="点赞数量", | ||
description="点赞数量"), | ||
|
||
'trample_num': openapi.Schema(type=openapi.TYPE_NUMBER, title="点踩数量", description="点踩数量"), | ||
'tokens_num': openapi.Schema(type=openapi.TYPE_NUMBER, title="token使用数量", | ||
description="token使用数量"), | ||
'chat_record_count': openapi.Schema(type=openapi.TYPE_NUMBER, title="对话次数", | ||
description="对话次数"), | ||
'customer_num': openapi.Schema(type=openapi.TYPE_NUMBER, title="客户数量", | ||
description="客户数量"), | ||
'customer_added_count': openapi.Schema(type=openapi.TYPE_NUMBER, title="客户新增数量", | ||
description="客户新增数量"), | ||
'day': openapi.Schema(type=openapi.TYPE_STRING, | ||
title="日期", | ||
description="日期,只有查询趋势的时候才有该字段"), | ||
} | ||
) | ||
|
||
class CustomerCountTrend(ApiMixin): | ||
@staticmethod | ||
def get_response_body_api(): | ||
return openapi.Schema( | ||
type=openapi.TYPE_OBJECT, | ||
required=['added_count'], | ||
properties={ | ||
'added_count': openapi.Schema(type=openapi.TYPE_NUMBER, title="新增数量", description="新增数量"), | ||
|
||
'day': openapi.Schema(type=openapi.TYPE_STRING, | ||
title="时间", | ||
description="时间"), | ||
} | ||
) | ||
|
||
class CustomerCount(ApiMixin): | ||
@staticmethod | ||
def get_response_body_api(): | ||
return openapi.Schema( | ||
type=openapi.TYPE_OBJECT, | ||
required=['added_count'], | ||
properties={ | ||
'today_added_count': openapi.Schema(type=openapi.TYPE_NUMBER, title="今日新增数量", | ||
description="今日新增数量"), | ||
'added_count': openapi.Schema(type=openapi.TYPE_NUMBER, title="新增数量", description="新增数量"), | ||
|
||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.