-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
325 additions
and
22 deletions.
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
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
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,87 @@ | ||
package util; | ||
|
||
/** | ||
* This class manages the TickingTimer instance used for refreshing. It computes the refresh rate period and change | ||
* the refresh period. | ||
*/ | ||
public class TickingTimerManager { | ||
|
||
|
||
public static final int BUFFER_TIME = 1; //To allow some additional time until next refresh. | ||
private final TickingTimer timer; | ||
public static final int REFRESH_PERIOD = 60; | ||
//The amount that is set aside for manual refresh or creation of issues, labels and milestones. | ||
public static final int APIQUOTA_BUFFER = 200; | ||
|
||
public TickingTimerManager(TickingTimer timer) { | ||
this.timer = timer; | ||
} | ||
|
||
public void startTimer() { | ||
timer.start(); | ||
} | ||
|
||
public void restartTimer() { | ||
timer.restart(); | ||
} | ||
|
||
/** | ||
* Computes the TickerTimer period that are used for refreshing the issues periodically. | ||
* @param apiQuota : The remaining allowed api request until the next api request allowance renewal. | ||
* @param remainingTime : The remaining time left until the next api request allowance renewal. | ||
* @param lastApiCallsUsed : The amount of api used in the last api pull. | ||
*/ | ||
public long computeTickerTimerPeriod(int apiQuota, long remainingTime, int lastApiCallsUsed) { | ||
|
||
assert apiQuota >= 0 && remainingTime >= 0 && lastApiCallsUsed >= 0; | ||
|
||
long refreshTimeInMins = 1; | ||
|
||
if (isDuringAppInitOrRemainingTimeIsZero(apiQuota, remainingTime, lastApiCallsUsed)) { | ||
refreshTimeInMins = Utility.secsToMins(REFRESH_PERIOD); | ||
return refreshTimeInMins; | ||
} | ||
|
||
if (isQuotaInsufficient(apiQuota, lastApiCallsUsed)) { | ||
refreshTimeInMins = remainingTime + BUFFER_TIME; | ||
return refreshTimeInMins; | ||
} | ||
|
||
if (isQuotaSufficient(apiQuota, lastApiCallsUsed)) { | ||
|
||
refreshTimeInMins = (long) Math.ceil(remainingTime / computeNoOfRefreshAllowed(apiQuota, lastApiCallsUsed)); | ||
|
||
if (computeNoOfRefreshAllowed(apiQuota, lastApiCallsUsed) == 1) { | ||
refreshTimeInMins++; | ||
} | ||
return refreshTimeInMins; | ||
} | ||
|
||
return refreshTimeInMins; | ||
} | ||
|
||
private double computeNoOfRefreshAllowed(int apiQuota, int lastApiCallsUsed) { | ||
return Math.floor((apiQuota - APIQUOTA_BUFFER) / (double) lastApiCallsUsed); | ||
} | ||
|
||
/** | ||
* Changes the timer refresh period. | ||
* @param period | ||
*/ | ||
public void changeTickingTimerPeriodInMins(int period) { | ||
timer.changePeriodInSecs((int) Utility.minsToSecs(period)); | ||
} | ||
|
||
private boolean isQuotaSufficient(int apiQuota, int lastApiCallsUsed) { | ||
return apiQuota - APIQUOTA_BUFFER >= lastApiCallsUsed; | ||
} | ||
|
||
private boolean isQuotaInsufficient(int apiQuota, int lastApiCallsUsed) { | ||
return apiQuota == 0 && lastApiCallsUsed == 0 || apiQuota <= APIQUOTA_BUFFER | ||
|| apiQuota - APIQUOTA_BUFFER > 0 && apiQuota - APIQUOTA_BUFFER < lastApiCallsUsed; | ||
} | ||
|
||
private boolean isDuringAppInitOrRemainingTimeIsZero(int apiQuota, long remainingTime, int lastApiCallsUsed) { | ||
return apiQuota != 0 && lastApiCallsUsed == 0 || remainingTime == 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.