def filenameIN(basename=None): """ File name for incoming stats for today or other day. """ if basename is None: basename = misc.gmtime2str('%d%m%y') return os.path.join(settings.BandwidthInDir(), basename)
def filenameOUT(basename=None): """ File name for outgoing stats for today or other day. """ if basename is None: basename = misc.gmtime2str('%d%m%y') return os.path.join(settings.BandwidthOutDir(), basename)
def files2send(): """ Return a list of file names to be read and send later. Sent files are market with ".sent" extension and skipped here. """ lg.out(6, 'bandwidth.files2send') listIN = [] listOUT = [] for filename in os.listdir(settings.BandwidthInDir()): # if we sent the file - skip it if filename.endswith('.sent'): continue # if filename is not a date - skip it if len(filename) != 6: continue # skip today bandwidth - it is still counting, right? if filename == misc.gmtime2str('%d%m%y'): continue filepath = os.path.join(settings.BandwidthInDir(), filename) # if filepath == filenameIN(): # continue listIN.append(filepath) for filename in os.listdir(settings.BandwidthOutDir()): if filename.endswith('.sent'): continue if len(filename) != 6: continue if filename == misc.gmtime2str('%d%m%y'): # time.strftime('%d%m%y'): continue filepath = os.path.join(settings.BandwidthOutDir(), filename) # if filepath == filenameOUT(): # continue listOUT.append(filepath) lg.out( 6, 'bandwidth.files2send listIN=%d listOUT=%d' % (len(listIN), len(listOUT))) for i in listIN: lg.out(6, ' ' + i) for i in listOUT: lg.out(6, ' ' + i) return listIN, listOUT
def files2send(): """ Return a list of file names to be read and send later. Sent files are market with ".sent" extension and skipped here. """ lg.out(6, 'bandwidth.files2send') listIN = [] listOUT = [] for filename in os.listdir(settings.BandwidthInDir()): # if we sent the file - skip it if filename.endswith('.sent'): continue # if filename is not a date - skip it if len(filename) != 6: continue # skip today bandwidth - it is still counting, right? if filename == misc.gmtime2str('%d%m%y'): continue filepath = os.path.join(settings.BandwidthInDir(), filename) # if filepath == filenameIN(): # continue listIN.append(filepath) for filename in os.listdir(settings.BandwidthOutDir()): if filename.endswith('.sent'): continue if len(filename) != 6: continue if filename == misc.gmtime2str('%d%m%y'): # time.strftime('%d%m%y'): continue filepath = os.path.join(settings.BandwidthOutDir(), filename) # if filepath == filenameOUT(): # continue listOUT.append(filepath) lg.out(6, 'bandwidth.files2send listIN=%d listOUT=%d' % (len(listIN), len(listOUT))) for i in listIN: lg.out(6, ' ' + i) for i in listOUT: lg.out(6, ' ' + i) return listIN, listOUT
def saveOUT(basename=None): """ Writes outgoing stats for today on disk. """ if basename is None: basename = misc.gmtime2str('%d%m%y') ret = os.path.isfile(filenameOUT(basename)) bpio._write_dict(filenameOUT(basename), getBandwidthOUT()) if not ret: lg.out(4, 'bandwidth.saveOUT to new file ' + basename) else: lg.out(22, 'bandwidth.saveOUT to ' + basename) return ret
def OUT(idurl, size): """ Call this when need to count outgoing bandwidth. ``size`` - how many bytes sent to user with ``idurl``. Typically called when outgoing packet were sent. """ global BandOutDict global CountTimeOut if not isExistOUT(): currentFileName = time.strftime('%d%m%y', time.localtime(CountTimeOut)) if currentFileName != misc.gmtime2str('%d%m%y'): saveOUT(currentFileName) clear_bandwidthOUT() CountTimeOut = time.time() currentV = int(BandOutDict.get(idurl, 0)) newV = currentV + size BandOutDict[idurl] = newV curMB = int(currentV / (1024.0 * 1024.0)) newMB = int(newV / (1024.0 * 1024.0)) if curMB == 0 or curMB != newMB: saveOUT()
def IN(idurl, size): """ Call this when need to count incoming bandwidth. ``size`` - how many incoming bytes received from user with ``idurl``. Typically called when incoming packet arrives. """ global BandInDict global CountTimeIn if not isExistIN(): currentFileName = time.strftime('%d%m%y', time.localtime(CountTimeIn)) if currentFileName != misc.gmtime2str('%d%m%y'): saveIN(currentFileName) clear_bandwidthIN() CountTimeIn = time.time() currentV = int(BandInDict.get(idurl, 0)) newV = currentV + size BandInDict[idurl] = newV curMB = int(currentV / (1024.0 * 1024.0)) newMB = int(newV / (1024.0 * 1024.0)) if curMB == 0 or curMB != newMB: saveIN()