-
Notifications
You must be signed in to change notification settings - Fork 19
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
Granular progress bar #834
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* RCTab | ||
* Copyright (c) 2017-2024 Bright Spots Developers. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
/* | ||
* Purpose: Track the progress of tabulation for a GUI progress bar. | ||
* Design: Tracks the number of files read, the number of candidates eliminated, and an | ||
* estimate of the percentage of time that will be spent doing each. Uses the percent | ||
* of eliminations completed as a proxy for the progress of tabulation. | ||
* Conditions: During tabulation, validation, and conversion. | ||
* Version history: see https://github.com/BrightSpots/rcv. | ||
*/ | ||
|
||
package network.brightspots.rcv; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
class Progress { | ||
private final BiConsumer<Double, Double> progressUpdate; | ||
private final float estPercentTimeTabulating; | ||
private final int numFilesToRead; | ||
private final int numToEliminate; | ||
private int numFilesRead = 0; | ||
private int numEliminated = 0; | ||
|
||
/** | ||
* Uses the contest configuration to determine the number of files to read and the number of | ||
* candidates that may be eliminated. | ||
* | ||
* @param config The contest configuration | ||
* @param estPercentTimeTabulating An estimate of the percentage of time that will be spent | ||
* tabulating, between 0 and 1. Set to 0 if no tabulation will | ||
* be done. | ||
* @param progressUpdate A consumer that will be called with the current progress percentage. | ||
* May be null if not linked to a progress bar anywhere. | ||
*/ | ||
public Progress(ContestConfig config, | ||
float estPercentTimeTabulating, | ||
BiConsumer<Double, Double> progressUpdate) { | ||
if (!config.isMultiSeatSequentialWinnerTakesAllEnabled()) { | ||
this.numFilesToRead = config.rawConfig.cvrFileSources.size(); | ||
this.numToEliminate = config.getNumCandidates() - config.getNumberOfWinners(); | ||
} else { | ||
int numPasses = config.getSequentialWinners().size(); | ||
|
||
// The maximum number of eliminations in each pass is the number of active candidates | ||
// minus the one winner. | ||
int totalEliminations = 0; | ||
for (int i = 0; i < numPasses; i++) { | ||
totalEliminations += config.getNumCandidates() - i - 1; | ||
} | ||
|
||
this.numFilesToRead = config.rawConfig.cvrFileSources.size() * numPasses; | ||
this.numToEliminate = totalEliminations; | ||
} | ||
this.estPercentTimeTabulating = estPercentTimeTabulating; | ||
this.progressUpdate = progressUpdate; | ||
} | ||
|
||
/** | ||
* Call this function after each CVR file is read to increment the read count. | ||
*/ | ||
public void markFileRead() { | ||
numFilesRead++; | ||
|
||
if (numFilesRead > numFilesToRead) { | ||
Logger.warning("Progress Bar error: numFilesRead exceeds numFilesToRead!"); | ||
} | ||
|
||
updateConsumer(); | ||
} | ||
|
||
/** | ||
* Call this function with the number of new eliminations that have occurred. | ||
* Do not call with the total number of eliminations. This function increments | ||
* the elimination count by the given number. | ||
*/ | ||
public void markCandidatesEliminated(int numEliminated) { | ||
this.numEliminated += numEliminated; | ||
|
||
if (this.numEliminated > numToEliminate) { | ||
Logger.warning("Progress Bar error: numBallotsTabulated exceeds numBallotsToTabulate!"); | ||
} | ||
|
||
updateConsumer(); | ||
} | ||
|
||
private void updateConsumer() { | ||
double percentFilesRead = (double) numFilesRead / numFilesToRead; | ||
double percentEliminationsComplete = numToEliminate != 0 | ||
? (double) numEliminated / numToEliminate | ||
: 0; | ||
if (progressUpdate != null) { | ||
progressUpdate.accept( | ||
percentFilesRead * (1 - estPercentTimeTabulating) | ||
+ percentEliminationsComplete * estPercentTimeTabulating, | ||
1.0); | ||
} | ||
} | ||
} |
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.
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.
Where is this actually used? I don't see any place in the GUI that shows a progress bar for converting to CDF.
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.
It's not used, happy to remove though I don't see any downsides leaving it in either
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.
No problem, fine to leave as-is. Merge away!