Commits

Marc Delorme committed 767d036 Draft

refactor to use mongoengine, MongoDB model

Comments (0)

Files changed (8)

 from django.http import HttpResponse
-from NS2Stats2.stats.models import Kill, EndGame, Performance, CPU, Activity, Framerate
+from NS2Stats2.stats.mongoModels import Kill, EndGame, Performance, CPU, Activity, Framerate
 
 import datetime
 
     
     eg = EndGame()
     eg.version = POST['version']
-    eg.winner = POST['winner']
-    eg.length = POST['length']
+    eg.winner = int(POST['winner'])
+    eg.length = float(POST['length'])
     eg.mapName = POST['map']
     eg.start_location1 = POST['start_location1']
     eg.start_location2 = POST['start_location2']
-    eg.start_path_distance = POST['start_path_distance']
+    eg.start_path_distance = float(POST['start_path_distance'])
     eg.start_hive_tech = POST['start_hive_tech']
     eg.date = datetime.datetime.now()
     eg.save()
         return HttpResponse('See the documentation')
     
     cpu = CPU()
-    cpu.cpuspeed = POST['cpuspeed']
-    cpu.cpucores = POST['cpucores']
-    cpu.cpubits = POST['cpubits']
-    cpu.cpumem = POST['cpumem']
+    cpu.cpuspeed = int(POST['cpuspeed'])
+    cpu.cpucores = int(POST['cpucores'])
+    cpu.cpubits = int(POST['cpubits'])
+    cpu.cpumem = float(POST['cpumem'])
     cpu.gpu = POST['gpu']
     cpu.gpuver = POST['gpuver']
-    cpu.gpumem = POST['gpumem']
+    cpu.gpumem = float(POST['gpumem'])
     cpu.res = POST['res']
-    cpu.quality = POST['quality']
+    cpu.quality = int(POST['quality'])
     cpu.address = request.META['REMOTE_ADDR']
     cpu.date = datetime.datetime.now()
     cpu.save()
     kill.version = POST['version']
     kill.mapName = POST['map']
     kill.attacker_type = POST['attacker_type']
-    kill.attacker_team = POST['attacker_team']
+    kill.attacker_team = int(POST['attacker_team'])
     kill.attacker_weapon = POST['attacker_weapon']
-    kill.attackerx = POST['attackerx']
-    kill.attackery = POST['attackery']
-    kill.attackerz = POST['attackerz']
+    kill.attackerx = float(POST['attackerx'])
+    kill.attackery = float(POST['attackery'])
+    kill.attackerz = float(POST['attackerz'])
     kill.target_type = POST['target_type']
-    kill.target_team = POST['target_team']
+    kill.target_team = int(POST['target_team'])
     kill.target_weapon = POST['target_weapon']
-    kill.targetx = POST['targetx']
-    kill.targety = POST['targety']
-    kill.targetz = POST['targetz']
-    kill.target_lifetime = POST['target_lifetime']
+    kill.targetx = float(POST['targetx'])
+    kill.targety = float(POST['targety'])
+    kill.targetz = float(POST['targetz'])
+    kill.target_lifetime = float(POST['target_lifetime'])
     kill.date = datetime.datetime.now()
     kill.save()
 
     perf.version = POST['version']
     perf.mapName = POST['map']
     perf.address = request.META['REMOTE_ADDR']
-    perf.serveravg = POST['serveravg']
-    perf.ents = POST['ents']
-    perf.clientmin = POST['clientmin']
-    perf.clientmax = POST['clientmax']
+    perf.serveravg = float(POST['serveravg'])
+    perf.ents = int(POST['ents'])
+    perf.clientmin = float(POST['clientmin'])
+    perf.clientmax = float(POST['clientmax'])
     perf.clientavg = float(POST['clientavg'])
     perf.date = datetime.datetime.now()
     perf.save()

stats/mongoModels.py

