btimby / cloudstrype

Personal cloud
MIT License
3 stars 0 forks source link

Cloud selection #12

Open btimby opened 7 years ago

btimby commented 7 years ago

Currently chunks are distributed randomly. Continue using random selection but weight clouds by available space.

btimby commented 7 years ago

def weighted_choice(choices):
   if isinstance(choices, dict):
       choices = choices.items()
   total = sum(w for c, w in choices)
   r = random.uniform(0, total)
   upto = 0
   for c, w in choices:
      upto += w
      if upto >= r:
         return c
   assert False, "no random choice could be made: bug!"

# Size / used
clouds = {
    'dropbox': 1000 / 100,
    'onedrive': 1000 / 10,
    'box': 1000 / 700,
}

>>> for i in range(100):
...     c[weighted_choice(clouds)] += 1
... 
>>> print(c)
Counter({'onedrive': 89, 'dropbox': 9, 'box': 2})