<commit_before>from django.conf import settings
from django.template import Context, RequestContext, loader
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404
from django.db import models, transaction
from django.forms import forms
from django.utils.translation import ugettext as _
from wlanlj.nodes.models import Node, NodeType, NodeStatus, Subnet, SubnetStatus, APClient, Pool, WhitelistItem, Link, Event, EventSubscription, SubscriptionType, Project, EventCode, EventSource, GraphItemNP, GraphType, PoolStatus
from wlanlj.nodes.forms import RegisterNodeForm, UpdateNodeForm, AllocateSubnetForm, WhitelistMacForm, InfoStickerForm, EventSubscribeForm, RenumberForm, RenumberAction, EditSubnetForm
from wlanlj.nodes.common import ValidationWarning
from wlanlj.generator.models import Profile
from wlanlj.account.models import UserAccount
from wlanlj.policy.models import Policy, PolicyFamily, TrafficControlClass
from datetime import datetime
from wlanlj.nodes import ipcalc
from django.core.urlresolvers import reverse

def nodes(request):
  """
  Display a list of all current nodes and their status.
  """
  # TODO: #362
  def type_ip_order(x, y):
    c = cmp(x.node_type, y.node_type)
    if c != 0:
      return c
    return cmp(long(ipcalc.IP(x.ip)), long(ipcalc.IP(y.ip)))
  nodes = list(Node.objects.all())
  nodes.sort(type_ip_order)
  return render_to_response('nodes/list.html',
    { 'nodes' : nodes },
    context_instance = RequestContext(request)
  )

def pools(request):
  """
  Displays IP allocation pools.
  """
  # TODO: #362
  def ip_order(x, y):
    return cmp(long(ipcalc.IP(str(x.ip_subnet))), long(ipcalc.IP(str(y.ip_subnet))))
  pools = list(Pool.objects.filter(parent = None))
  pools.sort(ip_order)
  return render_to_response('nodes/pools.html',
    { 'pools' : pools },
    context_instance = RequestContext(request)
  )

def pools_text(request):
  """
  Displays IP allocation pools as tab separated text values.
  """
  output = []
  for pool in Pool.objects.filter(parent = None).order_by('id'):
    output.append("%s\t%s\t%s" % (pool.ip_subnet, pool.family_as_string(), pool.description))

  return HttpResponse("\n".join(output), content_type = "text/plain")

def statistics(request):
  """
  Displays some global statistics.
  """
  # Nodes by status
  node_count = Node.objects.all().count()
  nodes_by_status = []
  for s in Node.objects.exclude(node_type = NodeType.Test).order_by('status').values('status').annotate(count = models.Count('ip')):
    nodes_by_status.append({ 'status' : NodeStatus.as_string(s['status']), 'count' : s['count'] })

  # Nodes by template usage
  others = Node.objects.exclude(node_type = NodeType.Test).count()
  templates_by_usage = []
  for t in Profile.objects.exclude(node__node_type = NodeType.Test).values('template__name').annotate(count = models.Count('node')).order_by('template__name'):
    templates_by_usage.append({ 'template' : t['template__name'], 'count' : t['count'] })
    others -= t['count']
  
  if others > 0:
    templates_by_usage.append({ 'template' : _("unknown"), 'count' : others, 'special' : True })

  # Nodes by project
  all_nodes = Node.objects.exclude(node_type = NodeType.Test).values('project__name', 'project__pk').annotate(count = models.Count('ip'))
  up_nodes = all_nodes.filter(status = NodeStatus.Up)
  nodes_by_project = []
  others = 0
  for p in all_nodes.order_by('project__id'):
    if not p['project__name']:
      others = p['count']
    else:
      try:
        up_count = filter(lambda x: x['project__pk'] == p['project__pk'], up_nodes)[0]['count']
      except IndexError:
        up_count = 0
      
      nodes_by_project.append({ 'name' : p['project__name'], 'count' : p['count'], 'up_count' : up_count})
  
  if others > 0:
    nodes_by_project.append({ 'name' : _("unknown"), 'count' : others, 'special' : True})

  return render_to_response('nodes/statistics.html',
    { 'node_count' : node_count,
      'nodes_by_status' : nodes_by_status,
      'nodes_by_project' : nodes_by_project,
      'nodes_warned' : Node.objects.filter(warnings = True).exclude(node_type = NodeType.Test).count(),
      'nodes_test' : Node.objects.filter(node_type = NodeType.Test).count(),
      'subnet_count' : Subnet.objects.all().count(),
      'clients_online' : APClient.objects.all().count(),
      'clients_ever' : Node.objects.aggregate(num = models.Sum('clients_so_far'))['num'],
      'external_ant' : Node.objects.filter(ant_external = True).exclude(node_type = NodeType.Test).count(),
      'template_usage' : templates_by_usage,
      'peers_avg' : Node.objects.filter(peers__gt = 0).aggregate(num = models.Avg('peers'))['num'],
      'graphs' : [
        GraphItemNP(1, GraphType.NodesByStatus, "global_nodes_by_status.png", "Nodes By Status"),
        GraphItemNP(2, GraphType.Clients, "global_client_count.png", "Global Client Count"),
        GraphItemNP(3, GraphType.GatewayTraffic, "global_replicator_traffic.png", "replicator - Traffic")
      ],
      'timespans' : [prefix for prefix, name in settings.GRAPH_TIMESPANS]
    },
    context_instance = RequestContext(request)
  )

