Skip to content

Commit

Permalink
fix: Fix voting node not running snapshot importer before registratio…
Browse files Browse the repository at this point in the history
…n_snapshot_time (#494)

# Description

Fixes an issue with the voting-node not taking snapshots before the
event's `registration_snapshot_time`.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

I ran the voting-node locally with different event parameters.

## Checklist

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules

Co-authored-by: Oleksandr Prokhorenko <[email protected]>
  • Loading branch information
FelipeRosa and minikin authored Aug 2, 2023
1 parent fc46799 commit af5ad53
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
2 changes: 1 addition & 1 deletion services/voting-node/voting_node/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async def fetch_snapshot(self, event_id: int) -> Snapshot:

async def fetch_voteplans(self, event_id: int) -> list[VotePlan]:
"""Fetch the voteplans for the event_id."""
query = "SELECT * FROM voteplan WHERE event_id = $1 ORDER BY id ASC"
query = "SELECT * FROM voteplan WHERE objective_id IN (SELECT row_id FROM objective WHERE event = $1) ORDER BY id ASC"
result = await self.conn().fetch(query, event_id)
if result is None:
raise Exception("voteplan DB error")
Expand Down
8 changes: 4 additions & 4 deletions services/voting-node/voting_node/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def ideascale_import_all(self, event_id: int):
config_path=os.environ["IDEASCALE_CONFIG_PATH"],
event_id=event_id,
campaign_group_id=int(os.environ["IDEASCALE_CAMPAIGN_GROUP"]),
stage_id=int(os.environ["IDEASCALE_STAGE_ID"]),
stage_ids=[int(os.environ["IDEASCALE_STAGE_ID"])],
proposals_scores_csv_path=None,
ideascale_api_url=os.environ["IDEASCALE_API_URL"],
)
Expand Down Expand Up @@ -149,7 +149,7 @@ def snapshot_start_has_passed(self) -> bool:
now = datetime.utcnow()
return now > self.snapshot_start

def _reimaining_intervals_n_seconds_to_next_snapshot(self, current_time: datetime, interval: int) -> tuple[int, int]:
def _remaining_intervals_n_seconds_to_next_snapshot(self, current_time: datetime, interval: int) -> tuple[int, int]:
"""Calculates the remaining number of intervals and seconds until the next snapshot.
:param current_time: The current datetime.
Expand All @@ -159,7 +159,7 @@ def _reimaining_intervals_n_seconds_to_next_snapshot(self, current_time: datetim
:return: A tuple containing the number of intervals until the next snapshot start and the number of seconds until the next interval.
:rtype: Tuple[int, int]
"""
delta = self.snapshot_start - current_time
delta = self.snapshot_start - min(current_time, self.snapshot_start)
delta_seconds = int(abs(delta.total_seconds()))
# calculate the number of intervals until the snapshot start time
num_intervals = int(delta_seconds / interval)
Expand Down Expand Up @@ -206,7 +206,7 @@ async def take_snapshots(self, event_id: int) -> None:
while True:
interval = int(os.getenv("SNAPSHOT_INTERVAL_SECONDS", 1800))
current_time = datetime.utcnow()
num_intervals, secs_to_sleep = self._reimaining_intervals_n_seconds_to_next_snapshot(current_time, interval)
num_intervals, secs_to_sleep = self._remaining_intervals_n_seconds_to_next_snapshot(current_time, interval)

logger.info(f"{num_intervals + 1} snapshots remaining. Next snapshot is in {secs_to_sleep} seconds...")
# Wait for the next snapshot interval
Expand Down
12 changes: 0 additions & 12 deletions services/voting-node/voting_node/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"set_node_secret",
"set_node_topology_key",
"set_node_config",
"wait_for_registration_snapshot_time",
"import_snapshot_data",
"collect_snapshot_data",
"setup_tally_committee",
Expand Down Expand Up @@ -474,17 +473,6 @@ async def fetch_upcoming_event(self):
logger.debug("current event retrieved from DB")
self.node.event = event

async def wait_for_registration_snapshot_time(self):
"""Wait for the event registration_snapshot_time."""
# get the snapshot start timestamp
# raises an exception otherwise
snapshot_time = self.node.get_registration_snapshot_time()
# check if now is after the snapshot start time
if not self.node.has_registration_ended():
raise Exception(f"registration snapshot time will be stable on {snapshot_time} UTC")

logger.debug("registration snapshot time reached.")

async def import_snapshot_data(self):
"""Collect the snapshot data from EventDB."""
event = self.node.get_event()
Expand Down

0 comments on commit af5ad53

Please sign in to comment.