Removed PopulationHi. and replaced it with a Redis storage.
This commit is contained in:
parent
f18ffed2b2
commit
2e369d98fd
@ -1,16 +1,10 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import Server, PopulationHistory
|
from .models import Server
|
||||||
|
|
||||||
class ServerAdmin(admin.ModelAdmin):
|
class ServerAdmin(admin.ModelAdmin):
|
||||||
list_display = ['title', 'site_url']
|
list_display = ['title', 'site_url']
|
||||||
search_fields = ['title']
|
search_fields = ['title']
|
||||||
|
|
||||||
class PopulationHistoryAdmin(admin.ModelAdmin):
|
|
||||||
list_display = ['timestamp', 'players', 'server']
|
|
||||||
list_filter = ['timestamp']
|
|
||||||
search_fields = ['server__title']
|
|
||||||
|
|
||||||
admin.site.register(Server, ServerAdmin)
|
admin.site.register(Server, ServerAdmin)
|
||||||
admin.site.register(PopulationHistory, PopulationHistoryAdmin)
|
|
||||||
|
|
||||||
|
|||||||
21
src/gameservers/migrations/0008_auto_20150223_1713.py
Normal file
21
src/gameservers/migrations/0008_auto_20150223_1713.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('gameservers', '0007_auto_20150218_2107'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='populationhistory',
|
||||||
|
name='server',
|
||||||
|
),
|
||||||
|
migrations.DeleteModel(
|
||||||
|
name='PopulationHistory',
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -1,5 +1,11 @@
|
|||||||
|
|
||||||
|
import datetime
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
import redis
|
||||||
|
|
||||||
|
|
||||||
class Server(models.Model):
|
class Server(models.Model):
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
game_url = models.URLField()
|
game_url = models.URLField()
|
||||||
@ -12,14 +18,29 @@ class Server(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
class PopulationHistory(models.Model):
|
|
||||||
timestamp = models.DateTimeField(auto_now_add=True)
|
|
||||||
server = models.ForeignKey(Server)
|
|
||||||
players = models.PositiveIntegerField()
|
|
||||||
|
|
||||||
class Meta:
|
class PlayerHistory(object):
|
||||||
ordering = ['-timestamp', 'server']
|
def __init__(self, redis_settings=dict(host='localhost', port=6379, db=0)):
|
||||||
|
self.redis = redis.StrictRedis(**redis_settings)
|
||||||
|
|
||||||
def __str__(self):
|
# 2688 = 4 times per hour * 24 hours * 7 days * 4 weeks
|
||||||
return '{} {}'.format(self.timestamp, self.server.title)
|
self.max_points = 2688
|
||||||
|
|
||||||
|
def add_point(self, server, time, players):
|
||||||
|
'''Add a new point in the player history.'''
|
||||||
|
self.redis.lpush(server, '{},{}'.format(time, players))
|
||||||
|
|
||||||
|
def trim_points(self, server):
|
||||||
|
'''Trim away too old points in the player history.'''
|
||||||
|
self.redis.ltrim(server, 0, self.max_points)
|
||||||
|
|
||||||
|
def get_points(self, server):
|
||||||
|
'''Get a range of points from the player history.'''
|
||||||
|
# TODO: not memory efficient, turn into a generator instead?
|
||||||
|
points = []
|
||||||
|
for tmp in self.redis.lrange(server, 0, self.max_points):
|
||||||
|
time, players = tmp.split(',')
|
||||||
|
time, players = datetime.datetime.fromtimestamp(float(time)), int(players)
|
||||||
|
points.append((time, players))
|
||||||
|
return points
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user