+from mongoengine import Document, StringField, IntField, FloatField, DateTimeField
+from mongoengine.queryset import QuerySet
+from mongoengine.connection import get_db
+
+import datetime
+
+class MyQuerySet(QuerySet):
+    def fieldCount(self, key):
+        collection_name = self._document._meta.get('collection')
+
+        collection = get_db()[collection_name]
+
+        if isinstance(key, str):
+            keys = [key]
+        else:
+            keys = key
+
+        init = {
+            'count' : 0
+        }
+
+        red = '''
+            function(doc, prev) {
+                prev.count += 1;
+            }
+        '''
+
+        fin = '''
+            function(doc) {
+                doc.%s__count = doc.count;
+            }
+        ''' % keys[0]
+
+        return collection.group(keys, self._query, init, red, fin)
+
+    def fieldAverage(self, key, field):
+        collection_name = self._document._meta.get('collection')
+
+        collection = get_db()[collection_name]
+
+        init = {
+            'total' : 0,
+            'count' : 0
+        }
+
+        red = '''
+            function(doc, prev) {
+                prev.total += doc.%s;
+                prev.count += 1;
+            }
+        ''' % field
+
+        fin = '''
+            function(doc) {
+                doc.%s__average = (1.0 * doc.total) / (1.0 * doc.count);
+            }
+        ''' % field
+
+        if isinstance(key, str):
+            keys = [key]
+        else:
+            keys = key
+
+        return collection.group(keys, self._query, init, red, fin)
+
+
+
+class EndGame(Document):
+    version             = StringField(max_length=255)
+    winner              = IntField()
+    length              = FloatField()
+    mapName             = StringField(max_length=255)
+    start_location1     = StringField(max_length=255)
+    start_location2     = StringField(max_length=255)
+    start_path_distance = FloatField()
+    start_hive_tech     = StringField(max_length=255)
+    date                = DateTimeField()
+
+    meta = {
+        'indexes' : ['version', 'winner', 'length', 'mapName', 'start_location1', 'start_location2', 'start_hive_tech'],
+        'queryset_class': MyQuerySet
+    }
+    
+class CPU(Document):
+    cpuspeed = IntField()
+    cpucores = IntField()
+    cpubits  = IntField()
+    cpumem   = FloatField()
+    gpu      = StringField(max_length=255)
+    gpuver   = StringField(max_length=255)
+    gpumem   = FloatField()
+    res      = StringField(max_length=255)
+    quality  = IntField()    
+    address  = StringField(max_length=255)
+    date     = DateTimeField()
+
+    meta = {
+        'indexes' : ['cpubits', 'quality'],
+        'queryset_class': MyQuerySet
+    }
+    
+class Kill(Document):
+    version         = StringField(max_length=255)
+    mapName         = StringField(max_length=255)
+    attacker_type   = StringField(max_length=255)
+    attacker_team   = IntField()
+    attacker_weapon = StringField(max_length=255)
+    attackerx       = FloatField()
+    attackery       = FloatField()
+    attackerz       = FloatField()
+    target_type     = StringField(max_length=255)
+    target_team     = IntField()
+    target_weapon   = StringField(max_length=255)
+    targetx         = FloatField()
+    targety         = FloatField()
+    targetz         = FloatField()
+    target_lifetime = FloatField()
+    date            = DateTimeField()
+
+    meta = {
+        'indexes' : ['version', 'mapName', 'attacker_type', 'attacker_team', 'attacker_weapon', 'target_type', 'target_team', 'target_weapon', 'target_lifetime'],
+        'queryset_class': MyQuerySet
+    }
+    
+class Performance(Document):
+    version   = StringField(max_length=255)
+    mapName   = StringField(max_length=255) 
+    address   = StringField(max_length=255)   
+    serveravg = FloatField()    
+    ents      = IntField()
+    clientmin = FloatField()
+    clientmax = FloatField()
+    clientavg = FloatField()    
+    date      = DateTimeField()
+
+    meta = {
+        'indexes' : ['version', 'mapName', 'serveravg', 'ents', 'clientavg'],
+        'queryset_class': MyQuerySet
+    }
+
+class Framerate(Document):
+    version    = StringField(max_length=255)
+    address    = StringField(max_length=255)
+    average    = FloatField(default=0)
+    count      = IntField(default=0)
+    createDate = DateTimeField()
+    updateDate = DateTimeField()
+
+    def __init__(self, *args, **kwargs):
+        super(Document, self).__init__(*args, **kwargs)
+        self.createDate = datetime.datetime.now()
+
+    meta = {
+        'indexes' : [
+            'average', 
+            {
+                'fields' : ('version', 'address'),
+                'unique' : True
+            }
+        ],
+        'queryset_class': MyQuerySet
+    }
+
+    def save(self, updateDate = True):
+        if updateDate:
+            self.updateDate = datetime.datetime.now()
+            
+        super(Framerate, self).save()
+
+    def addFPS(self, fps):
+        self.average = (self.average * self.count + fps) / (self.count + 1)
+        self.count += 1
+
+        return self
+
+
+    @classmethod
+    def getFramerate(cls, ver, addr):
+        try:
+            framerate = cls.objects.get(version=ver, address=addr)
+        except Framerate.DoesNotExist:
+            framerate = cls.objects.create(version=ver, address=addr)
+
+        return framerate
+
+class Activity(Document):
+    day              = DateTimeField()
+    killCount        = IntField(default=0)
+    cpuCount         = IntField(default=0)
+    endGameCount     = IntField(default=0)
+    performanceCount = IntField(default=0)
+
+    meta = {
+        'indexes' : [{ 
+            'fields' : ('day',), 
+            'unique' : True
+        }],
+        'queryset_class': MyQuerySet
+    }
+
+    def __init__(self, *args, **kwargs):
+        super(Activity, self).__init__(*args, **kwargs)
+        self.day = datetime.date.today()
+
+    def increment(self, field):
+        attr = field + 'Count'
+        setattr(self, attr, getattr(self, attr) + 1)
+
+        return self
+
+    @classmethod
+    def getTodayActivity(cls):
+        try:
+            activity = cls.objects.get(day=datetime.date.today())
+        except Activity.DoesNotExist:
+            activity = cls.objects.create()
+
+        return activity
+
+

