Skip to content

Commit

Permalink
PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AsafMah committed Sep 27, 2023
1 parent 140bbd4 commit e7d1877
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,77 @@ public Bucket() {

private final ArrayDeque<Bucket> buckets = new ArrayDeque<>();
private final String accountName;
private final int bucketCount;
private final int maxNumberOfBuckets;
private final int bucketDurationInSec;
private final TimeProvider timeProvider;

private long lastActionTimestamp;

public RankedStorageAccount(String accountName, int bucketCount, int bucketDurationInSec, TimeProvider timeProvider) {
public RankedStorageAccount(String accountName, int maxNumberOfBuckets, int bucketDurationInSec, TimeProvider timeProvider) {
this.accountName = accountName;
this.bucketCount = bucketCount;
this.maxNumberOfBuckets = maxNumberOfBuckets;
this.bucketDurationInSec = bucketDurationInSec;
this.timeProvider = timeProvider;
lastActionTimestamp = timeProvider.currentTimeMillis();
}

public void addResult(boolean success) {
adjustForTimePassed();
Bucket lastBucket = buckets.peek();
assert lastBucket != null;
Bucket lastBucket = adjustForTimePassed();
if (success) {
lastBucket.successCount++;
} else {
lastBucket.failureCount++;
}
}

private void adjustForTimePassed() {
private Bucket adjustForTimePassed() {
if (buckets.isEmpty()) {
buckets.push(new Bucket());
return;
}

long timePassed = timeProvider.currentTimeMillis() - lastActionTimestamp;
long bucketsToCreate = timePassed / (bucketDurationInSec * 1000L);
if (bucketsToCreate >= bucketCount) {
if (bucketsToCreate >= maxNumberOfBuckets) {
buckets.clear();
buckets.push(new Bucket());
return;
}

for (int i = 0; i < bucketsToCreate; i++) {
buckets.push(new Bucket());
if (buckets.size() > bucketCount) {
if (buckets.size() > maxNumberOfBuckets) {
buckets.poll();
}
}

lastActionTimestamp = timeProvider.currentTimeMillis();
return buckets.peek();
}

public double getRank() {
if (buckets.isEmpty()) {
return 1;
}

int penalty = buckets.size() + 1;
int bucketWeight = buckets.size() + 1;
double rank = 0;
double totalPenalty = 0;
double totalWeight = 0;

// For each bucket, calculate the success rate ( success / total ) and multiply it by the bucket weight.
// The older the bucket, the less weight it has. For example, if there are 3 buckets, the oldest bucket will have
// a weight of 1, the middle bucket will have a weight of 2 and the newest bucket will have a weight of 3.

for (Bucket bucket : buckets) {
int total = bucket.successCount + bucket.failureCount;
if (total == 0) {
penalty--;
bucketWeight--;
continue;
}
double successRate = (double) bucket.successCount / total;
rank += successRate * penalty;
totalPenalty += penalty;
penalty--;
rank += successRate * bucketWeight;
totalWeight += bucketWeight;
bucketWeight--;
}
return rank / totalPenalty;
return rank / totalWeight;
}

public String getAccountName() {
Expand Down

0 comments on commit e7d1877

Please sign in to comment.