@kernel-sig/console 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +50 -0
- package/LICENSE +127 -0
- package/Makefile +66 -0
- package/README.md +279 -0
- package/backend/.dockerignore +12 -0
- package/backend/Dockerfile +43 -0
- package/backend/alembic/env.py +75 -0
- package/backend/alembic/script.py.mako +25 -0
- package/backend/alembic/versions/0001_initial.py +88 -0
- package/backend/alembic/versions/350e2d9b6553_add_pr_comment_table.py +48 -0
- package/backend/alembic/versions/4f8342e727fe_sig_info_roster_fields_and_branch_.py +39 -0
- package/backend/alembic/versions/5a31ade90136_add_repository_credential_pull_request_.py +293 -0
- package/backend/alembic/versions/87a008142f17_add_classification_attention_and_ai_.py +436 -0
- package/backend/alembic/versions/a1c7d90e4b52_branch_belongs_to_a_repository.py +72 -0
- package/backend/alembic/versions/b41c9d7e5f28_extend_pr_kind_categories.py +43 -0
- package/backend/alembic/versions/c8e4f2a71b93_add_release_kind.py +40 -0
- package/backend/alembic/versions/d1275091dfdc_add_needs_detail_flag_to_pull_request.py +44 -0
- package/backend/alembic/versions/e5b27c9d3a41_downgrade_cve_without_ids.py +54 -0
- package/backend/alembic/versions/ee3159a4eff0_add_sig_meeting_member_and_release_.py +164 -0
- package/backend/alembic/versions/fcf3c186d63b_attention_rule_subscriptions.py +42 -0
- package/backend/alembic.ini +40 -0
- package/backend/app/__init__.py +0 -0
- package/backend/app/api/__init__.py +0 -0
- package/backend/app/api/deps.py +74 -0
- package/backend/app/api/v1/__init__.py +0 -0
- package/backend/app/api/v1/ai.py +386 -0
- package/backend/app/api/v1/analytics.py +48 -0
- package/backend/app/api/v1/attention.py +142 -0
- package/backend/app/api/v1/auth.py +106 -0
- package/backend/app/api/v1/classification.py +192 -0
- package/backend/app/api/v1/health.py +29 -0
- package/backend/app/api/v1/issues.py +143 -0
- package/backend/app/api/v1/pulls.py +388 -0
- package/backend/app/api/v1/repositories.py +273 -0
- package/backend/app/api/v1/router.py +32 -0
- package/backend/app/api/v1/sig.py +565 -0
- package/backend/app/api/v1/users.py +87 -0
- package/backend/app/api/v1/webhooks.py +135 -0
- package/backend/app/core/__init__.py +0 -0
- package/backend/app/core/config.py +72 -0
- package/backend/app/core/crypto.py +74 -0
- package/backend/app/core/db.py +79 -0
- package/backend/app/core/exceptions.py +60 -0
- package/backend/app/core/logging.py +69 -0
- package/backend/app/core/permissions.py +87 -0
- package/backend/app/core/queue.py +57 -0
- package/backend/app/core/security.py +69 -0
- package/backend/app/domain/__init__.py +0 -0
- package/backend/app/domain/attention.py +618 -0
- package/backend/app/domain/classification.py +689 -0
- package/backend/app/domain/meeting.py +408 -0
- package/backend/app/domain/release.py +163 -0
- package/backend/app/domain/review.py +416 -0
- package/backend/app/domain/sig.py +178 -0
- package/backend/app/domain/sig_info.py +201 -0
- package/backend/app/integrations/__init__.py +0 -0
- package/backend/app/integrations/atomgit/__init__.py +0 -0
- package/backend/app/integrations/atomgit/client.py +505 -0
- package/backend/app/integrations/atomgit/models.py +311 -0
- package/backend/app/integrations/llm/__init__.py +0 -0
- package/backend/app/integrations/llm/prompts.py +222 -0
- package/backend/app/integrations/llm/provider.py +326 -0
- package/backend/app/main.py +242 -0
- package/backend/app/middleware/__init__.py +0 -0
- package/backend/app/middleware/audit.py +129 -0
- package/backend/app/middleware/request_context.py +42 -0
- package/backend/app/models/__init__.py +112 -0
- package/backend/app/models/ai.py +214 -0
- package/backend/app/models/attention.py +131 -0
- package/backend/app/models/attention_settings.py +61 -0
- package/backend/app/models/audit.py +53 -0
- package/backend/app/models/base.py +35 -0
- package/backend/app/models/classification.py +144 -0
- package/backend/app/models/credential.py +56 -0
- package/backend/app/models/issue.py +132 -0
- package/backend/app/models/meeting.py +189 -0
- package/backend/app/models/pull_request.py +288 -0
- package/backend/app/models/repository.py +196 -0
- package/backend/app/models/sig.py +194 -0
- package/backend/app/models/user.py +44 -0
- package/backend/app/schemas/__init__.py +0 -0
- package/backend/app/schemas/ai.py +120 -0
- package/backend/app/schemas/attention.py +43 -0
- package/backend/app/schemas/auth.py +26 -0
- package/backend/app/schemas/classification.py +58 -0
- package/backend/app/schemas/common.py +43 -0
- package/backend/app/schemas/pull_request.py +214 -0
- package/backend/app/schemas/repository.py +94 -0
- package/backend/app/schemas/sig.py +246 -0
- package/backend/app/schemas/user.py +79 -0
- package/backend/app/services/__init__.py +0 -0
- package/backend/app/services/ai_service.py +569 -0
- package/backend/app/services/analytics_service.py +390 -0
- package/backend/app/services/attention_queue.py +451 -0
- package/backend/app/services/attention_service.py +432 -0
- package/backend/app/services/auth_service.py +74 -0
- package/backend/app/services/classification_service.py +658 -0
- package/backend/app/services/credential_service.py +105 -0
- package/backend/app/services/pull_query.py +273 -0
- package/backend/app/services/release_service.py +446 -0
- package/backend/app/services/repository_service.py +132 -0
- package/backend/app/services/sig_service.py +385 -0
- package/backend/app/services/sync_service.py +752 -0
- package/backend/app/services/user_service.py +68 -0
- package/backend/app/worker.py +389 -0
- package/backend/entrypoint.sh +10 -0
- package/backend/pyproject.toml +68 -0
- package/backend/tests/test_analysis_api.py +154 -0
- package/backend/tests/test_atomgit_client.py +360 -0
- package/backend/tests/test_atomgit_models.py +257 -0
- package/backend/tests/test_attention.py +291 -0
- package/backend/tests/test_audit_middleware.py +135 -0
- package/backend/tests/test_classification.py +498 -0
- package/backend/tests/test_config.py +41 -0
- package/backend/tests/test_crypto.py +68 -0
- package/backend/tests/test_exceptions.py +62 -0
- package/backend/tests/test_health.py +63 -0
- package/backend/tests/test_llm_provider.py +320 -0
- package/backend/tests/test_meeting_domain.py +169 -0
- package/backend/tests/test_permissions.py +69 -0
- package/backend/tests/test_pull_query_wiring.py +66 -0
- package/backend/tests/test_pull_schemas.py +82 -0
- package/backend/tests/test_release_domain.py +82 -0
- package/backend/tests/test_review_parser.py +301 -0
- package/backend/tests/test_schemas_user.py +97 -0
- package/backend/tests/test_security.py +92 -0
- package/cli/index.js +338 -0
- package/compose.yaml +105 -0
- package/frontend/.dockerignore +4 -0
- package/frontend/Dockerfile +27 -0
- package/frontend/index.html +14 -0
- package/frontend/nginx.conf +47 -0
- package/frontend/package-lock.json +5020 -0
- package/frontend/package.json +35 -0
- package/frontend/src/api/ai.ts +124 -0
- package/frontend/src/api/analytics.ts +66 -0
- package/frontend/src/api/attention.ts +181 -0
- package/frontend/src/api/auth.ts +74 -0
- package/frontend/src/api/classification.ts +170 -0
- package/frontend/src/api/issues.ts +90 -0
- package/frontend/src/api/pulls.ts +233 -0
- package/frontend/src/api/repositories.ts +95 -0
- package/frontend/src/api/sig.ts +232 -0
- package/frontend/src/app/antd-theme.ts +94 -0
- package/frontend/src/app/providers.tsx +59 -0
- package/frontend/src/app/router.tsx +411 -0
- package/frontend/src/app/search.ts +30 -0
- package/frontend/src/components/ClassificationBadge.tsx +58 -0
- package/frontend/src/components/GateBadge.tsx +13 -0
- package/frontend/src/components/SeverityBadge.tsx +20 -0
- package/frontend/src/components/layout/AppShell.tsx +16 -0
- package/frontend/src/components/layout/AuthLayout.tsx +32 -0
- package/frontend/src/components/layout/Sidebar.tsx +223 -0
- package/frontend/src/components/layout/TopBar.tsx +47 -0
- package/frontend/src/components/pulls/DiscussionTimeline.tsx +145 -0
- package/frontend/src/components/pulls/FacetRail.tsx +199 -0
- package/frontend/src/components/pulls/LabelChips.tsx +87 -0
- package/frontend/src/components/ui/alert.tsx +30 -0
- package/frontend/src/components/ui/badge.tsx +53 -0
- package/frontend/src/components/ui/button.tsx +51 -0
- package/frontend/src/components/ui/card.tsx +64 -0
- package/frontend/src/components/ui/chart-theme.ts +65 -0
- package/frontend/src/components/ui/data-table.tsx +39 -0
- package/frontend/src/components/ui/echart.tsx +70 -0
- package/frontend/src/components/ui/empty-state.tsx +22 -0
- package/frontend/src/components/ui/input.tsx +39 -0
- package/frontend/src/components/ui/lazy-chart.tsx +21 -0
- package/frontend/src/components/ui/skeleton.tsx +19 -0
- package/frontend/src/hooks/use-current-repository.ts +44 -0
- package/frontend/src/hooks/use-current-user.ts +38 -0
- package/frontend/src/lib/api-client.ts +93 -0
- package/frontend/src/lib/css-color.ts +60 -0
- package/frontend/src/lib/utils.ts +45 -0
- package/frontend/src/main.tsx +22 -0
- package/frontend/src/pages/attention/AttentionQueuePage.tsx +316 -0
- package/frontend/src/pages/attention/RuleSettingsPanel.tsx +177 -0
- package/frontend/src/pages/branches/BranchDetailPage.tsx +632 -0
- package/frontend/src/pages/branches/BranchListPage.tsx +308 -0
- package/frontend/src/pages/dashboard/DashboardPage.tsx +657 -0
- package/frontend/src/pages/issues/IssueListPage.tsx +284 -0
- package/frontend/src/pages/login/LoginPage.tsx +95 -0
- package/frontend/src/pages/meetings/MeetingDetailPage.tsx +403 -0
- package/frontend/src/pages/meetings/MeetingListPage.tsx +264 -0
- package/frontend/src/pages/members/MembersPage.tsx +534 -0
- package/frontend/src/pages/pulls/PullDetailPage.tsx +833 -0
- package/frontend/src/pages/pulls/PullListPage.tsx +399 -0
- package/frontend/src/pages/settings/AiSettingsPage.tsx +449 -0
- package/frontend/src/pages/settings/SettingsPage.tsx +321 -0
- package/frontend/src/pages/setup/SetupPage.tsx +144 -0
- package/frontend/src/styles/globals.css +147 -0
- package/frontend/tsconfig.json +22 -0
- package/frontend/vite.config.ts +57 -0
- package/package.json +48 -0
- package/scripts/e2e-auth-flow.py +131 -0
- package/scripts/e2e-pr-detail.py +128 -0
- package/scripts/e2e-verify.py +773 -0
- package/scripts/verify-ai-pipeline.py +616 -0
- package/scripts/verify-analysis-pipeline.py +303 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"""统计分析服务。
|
|
2
|
+
|
|
3
|
+
把已同步的数据转成维护者可决策的指标。
|
|
4
|
+
|
|
5
|
+
设计约束:所有聚合在数据库内完成。PR 表已有 1100+ 行且会持续增长,
|
|
6
|
+
把行拉到应用层再用 Python 聚合会在数据量上来后迅速劣化。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import UTC, datetime, timedelta
|
|
11
|
+
|
|
12
|
+
from sqlalchemy import func, select
|
|
13
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
14
|
+
|
|
15
|
+
from app.domain.review import GateStage, is_bot
|
|
16
|
+
from app.models.issue import Issue
|
|
17
|
+
from app.models.pull_request import PRFile, PullRequest, ReviewEvent, ReviewEventType
|
|
18
|
+
from app.models.repository import Repository
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async def overview(session: AsyncSession, repository_id: uuid.UUID) -> dict:
|
|
22
|
+
"""总览指标。"""
|
|
23
|
+
pr_counts = dict(
|
|
24
|
+
(
|
|
25
|
+
await session.execute(
|
|
26
|
+
select(PullRequest.state, func.count())
|
|
27
|
+
.where(PullRequest.repository_id == repository_id)
|
|
28
|
+
.group_by(PullRequest.state)
|
|
29
|
+
)
|
|
30
|
+
).all()
|
|
31
|
+
)
|
|
32
|
+
issue_counts = dict(
|
|
33
|
+
(
|
|
34
|
+
await session.execute(
|
|
35
|
+
select(Issue.state, func.count())
|
|
36
|
+
.where(Issue.repository_id == repository_id)
|
|
37
|
+
.group_by(Issue.state)
|
|
38
|
+
)
|
|
39
|
+
).all()
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
now = datetime.now(UTC)
|
|
43
|
+
|
|
44
|
+
async def pr_count_since(delta: timedelta) -> int:
|
|
45
|
+
return (
|
|
46
|
+
await session.scalar(
|
|
47
|
+
select(func.count())
|
|
48
|
+
.select_from(PullRequest)
|
|
49
|
+
.where(
|
|
50
|
+
PullRequest.repository_id == repository_id,
|
|
51
|
+
PullRequest.atomgit_created_at >= now - delta,
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
or 0
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
merged_recently = (
|
|
58
|
+
await session.scalar(
|
|
59
|
+
select(func.count())
|
|
60
|
+
.select_from(PullRequest)
|
|
61
|
+
.where(
|
|
62
|
+
PullRequest.repository_id == repository_id,
|
|
63
|
+
PullRequest.merged_at.is_not(None),
|
|
64
|
+
PullRequest.merged_at >= now - timedelta(days=7),
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
or 0
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
ready = (
|
|
71
|
+
await session.scalar(
|
|
72
|
+
select(func.count())
|
|
73
|
+
.select_from(PullRequest)
|
|
74
|
+
.where(
|
|
75
|
+
PullRequest.repository_id == repository_id,
|
|
76
|
+
PullRequest.gate_stage == GateStage.READY_TO_MERGE.value,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
or 0
|
|
80
|
+
)
|
|
81
|
+
blocked = (
|
|
82
|
+
await session.scalar(
|
|
83
|
+
select(func.count())
|
|
84
|
+
.select_from(PullRequest)
|
|
85
|
+
.where(
|
|
86
|
+
PullRequest.repository_id == repository_id,
|
|
87
|
+
PullRequest.state == "open",
|
|
88
|
+
PullRequest.gate_stage.in_(
|
|
89
|
+
[
|
|
90
|
+
GateStage.CI_FAILED.value,
|
|
91
|
+
GateStage.CLA_DENIED.value,
|
|
92
|
+
GateStage.REJECTED.value,
|
|
93
|
+
]
|
|
94
|
+
),
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
or 0
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
"pulls": {
|
|
102
|
+
"open": pr_counts.get("open", 0),
|
|
103
|
+
"merged": pr_counts.get("merged", 0),
|
|
104
|
+
"closed": pr_counts.get("closed", 0),
|
|
105
|
+
"created_last_7d": await pr_count_since(timedelta(days=7)),
|
|
106
|
+
"created_last_30d": await pr_count_since(timedelta(days=30)),
|
|
107
|
+
"merged_last_7d": merged_recently,
|
|
108
|
+
"ready_to_merge": ready,
|
|
109
|
+
"blocked": blocked,
|
|
110
|
+
},
|
|
111
|
+
"issues": {
|
|
112
|
+
"open": issue_counts.get("open", 0),
|
|
113
|
+
"closed": issue_counts.get("closed", 0),
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def waiting_time_stats(session: AsyncSession, repository_id: uuid.UUID) -> dict:
|
|
119
|
+
"""等待时长统计。
|
|
120
|
+
|
|
121
|
+
只看仍未评审的 PR:把已评审的混进来会把平均值拉平,
|
|
122
|
+
掩盖"确实有一批 PR 长期没人看"这个事实。
|
|
123
|
+
"""
|
|
124
|
+
now = datetime.now(UTC)
|
|
125
|
+
age_expr = func.extract("epoch", now - PullRequest.atomgit_created_at) / 86400.0
|
|
126
|
+
|
|
127
|
+
row = (
|
|
128
|
+
await session.execute(
|
|
129
|
+
select(
|
|
130
|
+
func.count(),
|
|
131
|
+
func.avg(age_expr),
|
|
132
|
+
func.percentile_cont(0.5).within_group(age_expr),
|
|
133
|
+
func.max(age_expr),
|
|
134
|
+
).where(
|
|
135
|
+
PullRequest.repository_id == repository_id,
|
|
136
|
+
PullRequest.state == "open",
|
|
137
|
+
PullRequest.gate_stage == GateStage.REVIEW_PENDING.value,
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
).one()
|
|
141
|
+
|
|
142
|
+
over_30 = (
|
|
143
|
+
await session.scalar(
|
|
144
|
+
select(func.count())
|
|
145
|
+
.select_from(PullRequest)
|
|
146
|
+
.where(
|
|
147
|
+
PullRequest.repository_id == repository_id,
|
|
148
|
+
PullRequest.state == "open",
|
|
149
|
+
PullRequest.gate_stage == GateStage.REVIEW_PENDING.value,
|
|
150
|
+
age_expr > 30,
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
or 0
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
"pending_review_count": row[0] or 0,
|
|
158
|
+
"avg_waiting_days": round(float(row[1]), 1) if row[1] is not None else 0.0,
|
|
159
|
+
"median_waiting_days": round(float(row[2]), 1) if row[2] is not None else 0.0,
|
|
160
|
+
"max_waiting_days": round(float(row[3]), 1) if row[3] is not None else 0.0,
|
|
161
|
+
"over_30_days": over_30,
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
async def reviewer_workload(
|
|
166
|
+
session: AsyncSession, repository_id: uuid.UUID, *, days: int = 90, limit: int = 20
|
|
167
|
+
) -> list[dict]:
|
|
168
|
+
"""评审人工作量。
|
|
169
|
+
|
|
170
|
+
数据来自 comment 来源的评审事件 —— 评审人身份只存在于评论里
|
|
171
|
+
(标签是汇总结果,不含具体人)。这也是为什么必须解析评论。
|
|
172
|
+
"""
|
|
173
|
+
since = datetime.now(UTC) - timedelta(days=days)
|
|
174
|
+
|
|
175
|
+
rows = (
|
|
176
|
+
await session.execute(
|
|
177
|
+
select(
|
|
178
|
+
ReviewEvent.actor_login,
|
|
179
|
+
func.count().filter(ReviewEvent.event_type == ReviewEventType.LGTM.value),
|
|
180
|
+
func.count().filter(ReviewEvent.event_type == ReviewEventType.APPROVE.value),
|
|
181
|
+
func.count().filter(ReviewEvent.event_type == ReviewEventType.ACK.value),
|
|
182
|
+
func.count(),
|
|
183
|
+
)
|
|
184
|
+
.where(
|
|
185
|
+
ReviewEvent.occurred_at >= since,
|
|
186
|
+
ReviewEvent.source == "comment",
|
|
187
|
+
ReviewEvent.actor_login != "",
|
|
188
|
+
ReviewEvent.pull_request_id.in_(
|
|
189
|
+
select(PullRequest.id).where(PullRequest.repository_id == repository_id)
|
|
190
|
+
),
|
|
191
|
+
)
|
|
192
|
+
.group_by(ReviewEvent.actor_login)
|
|
193
|
+
.order_by(func.count().desc())
|
|
194
|
+
.limit(limit)
|
|
195
|
+
)
|
|
196
|
+
).all()
|
|
197
|
+
|
|
198
|
+
return [
|
|
199
|
+
{
|
|
200
|
+
"actor_login": row[0],
|
|
201
|
+
"lgtm_count": row[1],
|
|
202
|
+
"approve_count": row[2],
|
|
203
|
+
"ack_count": row[3],
|
|
204
|
+
"total": row[4],
|
|
205
|
+
}
|
|
206
|
+
for row in rows
|
|
207
|
+
]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
async def stage_distribution(session: AsyncSession, repository_id: uuid.UUID) -> list[dict]:
|
|
211
|
+
"""按门禁阶段的 PR 分布。这是"卡在哪一环"的直接答案。"""
|
|
212
|
+
rows = (
|
|
213
|
+
await session.execute(
|
|
214
|
+
select(PullRequest.gate_stage, func.count())
|
|
215
|
+
.where(PullRequest.repository_id == repository_id, PullRequest.state == "open")
|
|
216
|
+
.group_by(PullRequest.gate_stage)
|
|
217
|
+
.order_by(func.count().desc())
|
|
218
|
+
)
|
|
219
|
+
).all()
|
|
220
|
+
return [{"stage": row[0], "count": row[1]} for row in rows]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
async def branch_distribution(session: AsyncSession, repository_id: uuid.UUID) -> list[dict]:
|
|
224
|
+
rows = (
|
|
225
|
+
await session.execute(
|
|
226
|
+
select(PullRequest.target_branch, func.count())
|
|
227
|
+
.where(
|
|
228
|
+
PullRequest.repository_id == repository_id,
|
|
229
|
+
PullRequest.state == "open",
|
|
230
|
+
PullRequest.target_branch.is_not(None),
|
|
231
|
+
)
|
|
232
|
+
.group_by(PullRequest.target_branch)
|
|
233
|
+
.order_by(func.count().desc())
|
|
234
|
+
.limit(15)
|
|
235
|
+
)
|
|
236
|
+
).all()
|
|
237
|
+
return [{"branch": row[0], "count": row[1]} for row in rows]
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
async def author_distribution(
|
|
241
|
+
session: AsyncSession, repository_id: uuid.UUID, *, limit: int = 15
|
|
242
|
+
) -> list[dict]:
|
|
243
|
+
"""人类作者的提交量。
|
|
244
|
+
|
|
245
|
+
**必须排除机器人**:实测作者榜前列被 openeuler-infra-bot、devstation-robot、
|
|
246
|
+
ci-robot1 占据(合计 333 个 PR),不排除则这份榜单毫无参考价值。
|
|
247
|
+
机器人判定复用评审解析器中的同一套规则,避免两处判定不一致。
|
|
248
|
+
"""
|
|
249
|
+
rows = (
|
|
250
|
+
await session.execute(
|
|
251
|
+
select(PullRequest.author_login, func.count())
|
|
252
|
+
.where(
|
|
253
|
+
PullRequest.repository_id == repository_id,
|
|
254
|
+
PullRequest.author_login.is_not(None),
|
|
255
|
+
)
|
|
256
|
+
.group_by(PullRequest.author_login)
|
|
257
|
+
.order_by(func.count().desc())
|
|
258
|
+
)
|
|
259
|
+
).all()
|
|
260
|
+
|
|
261
|
+
humans = [{"author": row[0], "count": row[1]} for row in rows if not is_bot(row[0])]
|
|
262
|
+
bot_count = sum(row[1] for row in rows if is_bot(row[0]))
|
|
263
|
+
|
|
264
|
+
result = humans[:limit]
|
|
265
|
+
if bot_count:
|
|
266
|
+
# 机器人合计单独列出而非丢弃:自动化贡献本身是有信息量的
|
|
267
|
+
result.append({"author": "(机器人合计)", "count": bot_count, "is_bot": True})
|
|
268
|
+
return result
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
async def interval_stats(session: AsyncSession, repository_id: uuid.UUID) -> dict:
|
|
272
|
+
"""各阶段的平均滞留时长。
|
|
273
|
+
|
|
274
|
+
从阶段进入时间算起并不直接可得(需要状态变更历史),
|
|
275
|
+
因此这里使用可观测的近似:以 PR 创建时间到合入/当前时间的区间,
|
|
276
|
+
配合阶段分布来定位瓶颈。比不提供任何时长信息更有用,
|
|
277
|
+
但不宣称是精确的阶段停留时间。
|
|
278
|
+
"""
|
|
279
|
+
merged_duration = (
|
|
280
|
+
func.extract("epoch", PullRequest.merged_at - PullRequest.atomgit_created_at) / 86400.0
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
row = (
|
|
284
|
+
await session.execute(
|
|
285
|
+
select(
|
|
286
|
+
func.avg(merged_duration),
|
|
287
|
+
func.percentile_cont(0.5).within_group(merged_duration),
|
|
288
|
+
func.count(),
|
|
289
|
+
).where(
|
|
290
|
+
PullRequest.repository_id == repository_id,
|
|
291
|
+
PullRequest.merged_at.is_not(None),
|
|
292
|
+
PullRequest.atomgit_created_at.is_not(None),
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
).one()
|
|
296
|
+
|
|
297
|
+
recent_merged = (
|
|
298
|
+
await session.scalar(
|
|
299
|
+
select(func.count())
|
|
300
|
+
.select_from(PullRequest)
|
|
301
|
+
.where(
|
|
302
|
+
PullRequest.repository_id == repository_id,
|
|
303
|
+
PullRequest.merged_at >= datetime.now(UTC) - timedelta(days=30),
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
or 0
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
"merged_count": row[2] or 0,
|
|
311
|
+
"avg_merge_days": round(float(row[0]), 1) if row[0] is not None else 0.0,
|
|
312
|
+
"median_merge_days": round(float(row[1]), 1) if row[1] is not None else 0.0,
|
|
313
|
+
"merged_last_30d": recent_merged,
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
async def quality_signals(session: AsyncSession, repository_id: uuid.UUID) -> dict:
|
|
318
|
+
"""数据质量信号。
|
|
319
|
+
|
|
320
|
+
这些是自动可查、且直接影响评审成本的事实,比主观判断可靠。
|
|
321
|
+
"""
|
|
322
|
+
binary_files = (
|
|
323
|
+
await session.scalar(
|
|
324
|
+
select(func.count())
|
|
325
|
+
.select_from(PRFile)
|
|
326
|
+
.join(PullRequest, PullRequest.id == PRFile.pull_request_id)
|
|
327
|
+
.where(
|
|
328
|
+
PullRequest.repository_id == repository_id,
|
|
329
|
+
PullRequest.state == "open",
|
|
330
|
+
PRFile.is_binary.is_(True),
|
|
331
|
+
)
|
|
332
|
+
)
|
|
333
|
+
or 0
|
|
334
|
+
)
|
|
335
|
+
conflicts = (
|
|
336
|
+
await session.scalar(
|
|
337
|
+
select(func.count())
|
|
338
|
+
.select_from(PullRequest)
|
|
339
|
+
.where(
|
|
340
|
+
PullRequest.repository_id == repository_id,
|
|
341
|
+
PullRequest.state == "open",
|
|
342
|
+
PullRequest.merge_conflict.is_(True),
|
|
343
|
+
)
|
|
344
|
+
)
|
|
345
|
+
or 0
|
|
346
|
+
)
|
|
347
|
+
drafts = (
|
|
348
|
+
await session.scalar(
|
|
349
|
+
select(func.count())
|
|
350
|
+
.select_from(PullRequest)
|
|
351
|
+
.where(
|
|
352
|
+
PullRequest.repository_id == repository_id,
|
|
353
|
+
PullRequest.state == "open",
|
|
354
|
+
PullRequest.draft.is_(True),
|
|
355
|
+
)
|
|
356
|
+
)
|
|
357
|
+
or 0
|
|
358
|
+
)
|
|
359
|
+
return {
|
|
360
|
+
"binary_files": binary_files,
|
|
361
|
+
"merge_conflicts": conflicts,
|
|
362
|
+
"drafts": drafts,
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
async def repository_summary(session: AsyncSession, repository: Repository) -> dict:
|
|
367
|
+
"""一次性返回仪表盘所需的全部指标。"""
|
|
368
|
+
return {
|
|
369
|
+
"overview": await overview(session, repository.id),
|
|
370
|
+
"waiting": await waiting_time_stats(session, repository.id),
|
|
371
|
+
"stages": await stage_distribution(session, repository.id),
|
|
372
|
+
"branches": await branch_distribution(session, repository.id),
|
|
373
|
+
"authors": await author_distribution(session, repository.id),
|
|
374
|
+
"reviewers": await reviewer_workload(session, repository.id),
|
|
375
|
+
"intervals": await interval_stats(session, repository.id),
|
|
376
|
+
"quality": await quality_signals(session, repository.id),
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
__all__ = [
|
|
381
|
+
"author_distribution",
|
|
382
|
+
"branch_distribution",
|
|
383
|
+
"interval_stats",
|
|
384
|
+
"overview",
|
|
385
|
+
"quality_signals",
|
|
386
|
+
"repository_summary",
|
|
387
|
+
"reviewer_workload",
|
|
388
|
+
"stage_distribution",
|
|
389
|
+
"waiting_time_stats",
|
|
390
|
+
]
|