Skip to content

Commit

Permalink
Added initial files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Nov 1, 2011
0 parents commit 218415a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
*.pyc
*~
.*.sw[po]
dist/
*.egg-info
build/
.build/
pip-log.txt
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright (c) 2011, Ionel Cristian Maries
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
=============================
django-redisboard
=============================


Brief redis monitoring in django admin

Installation guide
==================

Add ``redisboard`` to ``INSTALLED_APPS``::

INSTALLED_APPS += ("redisboard", )
Then you can add redis servers in the admin. You will see the stats in the changelist.
28 changes: 28 additions & 0 deletions setup.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- encoding: utf8 -*-
from setuptools import setup, find_packages

import os

setup(
name = "django-redisboard",
version = "0.1",
url = '',
download_url = '',
license = 'BSD',
description = "",
author = 'Ionel Cristian Mărieș',
author_email = 'contact@ionelmc.ro',
packages = find_packages('src'),
package_dir = {'':'src'},
include_package_data = True,
zip_safe = False,
classifiers = [
'Development Status :: 4 - Beta',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Internet :: WWW/HTTP',
]
)
Empty file added src/redisboard/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions src/redisboard/admin.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.contrib import admin
from .models import RedisServer

class RedisServerAdmin(admin.ModelAdmin):
list_display = (
'__unicode__', 'status', 'memory', 'clients', 'details'
)
def status(self, obj):
return obj.stats['status']

def memory(self, obj):
return obj.stats['memory']

def clients(self, obj):
return obj.stats['clients']

def details(self, obj):
return "<table>%s</table>" % ''.join(
"<tr><td>%s</td><td>%s</td></tr>" % i
for i in sorted(obj.stats['details'].items(), key=lambda (k,v): k)
)

details.allow_tags = True

admin.site.register(RedisServer, RedisServerAdmin)
52 changes: 52 additions & 0 deletions src/redisboard/models.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,52 @@
import redis

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator

from .utils import cached_property

class RedisServer(models.Model):
hostname = models.CharField(_("Hostname"), max_length=250)
port = models.IntegerField(_("Port"), validators=[
MaxValueValidator(65535), MinValueValidator(1)
], default=6379)
password = models.CharField(_("Password"), max_length=250,
null=True, blank=True)


@cached_property
def connection(self):
return redis.Redis(
host = self.hostname,
port = self.port,
password = self.password
)

@connection.deleter
def connection(self, value):
value.connection_pool.disconnect()

@cached_property
def stats(self):
try:
info = self.connection.info()
return {
'status': 'UP',
'details': info,
'memory': "%s (peak: %s)" % (
info['used_memory_human'],
info['used_memory_peak_human']
),
'clients': info['connected_clients'],
}
except redis.exceptions.ConnectionError:
return {
'status': 'DOWN',
'clients': 'n/a',
'memory': 'n/a',
'details': {},
}

def __unicode__(self):
return "%s:%s" % (self.hostname, self.port)
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends "admin/change_list.html" %}
{% block extrahead %}
{{ block.super }}
<meta http-equiv="refresh" content="30">
{% endblock %}
68 changes: 68 additions & 0 deletions src/redisboard/utils.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,68 @@
# shamelessly taken from kombu.utils

class cached_property(object):
"""Property descriptor that caches the return value
of the get function.
*Examples*
.. code-block:: python
@cached_property
def connection(self):
return Connection()
@connection.setter # Prepares stored value
def connection(self, value):
if value is None:
raise TypeError("Connection must be a connection")
return value
@connection.deleter
def connection(self, value):
# Additional action to do at del(self.attr)
if value is not None:
print("Connection %r deleted" % (value, ))
"""

def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.__get = fget
self.__set = fset
self.__del = fdel
self.__doc__ = doc or fget.__doc__
self.__name__ = fget.__name__
self.__module__ = fget.__module__

def __get__(self, obj, type=None):
if obj is None:
return self
try:
return obj.__dict__[self.__name__]
except KeyError:
value = obj.__dict__[self.__name__] = self.__get(obj)
return value

def __set__(self, obj, value):
if obj is None:
return self
if self.__set is not None:
value = self.__set(obj, value)
obj.__dict__[self.__name__] = value

def __delete__(self, obj):
if obj is None:
return self
try:
value = obj.__dict__.pop(self.__name__)
except KeyError:
pass
else:
if self.__del is not None:
self.__del(obj, value)

def setter(self, fset):
return self.__class__(self.__get, fset, self.__del)

def deleter(self, fdel):
return self.__class__(self.__get, self.__set, fdel)

0 comments on commit 218415a

Please sign in to comment.