stats/resources/js/webapp.js

 				var tab = $('<table></table>');
 
 				$.each(data, function(i, val) {
-					tab.append($('<tr><td>' + val['target_type'] + '</td><td>' + parseInt(val['lifetime_avg']) + '</td></tr>'));
+					tab.append($('<tr><td>' + val['target_type'] + '</td><td>' + parseInt(val['target_lifetime__average']) + '</td></tr>'));
 				});
 
 				avgDiv.append(tab);

stats/testRunner.py

+from django.test.simple import DjangoTestSuiteRunner
+from NS2Stats2 import settings
+from mongoengine.connection import connect, disconnect, get_connection
+
+
+class MongoTestRunner(DjangoTestSuiteRunner):
+
+    mongo_name = 'test_stats'
+
+    def setup_databases(self, **kwargs):
+    	db_name = super(MongoTestRunner,self).setup_databases(**kwargs)
+
+        disconnect()
+        connect(self.mongo_name)
+        print 'MongoTestRunner: setup %s' % (self.mongo_name)
+        return db_name
+
+    def teardown_databases(self, db_name, **kwargs):
+    	super(MongoTestRunner,self).teardown_databases(db_name, **kwargs)
+        print 'MongoTestRunner: teardown %s' % (self.mongo_name)
+        c = get_connection()
+        c.drop_database(self.mongo_name)
+        disconnect()
 from django.test import TestCase
 from django.test.client import Client
 
-from NS2Stats2.stats.models import Kill, EndGame, Performance, CPU, Activity, Framerate
+from NS2Stats2.stats.mongoModels import Kill, EndGame, Performance, CPU, Activity, Framerate
+from NS2Stats2.stats.mongoModels import MyQuerySet
+
+from mongoengine import Document, StringField, FloatField
+
 
 import datetime
 
+class Post(Document):
+    nom   = StringField(max_length=255)
+    tag   = StringField(max_length=255)
+    value = FloatField()
+
+    meta = {
+        'queryset_class': MyQuerySet
+    }
+
 def testAttr(obj, data):
     for key, val in data.items():
         attr = key
 
     return True
 