@login_required
def my_nodes(request):<commit_msg>Only display two decimal places for average peers per node.<commit_after>from django.conf import settings
from django.template import Context, RequestContext, loader
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404
from django.db import models, transaction
from django.forms import forms
from django.utils.translation import ugettext as _
from wlanlj.nodes.models import Node, NodeType, NodeStatus, Subnet, SubnetStatus, APClient, Pool, WhitelistItem, Link, Event, EventSubscription, SubscriptionType, Project, EventCode, EventSource, GraphItemNP, GraphType, PoolStatus
from wlanlj.nodes.forms import RegisterNodeForm, UpdateNodeForm, AllocateSubnetForm, WhitelistMacForm, InfoStickerForm, EventSubscribeForm, RenumberForm, RenumberAction, EditSubnetForm
from wlanlj.nodes.common import ValidationWarning
from wlanlj.generator.models import Profile
from wlanlj.account.models import UserAccount
from wlanlj.policy.models import Policy, PolicyFamily, TrafficControlClass
from datetime import datetime
from wlanlj.nodes import ipcalc
from django.core.urlresolvers import reverse

def nodes(request):
  """
  Display a list of all current nodes and their status.
  """
  # TODO: #362
  def type_ip_order(x, y):
    c = cmp(x.node_type, y.node_type)
    if c != 0:
      return c
    return cmp(long(ipcalc.IP(x.ip)), long(ipcalc.IP(y.ip)))
  nodes = list(Node.objects.all())
  nodes.sort(type_ip_order)
  return render_to_response('nodes/list.html',
    { 'nodes' : nodes },
    context_instance = RequestContext(request)
  )

def pools(request):
  """
  Displays IP allocation pools.
  """
  # TODO: #362
  def ip_order(x, y):
    return cmp(long(ipcalc.IP(str(x.ip_subnet))), long(ipcalc.IP(str(y.ip_subnet))))
  pools = list(Pool.objects.filter(parent = None))
  pools.sort(ip_order)
  return render_to_response('nodes/pools.html',
    { 'pools' : pools },
    context_instance = RequestContext(request)
  )

def pools_text(request):
  """
  Displays IP allocation pools as tab separated text values.
  """
  output = []
  for pool in Pool.objects.filter(parent = None).order_by('id'):
    output.append("%s\t%s\t%s" % (pool.ip_subnet, pool.family_as_string(), pool.description))

  return HttpResponse("\n".join(output), content_type = "text/plain")

def statistics(request):
  """
  Displays some global statistics.
  """
  # Nodes by status
  node_count = Node.objects.all().count()
  nodes_by_status = []
  for s in Node.objects.exclude(node_type = NodeType.Test).order_by('status').values('status').annotate(count = models.Count('ip')):
    nodes_by_status.append({ 'status' : NodeStatus.as_string(s['status']), 'count' : s['count'] })

  # Nodes by template usage
  others = Node.objects.exclude(node_type = NodeType.Test).count()
  templates_by_usage = []
  for t in Profile.objects.exclude(node__node_type = NodeType.Test).values('template__name').annotate(count = models.Count('node')).order_by('template__name'):
    templates_by_usage.append({ 'template' : t['template__name'], 'count' : t['count'] })
    others -= t['count']
  
  if others > 0:
    templates_by_usage.append({ 'template' : _("unknown"), 'count' : others, 'special' : True })

  # Nodes by project
  all_nodes = Node.objects.exclude(node_type = NodeType.Test).values('project__name', 'project__pk').annotate(count = models.Count('ip'))
  up_nodes = all_nodes.filter(status = NodeStatus.Up)
  nodes_by_project = []
  others = 0
  for p in all_nodes.order_by('project__id'):
    if not p['project__name']:
      others = p['count']
    else:
      try:
        up_count = filter(lambda x: x['project__pk'] == p['project__pk'], up_nodes)[0]['count']
      except IndexError:
        up_count = 0
      
      nodes_by_project.append({ 'name' : p['project__name'], 'count' : p['count'], 'up_count' : up_count})
  
  if others > 0:
    nodes_by_project.append({ 'name' : _("unknown"), 'count' : others, 'special' : True})

  return render_to_response('nodes/statistics.html',
    { 'node_count' : node_count,
      'nodes_by_status' : nodes_by_status,
      'nodes_by_project' : nodes_by_project,
      'nodes_warned' : Node.objects.filter(warnings = True).exclude(node_type = NodeType.Test).count(),
      'nodes_test' : Node.objects.filter(node_type = NodeType.Test).count(),
      'subnet_count' : Subnet.objects.all().count(),
      'clients_online' : APClient.objects.all().count(),
      'clients_ever' : Node.objects.aggregate(num = models.Sum('clients_so_far'))['num'],
      'external_ant' : Node.objects.filter(ant_external = True).exclude(node_type = NodeType.Test).count(),
      'template_usage' : templates_by_usage,
      'peers_avg' : round(Node.objects.filter(peers__gt = 0).aggregate(num = models.Avg('peers'))['num'], 2),
      'graphs' : [
        GraphItemNP(1, GraphType.NodesByStatus, "global_nodes_by_status.png", "Nodes By Status"),
        GraphItemNP(2, GraphType.Clients, "global_client_count.png", "Global Client Count"),
        GraphItemNP(3, GraphType.GatewayTraffic, "global_replicator_traffic.png", "replicator - Traffic")
      ],
      'timespans' : [prefix for prefix, name in settings.GRAPH_TIMESPANS]
    },
    context_instance = RequestContext(request)
  )

@login_required
def my_nodes(request):
{'fraction_non_alphanumeric': 0.13572367186033416, 'fraction_numerical': 0.004223765721794631, 'mean_word_length': 4.6856990394877265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2343207', 'n_tokens_mistral': 3483, 'n_tokens_neox': 3401, 'n_words': 721}