perf(api): eliminate unbounded in-memory queries and redundant DB fan-out across Analytics, Sales Chat, CRM, and Bio-Links
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- postgresql, redis, typescript
- 領域
- analytics, api, backend, databases, performance
調査の方向性
Start by reading the four named service files and tracing the query and response shapes for each endpoint. Run the existing API tests for analytics, sales chat, CRM exports, and bio-links to establish current behavior. Work through the listed query and pagination changes while preserving response shapes; done means bounded database work, accurate filtered pagination, and unchanged endpoint responses.
索引モデルが issue の本文から書いたものです。
説明
Problem Description
Four core read endpoints perform unbounded findMany queries, in-memory aggregations, or redundant query fan-out that work at <100 users but cause high latency, DB connection exhaustion, or Node OOM errors at 1,000–10,000+ users:
-
Analytics (
apps/api/src/analytics/analytics.service.ts,getAllDataL1166-1215 + 7 query methods):getAllDatafans out to 13 methods inPromise.all, redundantly executinggetOverview5x,getPipelineStagesAnalytics3x,getDailyTrends2x, andgetAccountIds15+x (~60-80 DB queries per page load).getPipelineStagesAnalytics,getTagDistributionAnalytics,getFollowUpEngineAnalytics,getIntentAnalytics,getOriginAttributionAnalytics, andgetSalesChatexecute unboundedfindManyqueries (several withouttimeRangefilters) and aggregate in Node.js memory.
-
Sales Chat (
apps/api/src/sales-chat/services/sales-chat-query.service.ts,getConversationsL127-335):- Tag, qualified/unqualified, and
followup_duefilters load all matchinginstagramUserProfile/pendingFollowUprows for the workspace into Node memory and pass largein/notInarrays to PostgreSQL. automationExecution.findMany(L310-335) omitsdistinct: ['instagramUserId'], loading all historical execution records for every conversation on the page.
- Tag, qualified/unqualified, and
-
CRM Email / Audience List (
apps/api/src/crm/services/crm-export.service.ts,getEmailListL42-95, L162-180, L250-258):- Every
GETrequest runs an unbounded "self-healing" sync over allinstagramUserProfilerows withemail != null || phone != null, issuing N+1findUnique+upsertqueries. allStatsCapturesloads all matchingcontactCapturerows into Node memory on every paginated request just to count emails and phones.channelFilter(email,phone,both) is applied in memory aftertake/skipandcount(), causing inaccuratetotal/hasMorevalues and short/empty pages.
- Every
-
Bio-Links (
apps/api/src/bio-links/bio-links.service.ts,getAnalyticsL733-903 &getBioPageLeadsL905-913):getAnalyticsloads everyBioAnalyticsEventrow for30d/90d/allinto Node memory and runsO(events * (links + buttons)).filter(...)loops. Viral pages (100k+ events) risk timeouts or OOM.getBioPageLeadsreturns all leads with no pagination (take/skip).
Expected Behavior
- All 4 endpoints return the exact same JSON response shapes (zero frontend breaking changes).
- Aggregations and counts are computed in PostgreSQL (
COUNT,GROUP BY,SUM) instead of loading unbounded tables into Node memory. AnalyticsService.getAllDataresolves shared sub-results once per request and optionally caches per-company responses safely in Redis (with graceful fallback when Redis is unavailable locally).getEmailListreturns accuratetotalandhasMorewhenchannelFilteris active, without running N+1 syncs on read requests.
Where to Look
apps/api/src/analytics/analytics.service.tsapps/api/src/sales-chat/services/sales-chat-query.service.tsapps/api/src/crm/services/crm-export.service.tsapps/api/src/bio-links/bio-links.service.ts
Solution / Implementation Guide (Zero-Regression Plan)
-
Analytics (
analytics.service.ts):- Compute
accountIds,overview,pipelineStages, anddailyTrendsonce ingetAllDataand pass them as optional precomputed parameters into dependent methods (keeping standalone method calls backward-compatible). - Replace unbounded
findMany+ JS loops withprisma.groupBy/count/ parameterized SQLGROUP BYqueries bounded bytimeRange. - Add a short-TTL Redis cache (
30-60s, keyed bycompanyId + timeRange) wrapped intry/catchso local dev without Redis still works cleanly.
- Compute
-
Sales Chat (
sales-chat-query.service.ts):- Add
distinct: ['instagramUserId'](ordered bystartedAt: 'desc') toautomationExecution.findMany. - Push tag, qualified, and
followup_duefilters into SQL/indexed Prismawhere/orderByclauses instead of loading full-company ID arrays into Node memory.
- Add
-
CRM Email List (
crm-export.service.ts):- Move the
instagramUserProfile -> contactCapturesync out ofGET /email-listinto write paths (or a one-time backfill script) so reads stay pureSELECTs. - Push
channelFilter(email,phone,both) into the PrismawhereClausebeforecount()andtake/skipso pagination andhasMoreare exact. - Replace
allStatsCapturesin-memory counting withprisma.contactCapture.count(...)/ SQLCOUNTaggregations.
- Move the
-
Bio-Links (
bio-links.service.ts):- Replace the full
bioAnalyticsEvent.findMany+ nested.filter(...)loops ingetAnalyticswith SQLGROUP BYaggregations (by day, type, device, referrer,linkId,platform), preserving the exact return object structure. - Add optional
limit/offsetpagination (defaulting to a safe cap like100) togetBioPageLeads.
- Replace the full
- 主要言語
- TypeScript
- スター
- 2
- フォーク
- 1
- 平均マージ
- 6分
- マージ済み PR(30日)
- 27
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
sumamakhan761/tryinstance のほかの issue
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#65 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
sumamakhan761/tryinstance#64 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
sumamakhan761/tryinstance#63 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#62 ·
メンテナーはふだん 1 日以内に返信
-
backend good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
sumamakhan761/tryinstance#60 ·
メンテナーはふだん 1 日以内に返信
sumamakhan761/tryinstance の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
makecindy/cindy#15342 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
kwitsch/claude-code-plugin-tooling#219 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信
-
no-repo-link tool-submission
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
BraveOPotato/NoSignups#2618 ·
-
enhancement good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100