+class MyQuerySetTest(TestCase):
+
+    def test_count(self):
+        Post.drop_collection()
+
+        post1 = Post()
+        post1.nom = "Jean"
+        post1.tag = "son"
+        post1.save()
+
+        post2 = Post()
+        post2.nom = "Marie"
+        post2.tag = "daughter"
+        post2.save()
+
+        post3 = Post()
+        post3.nom = "Paul"
+        post3.tag = "son"
+        post3.save()
+
+        r = Post.objects.all().fieldCount('tag')
+
+        for item in r:
+            if item['tag'] == "son":
+                self.assertEqual(item['tag__count'], 2)
+
+            if item['tag'] == "daughter":
+                self.assertEqual(item['tag__count'], 1)
+
+    def test_average(self):
+        Post.drop_collection()
+
+        post1 = Post()
+        post1.nom = "Jean"
+        post1.tag = "son"
+        post1.value = 2
+        post1.save()
+
+        post2 = Post()
+        post2.nom = "Marie"
+        post2.tag = "daughter"
+        post2.value = 2
+        post2.save()
+
+        post3 = Post()
+        post3.nom = "Paul"
+        post3.tag = "son"
+        post3.value = 4
+        post3.save()
+
+        r = Post.objects.all().fieldAverage('tag', 'value')
+
+        for item in r:
+            if item['tag'] == "son":
+                self.assertEqual(item['value__average'], 3)
+
+            if item['tag'] == "daughter":
+                self.assertEqual(item['value__average'], 2)
+
+
+
 class APITest(TestCase):
 
     def setUp(self):
 
         self.assertEqual(before[0] + 1, after[0])
         self.assertEqual(before[1] + 1, after[1])
-        self.assertTrue( testAttr(EndGame.objects.all().latest('date'), endgame))
+        self.assertTrue( testAttr(EndGame.objects.all().order_by('-date')[0], endgame))
 
     def test_performance(self):
         perf = {
         )
 
         count = Framerate.getFramerate(perf['version'], address).count
-        self.assertTrue( testAttr(Performance.objects.all().latest('date'), perf))
+        self.assertTrue( testAttr(Performance.objects.all().order_by('-date')[0], perf))
         self.assertEqual(before[0] + 1, after[0])
         self.assertEqual(before[1] + 1, after[1])
         self.assertEqual(before[2] * (count-1) + perf['clientavg'] / count, after[2])
 
         self.assertEqual(before[0] + 1, after[0])
         self.assertEqual(before[1] + 1, after[1])
