-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathcrud.py
More file actions
411 lines (359 loc) · 13.3 KB
/
crud.py
File metadata and controls
411 lines (359 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from datetime import datetime
from typing import Any
from loguru import logger
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
from crowdgit.enums import RepositoryPriority, RepositoryState
from crowdgit.errors import RepoLockingError
from crowdgit.models.repository import Repository
from crowdgit.models.service_execution import ServiceExecution
from crowdgit.settings import MAX_CONCURRENT_ONBOARDINGS, REPOSITORY_UPDATE_INTERVAL_HOURS
from .connection import get_db_connection
from .registry import execute, executemany, fetchrow, fetchval, query
async def insert_repository(url: str, priority: int = 0) -> str:
"""Insert a new repository"""
sql_query = """
INSERT INTO git.repositories (url, priority, state)
VALUES ($1, $2, 'pending')
RETURNING id
"""
result = await fetchval(sql_query, (url, priority))
return str(result)
async def get_repository_by_url(url: str) -> dict[str, Any] | None:
"""Get repository by URL"""
sql_query = """
SELECT id, url, state, priority, "lastProcessedAt", "lockedAt", "createdAt", "updatedAt", "maintainerFile", "forkedFrom"
FROM git.repositories
WHERE url = $1 AND "deletedAt" IS NULL
"""
result = await fetchrow(sql_query, (url,))
return dict(result) if result else None
async def get_recently_processed_repository_by_url(url: str) -> Repository | None:
"""
Get repository by URL that was processed within the configured update interval.
Returns the repository only if it was last processed within REPOSITORY_UPDATE_INTERVAL_HOURS
and has a COMPLETED state.
Used to check if a repository needs reprocessing based on the update interval.
"""
sql_query = """
SELECT id, url, state, priority, "lastProcessedAt", "lockedAt", "createdAt", "updatedAt", "maintainerFile", "forkedFrom", "segmentId"
FROM git.repositories
WHERE url = $1
AND "deletedAt" IS NULL
AND "lastProcessedAt" > NOW() - INTERVAL '1 hour' * $2
AND state = $3
"""
result = await fetchrow(
sql_query, (url, REPOSITORY_UPDATE_INTERVAL_HOURS, RepositoryState.COMPLETED)
)
return Repository.from_db(dict(result)) if result else None
async def acquire_onboarding_repo() -> Repository | None:
onboarding_repo_sql_query = """
WITH current_onboarding_count AS (
-- Count repositories currently being onboarded (processing + never processed before)
SELECT COUNT(*) as count
FROM git.repositories
WHERE state = $1
AND "lastProcessedCommit" IS NULL
AND "deletedAt" IS NULL
)
UPDATE git.repositories
SET "lockedAt" = NOW(),
state = $1,
"updatedAt" = NOW()
WHERE id = (
SELECT r.id
FROM git.repositories r
CROSS JOIN current_onboarding_count c
WHERE r.state = $2
AND r."lockedAt" IS NULL
AND r."deletedAt" IS NULL
AND c.count < $3 -- Only proceed if under the limit
ORDER BY r.priority ASC, r."createdAt" ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING id, url, state, priority, "lastProcessedAt", "lastProcessedCommit", "lockedAt", "createdAt", "updatedAt", "segmentId", "integrationId", "maintainerFile", "lastMaintainerRunAt", "branch", "forkedFrom"
"""
return await acquire_repository(
onboarding_repo_sql_query,
(RepositoryState.PROCESSING, RepositoryState.PENDING, MAX_CONCURRENT_ONBOARDINGS),
)
@retry(
retry=retry_if_exception_type(RepoLockingError),
stop=stop_after_attempt(3),
wait=wait_fixed(1),
reraise=True,
)
async def acquire_repository(query: str, params: tuple = None) -> Repository | None:
async with get_db_connection() as conn:
try:
async with conn.transaction():
result = await conn.fetchrow(query, *params)
if result:
repo = Repository.from_db(dict(result))
logger.info(f"Acquired repository: {repo.url}")
return repo
# logger.info(
# "No repository is available for processing based on the filtering rules"
# )
return None
except Exception as e:
logger.error(f"failed to acquire repository with error: {e}. Retrying...")
raise RepoLockingError() from e
async def acquire_recurrent_repo() -> Repository | None:
"""Acquire a regular (non-onboarding) repository, that were not processed in the last x hours (REPOSITORY_UPDATE_INTERVAL_HOURS)"""
recurrent_repo_sql_query = """
UPDATE git.repositories
SET "lockedAt" = NOW(),
state = $1,
"updatedAt" = NOW()
WHERE id = (
SELECT id
FROM git.repositories
WHERE NOT (state = ANY($2))
AND "lockedAt" IS NULL
AND "deletedAt" IS NULL
AND "lastProcessedAt" < NOW() - INTERVAL '1 hour' * $3
ORDER BY priority ASC, "lastProcessedAt" ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING id, url, state, priority, "lastProcessedAt", "lastProcessedCommit", "lockedAt", "createdAt", "updatedAt", "segmentId", "integrationId", "maintainerFile", "lastMaintainerRunAt", "branch", "forkedFrom"
"""
states_to_exclude = (RepositoryState.PENDING, RepositoryState.PROCESSING)
return await acquire_repository(
recurrent_repo_sql_query,
(RepositoryState.PROCESSING, states_to_exclude, REPOSITORY_UPDATE_INTERVAL_HOURS),
)
async def acquire_repo_for_processing() -> Repository | None:
# prioritizing onboarding repositories
# TODO: document priority logic and values(0, 1, 2)
repo_to_process = await acquire_onboarding_repo()
if not repo_to_process:
# Fallback to non-onboarding repos
repo_to_process = await acquire_recurrent_repo()
return repo_to_process
async def release_repo(repo_id: str):
"""
Release repository lock (lockedAt) after processing
"""
sql_query = """
UPDATE git.repositories
SET "lockedAt" = NULL,
"updatedAt" = NOW()
WHERE id = $1
"""
result = await execute(sql_query, (repo_id,))
return str(result)
async def update_last_processed_commit(repo_id: str, commit_hash: str, branch: str | None = None):
"""
Update last processed commit and optionally the branch after processing
"""
sql_query = """
UPDATE git.repositories
SET "lastProcessedCommit" = $1,
"branch" = $2,
"updatedAt" = NOW()
WHERE id = $3
"""
result = await execute(sql_query, (commit_hash, branch, repo_id))
return str(result)
async def mark_repo_as_processed(repo_id: str, repo_state: RepositoryState):
sql_query = """
UPDATE git.repositories
SET "state" = $2,
"lastProcessedAt" = NOW(),
"updatedAt" = NOW(),
"priority" = $3
WHERE id = $1
"""
result = await execute(sql_query, (repo_id, repo_state, RepositoryPriority.NORMAL))
return str(result)
async def batch_insert_activities(records: list[tuple], batch_size=100):
sql_query = """
INSERT INTO integration.results(id, state, data, "tenantId", "integrationId")
values($1, $2, $3, $4, $5)
"""
logger.info(f"Saving {len(records)} activity into integration.results")
for i in range(0, len(records), batch_size):
batch = records[i : i + batch_size]
await executemany(sql_query, batch)
logger.info("activities saved into integration.results")
async def find_github_identity(github_username: str):
sql_query = """
SELECT id
FROM "memberIdentities"
WHERE
platform = 'github'
AND value = $1
LIMIT 1
"""
result = await fetchval(
sql_query,
(github_username,),
)
return result
async def find_maintainer_identity_by_email(email: str):
sql_query = """
SELECT id
FROM "memberIdentities"
WHERE
platform IN ('github', 'git', 'gitlab')
AND "verified" = TRUE
AND value = $1
LIMIT 1
"""
result = await fetchval(
sql_query,
(email,),
)
return result
async def upsert_maintainer(
repo_id: str,
identity_id: str,
repo_url: str,
role: str,
original_role: str,
start_date: datetime | None = None,
):
sql_query = """
INSERT INTO "maintainersInternal"
("role", "originalRole", "repoUrl", "repoId", "identityId", "startDate", "endDate")
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT ("repoId", "identityId", "startDate", "endDate") DO UPDATE
SET role = EXCLUDED.role, "originalRole" = EXCLUDED."originalRole", "updatedAt" = NOW()
"""
await execute(
sql_query,
(role, original_role, repo_url, repo_id, identity_id, start_date, None),
)
async def update_maintainer_run(repo_id: str, maintainer_file: str):
# TODO: deprecate githubRepos once all repos migrated to git.repositories
# Update githubRepos table
github_repos_sql_query = """
UPDATE "githubRepos"
SET "maintainerFile" = $1,
"lastMaintainerRunAt" = NOW()
WHERE id = $2
"""
await execute(
github_repos_sql_query,
(maintainer_file, repo_id),
)
# Update git.repositories table
git_repos_sql_query = """
UPDATE git.repositories
SET "maintainerFile" = $1,
"lastMaintainerRunAt" = NOW(),
"updatedAt" = NOW()
WHERE id = $2
"""
await execute(
git_repos_sql_query,
(maintainer_file, repo_id),
)
async def get_maintainers_for_repo(repo_id: str):
maintainers_sql_query = """
SELECT mi.role, mi."originalRole", mi."repoUrl", mi."repoId", mi."identityId", mem.value as github_username
FROM "maintainersInternal" mi
JOIN "memberIdentities" mem ON mi."identityId" = mem.id
WHERE mi."repoId" = $1 AND mem.platform = 'github' AND mem.type = 'username' and mem.verified = True
"""
return await query(
maintainers_sql_query,
(repo_id,),
)
async def set_maintainer_end_date(
repo_id: str, identity_id: str, role: str, change_date: datetime
):
update_end_date_query = """
UPDATE "maintainersInternal"
SET "endDate" = $1,
"updatedAt" = NOW()
WHERE "repoId" = $2 AND "identityId" = $3 AND role = $4
"""
await execute(
update_end_date_query,
(
change_date,
repo_id,
identity_id,
role,
),
)
async def batch_check_parent_activities(
activity_keys: list[tuple[str, str, str]],
parent_channel: str,
parent_segment_id: str,
) -> set[str]:
"""
Batch check which activities exist in parent repo using full dedup key.
Args:
activity_keys: List of (timestamp, type, sourceId) tuples
parent_channel: Parent repository URL
parent_segment_id: Parent repository segment ID
Returns:
Set of sourceIds that exist in parent repo
"""
if not activity_keys:
return set()
# Use dedup index with ALL fields for optimal performance
# Index: (timestamp, platform, type, sourceId, channel, segmentId)
# Build OR conditions for each (timestamp, type, sourceId) combination
conditions = []
params = ["git", parent_channel, parent_segment_id]
param_idx = 4
for timestamp_str, activity_type, source_id in activity_keys:
conditions.append(
f'("timestamp" = ${param_idx} AND "type" = ${param_idx + 1} AND "sourceId" = ${param_idx + 2})'
)
timestamp = datetime.fromisoformat(timestamp_str)
params.append(timestamp)
params.append(activity_type)
params.append(source_id)
param_idx += 3
sql_query = f"""
SELECT DISTINCT "sourceId"
FROM "activityRelations"
WHERE "platform" = $1
AND "channel" = $2
AND "segmentId" = $3
AND ({" OR ".join(conditions)})
"""
result = await query(sql_query, tuple(params))
return {row["sourceId"] for row in result}
async def save_service_execution(service_execution: ServiceExecution) -> None:
"""
Save service execution record to database.
"""
try:
sql_query = """
INSERT INTO git."serviceExecutions" (
"repoId", "operationType", "status", "errorCode",
"errorMessage", "executionTimeSec", "metrics"
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
"""
db_data = service_execution.to_db_dict()
await execute(
sql_query,
(
db_data["repoId"],
db_data["operationType"],
db_data["status"],
db_data["errorCode"],
db_data["errorMessage"],
db_data["executionTimeSec"],
db_data["metrics"],
),
)
logger.debug(
f"Successfully saved service execution: {service_execution.operation_type} for repo {service_execution.repo_id}"
)
except Exception as e:
logger.error(
f"Failed to save service execution record: operation={service_execution.operation_type}, "
f"repo_id={service_execution.repo_id}, status={service_execution.status.value}, "
f"error: {e}"
)
# Do not re-raise - we don't want metrics saving to disrupt main operations