Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor: if to match (#31799)

Asuka Minato920db69ef2d5
diff --git a/api/commands.py b/api/commands.py
index 4b811fb1e6c..c4f2c9edbbe 100644
--- a/api/commands.py
+++ b/api/commands.py
@@ -1450,54 +1450,58 @@ def clear_orphaned_file_records(force: bool):
all_ids_in_tables = []
for ids_table in ids_tables:
query = ""
- if ids_table["type"] == "uuid":
- click.echo(
- click.style(
- f"- Listing file ids in column {ids_table['column']} in table {ids_table['table']}", fg="white"
+ match ids_table["type"]:
+ case "uuid":
+ click.echo(
+ click.style(
+ f"- Listing file ids in column {ids_table['column']} in table {ids_table['table']}",
+ fg="white",
+ )
)
- )
- query = (
- f"SELECT {ids_table['column']} FROM {ids_table['table']} WHERE {ids_table['column']} IS NOT NULL"
- )
- with db.engine.begin() as conn:
- rs = conn.execute(sa.text(query))
- for i in rs:
- all_ids_in_tables.append({"table": ids_table["table"], "id": str(i[0])})
- elif ids_table["type"] == "text":
- click.echo(
- click.style(
- f"- Listing file-id-like strings in column {ids_table['column']} in table {ids_table['table']}",
- fg="white",
+ c = ids_table["column"]
+ query = f"SELECT {c} FROM {ids_table['table']} WHERE {c} IS NOT NULL"
+ with db.engine.begin() as conn:
+ rs = conn.execute(sa.text(query))
+ for i in rs:
+ all_ids_in_tables.append({"table": ids_table["table"], "id": str(i[0])})
+ case "text":
+ t = ids_table["table"]
+ click.echo(
+ click.style(
+ f"- Listing file-id-like strings in column {ids_table['column']} in table {t}",
+ fg="white",
+ )
)
- )
- query = (
- f"SELECT regexp_matches({ids_table['column']}, '{guid_regexp}', 'g') AS extracted_id "
- f"FROM {ids_table['table']}"
- )
- with db.engine.begin() as conn:
- rs = conn.execute(sa.text(query))
- for i in rs:
- for j in i[0]:
- all_ids_in_tables.append({"table": ids_table["table"], "id": j})
- elif ids_table["type"] == "json":
- click.echo(
- click.style(
- (
- f"- Listing file-id-like JSON string in column {ids_table['column']} "
- f"in table {ids_table['table']}"
- ),
- fg="white",
+ query = (
+ f"SELECT regexp_matches({ids_table['column']}, '{guid_regexp}', 'g') AS extracted_id "
+ f"FROM {ids_table['table']}"
)
- )
- query = (
- f"SELECT regexp_matches({ids_table['column']}::text, '{guid_regexp}', 'g') AS extracted_id "
- f"FROM {ids_table['table']}"
- )
- with db.engine.begin() as conn:
- rs = conn.execute(sa.text(query))
- for i in rs:
- for j in i[0]:
- all_ids_in_tables.append({"table": ids_table["table"], "id": j})
+ with db.engine.begin() as conn:
+ rs = conn.execute(sa.text(query))
+ for i in rs:
+ for j in i[0]:
+ all_ids_in_tables.append({"table": ids_table["table"], "id": j})
+ case "json":
+ click.echo(
+ click.style(
+ (
+ f"- Listing file-id-like JSON string in column {ids_table['column']} "
+ f"in table {ids_table['table']}"
+ ),
+ fg="white",
+ )
+ )
+ query = (
+ f"SELECT regexp_matches({ids_table['column']}::text, '{guid_regexp}', 'g') AS extracted_id "
+ f"FROM {ids_table['table']}"
+ )
+ with db.engine.begin() as conn:
+ rs = conn.execute(sa.text(query))
+ for i in rs:
+ for j in i[0]:
+ all_ids_in_tables.append({"table": ids_table["table"], "id": j})
+ case _:
+ pass
click.echo(click.style(f"Found {len(all_ids_in_tables)} file ids in tables.", fg="white"))
except Exception as e:
@@ -1737,59 +1741,18 @@ def file_usage(
if src_filter != src:
continue
- if ids_table["type"] == "uuid":
- # Direct UUID match
- query = (
- f"SELECT {ids_table['pk_column']}, {ids_table['column']} "
- f"FROM {ids_table['table']} WHERE {ids_table['column']} IS NOT NULL"
- )
- with db.engine.begin() as conn:
- rs = conn.execute(sa.text(query))
- for row in rs:
- record_id = str(row[0])
- ref_file_id = str(row[1])
- if ref_file_id not in file_key_map:
- continue
- storage_key = file_key_map[ref_file_id]
-
- # Apply filters
- if file_id and ref_file_id != file_id:
- continue
- if key and not storage_key.endswith(key):
- continue
-
- # Only collect items within the requested page range
- if offset <= total_count < offset + limit:
- paginated_usages.append(
- {
- "src": f"{ids_table['table']}.{ids_table['column']}",
- "record_id": record_id,
- "file_id": ref_file_id,
- "key": storage_key,
- }
- )
- total_count += 1
-
- elif ids_table["type"] in ("text", "json"):
- # Extract UUIDs from text/json content
- column_cast = f"{ids_table['column']}::text" if ids_table["type"] == "json" else ids_table["column"]
- query = (
- f"SELECT {ids_table['pk_column']}, {column_cast} "
- f"FROM {ids_table['table']} WHERE {ids_table['column']} IS NOT NULL"
- )
- with db.engine.begin() as conn:
- rs = conn.execute(sa.text(query))
- for row in rs:
- record_id = str(row[0])
- content = str(row[1])
-
- # Find all UUIDs in the content
- import re
-
- uuid_pattern = re.compile(guid_regexp, re.IGNORECASE)
- matches = uuid_pattern.findall(content)
-
- for ref_file_id in matches:
+ match ids_table["type"]:
+ case "uuid":
+ # Direct UUID match
+ query = (
+ f"SELECT {ids_table['pk_column']}, {ids_table['column']} "
+ f"FROM {ids_table['table']} WHERE {ids_table['column']} IS NOT NULL"
+ )
+ with db.engine.begin() as conn:
+ rs = conn.execute(sa.text(query))
+ for row in rs:
+ record_id = str(row[0])
+ ref_file_id = str(row[1])
if ref_file_id not in file_key_map:
continue
storage_key = file_key_map[ref_file_id]
@@ -1812,6 +1775,50 @@ def file_usage(
)
total_count += 1
+ case "text" | "json":
+ # Extract UUIDs from text/json content
+ column_cast = f"{ids_table['column']}::text" if ids_table["type"] == "json" else ids_table["column"]
+ query = (
+ f"SELECT {ids_table['pk_column']}, {column_cast} "
+ f"FROM {ids_table['table']} WHERE {ids_table['column']} IS NOT NULL"
+ )
+ with db.engine.begin() as conn:
+ rs = conn.execute(sa.text(query))
+ for row in rs:
+ record_id = str(row[0])
+ content = str(row[1])
+
+ # Find all UUIDs in the content
+ import re
+
+ uuid_pattern = re.compile(guid_regexp, re.IGNORECASE)
+ matches = uuid_pattern.findall(content)
+
+ for ref_file_id in matches:
+ if ref_file_id not in file_key_map:
+ continue
+ storage_key = file_key_map[ref_file_id]
+
+ # Apply filters
+ if file_id and ref_file_id != file_id:
+ continue
+ if key and not storage_key.endswith(key):
+ continue
+
+ # Only collect items within the requested page range
+ if offset <= total_count < offset + limit:
+ paginated_usages.append(
+ {
+ "src": f"{ids_table['table']}.{ids_table['column']}",
+ "record_id": record_id,
+ "file_id": ref_file_id,
+ "key": storage_key,
+ }
+ )
+ total_count += 1
+ case _:
+ pass
+
# Output results
if output_json:
result = {
diff --git a/api/controllers/console/app/conversation.py b/api/controllers/console/app/conversation.py
index 55fdcb51e42..82cc957d045 100644
--- a/api/controllers/console/app/conversation.py
+++ b/api/controllers/console/app/conversation.py
@@ -508,16 +508,19 @@ class ChatConversationApi(Resource):
case "created_at" | "-created_at" | _:
query = query.where(Conversation.created_at <= end_datetime_utc)
- if args.annotation_status == "annotated":
- query = query.options(joinedload(Conversation.message_annotations)).join( # type: ignore
- MessageAnnotation, MessageAnnotation.conversation_id == Conversation.id
- )
- elif args.annotation_status == "not_annotated":
- query = (
- query.outerjoin(MessageAnnotation, MessageAnnotation.conversation_id == Conversation.id)
- .group_by(Conversation.id)
- .having(func.count(MessageAnnotation.id) == 0)
- )
+ match args.annotation_status:
+ case "annotated":
+ query = query.options(joinedload(Conversation.message_annotations)).join( # type: ignore
+ MessageAnnotation, MessageAnnotation.conversation_id == Conversation.id
+ )
+ case "not_annotated":
+ query = (
+ query.outerjoin(MessageAnnotation, MessageAnnotation.conversation_id == Conversation.id)
+ .group_by(Conversation.id)
+ .having(func.count(MessageAnnotation.id) == 0)
+ )
+ case "all":
+ pass
if app_model.mode == AppMode.ADVANCED_CHAT:
query = query.where(Conversation.invoke_from != InvokeFrom.DEBUGGER)
diff --git a/api/controllers/console/datasets/datasets_document.py b/api/controllers/console/datasets/datasets_document.py
index 6a0c9e5f77b..e8b8f2ec6d5 100644
--- a/api/controllers/console/datasets/datasets_document.py
+++ b/api/controllers/console/datasets/datasets_document.py
@@ -576,63 +576,62 @@ class DocumentBatchIndexingEstimateApi(DocumentResource):
if document.indexing_status in {"completed", "error"}:
raise DocumentAlreadyFinishedError()
data_source_info = document.data_source_info_dict
+ match document.data_source_type:
+ case "upload_file":
+ if not data_source_info:
+ continue
+ file_id = data_source_info["upload_file_id"]
+ file_detail = (
+ db.session.query(UploadFile)
+ .where(UploadFile.tenant_id == current_tenant_id, UploadFile.id == file_id)
+ .first()
+ )
- if document.data_source_type == "upload_file":
- if not data_source_info:
- continue
- file_id = data_source_info["upload_file_id"]
- file_detail = (
- db.session.query(UploadFile)
- .where(UploadFile.tenant_id == current_tenant_id, UploadFile.id == file_id)
- .first()
- )
-
- if file_detail is None:
- raise NotFound("File not found.")
-
- extract_setting = ExtractSetting(
- datasource_type=DatasourceType.FILE, upload_file=file_detail, document_model=document.doc_form
- )
- extract_settings.append(extract_setting)
+ if file_detail is None:
+ raise NotFound("File not found.")
- elif document.data_source_type == "notion_import":
- if not data_source_info:
- continue
- extract_setting = ExtractSetting(
- datasource_type=DatasourceType.NOTION,
- notion_info=NotionInfo.model_validate(
- {
- "credential_id": data_source_info.get("credential_id"),
- "notion_workspace_id": data_source_info["notion_workspace_id"],
- "notion_obj_id": data_source_info["notion_page_id"],
- "notion_page_type": data_source_info["type"],
- "tenant_id": current_tenant_id,
- }
- ),
- document_model=document.doc_form,
- )
- extract_settings.append(extract_setting)
- elif document.data_source_type == "website_crawl":
- if not data_source_info:
- continue
- extract_setting = ExtractSetting(
- datasource_type=DatasourceType.WEBSITE,
- website_info=WebsiteInfo.model_validate(
- {
- "provider": data_source_info["provider"],
- "job_id": data_source_info["job_id"],
- "url": data_source_info["url"],
- "tenant_id": current_tenant_id,
- "mode": data_source_info["mode"],
- "only_main_content": data_source_info["only_main_content"],
- }
- ),
- document_model=document.doc_form,
- )
- extract_settings.append(extract_setting)
+ extract_setting = ExtractSetting(
+ datasource_type=DatasourceType.FILE, upload_file=file_detail, document_model=document.doc_form
+ )
+ extract_settings.append(extract_setting)
+ case "notion_import":
+ if not data_source_info:
+ continue
+ extract_setting = ExtractSetting(
+ datasource_type=DatasourceType.NOTION,
+ notion_info=NotionInfo.model_validate(
+ {
+ "credential_id": data_source_info.get("credential_id"),
+ "notion_workspace_id": data_source_info["notion_workspace_id"],
+ "notion_obj_id": data_source_info["notion_page_id"],
+ "notion_page_type": data_source_info["type"],
+ "tenant_id": current_tenant_id,
+ }
+ ),
+ document_model=document.doc_form,
+ )
+ extract_settings.append(extract_setting)
+ case "website_crawl":
+ if not data_source_info:
+ continue
+ extract_setting = ExtractSetting(
+ datasource_type=DatasourceType.WEBSITE,
+ website_info=WebsiteInfo.model_validate(
+ {
+ "provider": data_source_info["provider"],
+ "job_id": data_source_info["job_id"],
+ "url": data_source_info["url"],
+ "tenant_id": current_tenant_id,
+ "mode": data_source_info["mode"],
+ "only_main_content": data_source_info["only_main_content"],
+ }
+ ),
+ document_model=document.doc_form,
+ )
+ extract_settings.append(extract_setting)
- else:
- raise ValueError("Data source type not support")
+ case _:
+ raise ValueError("Data source type not support")
indexing_runner = IndexingRunner()
try:
response = indexing_runner.indexing_estimate(
diff --git a/api/controllers/service_api/wraps.py b/api/controllers/service_api/wraps.py
index 24acced0d1a..e597a72fc0f 100644
--- a/api/controllers/service_api/wraps.py
+++ b/api/controllers/service_api/wraps.py
@@ -73,14 +73,14 @@ def validate_app_token(view: Callable[P, R] | None = None, *, fetch_user_arg: Fe
# If caller needs end-user context, attach EndUser to current_user
if fetch_user_arg:
- if fetch_user_arg.fetch_from == WhereisUserArg.QUERY:
- user_id = request.args.get("user")
- elif fetch_user_arg.fetch_from == WhereisUserArg.JSON:
- user_id = request.get_json().get("user")
- elif fetch_user_arg.fetch_from == WhereisUserArg.FORM:
- user_id = request.form.get("user")
- else:
- user_id = None
+ user_id = None
+ match fetch_user_arg.fetch_from:
+ case WhereisUserArg.QUERY:
+ user_id = request.args.get("user")
+ case WhereisUserArg.JSON:
+ user_id = request.get_json().get("user")
+ case WhereisUserArg.FORM:
+ user_id = request.form.get("user")
if not user_id and fetch_user_arg.required:
raise ValueError("Arg user must be provided.")