def endpoint_register(request): """Registers an endpoint server. An endpoint server is one which is sending data to end users. It might be an endpoint in smaller setups, or in more advanced systems an amplifier. """ response, group, ip = endpoint_common(request) if response is not None: return response try: data = simplejson.loads(request.POST.get('data', '{}')) # Check that the data doesn't override these two important values assert 'ip' not in data assert 'group' not in data s = models.Endpoint(group=group, ip=ip, **data) s.save() response = http.HttpResponse(content_type='text/plain') response.write('OK\n') return response except Exception, e: response = http.HttpResponse(content_type='text/plain') response.write('ERROR %s\n' % e.__class__.__name__) traceback.print_exc(file=response) return response
def endpoint_stats(request): """Print out some stats about registered endpoints.""" response = http.HttpResponse() inactive_servers = [] active_servers = [] ten_mins_ago = datetime.datetime.now() - datetime.timedelta(minutes=10) endpoints = models.Endpoint.active(delta=datetime.timedelta(days=7)) for server in endpoints: if server.lastseen < ten_mins_ago: inactive_servers.append(server) else: active_servers.append(server) types = list( sorted([x for x in dir(models.Endpoint()) if x.endswith('_clients')])) all_types = list( sorted([x for x in dir(models.Endpoint()) if not x.startswith('_')])) active_servers = sorted(active_servers, cmp=lambda a, b: cmp((a.group, a.overall_bitrate), (b.group, b.overall_bitrate))) active_overall = funnydict( (t, sum([0, getattr(x, t, None)][isinstance(getattr(x, t, None), (int, float))] for x in active_servers)) for t in all_types) inactive_servers = sorted(inactive_servers, cmp=lambda a, b: cmp( (a.group, a.overall_bitrate), (b.group, b.overall_bitrate))) inactive_overall = funnydict( (t, sum([0, getattr(x, t, None)][isinstance(getattr(x, t, None), (int, float))] for x in inactive_servers)) for t in all_types) return render(request, 'stats.html', locals(), content_type='text/html', context_instance=template.RequestContext(request))
def youtube_endpoint(group): return models.Endpoint(group=group, ip='127.0.0.1')