Skip to content

Commit 8b803d4

Browse files
committed
Merge remote-tracking branch 'upstream/main' into repo_refactor
2 parents 638f59e + d05b469 commit 8b803d4

File tree

22 files changed

+153
-76
lines changed

22 files changed

+153
-76
lines changed

backend/apps/github/graphql/nodes/repository_contributor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ class RepositoryContributorNode(graphene.ObjectType):
1010
contributions_count = graphene.Int()
1111
login = graphene.String()
1212
name = graphene.String()
13+
project_key = graphene.String()
1314
project_name = graphene.String()
14-
project_url = graphene.String()

backend/apps/github/graphql/queries/repository_contributor.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""OWASP repository contributor GraphQL queries."""
22

33
import graphene
4-
from django.db.models import F, OuterRef, Subquery, Window
4+
from django.db.models import F, Window
55
from django.db.models.functions import Rank
66

77
from apps.common.graphql.queries import BaseQuery
88
from apps.github.graphql.nodes.repository_contributor import RepositoryContributorNode
99
from apps.github.models.repository_contributor import RepositoryContributor
10-
from apps.owasp.models.project import Project
1110

1211

1312
class RepositoryContributorQuery(BaseQuery):
@@ -36,6 +35,14 @@ def resolve_top_contributors(root, info, limit, organization=None):
3635
RepositoryContributor.objects.by_humans()
3736
.to_community_repositories()
3837
.filter(repository__project__isnull=False)
38+
.select_related("repository__project", "user")
39+
.annotate(
40+
rank=Window(
41+
expression=Rank(),
42+
order_by=F("contributions_count").desc(),
43+
partition_by=F("user__login"),
44+
)
45+
)
3946
)
4047

4148
if organization:
@@ -46,34 +53,18 @@ def resolve_top_contributors(root, info, limit, organization=None):
4653
)
4754

4855
top_contributors = (
49-
queryset.annotate(
50-
project_id=Subquery(
51-
Project.repositories.through.objects.filter(
52-
repository=OuterRef("repository_id")
53-
)
54-
.values("project_id")
55-
.order_by("project_id")[:1] # Select the first project ID per repository
56-
),
57-
project_name=Subquery(
58-
Project.objects.filter(id=OuterRef("project_id")).values("name")[:1]
59-
),
60-
project_url=Subquery(
61-
Project.objects.filter(id=OuterRef("project_id")).values("key")[:1]
62-
),
63-
rank=Window(
64-
expression=Rank(),
65-
order_by=F("contributions_count").desc(),
66-
partition_by=F("user__login"),
67-
),
56+
queryset.filter(rank=1)
57+
.annotate(
58+
project_name=F("repository__project__name"),
59+
project_key=F("repository__project__key"),
6860
)
69-
.filter(rank=1) # Keep only the highest contribution per user
7061
.values(
7162
"contributions_count",
7263
"user__avatar_url",
7364
"user__login",
7465
"user__name",
66+
"project_key",
7567
"project_name",
76-
"project_url",
7768
)
7869
.order_by("-contributions_count")[:limit]
7970
)
@@ -84,8 +75,8 @@ def resolve_top_contributors(root, info, limit, organization=None):
8475
contributions_count=trc["contributions_count"],
8576
login=trc["user__login"],
8677
name=trc["user__name"],
78+
project_key=trc["project_key"].replace("www-project-", ""),
8779
project_name=trc["project_name"],
88-
project_url=trc["project_url"],
8980
)
9081
for trc in top_contributors
9182
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by Django 5.2 on 2025-04-14 07:40
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("github", "0020_repositorycontributor_user_contrib_idx"),
9+
]
10+
11+
operations = [
12+
migrations.AddIndex(
13+
model_name="release",
14+
index=models.Index(fields=["-published_at"], name="release_published_at_desc_idx"),
15+
),
16+
]

backend/apps/github/models/release.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Meta:
1414
db_table = "github_releases"
1515
indexes = [
1616
models.Index(fields=["-created_at"]),
17+
models.Index(fields=["-published_at"], name="release_published_at_desc_idx"),
1718
]
1819
verbose_name_plural = "Releases"
1920

backend/apps/owasp/graphql/queries/sponsor.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class SponsorQuery(BaseQuery):
1414

1515
def resolve_sponsors(root, info):
1616
"""Resolve sponsors."""
17-
priority_order = {
18-
Sponsor.SponsorType.DIAMOND: 1,
19-
Sponsor.SponsorType.PLATINUM: 2,
20-
Sponsor.SponsorType.GOLD: 3,
21-
Sponsor.SponsorType.SILVER: 4,
22-
Sponsor.SponsorType.SUPPORTER: 5,
23-
Sponsor.SponsorType.NOT_SPONSOR: 6,
24-
}
25-
return sorted(Sponsor.objects.all(), key=lambda x: priority_order[x.sponsor_type])
17+
return sorted(
18+
Sponsor.objects.all(),
19+
key=lambda x: {
20+
Sponsor.SponsorType.DIAMOND: 1,
21+
Sponsor.SponsorType.PLATINUM: 2,
22+
Sponsor.SponsorType.GOLD: 3,
23+
Sponsor.SponsorType.SILVER: 4,
24+
Sponsor.SponsorType.SUPPORTER: 5,
25+
Sponsor.SponsorType.NOT_SPONSOR: 6,
26+
}[x.sponsor_type],
27+
)

backend/apps/owasp/graphql/queries/stats.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@ def resolve_stats_overview(self, info, **kwargs):
3636
.count()
3737
)
3838

39-
active_projects_stats = (active_projects_stats // 10) * 10 # nearest 10
40-
active_chapters_stats = (active_chapters_stats // 10) * 10
41-
contributors_stats = (contributors_stats // 100) * 100 # nearest 100
42-
countries_stats = (countries_stats // 10) * 10
43-
4439
return StatsNode(
45-
active_projects_stats,
46-
active_chapters_stats,
47-
contributors_stats,
48-
countries_stats,
40+
(active_projects_stats // 10) * 10, # nearest 10
41+
(active_chapters_stats // 10) * 10, # nearest 10
42+
(contributors_stats // 100) * 100, # nearest 100
43+
(countries_stats // 10) * 10, # nearest 10
4944
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 5.2 on 2025-04-14 07:27
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("github", "0020_repositorycontributor_user_contrib_idx"),
9+
("owasp", "0030_chapter_is_leaders_policy_compliant_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="chapter",
15+
name="created_at",
16+
field=models.DateTimeField(default=None, verbose_name="Created at"),
17+
),
18+
migrations.AddIndex(
19+
model_name="chapter",
20+
index=models.Index(fields=["-created_at"], name="chapter_created_at_desc_idx"),
21+
),
22+
migrations.AddIndex(
23+
model_name="event",
24+
index=models.Index(fields=["-start_date"], name="event_start_date_desc_idx"),
25+
),
26+
migrations.AddIndex(
27+
model_name="post",
28+
index=models.Index(fields=["-published_at"], name="post_published_at_desc_idx"),
29+
),
30+
migrations.AddIndex(
31+
model_name="project",
32+
index=models.Index(fields=["-created_at"], name="project_created_at_desc_idx"),
33+
),
34+
]

backend/apps/owasp/models/chapter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class Chapter(
2828

2929
class Meta:
3030
db_table = "owasp_chapters"
31+
indexes = [
32+
models.Index(fields=["-created_at"], name="chapter_created_at_desc_idx"),
33+
]
3134
verbose_name_plural = "Chapters"
3235

3336
level = models.CharField(verbose_name="Level", max_length=5, default="", blank=True)

backend/apps/owasp/models/event.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Event(BulkSaveModel, TimestampedModel):
1717

1818
class Meta:
1919
db_table = "owasp_events"
20+
indexes = [
21+
models.Index(fields=["-start_date"], name="event_start_date_desc_idx"),
22+
]
2023
verbose_name_plural = "Events"
2124

2225
class Category(models.TextChoices):

backend/apps/owasp/models/post.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class Post(BulkSaveModel, TimestampedModel):
1313

1414
class Meta:
1515
db_table = "owasp_posts"
16+
indexes = [
17+
models.Index(fields=["-published_at"], name="post_published_at_desc_idx"),
18+
]
1619
verbose_name_plural = "Posts"
1720

1821
author_image_url = models.URLField(verbose_name="Author image URL", blank=True, default="")

0 commit comments

Comments
 (0)