def _stats(params): from contacts import contactsdb from p2p import contact_status from lib import diskspace result = {} result['suppliers'] = contactsdb.num_suppliers() result['max_suppliers'] = settings.getSuppliersNumberDesired() result['online_suppliers'] = contact_status.countOnlineAmong( contactsdb.suppliers()) result['customers'] = contactsdb.num_customers() result['bytes_donated'] = settings.getDonatedBytes() result['value_donated'] = diskspace.MakeStringFromBytes( settings.getDonatedBytes()) result['bytes_needed'] = settings.getNeededBytes() result['value_needed'] = diskspace.MakeStringFromBytes( settings.getNeededBytes()) result['bytes_used_total'] = backup_fs.sizebackups() result['value_used_total'] = diskspace.MakeStringFromBytes( backup_fs.sizebackups()) result['bytes_used_supplier'] = 0 if ( contactsdb.num_suppliers() == 0) else (int(backup_fs.sizebackups() / contactsdb.num_suppliers())) result['bytes_indexed'] = backup_fs.sizefiles() + backup_fs.sizefolders() result['files_count'] = backup_fs.numberfiles() result['folders_count'] = backup_fs.numberfolders() result['items_count'] = backup_fs.counter() result['timestamp'] = time.time() return { 'result': result, }
def _stats(params): from contacts import contactsdb from p2p import contact_status from lib import diskspace result = {} result['suppliers'] = contactsdb.num_suppliers() result['max_suppliers'] = settings.getSuppliersNumberDesired() result['online_suppliers'] = contact_status.countOnlineAmong(contactsdb.suppliers()) result['customers'] = contactsdb.num_customers() result['bytes_donated'] = settings.getDonatedBytes() result['value_donated'] = diskspace.MakeStringFromBytes(settings.getDonatedBytes()) result['bytes_needed'] = settings.getNeededBytes() result['value_needed'] = diskspace.MakeStringFromBytes(settings.getNeededBytes()) result['bytes_used_total'] = backup_fs.sizebackups() result['value_used_total'] = diskspace.MakeStringFromBytes(backup_fs.sizebackups()) result['bytes_used_supplier'] = 0 if (contactsdb.num_suppliers() == 0) else (int(backup_fs.sizebackups() / contactsdb.num_suppliers())) result['bytes_indexed'] = backup_fs.sizefiles() + backup_fs.sizefolders() result['files_count'] = backup_fs.numberfiles() result['folders_count'] = backup_fs.numberfolders() result['items_count'] = backup_fs.counter() result['timestamp'] = time.time() return {'result': result, }
def doTestMyCapacity2(self, arg): """ Here are some values. - donated_bytes : you set this in the config - spent_bytes : how many space is taken from you by other users right now - free_bytes = donated_bytes - spent_bytes : not yet allocated space - used_bytes : size of all files, which you store on your disk for your customers """ current_customers = contactsdb.customers() removed_customers = [] spent_bytes = 0 donated_bytes = settings.getDonatedBytes() if os.path.isfile(settings.CustomersSpaceFile()): space_dict = bpio._read_dict(settings.CustomersSpaceFile(), {}) else: space_dict = {'free': donated_bytes} used_dict = bpio._read_dict(settings.CustomersUsedSpaceFile(), {}) lg.out( 8, 'customers_rejector.doTestMyCapacity donated=%d' % donated_bytes) try: int(space_dict['free']) for idurl, customer_bytes in space_dict.items(): if idurl != 'free': spent_bytes += int(customer_bytes) except: lg.exc() space_dict = {'free': donated_bytes} spent_bytes = 0 removed_customers = list(current_customers) current_customers = [] self.automat('space-overflow', (space_dict, spent_bytes, current_customers, removed_customers)) return lg.out(8, ' spent=%d' % spent_bytes) if spent_bytes < donated_bytes: space_dict['free'] = donated_bytes - spent_bytes bpio._write_dict(settings.CustomersSpaceFile(), space_dict) lg.out(8, ' space is OK !!!!!!!!') self.automat('space-enough') return used_space_ratio_dict = {} for customer_pos in range(contactsdb.num_customers()): customer_idurl = contactsdb.customer(customer_pos) try: allocated_bytes = int(space_dict[customer_idurl]) except: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s allocated space unknown' % customer_idurl) continue if allocated_bytes <= 0: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s allocated_bytes==0' % customer_idurl) continue try: files_size = int(used_dict.get(customer_idurl, 0)) ratio = float(files_size) / float(allocated_bytes) except: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s used_dict have wrong value' % customer_idurl) continue if ratio > 1.0: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) spent_bytes -= allocated_bytes lg.warn('%s space overflow, where is bptester?' % customer_idurl) continue used_space_ratio_dict[customer_idurl] = ratio customers_sorted = sorted( current_customers, key=lambda i: used_space_ratio_dict[i], ) while len(customers_sorted) > 0: customer_idurl = customers_sorted.pop() allocated_bytes = int(space_dict[customer_idurl]) spent_bytes -= allocated_bytes space_dict.pop(customer_idurl) current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) lg.out(8, ' customer %s REMOVED' % customer_idurl) if spent_bytes < donated_bytes: break space_dict['free'] = donated_bytes - spent_bytes lg.out(8, ' SPACE NOT ENOUGH !!!!!!!!!!') self.automat( 'space-overflow', (space_dict, spent_bytes, current_customers, removed_customers))
def report_donated_storage(): space_dict, free_space = read_customers_quotas() used_space_dict = read_customers_usage() r = {} r['customers_num'] = contactsdb.num_customers() r['customers'] = [] r['old_customers'] = [] r['errors'] = [] r['consumed'] = 0 r['donated'] = settings.getDonatedBytes() # r['donated_str'] = diskspace.MakeStringFromBytes(r['donated']) r['real'] = bpio.getDirectorySize(settings.getCustomersFilesDir()) try: r['free'] = int(free_space) except: r['free'] = 0 used = 0 for idurl in id_url.to_bin_list(contactsdb.customers()): consumed_by_customer = 0 used_by_customer = 0 if idurl not in list(space_dict.keys()): r['errors'].append('space consumed by customer %r is unknown' % idurl) else: try: consumed_by_customer = int(space_dict.pop(idurl)) r['consumed'] += consumed_by_customer except: r['errors'].append( 'incorrect value of consumed space for customer %r' % idurl) continue if idurl in list(used_space_dict.keys()): try: used_by_customer = int(used_space_dict.pop(idurl)) used += used_by_customer except: r['errors'].append( 'incorrect value of used space for customer %r' % idurl) continue if consumed_by_customer < used_by_customer: r['errors'].append( 'customer %r currently using more space than requested' % idurl) c = {} c['idurl'] = strng.to_text(idurl) c['used'] = used_by_customer # c['used_str'] = diskspace.MakeStringFromBytes(c['used']) c['consumed'] = consumed_by_customer # c['consumed_str'] = diskspace.MakeStringFromBytes(c['consumed']) c['real'] = bpio.getDirectorySize(settings.getCustomerFilesDir(idurl)) # c['real_str'] = diskspace.MakeStringFromBytes(c['real']) r['customers'].append(c) r['used'] = used # r['used_str'] = diskspace.MakeStringFromBytes(r['used']) # r['consumed_str'] = diskspace.MakeStringFromBytes(r['consumed']) if r['donated'] != r['free'] + r['consumed']: r['errors'].append( 'total consumed %d and known free %d (%d total) bytes not match with donated %d bytes' % (r['consumed'], r['free'], r['consumed'] + r['free'], r['donated'])) if r['used'] > r['donated']: r['errors'].append( 'total space used by customers exceed the donated limit') if len(space_dict) > 0: r['errors'].append('found %d incorrect records of consumed space' % len(space_dict)) if r['real'] != r['used']: r['errors'].append( 'current info needs update, known size is %d bytes but real is %d bytes' % (r['used'], r['real'])) old_customers_used = 0 old_customers_real = 0 for idurl in used_space_dict.keys(): real = bpio.getDirectorySize(settings.getCustomerFilesDir(idurl)) try: used = int(used_space_dict[idurl]) except: r['errors'].append( 'incorrect value of used space for customer %r' % idurl) continue r['old_customers'].append({ 'idurl': strng.to_text(idurl), 'used': used, # 'used_str': diskspace.MakeStringFromBytes(used_space_dict[idurl]), 'real': real, # 'real_str': diskspace.MakeStringFromBytes(real), }) old_customers_used += used old_customers_real += real r['old_customers_used'] = old_customers_used r['old_customers_real'] = old_customers_real try: r['used_percent'] = misc.value2percent(float(r['used']), float(r['donated']), 5) except: r['used_percent'] = '' try: r['consumed_percent'] = misc.value2percent(float(r['consumed']), float(r['donated']), 5) except: r['consumed_percent'] = '' return r
def report_donated_storage(): space_dict = read_customers_quotas() used_space_dict = read_customers_usage() r = {} r['customers_num'] = contactsdb.num_customers() r['customers'] = [] r['old_customers'] = [] r['errors'] = [] r['consumed'] = 0 r['donated'] = settings.getDonatedBytes() r['donated_str'] = diskspace.MakeStringFromBytes(r['donated']) r['real'] = bpio.getDirectorySize(settings.getCustomersFilesDir()) try: r['free'] = int(space_dict.pop('free')) except: r['free'] = 0 used = 0 for idurl in contactsdb.customers(): consumed_by_customer = 0 used_by_customer = 0 if idurl not in space_dict.keys(): r['errors'].append('space consumed by customer %s is unknown' % idurl) else: try: consumed_by_customer = int(space_dict.pop(idurl)) r['consumed'] += consumed_by_customer except: r['errors'].append('incorrect value of consumed space for customer %s' % idurl) if idurl in used_space_dict.keys(): try: used_by_customer = int(used_space_dict.pop(idurl)) used += used_by_customer except: r['errors'].append('incorrect value of used space for customer %s' % idurl) if consumed_by_customer < used_by_customer: r['errors'].append('customer %s currently using more space than requested' % idurl) c = {} c['idurl'] = idurl c['used'] = used_by_customer c['used_str'] = diskspace.MakeStringFromBytes(c['used']) c['consumed'] = consumed_by_customer c['consumed_str'] = diskspace.MakeStringFromBytes(c['consumed']) c['real'] = bpio.getDirectorySize(settings.getCustomerFilesDir(idurl)) c['real_str'] = diskspace.MakeStringFromBytes(c['real']) r['customers'].append(c) r['used'] = used r['used_str'] = diskspace.MakeStringFromBytes(r['used']) r['consumed_str'] = diskspace.MakeStringFromBytes(r['consumed']) if r['donated'] != r['free'] + r['consumed']: r['errors'].append('total consumed %d and known free %d (%d total) bytes not match with donated %d bytes' % ( r['consumed'], r['free'], r['consumed'] + r['free'], r['donated'])) if r['used'] > r['donated']: r['errors'].append('total space used by customers exceed the donated limit') if len(space_dict) > 0: r['errors'].append('found %d incorrect records of consumed space' % len(space_dict)) if r['real'] != r['used']: r['errors'].append('current info needs update, known size is %d bytes but real is %d bytes' % ( r['used'], r['real'])) for idurl in used_space_dict.keys(): real = bpio.getDirectorySize(settings.getCustomerFilesDir(idurl)) r['old_customers'].append({ 'idurl': idurl, 'used': used_space_dict['idurl'], 'used_str': diskspace.MakeStringFromBytes(used_space_dict['idurl']), 'real': real, 'real_str': diskspace.MakeStringFromBytes(real), }) try: r['used_percent'] = misc.percent2string(float(r['used']) / float(r['donated']), 5) except: r['used_percent'] = '' try: r['consumed_percent'] = misc.percent2string(float(r['consumed']) / float(r['donated']), 5) except: r['consumed_percent'] = '' return r
def doTestMyCapacity2(self, arg): """ Here are some values. - donated_bytes : you set this in the config - spent_bytes : how many space is taken from you by other users right now - free_bytes = donated_bytes - spent_bytes : not yet allocated space - used_bytes : size of all files, which you store on your disk for your customers """ current_customers = contactsdb.customers() removed_customers = [] spent_bytes = 0 donated_bytes = settings.getDonatedBytes() if os.path.isfile(settings.CustomersSpaceFile()): space_dict = bpio._read_dict(settings.CustomersSpaceFile(), {}) else: space_dict = {'free': donated_bytes} used_dict = bpio._read_dict(settings.CustomersUsedSpaceFile(), {}) lg.out(8, 'customers_rejector.doTestMyCapacity donated=%d' % donated_bytes) try: int(space_dict['free']) for idurl, customer_bytes in space_dict.items(): if idurl != 'free': spent_bytes += int(customer_bytes) except: lg.exc() space_dict = {'free': donated_bytes} spent_bytes = 0 removed_customers = list(current_customers) current_customers = [] self.automat('space-overflow', (space_dict, spent_bytes, current_customers, removed_customers)) return lg.out(8, ' spent=%d' % spent_bytes) if spent_bytes < donated_bytes: space_dict['free'] = donated_bytes - spent_bytes bpio._write_dict(settings.CustomersSpaceFile(), space_dict) lg.out(8, ' space is OK !!!!!!!!') self.automat('space-enough') return used_space_ratio_dict = {} for customer_pos in xrange(contactsdb.num_customers()): customer_idurl = contactsdb.customer(customer_pos) try: allocated_bytes = int(space_dict[customer_idurl]) except: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s allocated space unknown' % customer_idurl) continue if allocated_bytes <= 0: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s allocated_bytes==0' % customer_idurl) continue try: files_size = int(used_dict.get(customer_idurl, 0)) ratio = float(files_size) / float(allocated_bytes) except: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) lg.warn('%s used_dict have wrong value' % customer_idurl) continue if ratio > 1.0: if customer_idurl in current_customers: current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) else: lg.warn('%s not customers' % customer_idurl) spent_bytes -= allocated_bytes lg.warn('%s space overflow, where is bptester?' % customer_idurl) continue used_space_ratio_dict[customer_idurl] = ratio customers_sorted = sorted(current_customers, key=lambda i: used_space_ratio_dict[i],) while len(customers_sorted) > 0: customer_idurl = customers_sorted.pop() allocated_bytes = int(space_dict[customer_idurl]) spent_bytes -= allocated_bytes space_dict.pop(customer_idurl) current_customers.remove(customer_idurl) removed_customers.append(customer_idurl) lg.out(8, ' customer %s REMOVED' % customer_idurl) if spent_bytes < donated_bytes: break space_dict['free'] = donated_bytes - spent_bytes lg.out(8, ' SPACE NOT ENOUGH !!!!!!!!!!') self.automat('space-overflow', (space_dict, spent_bytes, current_customers, removed_customers))