Debian Codegraph @

azure-kusto-ingest/azure/kusto/ingest/_ranked_storage_account.py

raw
1 from typing import Callable
2
3
4 class _StorageAccountStats:
5 def __init__(self):
6 self.success_count = 0
7 self.total_count = 0
8
9 def log_result(self, success: bool):
10 self.total_count += 1
11 if success:
12 self.success_count += 1
13
14 def reset(self):
15 self.success_count = 0
16 self.total_count = 0
17
18
19 class _RankedStorageAccount:
20 """_RankedStorageAccount is a class that represents a storage account with a rank.
21 The rank is used to determine the order in which the storage accounts are used for ingestion.
22 """
23
24 def __init__(self, account_name: str, number_of_buckets: int, bucket_duration: float, time_provider: Callable[[], float]):
25 self.account_name = account_name
26 self.number_of_buckets = number_of_buckets
27 self.bucket_duration = bucket_duration
28 self.time_provider = time_provider
29 self.buckets = [_StorageAccountStats() for _ in range(number_of_buckets)]
30 self.last_update_time = self.time_provider()
31 self.current_bucket_index = 0
32
33 def log_result(self, success: bool):
34 self.current_bucket_index = self._adjust_for_time_passed()
35 self.buckets[self.current_bucket_index].log_result(success)
36
37 def get_account_name(self) -> str:
38 return self.account_name
39
40 def get_rank(self) -> float:
41 rank = 0
42 total_weight = 0
43
44 # For each bucket, calculate the success rate ( success / total ) and multiply it by the bucket weight.
45 # The older the bucket, the less weight it has. For example, if there are 3 buckets, the oldest bucket will have
46 # a weight of 1, the middle bucket will have a weight of 2 and the newest bucket will have a weight of 3.
47
48 for i in range(1, self.number_of_buckets + 1):
49 bucket_index = (self.current_bucket_index + i) % self.number_of_buckets
50 bucket = self.buckets[bucket_index]
51 if bucket.total_count == 0:
52 continue
53 success_rate = bucket.success_count / bucket.total_count
54 rank += success_rate * i
55 total_weight += i
56
57 # If there are no buckets with data, return 1 (highest rank)
58 if total_weight == 0:
59 return 1
60
61 return rank / total_weight
62
63 def _adjust_for_time_passed(self) -> int:
64 # Get the current window (bucket) index and reset old windows.
65 # This is part of the moving avarge calculation.
66 current_time = self.time_provider()
67 time_delta = current_time - self.last_update_time
68 window_size = 0
69
70 if time_delta >= self.bucket_duration:
71 self.last_update_time = current_time
72 window_size = min(int(time_delta / self.bucket_duration), self.number_of_buckets)
73 for i in range(1, window_size + 1):
74 index_to_reset = (self.current_bucket_index + i) % self.number_of_buckets
75 self.buckets[index_to_reset].reset()
76
77 return (self.current_bucket_index + window_size) % self.number_of_buckets