root / core / views.py @ 04733cdb
History | View | Annotate | Download (4.3 kB)
1 |
# -*- coding: utf-8 -*- vim:encoding=utf-8:
|
---|---|
2 |
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
|
3 |
|
4 |
from core.models import * |
5 |
from graphs.models import * |
6 |
from django.core.urlresolvers import reverse |
7 |
from django.shortcuts import render_to_response,get_object_or_404 |
8 |
from django.http import HttpResponse,HttpResponseRedirect,Http404 |
9 |
from django.conf import settings |
10 |
|
11 |
from django.views.decorators.cache import cache_page |
12 |
from django.views.decorators.cache import never_cache |
13 |
from django.core.cache import cache |
14 |
|
15 |
from django.template import RequestContext |
16 |
|
17 |
import json |
18 |
|
19 |
import datetime |
20 |
import time |
21 |
import rrdtool |
22 |
import tempfile |
23 |
import os |
24 |
import re |
25 |
import random |
26 |
import pprint |
27 |
|
28 |
from gevent.pool import Pool |
29 |
from gevent.timeout import Timeout |
30 |
|
31 |
from util.analyze import analyze |
32 |
|
33 |
def index(request): |
34 |
luns = Lun.objects.all() |
35 |
graphs = Graph.objects.all() |
36 |
nl = cache.get('lun:stats')
|
37 |
if nl is None: |
38 |
nl = analyze(luns) |
39 |
urls = [] |
40 |
|
41 |
for i in nl: |
42 |
lun = luns.get(pk=i['lunpk'])
|
43 |
graph = graphs.get(pk=i['graph'])
|
44 |
if int(i['avg']) == 0: |
45 |
continue
|
46 |
#for graph in lungraphs:
|
47 |
graph_dict = {} |
48 |
graph.url = reverse(thumb, args=(graph.pk,)) |
49 |
graph_dict['url'] = graph.url
|
50 |
graph_dict['graph'] = graph
|
51 |
graph_dict['lun'] = lun.name
|
52 |
urls.append(graph_dict) |
53 |
ret = { |
54 |
'urls' :urls
|
55 |
} |
56 |
return render_to_response('media.html', ret, |
57 |
context_instance=RequestContext(request)) |
58 |
|
59 |
def graphjson(request, graph_id): |
60 |
ret = {"graph_pk": graph_id}
|
61 |
return render_to_response('graph.html', ret, |
62 |
context_instance=RequestContext(request)) |
63 |
|
64 |
|
65 |
def graphperiods(request, graph_id): |
66 |
graph = Graph.objects.get(pk=graph_id) |
67 |
return render_to_response('graphperiods.html', {'graph': graph, }, |
68 |
context_instance=RequestContext(request)) |
69 |
|
70 |
|
71 |
def get_all_json(lazy=False, legend=False, start='-2d', end='-8m'): |
72 |
graphs = Graph.objects.all() |
73 |
kwargs = { 'start': str(start), 'end': str(end), 'lazy': lazy, 'legend': legend, 'jsonexport':True } |
74 |
results = [] |
75 |
for graph in graphs: |
76 |
results.extend(graph.create_graph(path=None, **kwargs))
|
77 |
# p = Pool(40)
|
78 |
#
|
79 |
# def _get_graphs_json(graph):
|
80 |
# t = Timeout(5)
|
81 |
# t.start()
|
82 |
# try:
|
83 |
# results.extend(graph.create_graph(path=None, **kwargs))
|
84 |
# except (Timeout):
|
85 |
# pass
|
86 |
# finally:
|
87 |
# t.cancel()
|
88 |
# p.imap(_get_graphs_json, graphs)
|
89 |
return results
|
90 |
|
91 |
|
92 |
def getpngdata(path,static): |
93 |
|
94 |
file = open(path,'r') |
95 |
data = file.read()
|
96 |
file.close()
|
97 |
|
98 |
if static == False: |
99 |
os.remove(path) |
100 |
|
101 |
response = HttpResponse(mimetype='image/png')
|
102 |
response.write(data) |
103 |
return response
|
104 |
|
105 |
def drawpng(request, graph_id, static=True, lazy=False, legend=False, start='-30h', end='-8m', jsonexport=False): |
106 |
|
107 |
graph = get_object_or_404(Graph,pk=graph_id) |
108 |
if static == True: |
109 |
lazy = True
|
110 |
path = '%s/%s%s%s.png' % (settings.RRD_PNG_STATICPATH, graph_id, start, end)
|
111 |
try:
|
112 |
statinfo = os.stat(path) |
113 |
if time.time() - statinfo.st_mtime < 300 : |
114 |
return getpngdata(path,static)
|
115 |
except OSError: |
116 |
pass
|
117 |
else:
|
118 |
path = tempfile.NamedTemporaryFile().name |
119 |
'''Removing unicode from strings while constructing kwargs'''
|
120 |
kwargs = { 'start': str(start), 'end': str(end), 'lazy': lazy, 'legend': legend, 'jsonexport':jsonexport } |
121 |
result = graph.create_graph(path, **kwargs) |
122 |
if result == None: |
123 |
raise Http404
|
124 |
if jsonexport:
|
125 |
return HttpResponse(json.dumps(result), mimetype="application/json") |
126 |
return getpngdata(path, static)
|
127 |
|
128 |
|
129 |
def thumb(request,graph_id,legend=True,lazy=True,static=True): |
130 |
|
131 |
start='-1d'
|
132 |
end='-300'
|
133 |
|
134 |
graph = get_object_or_404(Graph,pk=graph_id) |
135 |
|
136 |
path = '%s/thumb-%s.png' % (settings.RRD_PNG_STATICPATH, graph_id)
|
137 |
try:
|
138 |
statinfo = os.stat(path) |
139 |
if time.time() - statinfo.st_mtime < 300 : |
140 |
return getpngdata(path,static)
|
141 |
except OSError: |
142 |
pass
|
143 |
|
144 |
'''Removing unicode from strings while constructing kwargs'''
|
145 |
kwargs = { 'start': str(start), 'end': str(end), 'lazy': lazy, 'legend': legend, 'thumb': True, } |
146 |
|
147 |
result=graph.create_graph(path,**kwargs) |
148 |
return getpngdata(path,static)
|
149 |
|