azure-kusto-ingest
Version:
Azure Data Explorer Ingestion SDK
69 lines • 2.45 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
class StorageAccountStats {
constructor() {
this.successCount = 0;
this.totalCount = 0;
}
logResult(success) {
this.totalCount += 1;
if (success) {
this.successCount += 1;
}
}
reset() {
this.successCount = 0;
this.totalCount = 0;
}
}
export class RankedStorageAccount {
constructor(accountName, numberOfBuckets, bucketDuration, timeProvider) {
this.accountName = accountName;
this.numberOfBuckets = numberOfBuckets;
this.bucketDuration = bucketDuration;
this.timeProvider = timeProvider;
this.buckets = new Array(numberOfBuckets).fill(new StorageAccountStats()).map(() => new StorageAccountStats());
this.lastUpdateTime = this.timeProvider();
this.currentBucketIndex = 0;
}
logResult(success) {
this.currentBucketIndex = this.adjustForTimePassed();
this.buckets[this.currentBucketIndex].logResult(success);
}
getAccountName() {
return this.accountName;
}
adjustForTimePassed() {
const currentTime = this.timeProvider();
const timeDelta = currentTime - this.lastUpdateTime;
let window_size = 0;
if (timeDelta >= this.bucketDuration) {
this.lastUpdateTime = currentTime;
window_size = Math.min(Math.floor(timeDelta / this.bucketDuration), this.numberOfBuckets);
for (let i = 1; i < window_size + 1; i++) {
const indexToReset = (this.currentBucketIndex + i) % this.numberOfBuckets;
this.buckets[indexToReset].reset();
}
}
return (this.currentBucketIndex + window_size) % this.numberOfBuckets;
}
getRank() {
let rank = 0;
let totalWeight = 0;
for (let i = 1; i <= this.numberOfBuckets; i++) {
const bucketIndex = (this.currentBucketIndex + i) % this.numberOfBuckets;
const bucket = this.buckets[bucketIndex];
if (bucket.totalCount === 0) {
continue;
}
const successRate = bucket.successCount / bucket.totalCount;
rank += successRate * i;
totalWeight += i;
}
if (totalWeight === 0) {
return 1;
}
return rank / totalWeight;
}
}
//# sourceMappingURL=rankedStorageAccount.js.map