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