-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/add-langsmith-dotted-order' into deploy/dev
* feat/add-langsmith-dotted-order: feat: add langsmith dotted order Feat/clean message records (#10588) Feat/account not found (#10804)
- Loading branch information
Showing
9 changed files
with
183 additions
and
9 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
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
31 changes: 31 additions & 0 deletions
31
api/migrations/versions/2024_11_12_0925-01d6889832f7_add_created_at_index_for_messages.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,31 @@ | ||
"""add_created_at_index_for_messages | ||
Revision ID: 01d6889832f7 | ||
Revises: 09a8d1878d9b | ||
Create Date: 2024-11-12 09:25:05.527827 | ||
""" | ||
from alembic import op | ||
import models as models | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '01d6889832f7' | ||
down_revision = '09a8d1878d9b' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('messages', schema=None) as batch_op: | ||
batch_op.create_index('message_created_at_idx', ['created_at'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('messages', schema=None) as batch_op: | ||
batch_op.drop_index('message_created_at_idx') | ||
# ### end Alembic commands ### |
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,79 @@ | ||
import datetime | ||
import time | ||
|
||
import click | ||
from werkzeug.exceptions import NotFound | ||
|
||
import app | ||
from configs import dify_config | ||
from extensions.ext_database import db | ||
from extensions.ext_redis import redis_client | ||
from models.model import ( | ||
App, | ||
Message, | ||
MessageAgentThought, | ||
MessageAnnotation, | ||
MessageChain, | ||
MessageFeedback, | ||
MessageFile, | ||
) | ||
from models.web import SavedMessage | ||
from services.feature_service import FeatureService | ||
|
||
|
||
@app.celery.task(queue="dataset") | ||
def clean_messages(): | ||
click.echo(click.style("Start clean messages.", fg="green")) | ||
start_at = time.perf_counter() | ||
plan_sandbox_clean_message_day = datetime.datetime.now() - datetime.timedelta( | ||
days=dify_config.PLAN_SANDBOX_CLEAN_MESSAGE_DAY_SETTING | ||
) | ||
page = 1 | ||
while True: | ||
try: | ||
# Main query with join and filter | ||
messages = ( | ||
db.session.query(Message) | ||
.filter(Message.created_at < plan_sandbox_clean_message_day) | ||
.order_by(Message.created_at.desc()) | ||
.paginate(page=page, per_page=100) | ||
) | ||
|
||
except NotFound: | ||
break | ||
if messages.items is None or len(messages.items) == 0: | ||
break | ||
for message in messages.items: | ||
app = App.query.filter_by(id=message.app_id).first() | ||
features_cache_key = f"features:{app.tenant_id}" | ||
plan_cache = redis_client.get(features_cache_key) | ||
if plan_cache is None: | ||
features = FeatureService.get_features(app.tenant_id) | ||
redis_client.setex(features_cache_key, 600, features.billing.subscription.plan) | ||
plan = features.billing.subscription.plan | ||
else: | ||
plan = plan_cache.decode() | ||
if plan == "sandbox": | ||
# clean related message | ||
db.session.query(MessageFeedback).filter(MessageFeedback.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(MessageAnnotation).filter(MessageAnnotation.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(MessageChain).filter(MessageChain.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(MessageAgentThought).filter(MessageAgentThought.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(MessageFile).filter(MessageFile.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(SavedMessage).filter(SavedMessage.message_id == message.id).delete( | ||
synchronize_session=False | ||
) | ||
db.session.query(Message).filter(Message.id == message.id).delete() | ||
db.session.commit() | ||
end_at = time.perf_counter() | ||
click.echo(click.style("Cleaned unused dataset from db success latency: {}".format(end_at - start_at), fg="green")) |
Oops, something went wrong.