chore: Modify database key

This commit is contained in:
Fangyin Cheng
2024-08-22 12:09:41 +08:00
parent de702ef8e6
commit 3d8e6e4d50
5 changed files with 42 additions and 34 deletions

View File

@@ -98,7 +98,8 @@ CREATE TABLE IF NOT EXISTS `chat_history`
`gmt_created` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'created time', `gmt_created` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'created time',
`gmt_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time', `gmt_modified` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time',
UNIQUE KEY `conv_uid` (`conv_uid`), UNIQUE KEY `conv_uid` (`conv_uid`),
PRIMARY KEY (`id`) PRIMARY KEY (`id`),
KEY `idx_chat_his_app_code` (`app_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT 'Chat history'; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT 'Chat history';
CREATE TABLE IF NOT EXISTS `chat_history_message` CREATE TABLE IF NOT EXISTS `chat_history_message`
@@ -446,7 +447,7 @@ CREATE TABLE `recommend_question` (
`params` text DEFAULT NULL COMMENT 'question param', `params` text DEFAULT NULL COMMENT 'question param',
`is_hot_question` varchar(10) DEFAULT 'false' COMMENT 'Is it a popular recommendation question?', `is_hot_question` varchar(10) DEFAULT 'false' COMMENT 'Is it a popular recommendation question?',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_app_code` (`app_code`) KEY `idx_rec_q_app_code` (`app_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="AI application related recommendation issues"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="AI application related recommendation issues";
-- dbgpt.user_recent_apps definition -- dbgpt.user_recent_apps definition
@@ -459,7 +460,7 @@ CREATE TABLE `user_recent_apps` (
`user_code` varchar(255) DEFAULT NULL COMMENT 'user code', `user_code` varchar(255) DEFAULT NULL COMMENT 'user code',
`sys_code` varchar(255) DEFAULT NULL COMMENT 'system app code', `sys_code` varchar(255) DEFAULT NULL COMMENT 'system app code',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_app_code` (`app_code`), KEY `idx_user_r_app_code` (`app_code`),
KEY `idx_last_accessed` (`last_accessed`), KEY `idx_last_accessed` (`last_accessed`),
KEY `idx_user_code` (`user_code`) KEY `idx_user_code` (`user_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User recently used apps' ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User recently used apps'

View File

@@ -52,7 +52,7 @@ CREATE TABLE `recommend_question` (
`params` text DEFAULT NULL COMMENT 'question param', `params` text DEFAULT NULL COMMENT 'question param',
`is_hot_question` varchar(10) DEFAULT 'false' COMMENT 'Is it a popular recommendation question?', `is_hot_question` varchar(10) DEFAULT 'false' COMMENT 'Is it a popular recommendation question?',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_app_code` (`app_code`) KEY `idx_rec_q_app_code` (`app_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="AI application related recommendation issues"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT="AI application related recommendation issues";
-- dbgpt.user_recent_apps definition -- dbgpt.user_recent_apps definition
@@ -65,7 +65,7 @@ CREATE TABLE `user_recent_apps` (
`user_code` varchar(255) DEFAULT NULL COMMENT 'user code', `user_code` varchar(255) DEFAULT NULL COMMENT 'user code',
`sys_code` varchar(255) DEFAULT NULL COMMENT 'system app code', `sys_code` varchar(255) DEFAULT NULL COMMENT 'system app code',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `idx_app_code` (`app_code`), KEY `idx_user_r_app_code` (`app_code`),
KEY `idx_last_accessed` (`last_accessed`), KEY `idx_last_accessed` (`last_accessed`),
KEY `idx_user_code` (`user_code`) KEY `idx_user_code` (`user_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User recently used apps'; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User recently used apps';

View File

@@ -87,7 +87,7 @@ class RecommendQuestionEntity(Model):
default=False, default=False,
comment="hot question would be displayed on the main page.", comment="hot question would be displayed on the main page.",
) )
__table_args__ = (Index("idx_app_code", "app_code"),) __table_args__ = (Index("idx_rec_q_app_code", "app_code"),)
class RecommendQuestionDao(BaseDao): class RecommendQuestionDao(BaseDao):

View File

@@ -268,7 +268,7 @@ class UserRecentAppsEntity(Model):
) )
last_accessed = Column(DateTime, default=None, comment="last access time") last_accessed = Column(DateTime, default=None, comment="last access time")
__table_args__ = ( __table_args__ = (
Index("idx_app_code", "app_code"), Index("idx_user_r_app_code", "app_code"),
Index("idx_user_code", "user_code"), Index("idx_user_code", "user_code"),
Index("idx_last_accessed", "last_accessed"), Index("idx_last_accessed", "last_accessed"),
) )
@@ -451,9 +451,11 @@ class UserRecentAppsDao(BaseDao):
"sys_code": sys_code, "sys_code": sys_code,
"user_code": user_code, "user_code": user_code,
"last_accessed": last_accessed, "last_accessed": last_accessed,
"gmt_create": existing_app.gmt_create "gmt_create": (
if existing_app existing_app.gmt_create
else new_app.gmt_create, if existing_app
else new_app.gmt_create
),
"gmt_modified": last_accessed, "gmt_modified": last_accessed,
} }
) )
@@ -655,9 +657,9 @@ class GptsAppDao(BaseDao):
apps = sorted( apps = sorted(
apps, apps,
key=lambda obj: float("-inf") key=lambda obj: (
if obj.hot_value is None float("-inf") if obj.hot_value is None else obj.hot_value
else obj.hot_value, ),
reverse=True, reverse=True,
) )
app_resp.total_count = total_count app_resp.total_count = total_count
@@ -696,19 +698,19 @@ class GptsAppDao(BaseDao):
for item in app_details for item in app_details
], ],
"published": app_info.published, "published": app_info.published,
"param_need": json.loads(app_info.param_need) "param_need": (
if app_info.param_need json.loads(app_info.param_need) if app_info.param_need else None
else None, ),
"hot_value": hot_app_map.get(app_info.app_code, 0) "hot_value": (
if hot_app_map is not None hot_app_map.get(app_info.app_code, 0) if hot_app_map is not None else 0
else 0, ),
"owner_name": app_info.user_code, "owner_name": app_info.user_code,
"owner_avatar_url": owner_avatar_url, "owner_avatar_url": owner_avatar_url,
"recommend_questions": [ "recommend_questions": (
RecommendQuestion.from_entity(item) for item in recommend_questions [RecommendQuestion.from_entity(item) for item in recommend_questions]
] if recommend_questions
if recommend_questions else []
else [], ),
"admins": [], "admins": [],
} }
@@ -848,9 +850,9 @@ class GptsAppDao(BaseDao):
updated_at=gpts_app.updated_at, updated_at=gpts_app.updated_at,
icon=gpts_app.icon, icon=gpts_app.icon,
published="true" if gpts_app.published else "false", published="true" if gpts_app.published else "false",
param_need=json.dumps(gpts_app.param_need) param_need=(
if gpts_app.param_need json.dumps(gpts_app.param_need) if gpts_app.param_need else None
else None, ),
) )
session.add(app_entity) session.add(app_entity)
@@ -869,9 +871,11 @@ class GptsAppDao(BaseDao):
resources=json.dumps(resource_dicts, ensure_ascii=False), resources=json.dumps(resource_dicts, ensure_ascii=False),
prompt_template=item.prompt_template, prompt_template=item.prompt_template,
llm_strategy=item.llm_strategy, llm_strategy=item.llm_strategy,
llm_strategy_value=None llm_strategy_value=(
if item.llm_strategy_value is None None
else json.dumps(tuple(item.llm_strategy_value.split(","))), if item.llm_strategy_value is None
else json.dumps(tuple(item.llm_strategy_value.split(",")))
),
created_at=item.created_at, created_at=item.created_at,
updated_at=item.updated_at, updated_at=item.updated_at,
) )
@@ -936,9 +940,11 @@ class GptsAppDao(BaseDao):
resources=json.dumps(resource_dicts, ensure_ascii=False), resources=json.dumps(resource_dicts, ensure_ascii=False),
prompt_template=item.prompt_template, prompt_template=item.prompt_template,
llm_strategy=item.llm_strategy, llm_strategy=item.llm_strategy,
llm_strategy_value=None llm_strategy_value=(
if item.llm_strategy_value is None None
else json.dumps(tuple(item.llm_strategy_value.split(","))), if item.llm_strategy_value is None
else json.dumps(tuple(item.llm_strategy_value.split(",")))
),
created_at=item.created_at, created_at=item.created_at,
updated_at=item.updated_at, updated_at=item.updated_at,
) )

View File

@@ -1,4 +1,5 @@
"""Chat history database model.""" """Chat history database model."""
import logging import logging
from datetime import datetime from datetime import datetime
from typing import Optional from typing import Optional
@@ -56,7 +57,7 @@ class ChatHistoryEntity(Model):
Index("idx_q_user", "user_name") Index("idx_q_user", "user_name")
Index("idx_q_mode", "chat_mode") Index("idx_q_mode", "chat_mode")
Index("idx_q_conv", "summary") Index("idx_q_conv", "summary")
Index("idx_app_code", "app_code") Index("idx_chat_his_app_code", "app_code")
class ChatHistoryMessageEntity(Model): class ChatHistoryMessageEntity(Model):