-
-
Notifications
You must be signed in to change notification settings - Fork 661
Add management command to backfill ActivityEvent histor #5338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anurag2787
wants to merge
11
commits into
OWASP:feature/owasp-pulse
Choose a base branch
from
anurag2787:backfill-pulse-command
base: feature/owasp-pulse
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b10a3f3
Implemented data model
anurag2787 dc695f5
adress review
anurag2787 ef35b8c
Merge branch 'feature/owasp-pulse' into pulse-activityevent-model
anurag2787 61931da
added actitivty builder
anurag2787 2004c75
Merge branch 'pulse-activityevent-model' of github.com:anurag2787/Nes…
anurag2787 fda1f30
Merge branch 'feature/owasp-pulse' into pulse-activityevent-model
anurag2787 ae10e4f
Address review
anurag2787 cc0f1de
updated order
anurag2787 101805c
Added a command
anurag2787 279d99a
adress review
anurag2787 e716bd0
fixed
anurag2787 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """OWASP app ActivityEvent model admin.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.owasp.models.activity_event import ActivityEvent | ||
|
|
||
|
|
||
| class ActivityEventAdmin(admin.ModelAdmin): | ||
| """Admin for ActivityEvent model.""" | ||
|
|
||
| autocomplete_fields = ( | ||
| "github_user", | ||
| "github_repository", | ||
| ) | ||
| list_display = ( | ||
| "activity_type", | ||
| "github_repository", | ||
| "github_user", | ||
| "occurred_at", | ||
| ) | ||
|
anurag2787 marked this conversation as resolved.
|
||
| list_filter = ( | ||
| "activity_type", | ||
| "occurred_at", | ||
| ) | ||
| search_fields = ( | ||
| "activity_type", | ||
| "github_repository__name", | ||
| "github_user__login", | ||
| ) | ||
|
|
||
|
|
||
| admin.site.register(ActivityEvent, ActivityEventAdmin) | ||
100 changes: 100 additions & 0 deletions
100
backend/src/apps/owasp/management/commands/owasp_backfill_activity_events.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """A command to backfill activity events for existing pull requests, issues, and releases.""" | ||
|
|
||
| import logging | ||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
| from django.db.models import QuerySet | ||
|
|
||
| from apps.github.models.issue import Issue | ||
| from apps.github.models.pull_request import PullRequest | ||
| from apps.github.models.release import Release | ||
| from apps.owasp.models.activity_event import ActivityEvent | ||
|
|
||
| logger: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Backfill ActivityEvent records for existing pull requests, issues, and releases." | ||
|
|
||
| def add_arguments(self, parser) -> None: | ||
| """Add command-line arguments to the parser.""" | ||
| parser.add_argument( | ||
| "--offset", | ||
| default=0, | ||
| required=False, | ||
| type=int, | ||
|
anurag2787 marked this conversation as resolved.
|
||
| help="Number of records to skip before starting backfill.", | ||
| ) | ||
| parser.add_argument( | ||
| "--model", | ||
| default="all", | ||
| required=False, | ||
| choices=["all", "issue", "pull_request", "release"], | ||
| help="Which model type to backfill. Defaults to 'all'.", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options) -> None: | ||
| """Handle the command execution.""" | ||
| offset = options["offset"] | ||
| model = options["model"] | ||
|
|
||
| if model in ("all", "issue"): | ||
| self.backfill_issues(offset) | ||
|
|
||
| if model in ("all", "pull_request"): | ||
| self.backfill_pull_requests(offset) | ||
|
|
||
| if model in ("all", "release"): | ||
| self.backfill_releases(offset) | ||
|
|
||
| def backfill_objects( | ||
| self, | ||
| queryset: QuerySet, | ||
| offset: int, | ||
| noun: str, | ||
| get_label: Callable[[Any], str], | ||
| ) -> None: | ||
| """Backfill ActivityEvent records for a queryset of GitHub objects.""" | ||
| count = queryset.count() | ||
| self.stdout.write(f"Backfilling activity events for {count} {noun}...\n") | ||
|
|
||
| created_count = 0 | ||
| for obj in queryset[offset:].iterator(chunk_size=2000): | ||
| if not obj.repository: | ||
| logger.warning("Skipping %s %s: no repository", noun.rstrip("s"), get_label(obj)) | ||
| continue | ||
|
|
||
| try: | ||
| ActivityEvent.update_data(obj) | ||
| created_count += 1 | ||
| except Exception: | ||
| logger.exception( | ||
| "Error backfilling activity events for %s %s", | ||
| noun.rstrip("s"), | ||
| get_label(obj), | ||
| ) | ||
|
|
||
| self.stdout.write(f"{noun.capitalize()} processed: {created_count}\n") | ||
|
|
||
| def backfill_issues(self, offset: int) -> None: | ||
| """Backfill ActivityEvent records for existing issues.""" | ||
| queryset = Issue.objects.select_related("author", "repository").order_by( | ||
| "created_at", "pk" | ||
| ) | ||
| self.backfill_objects(queryset, offset, "issues", lambda obj: f"#{obj.number}") | ||
|
|
||
| def backfill_pull_requests(self, offset: int) -> None: | ||
| """Backfill ActivityEvent records for existing pull requests.""" | ||
| queryset = PullRequest.objects.select_related("author", "repository").order_by( | ||
| "created_at", "pk" | ||
| ) | ||
| self.backfill_objects(queryset, offset, "pull requests", lambda obj: f"#{obj.number}") | ||
|
|
||
| def backfill_releases(self, offset: int) -> None: | ||
| """Backfill ActivityEvent records for existing releases.""" | ||
| queryset = Release.objects.select_related("author", "repository").order_by( | ||
| "created_at", "pk" | ||
| ) | ||
| self.backfill_objects(queryset, offset, "releases", lambda obj: obj.tag_name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Generated by Django 6.0.7 on 2026-07-28 07:54 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("contenttypes", "0002_remove_content_type_name"), | ||
| ("github", "0044_user_indexes"), | ||
| ("owasp", "0072_project_project_name_gin_idx_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="ActivityEvent", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ( | ||
| "activity_type", | ||
| models.CharField( | ||
| choices=[ | ||
| ("issue_closed", "Issue Closed"), | ||
| ("issue_opened", "Issue Opened"), | ||
| ("pr_closed", "PR Closed"), | ||
| ("pr_merged", "PR Merged"), | ||
| ("pr_opened", "PR Opened"), | ||
| ("release_published", "Release Published"), | ||
| ], | ||
| max_length=32, | ||
| verbose_name="Activity Type", | ||
| ), | ||
| ), | ||
| ("object_id", models.PositiveBigIntegerField()), | ||
| ( | ||
| "occurred_at", | ||
| models.DateTimeField( | ||
| help_text="Timestamp when the activity event occurred on GitHub", | ||
| verbose_name="Occurred at", | ||
| ), | ||
| ), | ||
| ( | ||
| "content_type", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="contenttypes.contenttype" | ||
| ), | ||
| ), | ||
| ( | ||
| "github_repository", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="activity_events", | ||
| to="github.repository", | ||
| verbose_name="GitHub Repository", | ||
| ), | ||
| ), | ||
| ( | ||
| "github_user", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="activity_events", | ||
| to="github.user", | ||
| verbose_name="GitHub User", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name_plural": "Activity Events", | ||
| "db_table": "github_activity_events", | ||
| "indexes": [ | ||
| models.Index(fields=["activity_type"], name="activity_event_type_idx"), | ||
| models.Index(fields=["github_user"], name="activity_event_github_user_idx"), | ||
| models.Index( | ||
| fields=["content_type", "object_id"], name="activity_event_source_idx" | ||
| ), | ||
| models.Index(fields=["occurred_at"], name="activity_event_occurred_at_idx"), | ||
| models.Index( | ||
| fields=["github_repository"], name="activity_event_github_repo_idx" | ||
| ), | ||
| ], | ||
| "constraints": [ | ||
| models.UniqueConstraint( | ||
| fields=("activity_type", "content_type", "object_id", "occurred_at"), | ||
| name="unique_activity_event", | ||
| ) | ||
| ], | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.