Martin Mahner avatar Martin Mahner committed 15bbc61

added initial support to format each stat-value

Comments (0)

Files changed (1)

memcache_status/templatetags/memcache_status_tags.py

 register = template.Library()
 
 class CacheStats(template.Node):
+    
     def render(self, context):
         try:
-            cache_stats =  sorted(cache._cache.get_stats())
+            cache_stats = cache._cache.get_stats()
+            cache_stats_sorted = []
+            for server, stats_dict in sorted(cache_stats):
+                cache_stats_sorted.append((server, self.reformat_stats(stats_dict)))
+            cache_stats = cache_stats_sorted
         # The current cache backend does not provide any statistics
         except AttributeError:
             cache_stats = None
         context['cache_stats'] = cache_stats
         return ''
+    
+    def reformat_stats(self, stats_dict):
+        # Reformat stat values
+        formatted_stats = {}
+        for key in stats_dict.keys():
+            value = stats_dict[key]
+            try:
+                func = getattr(self, 'format_%s_value' % key.lower())
+                formatted_stats[key] = func(value)
+            except AttributeError:
+                formatted_stats[key] = value
+        return formatted_stats
+    
+    def format_uptime_value(self, value):
+        return self.fractTimestamp(int(value))
+
+    def fractTimestamp(self, s):
+        years, s = divmod(s, 31556952)
+        min, s = divmod(s, 60)
+        h, min = divmod(min, 60)
+        d, h = divmod(h, 24)
+        return '%sy, %sd, %sh, %sm, %ss' % (years, d, h, min, s)
+
 
 @register.tag
 def get_cache_stats(parser, token):
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.