Skip to content

Private/issue 1096 api latency logging#1965

Draft
ljluestc wants to merge 15 commits into
apecloud:mainfrom
ljluestc:private/issue-1096-api-latency-logging
Draft

Private/issue 1096 api latency logging#1965
ljluestc wants to merge 15 commits into
apecloud:mainfrom
ljluestc:private/issue-1096-api-latency-logging

Conversation

@ljluestc

Copy link
Copy Markdown

Summary

This PR updates marketplace collection graph pages to use marketplace-safe graph APIs and improves graph page layout behavior on shorter screens.

What changed

  • Render the marketplace collection /graph page with the new graph-hybrid view.
  • Add read-only marketplace graph endpoints for embedding-map and entity-search so published marketplace collections do not call workspace-only graph APIs.
  • Let CollectionGraphHybrid switch between workspace and marketplace data sources.
  • Give graph pages a 720px minimum graph area so shorter screens can scroll instead of compressing the canvas.

Why

Marketplace collection graph pages should not depend on workspace-only APIs. This change introduces marketplace-safe read-only graph endpoints and updates frontend routing/data-source behavior so graph rendering works correctly for published collections while preserving workspace behavior.

Validation

  • make openapi-check
  • uv run ruff check aperag/domains/marketplace/api/routes.py
  • yarn type-check
  • yarn lint (passes with pre-existing unrelated warnings)

earayu and others added 13 commits March 5, 2026 11:18
Adds identical ChatGPT-style interface from kubernetes_rag,
served at /chatgpt-ui/ with SSO and knowledge graph integration.
Describe MCP entry, REST API, execute_search_flow DAG construction,
FlowEngine (topological sort, parallel groups), and retrieval runners.

Made-with: Cursor
… label) (apecloud#1456)

GitHub Mermaid treats unquoted { as diamond node start; use <collection_id>
in quoted edge text and add a short note linking to OpenAPI path form.

Made-with: Cursor
* docs: remove hosted live demo line from README

Drop the "Try ApeRAG Live Demo" promotional line; README still documents
self-hosted usage and MCP examples.

Made-with: Cursor

* docs: align zh README and integration examples with self-hosted URLs

- Remove Chinese hosted-demo line from README-zh.md
- Use localhost MCP URL in README & MCP API examples; Dify guide points to
  Quick Start + local web/API instead of a fixed public host

Made-with: Cursor
Closes apecloud#1096

## Log and Store API Latency

- Add LatencyLoggingMiddleware (aperag/middleware/latency.py) that wraps
  every HTTP request, logs 'METHOD path status Nms' at INFO level, and
  sets an X-Response-Time response header.  Health/docs paths are
  logged at DEBUG to reduce noise.

- Add duration_ms column (BigInteger, nullable) to AuditLog so the
  pre-computed latency is stored alongside start_time/end_time.  An index
  on duration_ms makes slow-request queries efficient.

- audit_service.log_audit() now computes duration_ms = end_time -
  start_time before opening the DB session, emits a structured INFO log
  line ('audit METHOD path api_name status=N duration_ms=N'), and stores
  the value in the new column.

- audit_decorator.audit() logs 'api METHOD path (api_name) status=200
  duration_ms=N' immediately after each decorated endpoint returns,
  giving a per-endpoint latency line independently of the async DB write.

- list_audit_logs sort_mapping now uses AuditLog.duration_ms (a real
  column) instead of the SQL expression end_time - start_time.
  Existing NULL rows are back-filled on the fly during listing.

## Refactor Long Operations Out of Session Context

- Replace the 'async for session in get_async_session(): ... break'
  antipattern in audit_service with async with self._make_session(),
  which uses SQLAlchemy's AsyncSession context manager directly.

- All expensive work (JSON serialisation, duration calculation, resource
  ID extraction) is now performed outside the session so DB connections
  are held only for the actual INSERT/SELECT.

## Migration

aperag/migration/versions/20251001120000-add_duration_ms_to_audit_log.py
adds the column and index; downgrade removes both.
… decorator

- tests/unit/test_latency_middleware.py: 6 tests for LatencyLoggingMiddleware
  covering X-Response-Time header presence/format, body preservation,
  INFO vs DEBUG log level for normal vs skip paths, and log line content.
- tests/unit/test_audit_service.py: 6 tests for AuditService verifying
  duration_ms computation, DB persistence, commit lifecycle, error
  swallowing, and list_audit_logs back-fill for legacy rows.
- tests/unit/test_audit_decorator.py: 6 tests for the @Audit decorator
  covering start/end time recording, non-negative duration, status 200 on
  success, status 500 + re-raise on error, GET-request skip, and
  api_name defaulting to the function name.
- Fix latency.py: initialise response=None before the try block and guard
  the X-Response-Time header assignment so the finally branch does not
  raise NameError when call_next raises before returning a Response.
@apecloud-bot apecloud-bot added the size/XXL Denotes a PR that changes 1000+ lines. label Jul 13, 2026
ljluestc and others added 2 commits July 12, 2026 22:51
Co-Authored-By: Oz <oz-agent@warp.dev>
…d 720px min height

- Add search_entities() and get_embedding_map() methods to GraphService
- Add workspace endpoints: GET /collections/{id}/graphs/embedding-map and entity-search
- Create aperag/domains/marketplace/api/routes.py with read-only marketplace
  versions of embedding-map and entity-search (resolves to owner's user_id)
- Register new marketplace domain router in aperag/app.py
- Update OpenAPI specs with path entries for all four new endpoints
- Add make openapi-check target (redocly bundle structural validation)
- Create CollectionGraphHybrid component wrapping CollectionGraph with
  marketplace data-source switching and 720px min-height layout
- Update marketplace /graph page to use CollectionGraphHybrid
- Change h-[calc(...)] to min-h-[calc(...)] on both workspace and marketplace
  graph pages so shorter screens can scroll instead of compressing the canvas

Co-Authored-By: Oz <oz-agent@warp.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XXL Denotes a PR that changes 1000+ lines.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants