mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
Collecting stats on sites loaded and avg response time. Displaying stats in graphs.
This commit is contained in:
parent
909339381a
commit
9cb9fa6073
6 changed files with 59 additions and 15 deletions
|
@ -1,11 +1,12 @@
|
|||
import datetime
|
||||
import mongoengine as mongo
|
||||
from apps.rss_feeds.models import MFeedFetchHistory
|
||||
from django.db.models import Avg, Count
|
||||
from apps.rss_feeds.models import MFeedFetchHistory, FeedLoadtime
|
||||
from apps.profile.models import Profile
|
||||
|
||||
from utils import json_functions as json
|
||||
class MStatistics(mongo.Document):
|
||||
key = mongo.StringField(unique=True)
|
||||
value = mongo.IntField(default=0)
|
||||
value = mongo.StringField()
|
||||
|
||||
meta = {
|
||||
'collection': 'statistics',
|
||||
|
@ -18,7 +19,17 @@ class MStatistics(mongo.Document):
|
|||
|
||||
@classmethod
|
||||
def all(cls):
|
||||
return dict([(stat.key, stat.value) for stat in cls.objects.all()])
|
||||
values = dict([(stat.key, stat.value) for stat in cls.objects.all()])
|
||||
for key, value in values.items():
|
||||
if key in ('avg_time_taken', 'sites_loaded'):
|
||||
values[key] = json.decode(value)
|
||||
elif key in ('feeds_fetched', 'premium_users', 'standard_users', 'latest_sites_loaded',
|
||||
'max_sites_loaded'):
|
||||
values[key] = int(value)
|
||||
elif key in ('latest_avg_time_taken', 'max_avg_time_taken'):
|
||||
values[key] = float(value)
|
||||
|
||||
return values
|
||||
|
||||
@classmethod
|
||||
def collect_statistics(cls):
|
||||
|
@ -30,4 +41,28 @@ class MStatistics(mongo.Document):
|
|||
cls.objects(key='premium_users').update_one(upsert=True, key='premium_users', value=premium_users)
|
||||
|
||||
standard_users = Profile.objects.filter(last_seen_on__gte=last_day, is_premium=False).count()
|
||||
cls.objects(key='standard_users').update_one(upsert=True, key='standard_users', value=standard_users)
|
||||
cls.objects(key='standard_users').update_one(upsert=True, key='standard_users', value=standard_users)
|
||||
|
||||
now = datetime.datetime.now()
|
||||
sites_loaded = []
|
||||
avg_time_taken = []
|
||||
for hour in range(24):
|
||||
start_hours_ago = now - datetime.timedelta(hours=hour)
|
||||
end_hours_ago = now - datetime.timedelta(hours=hour+1)
|
||||
aggregates = dict(count=Count('loadtime'), avg=Avg('loadtime'))
|
||||
load_times = FeedLoadtime.objects.filter(
|
||||
date_accessed__lte=start_hours_ago,
|
||||
date_accessed__gte=end_hours_ago
|
||||
).aggregate(**aggregates)
|
||||
sites_loaded.append(load_times['count'] or 0)
|
||||
avg_time_taken.append(load_times['avg'] or 0)
|
||||
sites_loaded.reverse()
|
||||
avg_time_taken.reverse()
|
||||
cls.objects(key='sites_loaded').update_one(upsert=True, key='sites_loaded', value=json.encode(sites_loaded))
|
||||
cls.objects(key='avg_time_taken').update_one(upsert=True, key='avg_time_taken', value=json.encode(avg_time_taken))
|
||||
cls.objects(key='latest_sites_loaded').update_one(upsert=True, key='latest_sites_loaded', value=sites_loaded[-1])
|
||||
cls.objects(key='latest_avg_time_taken').update_one(upsert=True, key='latest_avg_time_taken', value=avg_time_taken[-1])
|
||||
print max(sites_loaded), max(avg_time_taken)
|
||||
cls.objects(key='max_sites_loaded').update_one(upsert=True, key='max_sites_loaded', value=max(sites_loaded))
|
||||
cls.objects(key='max_avg_time_taken').update_one(upsert=True, key='max_avg_time_taken', value=max(avg_time_taken))
|
||||
|
0
apps/statistics/templatetags/__init__.py
Normal file
0
apps/statistics/templatetags/__init__.py
Normal file
9
apps/statistics/templatetags/statistics_tags.py
Normal file
9
apps/statistics/templatetags/statistics_tags.py
Normal file
|
@ -0,0 +1,9 @@
|
|||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter
|
||||
def format_graph(n, max_value, height=30):
|
||||
if n == 0:
|
||||
return 1
|
||||
return max(1, height / (n/(max_value or 1)))
|
|
@ -59,7 +59,7 @@
|
|||
.NB-modal h5,
|
||||
.NB-module h5 {
|
||||
margin: 0;
|
||||
padding: 8px 8px 6px;
|
||||
padding: 8px 12px 6px;
|
||||
background-color: #D1E1FA;
|
||||
background-image: -webkit-gradient(
|
||||
linear,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% load typogrify_tags recommendations_tags utils_tags %}
|
||||
{% load typogrify_tags recommendations_tags utils_tags statistics_tags %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
|
@ -331,26 +331,26 @@ $(document).ready(function() {
|
|||
|
||||
<div class="NB-module-stats-count">
|
||||
<div class="NB-module-stats-count-graph">
|
||||
{% for i in 24|get_range %}
|
||||
{% for i in statistics.sites_loaded %}
|
||||
<div class="NB-graph-value">
|
||||
<div class="NB-graph-label">{{ i }}</div>
|
||||
<div class="NB-graph-bar" style="height:{{ i }}px"></div>
|
||||
<div class="NB-graph-bar" style="height:{{ i|format_graph:statistics.max_sites_loaded }}px"></div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="NB-module-stats-count-number">420</div>
|
||||
<div class="NB-module-stats-count-number">{{ statistics.latest_sites_loaded }}</div>
|
||||
<div class="NB-module-stats-count-description">Sites loaded</div>
|
||||
</div>
|
||||
<div class="NB-module-stats-count">
|
||||
<div class="NB-module-stats-count-graph">
|
||||
{% for i in 24|get_range %}
|
||||
{% for i in statistics.avg_time_taken %}
|
||||
<div class="NB-graph-value">
|
||||
<div class="NB-graph-label">{{ i }}</div>
|
||||
<div class="NB-graph-bar" style="height:{{ i }}px"></div>
|
||||
<div class="NB-graph-label">{{ i|floatformat:2 }}</div>
|
||||
<div class="NB-graph-bar" style="height:{{ i|format_graph:statistics.max_avg_time_taken }}px"></div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<div class="NB-module-stats-count-number">.28 <small>sec</small></div>
|
||||
<div class="NB-module-stats-count-number">{{ statistics.latest_avg_time_taken|floatformat:2 }} <small>sec</small></div>
|
||||
<div class="NB-module-stats-count-description">Avg. load time</div>
|
||||
</div>
|
||||
<div class="NB-module-stats-count">
|
||||
|
|
|
@ -59,7 +59,7 @@ def commify(n):
|
|||
dollars, cents = n, None
|
||||
|
||||
r = []
|
||||
for i, c in enumerate(str(dollars)[::-1]):
|
||||
for i, c in enumerate(reversed(dollars)):
|
||||
if i and (not (i % 3)):
|
||||
r.insert(0, ',')
|
||||
r.insert(0, c)
|
||||
|
|
Loading…
Add table
Reference in a new issue