report bandwidth, only pull changes

This commit is contained in:
j 2015-12-01 00:26:35 +01:00
parent 1e17e4a9d1
commit 68383b8834
8 changed files with 78 additions and 26 deletions

26
oml/bandwidth.py Normal file
View file

@ -0,0 +1,26 @@
import state
from websocket import trigger_event
class Bandwidth(object):
up = 0
down = 0
_last = {}
def __init__(self):
self.update()
def update(self):
bandwidth = {'up': self.up, 'down': self.down}
if bandwidth != self._last:
trigger_event('bandwidth', bandwidth)
self._last = bandwidth
self.up = 0
self.down = 0
state.main.call_later(1, self.update)
def download(self, amount):
self.down += amount * 8
def upload(self, amount):
self.up += amount * 8

View file

@ -58,8 +58,8 @@ class Changelog(db.Model):
_data = _data.encode() _data = _data.encode()
state.db.session.add(c) state.db.session.add(c)
state.db.session.commit() state.db.session.commit()
if state.nodes: #if state.nodes:
state.nodes.queue('peered', 'pushChanges', [c.json()]) # state.nodes.queue('peered', 'pushChanges', [c.json()])
@classmethod @classmethod
def apply_changes(cls, user, changes): def apply_changes(cls, user, changes):

View file

@ -34,6 +34,8 @@ def api_pullChanges(remote_id, user_id=None, from_=None, to=None):
return [c.json() for c in qs] return [c.json() for c in qs]
def api_pushChanges(user_id, changes): def api_pushChanges(user_id, changes):
logger.debug('pushChanges no longer used, ignored')
return True
user = User.get(user_id) user = User.get(user_id)
if not Changelog.apply_changes(user, changes): if not Changelog.apply_changes(user, changes):
logger.debug('FAILED TO APPLY CHANGE') logger.debug('FAILED TO APPLY CHANGE')

View file

@ -244,7 +244,7 @@ class Node(Thread):
else: else:
#fixme, what about cancel/reject peering here? #fixme, what about cancel/reject peering here?
self.peering('removePeering') self.peering('removePeering')
if self.online: if self.peered and self.online:
self.pullChanges() self.pullChanges()
except: except:
logger.debug('failed to connect to %s', self.user_id) logger.debug('failed to connect to %s', self.user_id)
@ -316,24 +316,30 @@ class Node(Thread):
return False return False
if r.getcode() == 200: if r.getcode() == 200:
try: try:
fileobj = r
if r.headers.get('content-encoding', None) == 'gzip': if r.headers.get('content-encoding', None) == 'gzip':
content = gzip.GzipFile(fileobj=r).read() fileobj = gzip.GzipFile(fileobj=r)
else: content = b''
content = b'' ct = datetime.utcnow()
ct = datetime.utcnow() size = 0
for chunk in iter(lambda: r.read(16*1024), b''): for chunk in iter(lambda: fileobj.read(16*1024), b''):
content += chunk content += chunk
if (datetime.utcnow() - ct).total_seconds() > 1: size += len(chunk)
ct = datetime.utcnow() since_ct = (datetime.utcnow() - ct).total_seconds()
t = Transfer.get(item.id) if since_ct > 1:
t.progress = len(content) / item.info['size'] ct = datetime.utcnow()
t.save() t = Transfer.get(item.id)
trigger_event('transfer', { t.progress = len(content) / item.info['size']
'id': item.id, 'progress': t.progress t.save()
}) trigger_event('transfer', {
''' 'id': item.id, 'progress': t.progress
content = r.read() })
''' if state.bandwidth:
state.bandwidth.download(size/since_ct)
size = 0
'''
content = fileobj.read()
'''
t2 = datetime.utcnow() t2 = datetime.utcnow()
duration = (t2-t1).total_seconds() duration = (t2-t1).total_seconds()
@ -468,3 +474,8 @@ def check_nodes():
if not state.nodes.is_online(u.id): if not state.nodes.is_online(u.id):
logger.debug('queued peering message for %s trying to connect...', u.id) logger.debug('queued peering message for %s trying to connect...', u.id)
state.nodes.queue('add', u.id) state.nodes.queue('add', u.id)
for u in user.models.User.query.filter_by(peered=True):
if state.nodes.is_online(u.id):
u.pullChanges()
else:
u.go_online()

View file

@ -110,6 +110,8 @@ def run():
import downloads import downloads
import nodes import nodes
import tor import tor
import bandwidth
state.bandwidth = bandwidth.Bandwidth()
state.tor = tor.Tor() state.tor = tor.Tor()
state.node = node.server.start() state.node = node.server.start()
state.downloads = downloads.Downloads() state.downloads = downloads.Downloads()

View file

@ -1,3 +1,4 @@
bandwidth = None
host = None host = None
main = None main = None
nodes = False nodes = False

View file

@ -184,7 +184,7 @@ def getVersion(data):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate() stdout, stderr = p.communicate()
new = stdout.strip()[:40] new = stdout.strip()[:40]
response['update'] = current != new response['update'] = len(new) == 40 and current != new
else: else:
if not os.path.exists(os.path.join(settings.updates_path, 'release.json')): if not os.path.exists(os.path.join(settings.updates_path, 'release.json')):
return response return response

View file

@ -10,24 +10,34 @@ oml.ui.connectionButton = function() {
}) })
.bindEvent({ .bindEvent({
// ... // ...
}); }),
bandwidth;
/* /*
oml.ui.statusIcon(oml.user.online ? 'connected' : 'disconnected') oml.ui.statusIcon(oml.user)
.css({float: 'left'}) .css({float: 'left'})
.appendTo(that); .appendTo(that);
*/ */
Ox.Element() function formatBandwidth(up, down) {
return '↓'+Ox.formatValue(down, 'b')+' / ↑'+Ox.formatValue(up, 'b')+'';
}
bandwidth = Ox.Element()
.addClass('OxLight') .addClass('OxLight')
.css({ .css({
float: 'left', float: 'left',
marginTop: '2px', marginTop: '2px',
fontSize: '9px' fontSize: '9px'
}) })
.html('↓0K/↑0K') .html(formatBandwidth(0, 0))
.appendTo(that); .appendTo(that);
oml.bindEvent({
bandwidth: function(data) {
bandwidth.html(formatBandwidth(data.up, data.down));
}
});
return that; return that;
}; };