azure-kusto-ingest
Version:
Azure Data Explorer Ingestion SDK
57 lines • 2.53 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { RankedStorageAccount } from "./rankedStorageAccount.js";
export class RankedStorageAccountSet {
constructor(numberOfBuckets = RankedStorageAccountSet.DefaultNumberOfBuckets, bucketDuration = RankedStorageAccountSet.DefaultBucketDurationInSeconds, tiers = RankedStorageAccountSet.DefaultTiers, timeProvider = RankedStorageAccountSet.DefaultTimeProviderInSeconds) {
this.numberOfBuckets = numberOfBuckets;
this.bucketDuration = bucketDuration;
this.tiers = tiers;
this.timeProvider = timeProvider;
this.accounts = new Map();
}
logResultToAccount(accountName, result) {
var _a;
if (!this.accounts.has(accountName)) {
throw new Error("Storage account name is not part of the set.");
}
(_a = this.accounts.get(accountName)) === null || _a === void 0 ? void 0 : _a.logResult(result);
}
registerStorageAccount(accountName) {
if (this.accounts.has(accountName)) {
return;
}
this.accounts.set(accountName, new RankedStorageAccount(accountName, this.numberOfBuckets, this.bucketDuration, this.timeProvider));
}
getStorageAccount(accountName) {
const account = this.accounts.get(accountName);
if (account) {
return account;
}
throw new Error("Storage account name is not part of the set.");
}
getRankedShuffledAccounts() {
const accountsByTier = new Array(this.tiers.length);
// Group accounts by tier and rank
for (const account of this.accounts.values()) {
const rank = account.getRank() * 100;
const tierInedx = this.tiers.findIndex((tier) => rank >= tier);
accountsByTier[tierInedx] = accountsByTier[tierInedx] || [];
accountsByTier[tierInedx].push(account);
}
// Shuffle each tier
for (let i = 0; i < this.tiers.length; i++) {
if (accountsByTier[i]) {
accountsByTier[i].sort(() => Math.random() - 0.5);
}
}
// Flatten the array
return accountsByTier.flat();
}
}
RankedStorageAccountSet.DefaultNumberOfBuckets = 6;
RankedStorageAccountSet.DefaultBucketDurationInSeconds = 10;
RankedStorageAccountSet.DefaultTiers = [90, 70, 30, 0];
RankedStorageAccountSet.DefaultTimeProviderInSeconds = () => {
return new Date().getTime() / 1000;
};
//# sourceMappingURL=rankedStorageAccountSet.js.map