summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2010-09-21 18:31:26 -0500
committerDan McGee <dan@archlinux.org>2010-09-21 18:31:26 -0500
commit5b87b21ccdd5938acf81afed00ccf0d957f5047c (patch)
tree8da26c8b6a90a19724568505f4fa81c5c844a8d6
parent818028562d5d99482c781457e0219f7b5afe4e2c (diff)
Allow caching of mirror status info
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--mirrors/utils.py45
-rw-r--r--mirrors/views.py33
2 files changed, 48 insertions, 30 deletions
diff --git a/mirrors/utils.py b/mirrors/utils.py
new file mode 100644
index 00000000..bbbaca26
--- /dev/null
+++ b/mirrors/utils.py
@@ -0,0 +1,45 @@
+from django.db.models import Avg, Count, Max, Min, StdDev
+
+from main.utils import cache_function
+from .models import MirrorLog, MirrorProtocol, MirrorUrl
+
+import datetime
+
+cutoff_time = datetime.datetime.utcnow() - datetime.timedelta(hours=24)
+
+@cache_function(300)
+def get_mirror_statuses():
+ protocols = MirrorProtocol.objects.exclude(protocol__iexact='rsync')
+ # I swear, this actually has decent performance...
+ urls = MirrorUrl.objects.select_related(
+ 'mirror', 'protocol').filter(
+ mirror__active=True, mirror__public=True,
+ protocol__in=protocols).filter(
+ logs__check_time__gte=cutoff_time).annotate(
+ check_count=Count('logs'), last_sync=Max('logs__last_sync'),
+ last_check=Max('logs__check_time'),
+ duration_avg=Avg('logs__duration'), duration_min=Min('logs__duration'),
+ duration_max=Max('logs__duration'), duration_stddev=StdDev('logs__duration')
+ ).order_by('-last_sync', '-duration_avg')
+
+ for url in urls:
+ if url.last_check and url.last_sync:
+ d = url.last_check - url.last_sync
+ url.delay = d
+ url.score = d.days * 24 + d.seconds / 3600 + url.duration_avg + url.duration_stddev
+ else:
+ url.delay = None
+ url.score = None
+ return urls
+
+@cache_function(300)
+def get_mirror_errors():
+ errors = MirrorLog.objects.filter(
+ is_success=False, check_time__gte=cutoff_time).values(
+ 'url__url', 'url__protocol__protocol', 'url__mirror__country',
+ 'error').annotate(
+ error_count=Count('error'), last_occurred=Max('check_time')
+ ).order_by('-last_occurred', '-error_count')
+ return list(errors)
+
+# vim: set ts=4 sw=4 et:
diff --git a/mirrors/views.py b/mirrors/views.py
index b13c9148..17f65cc7 100644
--- a/mirrors/views.py
+++ b/mirrors/views.py
@@ -6,7 +6,7 @@ from django.views.generic.simple import direct_to_template
from main.utils import make_choice
from .models import Mirror, MirrorUrl, MirrorProtocol
-from .models import MirrorLog
+from .utils import get_mirror_statuses, get_mirror_errors
import datetime
@@ -55,28 +55,8 @@ def find_mirrors(request, countries=None, protocols=None):
mimetype='text/plain')
def status(request):
- cutoff_time = datetime.datetime.utcnow() - datetime.timedelta(hours=24)
bad_timedelta = datetime.timedelta(days=3)
-
- protocols = MirrorProtocol.objects.exclude(protocol__iexact='rsync')
- # I swear, this actually has decent performance...
- urls = MirrorUrl.objects.select_related(
- 'mirror', 'protocol').filter(
- mirror__active=True, mirror__public=True,
- protocol__in=protocols).filter(
- logs__check_time__gte=cutoff_time).annotate(
- check_count=Count('logs'), last_sync=Max('logs__last_sync'),
- last_check=Max('logs__check_time'),
- duration_avg=Avg('logs__duration'), duration_min=Min('logs__duration'),
- duration_max=Max('logs__duration'), duration_stddev=StdDev('logs__duration')
- ).order_by('-last_sync', '-duration_avg')
- # errors during check process go in another table
- error_logs = MirrorLog.objects.filter(
- is_success=False, check_time__gte=cutoff_time).values(
- 'url__url', 'url__protocol__protocol', 'url__mirror__country',
- 'error').annotate(
- error_count=Count('error'), last_occurred=Max('check_time')
- ).order_by('-last_occurred', '-error_count')
+ urls = get_mirror_statuses()
if urls:
last_check = max([u.last_check for u in urls])
@@ -86,13 +66,6 @@ def status(request):
good_urls = []
bad_urls = []
for url in urls:
- if url.last_check and url.last_sync:
- d = url.last_check - url.last_sync
- url.delay = d
- url.score = d.days * 24 + d.seconds / 3600 + url.duration_avg + url.duration_stddev
- else:
- url.delay = None
- url.score = None
# split them into good and bad lists based on delay
if not url.delay or url.delay > bad_timedelta:
bad_urls.append(url)
@@ -103,7 +76,7 @@ def status(request):
'last_check': last_check,
'good_urls': good_urls,
'bad_urls': bad_urls,
- 'error_logs': error_logs,
+ 'error_logs': get_mirror_errors(),
}
return direct_to_template(request, 'mirrors/status.html', context)