-        self.assertTrue( testAttr(Kill.objects.all().latest('date'), kill))
+        self.assertTrue( testAttr(Kill.objects.all().order_by('-date')[0], kill))
 
     def test_cpu(self):
         cpu = {
 
         self.assertEqual(before[0] + 1, after[0])
         self.assertEqual(before[1] + 1, after[1])      
-        self.assertTrue( testAttr(CPU.objects.all().latest('date'), cpu))
+        self.assertTrue( testAttr(CPU.objects.all().order_by('-date')[0], cpu))
 
     def test_activity(self):
         todayActivity = Activity.getTodayActivity()
         self.assertEqual(activity.endGameCount, endgame + 2)
         self.assertEqual(activity.cpuCount, cpu + 4)
 
-    def test_Framerate(self):
+    def test_framerate(self):
         ver = "192"
         addr = "127.0.0.1"
         fps = 23

stats/util/sqlToMongo.py

+from django.core.management import setup_environ
+from NS2Stats2 import settings
+setup_environ(settings)
+
+from django.contrib.auth.models import User as DjangoUser
+from mongoengine.django.auth import User as MongoUser
+import argparse
+
+from NS2Stats2.stats import models as sql, mongoModels as mongo
+
+parser = argparse.ArgumentParser(description='Convert django model from SQL model to MongoDB model')
+parser.add_argument('--all', dest='all', action='store_true', help='convert every models')
+parser.add_argument('--models', '-m', dest='models', nargs='*', help='convert specified models')
+arguments = parser.parse_args()
+
+i = 0
+# User
+if arguments.all or 'User' in arguments.models:
+    for sqlRow in DjangoUser.objects.all():
+        mongoRow = MongoUser()
+
+        mongoRow.username = sqlRow.username
+        mongoRow.first_name = sqlRow.first_name
+        mongoRow.last_name = sqlRow.last_name
+        mongoRow.email = sqlRow.email
+        mongoRow.password = sqlRow.password
+        mongoRow.is_staff = sqlRow.is_staff
+        mongoRow.is_active = sqlRow.is_active
+        mongoRow.is_superuser = sqlRow.is_superuser
+        mongoRow.last_login = sqlRow.last_login
+        mongoRow.date_joined = sqlRow.date_joined
+
+        mongoRow.save()
+
+        i += 1
+        print "User" + str(i)
+
+
+i = 0
+# EndGame
+if arguments.all or 'EndGame' in arguments.models:
+    for sqlRow in sql.EndGame.objects.all():
+        mongoRow = mongo.EndGame()
+
+        mongoRow.version = sqlRow.version
+        mongoRow.winner = sqlRow.winner
+        mongoRow.length = sqlRow.length
+        mongoRow.mapName = sqlRow.mapName
+        mongoRow.start_location1 = sqlRow.start_location1
+        mongoRow.start_location2 = sqlRow.start_location2
+        mongoRow.start_path_distance = sqlRow.start_path_distance
+        mongoRow.start_hive_tech = sqlRow.start_hive_tech
+        mongoRow.date = sqlRow.date
+
+        mongoRow.save()
+
+        i += 1
+        print "EndGame" + str(i)
+
+i = 0
+# CPU
+if arguments.all or 'CPU' in arguments.models:
+    for sqlRow in sql.CPU.objects.all():
+        mongoRow = mongo.CPU()
+
+        mongoRow.cpuspeed = sqlRow.cpuspeed
+        mongoRow.cpucores = sqlRow.cpucores
+        mongoRow.cpubits  = sqlRow.cpubits
+        mongoRow.cpumem   = sqlRow.cpumem
+        mongoRow.gpu      = sqlRow.gpu
+        mongoRow.gpuver   = sqlRow.gpuver
+        mongoRow.gpumem   = sqlRow.gpumem
+        mongoRow.res      = sqlRow.res
+        mongoRow.quality  = sqlRow.quality
+        mongoRow.address  = sqlRow.address
+        mongoRow.date     = sqlRow.date
+
+        mongoRow.save()
+
+        i += 1
+        print "CPU" + str(i)
+
+i = 0
+# Framerate
+if arguments.all or 'Framerate' in arguments.models:
+    for sqlRow in sql.Framerate.objects.all():
+        mongoRow = mongo.Framerate()
+
+        mongoRow.version    = sqlRow.version  
+        mongoRow.address    = sqlRow.address
+        mongoRow.average    = sqlRow.average
+        mongoRow.count      = sqlRow.count
+        mongoRow.createDate = sqlRow.createDate
+        mongoRow.updateDate = sqlRow.updateDate
+
+        mongoRow.save()
+
+        i += 1
+        print "Framerate" + str(i)
+
+i = 0
+# Activity
+if arguments.all or 'Activity' in arguments.models:
+    for sqlRow in sql.Activity.objects.all():
+        mongoRow = mongo.Activity()
+
+        mongoRow.day              = sqlRow.day
+        mongoRow.killCount        = sqlRow.killCount
+        mongoRow.cpuCount         = sqlRow.cpuCount
+        mongoRow.endGameCount     = sqlRow.endGameCount
+        mongoRow.performanceCount = sqlRow.performanceCount
+
+        mongoRow.save()
+
+        i += 1
+        print "Activity" + str(i)
+
+i = 0
+# Kill
+if arguments.all or 'Kill' in arguments.models:
+    for sqlRow in sql.Kill.objects.all():
+        mongoRow = mongo.Kill()
+
+        mongoRow.version         = sqlRow.version  
+        mongoRow.mapName         = sqlRow.mapName
+        mongoRow.attacker_type   = sqlRow.attacker_type
+        mongoRow.attacker_team   = sqlRow.attacker_team
+        mongoRow.attacker_weapon = sqlRow.attacker_weapon
+        mongoRow.attackerx       = sqlRow.attackerx
+        mongoRow.attackery       = sqlRow.attackery
+        mongoRow.attackerz       = sqlRow.attackerz
+        mongoRow.target_type     = sqlRow.target_type
+        mongoRow.target_team     = sqlRow.target_team
+        mongoRow.target_weapon   = sqlRow.target_weapon
+        mongoRow.targetx         = sqlRow.targetx
+        mongoRow.targety         = sqlRow.targety
+        mongoRow.targetz         = sqlRow.targetz
+        mongoRow.target_lifetime = sqlRow.target_lifetime
+        mongoRow.date            = sqlRow.date
+
+        mongoRow.save()
+
+        i += 1
+        print "Kill" + str(i)
+
+i = 0
+# Performance
+if arguments.all or 'Performance' in arguments.models:
+    for sqlRow in sql.Performance.objects.all():
+        if i == 0:
+            print "start the loop"
+        mongoRow = mongo.Performance()
+
+        mongoRow.version   = sqlRow.version  
+        mongoRow.mapName   = sqlRow.mapName
+        mongoRow.address   = sqlRow.address
+        mongoRow.serveravg = sqlRow.serveravg
+        mongoRow.ents      = sqlRow.ents
+        mongoRow.clientmin = sqlRow.clientmin
+        mongoRow.clientmax = sqlRow.clientmax
+        mongoRow.clientavg = sqlRow.clientavg
+        mongoRow.date      = sqlRow.date
+
+        mongoRow.save()
+
+        i += 1
+        if i % 1000 == 0:
+            print "Performance" + str(i)
+
+print "-- end conversion --"
+
+
+
 from django.http import HttpResponse
 from django.shortcuts import render_to_response
-from django.db.models import Count, Avg, Q
+from django.db.models import Count, Avg
 from django.conf import settings
 from django.views.decorators.cache import cache_page
 from django.core.cache import cache
 from django.contrib.auth.decorators import login_required
 
-from NS2Stats2.stats.models import Kill, EndGame, Performance, CPU
+from mongoengine.queryset import Q
+
+from NS2Stats2.stats.mongoModels import Kill, EndGame, Performance, CPU
 from NS2Stats2.stats.util.helpers import findCategory
 
 import json, math, datetime
     return HttpResponse('flushed')
 
 @login_required(login_url='/login/')
-def game(request):
-    d = { 
-            'versions' : settings.VERSIONS,
-            'maps' : settings.MAPS,
-            'current_url' : request.path
-        }
-    return render_to_response('game.html', d)
-
-@login_required(login_url='/login/')
-def duel(request):
-    types = list(Kill.objects.all().values('attacker_type').distinct())
-    types = [t['attacker_type'] for t in types]
-    
-    d = { 
-            'versions' : settings.VERSIONS,
-            'maps' : settings.MAPS,
-            'current_url' : request.path,
-            'types' : types
-        }
-    
-    return render_to_response('duel.html', d)
-
-@login_required(login_url='/login/')
-def player(request):
-    d = { 
-        'versions' : settings.VERSIONS,
-        'maps' : settings.MAPS,
-        'current_url' : request.path
-    }    
-    return render_to_response('player.html', d)
-
-@login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def winPie(request):
     GET = request.GET
     endGameQuery = filterByMapAndVersion(endGameQuery, GET)
     endGameQuery = filterByLocation(endGameQuery, GET)
         
-    t1 = endGameQuery.filter(winner=1).count()
-    t2 = endGameQuery.filter(winner=2).count()
+    t1 = endGameQuery.clone().filter(winner=1).count()
+    t2 = endGameQuery.clone().filter(winner=2).count()
     data = { 
         'data' : [
             ['Marines', t1],
         
     killQuery = filterByMapAndVersion(killQuery, GET)
         
-    killQuery = killQuery.values('target_type').annotate(Count('target_type')) \
-        .annotate(lifetime_avg=Avg('target_lifetime')) \
-        .values('target_type', 'lifetime_avg') \
-        .order_by('lifetime_avg')
+    #killQuery = killQuery.values('target_type').annotate(Count('target_type')) \
+    #    .annotate(lifetime_avg=Avg('target_lifetime')) \
+    #    .values('target_type', 'lifetime_avg') \
+    #    .order_by('lifetime_avg')
+
+    killQuery = killQuery.scalar('target_type', 'target_lifetime').fieldAverage('target_type', 'target_lifetime')
     
     dataJSON = json.dumps(list(killQuery))    
     return HttpResponse(dataJSON)
         
     endQuery1 = filterByMapAndVersion(endQuery1, GET)
         
-    endQuery1 = endQuery1.values('start_location1').annotate(Count('start_location1')) \
-        .values('start_location1', 'start_location1__count') \
-        .order_by('-start_location1__count')
+    endQuery1 = endQuery1.scalar('start_location1').fieldCount('start_location1')
 
     endQuery2 = EndGame.objects.all()
         
     endQuery2 = filterByMapAndVersion(endQuery2, GET)
         
-    endQuery2 = endQuery2.values('start_location2').annotate(Count('start_location2')) \
-        .values('start_location2', 'start_location2__count') \
-        .order_by('-start_location2__count')
+    endQuery2 = endQuery2.scalar('start_location2').fieldCount('start_location2')
 
     data = {
         'type1' : list(endQuery1),
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def resolution(request):
-    cpuQuery = CPU.objects.all().values('res').annotate(Count('res'))
+    cpuQuery = CPU.objects.all().scalar('res').fieldCount('res')
     r = [ [row['res'], row['res__count']]for row in list(cpuQuery)]
     r = sorted(r, key=lambda x: -x[1])
     
     endGameQuery = filterByMapAndVersion(endGameQuery, GET)
     endGameQuery = filterByLocation(endGameQuery, GET)
     
-    q1 = endGameQuery.filter(winner=1)
-    q2 = endGameQuery.filter(winner=2)
+    q1 = endGameQuery.clone().filter(winner=1)
+    q2 = endGameQuery.clone().filter(winner=2)
     
-    data1 = {}
+    data1 = {} 
     for g in q1:
         l = g.length
         i,cat = findCategory(l / 60, delta, iMax, unit,offset)
     endGameQuery = filterByMapAndVersion(endGameQuery, GET)
     endGameQuery = filterByLocation(endGameQuery, GET)
     
-    q1 = endGameQuery.filter(winner=1)
-    q2 = endGameQuery.filter(winner=2)
+    q1 = endGameQuery.clone().filter(winner=1)
+    q2 = endGameQuery.clone().filter(winner=2)
     
     data1 = {}
     for g in q1:
     
     # Duel
     if 'type1' in GET and 'type2' in GET:
-        t1 = killQuery.filter(attacker_type=GET['type1'],target_type=GET['type2']).count()
-        t2 = killQuery.filter(attacker_type=GET['type2'],target_type=GET['type1']).count()
+        t1 = killQuery.clone().filter(attacker_type=GET['type1'],target_type=GET['type2']).count()
+        t2 = killQuery.clone().filter(attacker_type=GET['type2'],target_type=GET['type1']).count()
         
         data = { 
             'data' : [
         }
     # General        
     else:
-        t1 = killQuery.filter(attacker_team=1).count()
-        t2 = killQuery.filter(attacker_team=2).count()
+        t1 = killQuery.clone().filter(attacker_team=1).count()
+        t2 = killQuery.clone().filter(attacker_team=2).count()
         data = { 
             'data' : [
                 ['Marines', t1],
     
     killQuery = filterByMapAndVersion(killQuery, GET)
         
-    q1 = killQuery.filter(attacker_type=GET['type1'],target_type=GET['type2']).values('attacker_weapon','target_weapon').annotate(Count('attacker_weapon'))
-    q2 = killQuery.filter(attacker_type=GET['type2'],target_type=GET['type1']).values('attacker_weapon','target_weapon').annotate(Count('attacker_weapon'))
+    q1 = killQuery.clone().filter(attacker_type=GET['type1'],target_type=GET['type2']).fieldCount(['attacker_weapon', 'target_weapon'])
+    q2 = killQuery.clone().filter(attacker_type=GET['type2'],target_type=GET['type1']).fieldCount(['attacker_weapon', 'target_weapon'])
 
     duelList = {}
     for duel in q1:
+        print duel
         duelName = duel['attacker_weapon'] + ' vs ' + duel['target_weapon']
         type1Name = GET['type1'] + ' - ' + duel['attacker_weapon']
         duelList[ duelName ] = [ { 'label' : type1Name, 'data' : duel['attacker_weapon__count']}]
 @cache_page(60 * 60 * 2)
 def gpu(request):
     cpuQuery = CPU.objects.all()
-    nvidia = cpuQuery.filter(gpu__startswith = 'NVIDIA').count()
-    ati = cpuQuery.filter(Q(gpu__startswith = 'AMD') | Q(gpu__startswith = 'ATI') ).count()
+    nvidia = cpuQuery.clone().filter(gpu__startswith = 'NVIDIA').count()
+    ati = cpuQuery.clone().filter(Q(gpu__startswith = 'AMD') | Q(gpu__startswith = 'ATI') ).count()
     
     data = { 
         'data' : [
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def cpucore(request):
-    cpuQuery = CPU.objects.all().values('cpucores').annotate(count=Count('cpucores')).filter(count__gt = 50)
+    cpuQuery = CPU.objects.all().scalar('cpucores').fieldCount('cpucores')
     
     data = { 
-        'data' : [ [str(c['cpucores']), c['count']] for c in cpuQuery]
+        'data' : [ [str(c['cpucores']), c['cpucores__count']] for c in cpuQuery]
     }
     
     dataJSON = json.dumps(data)
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def cpuspeed(request):
-    cpuQuery = CPU.objects.all().values('cpuspeed')
+    cpuQuery = CPU.objects.all().scalar('cpuspeed')
     data = {}
     for c in cpuQuery:
-        i,cat = findCategory(c['cpuspeed'] * 1.0 / 1000,0.5,6,'GHz')
+        i,cat = findCategory(c * 1.0 / 1000,0.5,6,'GHz')
         if not data.has_key(cat):
             data[cat] = 0
         data[cat] += 1
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def typeList(request):
-    types = list(Kill.objects.all().values('attacker_type').distinct())
-    types = [{'name': t['attacker_type']} for t in types]
+    types = list(Kill.objects.all().distinct('attacker_type'))
+    types = [{'name': t} for t in types]
     typesJSON = json.dumps(types)
     return HttpResponse(typesJSON)
 
 def startLocation1List(request):
     GET = request.GET
     
-    query = EndGame.objects.all().exclude(start_location1=None)
+    query = EndGame.objects.all().filter(start_location1__ne=None)
     query = filterByMapAndVersion(query, GET)
     
-    locations = list( query.values('start_location1').distinct() )
+    locations = list( query.distinct('start_location1') )
     
-    locations = [{'name': t['start_location1']} for t in locations]
+    locations = [{'name': t} for t in locations]
     locationsJSON = json.dumps(locations)
     return HttpResponse(locationsJSON)
 
 def startLocation2List(request):
     GET = request.GET
     
-    query = EndGame.objects.all().exclude(start_location2=None)
+    query = EndGame.objects.all().filter(start_location2__ne=None)
     query = filterByMapAndVersion(query, GET)
     
-    locations = list( query.values('start_location2').distinct() )
+    locations = list( query.distinct('start_location2') )
     
-    locations = [{'name': t['start_location2']} for t in locations]
+    locations = [{'name': t} for t in locations]
     locationsJSON = json.dumps(locations)
     return HttpResponse(locationsJSON)
 
     if 'type' in GET:
         killQuery = killQuery.filter(attacker_type=GET['type'])
         
-    weapons = list(killQuery.values('attacker_weapon').distinct())
-    weapons = [w['attacker_weapon'] for w in weapons]
+    weapons = list(killQuery.distinct('attacker_weapon'))
+    weapons = [w[0] for w in weapons]
     weaponsJSON = json.dumps(weapons)
     return HttpResponse(weaponsJSON)
 
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def mapList(request):
-    types = list(EndGame.objects.all().values('mapName').distinct())
-    types = [ { 'name': t['mapName']} for t in types]
+    types = list(EndGame.objects.all().distinct('mapName'))
+    types = [ { 'name': t} for t in types]
     typesJSON = json.dumps(types)
     return HttpResponse(typesJSON)
 
 @login_required(login_url='/login/')
 @cache_page(60 * 60 * 2)
 def versionList(request):
-    types = list(EndGame.objects.all().values('version').distinct())
-    types = [ { 'name': t['version']} for t in types]
+    types = list(EndGame.objects.all().distinct('version'))
+    types = [ { 'name': t} for t in types]
     types.sort(key=lambda t: int(t['name']))
     typesJSON = json.dumps(types)
     return HttpResponse(typesJSON)
     # website
     url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
     url(r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'}),
-    url(r'^old$', 'NS2Stats2.stats.views.home'),
-    url(r'^game$', 'NS2Stats2.stats.views.game'),
     url(r'^$', 'NS2Stats2.stats.views.webapp'),
-    url(r'^duel$', 'NS2Stats2.stats.views.duel'),
-    url(r'^player$', 'NS2Stats2.stats.views.player'),
     url(r'^' + settings.STATIC_URL[1:] + '(?P<path>.*)$', 'django.views.static.serve',  {'document_root': settings.STATIC_ROOT}),
     
     # data
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.