def __init__( self ):
     
     HydrusSerialisable.SerialisableBase.__init__( self )
     
     self.engine = None
     
     self._dirty = False
     
     self._lock = threading.Lock()
     
     self._last_pages_gallery_query_timestamps = collections.defaultdict( lambda: 0 )
     self._last_subscriptions_gallery_query_timestamps = collections.defaultdict( lambda: 0 )
     self._last_watchers_query_timestamps = collections.defaultdict( lambda: 0 )
     
     self._tracker_container_names_to_tracker_containers = {}
     self._network_contexts_to_tracker_containers = {}
     
     self._tracker_container_names = set()
     self._dirty_tracker_container_names = set()
     self._deletee_tracker_container_names = set()
     
     self._my_bandwidth_tracker = HydrusNetworking.BandwidthTracker()
     
     self._network_contexts_to_bandwidth_rules = collections.defaultdict( HydrusNetworking.BandwidthRules )
     
     for context_type in [ CC.NETWORK_CONTEXT_GLOBAL, CC.NETWORK_CONTEXT_HYDRUS, CC.NETWORK_CONTEXT_DOMAIN, CC.NETWORK_CONTEXT_DOWNLOADER_PAGE, CC.NETWORK_CONTEXT_SUBSCRIPTION, CC.NETWORK_CONTEXT_WATCHER_PAGE ]:
         
         self._network_contexts_to_bandwidth_rules[ ClientNetworkingContexts.NetworkContext( context_type ) ] = HydrusNetworking.BandwidthRules()
 def _GetTracker( self, network_context: ClientNetworkingContexts.NetworkContext, making_it_dirty = False ):
     
     if network_context not in self._network_contexts_to_tracker_containers:
         
         bandwidth_tracker = HydrusNetworking.BandwidthTracker()
         
         tracker_container_name = HydrusData.GenerateKey().hex()
         
         tracker_container = NetworkBandwidthManagerTrackerContainer( tracker_container_name, network_context = network_context, bandwidth_tracker = bandwidth_tracker )
         
         self._tracker_container_names_to_tracker_containers[ tracker_container_name ] = tracker_container
         self._network_contexts_to_tracker_containers[ network_context ] = tracker_container
         
         # note this discards ephemeral network contexts, which have temporary identifiers that are generally invisible to the user
         
         if not network_context.IsEphemeral():
             
             self._tracker_container_names.add( tracker_container_name )
             self._dirty_tracker_container_names.add( tracker_container_name )
             
         
         self._SetDirty()
         
     
     tracker_container = self._network_contexts_to_tracker_containers[ network_context ]
     
     if making_it_dirty and not network_context.IsEphemeral():
         
         self._dirty_tracker_container_names.add( tracker_container.GetName() )
         
     
     return tracker_container.bandwidth_tracker
 def GetTracker( self, network_context ):
     
     with self._lock:
         
         if network_context in self._network_contexts_to_tracker_containers:
             
             return self._GetTracker( network_context )
             
         else:
             
             return HydrusNetworking.BandwidthTracker()
    def __init__(self, name, network_context=None, bandwidth_tracker=None):

        if network_context is None:

            network_context = ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT

        if bandwidth_tracker is None:

            bandwidth_tracker = HydrusNetworking.BandwidthTracker()

        HydrusSerialisable.SerialisableBaseNamed.__init__(self, name)

        self.network_context = network_context
        self.bandwidth_tracker = bandwidth_tracker
 def test_bandwidth_tracker( self ):
     
     bandwidth_tracker = HydrusNetworking.BandwidthTracker()
     
     self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 0B in 0 requests this month' )
     
     now = HydrusData.GetNow()
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 0 )
         
         #
         
         bandwidth_tracker.ReportDataUsed( 1024 )
         bandwidth_tracker.ReportRequestUsed()
         
         self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 1.0KB in 1 requests this month' )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 1 )
         
     
     #
     
     five_secs_from_now = now + 5
     
     with patch.object( HydrusData, 'GetNow', return_value = five_secs_from_now ):
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 1 )
         
         #
         
         bandwidth_tracker.ReportDataUsed( 32 )
         bandwidth_tracker.ReportRequestUsed()
         
         bandwidth_tracker.ReportDataUsed( 32 )
         bandwidth_tracker.ReportRequestUsed()
         
         self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 1.1KB in 3 requests this month' )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 64 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 2 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 64 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 2 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 3 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 3 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 3 )
from mock import patch

from hydrus.core import HydrusConstants as HC
from hydrus.core import HydrusData
from hydrus.core.networking import HydrusNetworking

now = HydrusData.GetNow()

now_10 = now + 10

now_20 = now + 20

with patch.object( HydrusData, 'GetNow', return_value = now ):
    
    HIGH_USAGE = HydrusNetworking.BandwidthTracker()
    
    for i in range( 100 ):
        
        HIGH_USAGE.ReportRequestUsed()
        HIGH_USAGE.ReportDataUsed( random.randint( 512, 1024 ) )
        
    
    LOW_USAGE = HydrusNetworking.BandwidthTracker()
    
    LOW_USAGE.ReportRequestUsed()
    LOW_USAGE.ReportDataUsed( 1024 )
    
    ZERO_USAGE = HydrusNetworking.BandwidthTracker()
    
class TestBandwidthRules( unittest.TestCase ):