##// END OF EJS Templates
Better navigation, sidebar, update jroplots #TODO OverJRO
Better navigation, sidebar, update jroplots #TODO OverJRO

File last commit:

r39:b80dda8fb07c
r39:b80dda8fb07c
Show More
views.py
239 lines | 7.3 KiB | text/x-python | PythonLexer
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import time
from datetime import datetime
from django import forms
from django.contrib import messages
from django.utils.safestring import mark_safe
from django.shortcuts import render
from django.http import HttpResponse
import mongoengine
from plotter.models import Experiment, ExpDetail, PlotMeta, PlotData
from utils.plots import skynoise_plot
host = os.environ.get('HOST_MONGO', 'localhost')
mongoengine.connect('dbplots', host=host, port=27017)
# Forms
class SearchForm(forms.Form):
experiment = forms.ChoiceField()
plot = forms.ChoiceField()
def __init__(self, *args, **kwargs):
exp_choices = kwargs.pop('exp_choices', [])
plt_choices = kwargs.pop('plt_choices', [])
super(SearchForm, self).__init__(*args, **kwargs)
self.fields['experiment'].choices = [(0, 'Select Experiment')] + exp_choices
self.fields['plot'].choices = [(0, 'Select Plot')] + plt_choices
# we use this class to change the parameter in Scatter plot using the function plotly.restyle in jroplot.js
class ScatterSetupForm(forms.Form):
plotdiv = forms.CharField(widget=forms.HiddenInput())
ymax = forms.CharField(initial=30)
ymin = forms.CharField(initial=10)
# we use this class to change the parameter in RTI plot using the function plotly.restyle in jroplot.js
class RTISetupForm(forms.Form):
plotdiv = forms.CharField(widget=forms.HiddenInput())
colormap = forms.ChoiceField(choices=[('Jet', 'Jet'), ('Viridis', 'Viridis'), ('RdBu', 'RdBu')])
zmax = forms.CharField(initial=30)
zmin = forms.CharField(initial=10)
ymax = forms.CharField(initial=180)
ymin = forms.CharField(initial=80)
# we use this class to change the parameter in SPC plot using the function plotly.restyle in jroplot.js
class SPCSetupForm(forms.Form):
plotdiv = forms.CharField(widget=forms.HiddenInput())
colormap = forms.ChoiceField(choices=[('Jet', 'Jet'), ('Viridis', 'Viridis'), ('RdBu', 'RdBu')])
#como es un perfil xmin y xmax deben ser iguales a zmin y zmax
xmax = forms.CharField(initial=30)
xmin = forms.CharField(initial=10)
#x2max = forms.CharField(initial=30)
#x2min = forms.CharField(initial=10)
ymax = forms.CharField(initial=180)
ymin = forms.CharField(initial=80)
zmax = forms.CharField(initial=30)
zmin = forms.CharField(initial=10)
# Create your views here.
def main(request, tag=None):
kwargs = {}
date = request.GET.get('date', datetime.now().strftime('%d-%m-%Y'))
exps = ExpDetail.objects(date=datetime.strptime(date, '%d-%m-%Y'))
tmp = {}
for exp in exps:
label = exp.tag.lower().strip() if exp.tag else 'other'
if label in tmp:
tmp[label] += 1
else:
tmp[label] = 1
tags = []
for key, value in tmp.items():
if tag == key:
tags.append({'name': key, 'n': tmp[key], 'active': 'active'})
else:
tags.append({'name': key, 'n': tmp[key]})
kwargs['tags'] = tags
if tag:
experiments = []
for exp in exps:
label = exp.tag.lower().strip() if exp.tag else 'other'
if label != tag:
continue
dum = {}
dum['code'] = exp.experiment.code
dum['plots'] = []
dum['name'] = exp.experiment.name
dt = datetime.now()
t = time.mktime(dt.timetuple())
t -= 5*60*60
if (t-exp['last_time']) > 6*exp['interval']:
status = 'Offline'
clase = 'alertas-offline'
style = 'danger'
lastDataDate = exp['last_time']
elif (t-exp['last_time']) > 3*exp['interval']:
status = 'Delayed'
clase = 'alertas-delayed'
style = 'warning'
lastDataDate = exp['last_time']
else:
status = 'Online'
clase = 'alertas-online'
style = 'success'
lastDataDate = exp['last_time']
dum['status'] = status
dum['class'] = clase
dum['style']= style
dum['date']= datetime.utcfromtimestamp(lastDataDate)
for plot in exp.plots():
dum['plots'].append({'plot': plot.plot, 'name': plot.plot.replace('_', ' ').title(), 'id':plot.id})
experiments.append(dum)
kwargs['experiments'] = experiments
kwargs['date'] = date
kwargs['title'] = 'Realtime Experiments at JRO'
kwargs['sidebar'] = True
return render(request, 'home.html', kwargs)
def about(request):
'''
'''
kwargs = {
'title': 'About'
}
return render(request, 'about.html', kwargs)
def tools(request):
'''
'''
kwargs = {
'title': 'Tools'
}
return render(request, 'tools.html', kwargs)
def reports(request):
'''
'''
kwargs = {
'title': 'Reports',
}
return render(request, 'reports.html', kwargs)
def plot(request, code=None, plot=None):
'''
'''
realtime = False
date = request.GET.get('date', None)
if date is None:
date = datetime.now().strftime('%d-%m-%Y')
realtime = True
exp = Experiment.objects.get(code=int(code))
detail = ExpDetail.objects.get(experiment=exp, date=datetime.strptime(date, '%d-%m-%Y'))
meta = PlotMeta.objects.get(exp_detail=detail, plot=plot)
kwargs = {
'code': code,
'plot': plot,
'meta':meta,
'date': date,
'id': meta.pk,
'realtime': realtime,
'title': 'Realtime Experiments at JRO',
'name' : exp.name,
'sidebar': True,
'plots': []
}
for plt in detail.plots():
kwargs['plots'].append({'plot': plt.plot, 'name': plt.plot.replace('_', ' ').title()})
# Logic to show my views
if meta.metadata['type'] == 'pcolorbuffer':
kwargs['setup_form'] = RTISetupForm()
kwargs['fn_plot'] = 'PcolorBuffer'
return render(request, 'plot.html', kwargs)
elif meta.metadata['type'] == 'pcolor':
kwargs['setup_form'] = SPCSetupForm()
kwargs['fn_plot'] = 'Pcolor'
return render(request, 'plot.html', kwargs)
elif meta.metadata['type'] == 'scatterbuffer':
kwargs['setup_form'] = ScatterSetupForm()
kwargs['fn_plot'] = 'ScatterBuffer'
return render(request, 'plot.html', kwargs)
elif meta.metadata['type'] == 'image':
kwargs['image'] = True
kwargs['fn_plot'] = 'StaticPlot'
return render(request, 'plot.html', kwargs)
else:
return render(request, 'home.html', {})
def plot_skynoise(request):
date = request.GET.get('date', None)
if date is None:
date = datetime.now()
else:
date = datetime.strptime(date, '%Y-%m-%d')
data = skynoise_plot(date.year, date.month, date.day)
response = HttpResponse(data.getvalue(), content_type='image/png')
return response
def plot_overjro(request):
date = request.GET.get('date', None)
if date is None:
date = datetime.now()
else:
date = datetime.strptime(date, '%Y-%m-%d')
data = skynoise_plot(date.year, date.month, date.day)
response = HttpResponse(data.getvalue(), content_type='image/png')
return response