-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fetch results of evening voting sessions #957
Open
tillprochaska
wants to merge
10
commits into
main
Choose a base branch
from
917-fix-multiple-voting-sessions
base: main
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
10 commits
Select commit
Hold shift + click to select a range
076f0d4
Fetch results of evening voting sessions
tillprochaska f93eed5
Move `load_fixture` helper to parent package as it's useful for other…
tillprochaska a7bd688
Allow passing a checksum of a previous run to `RCVListPipeline` and e…
tillprochaska 708afa2
Add common base class for all pipelines
tillprochaska 89de9ab
Refactor pipelines to use `BasePipeline`
tillprochaska a7589a2
Rename `PipelineRunResult` to `PipelineStatus`
tillprochaska e59f27d
Move error handling from worker to pipeline base class
tillprochaska 2a969eb
Store pipeline checksums and provide them to subsequent runs on the s…
tillprochaska 4189b40
Refactor: Don’t compute checksums in scraper
tillprochaska efa13e3
Refactor: Remove unnecessary package exports
tillprochaska 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
23 changes: 23 additions & 0 deletions
23
backend/howtheyvote/alembic/versions/1f516b18c4f6_rename_result_column_to_status.py
This file contains 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,23 @@ | ||
"""Rename result column to status | ||
|
||
Revision ID: 1f516b18c4f6 | ||
Revises: 9b35d19b64c4 | ||
Create Date: 2024-12-08 11:25:26.051408 | ||
|
||
""" | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "1f516b18c4f6" | ||
down_revision = "9b35d19b64c4" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.alter_column("pipeline_runs", column_name="result", new_column_name="status") | ||
|
||
|
||
def downgrade() -> None: | ||
op.alter_column("pipeline_runs", column_name="status", new_column_name="result") |
24 changes: 24 additions & 0 deletions
24
backend/howtheyvote/alembic/versions/2f958a6f147d_add_checksum_column_to_pipeline_runs_.py
This file contains 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,24 @@ | ||
"""Add checksum column to pipeline_runs table | ||
|
||
Revision ID: 2f958a6f147d | ||
Revises: 1f516b18c4f6 | ||
Create Date: 2024-12-07 17:12:10.792707 | ||
|
||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "2f958a6f147d" | ||
down_revision = "1f516b18c4f6" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.add_column("pipeline_runs", sa.Column("checksum", sa.Unicode)) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_column("pipeline_runs", "checksum") |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,6 +1,68 @@ | ||
import hashlib | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
from requests import Response | ||
from structlog import get_logger | ||
|
||
from ..models import PipelineStatus | ||
from ..scrapers import ScrapingError | ||
|
||
log = get_logger(__name__) | ||
|
||
|
||
@dataclass | ||
class PipelineResult: | ||
status: PipelineStatus | ||
checksum: str | None | ||
|
||
|
||
class PipelineError(Exception): | ||
pass | ||
|
||
|
||
class DataUnavailableError(PipelineError): | ||
pass | ||
|
||
|
||
class DataUnchangedError(PipelineError): | ||
pass | ||
|
||
|
||
class BasePipeline(ABC): | ||
last_run_checksum: str | None | ||
checksum: str | None | ||
|
||
def __init__(self, last_run_checksum: str | None = None, **kwargs: Any) -> None: | ||
self.last_run_checksum = last_run_checksum | ||
self.checksum = None | ||
self._log = log.bind(pipeline=type(self).__name__, **kwargs) | ||
|
||
def run(self) -> PipelineResult: | ||
self._log.info("Running pipeline") | ||
|
||
try: | ||
self._run() | ||
status = PipelineStatus.SUCCESS | ||
except DataUnavailableError: | ||
status = PipelineStatus.DATA_UNAVAILABLE | ||
except DataUnchangedError: | ||
status = PipelineStatus.DATA_UNCHANGED | ||
except ScrapingError: | ||
status = PipelineStatus.FAILURE | ||
self._log.exception("Failed running pipeline") | ||
|
||
return PipelineResult( | ||
status=status, | ||
checksum=self.checksum, | ||
) | ||
|
||
@abstractmethod | ||
def _run(self) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
def compute_response_checksum(response: Response) -> str: | ||
"""Compute the SHA256 hash of the response contents.""" | ||
return hashlib.sha256(response.content).hexdigest() |
This file contains 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: These aren’t actually errors, they are exceptions used for flow control. I think I’ve blindly followed the linter recommendation, but this naming doesn’t really make sense for this case.
According to PEP 8, exception classes should have an
Error
suffix, but only if they actually are errors, otherwise they should have no special suffix, so in this caseDataUnchanged
andDataUnavailable
would be more appropriate.https://peps.python.org/pep-0008/#exception-names
It probably makes sense to fix this separately though, there might be other similar cases elsewhere.