Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a page for Roboshifter Logs #274

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
27 changes: 27 additions & 0 deletions minard/roboshifter_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

from wtforms.fields.html5 import DateField
from wtforms import Form

from datetime import date

# TODO replace this with WHERE the log will actually be stored once deployed
# alternatively add to some type of config somewhere, don't think this is how it's done in minard through
LOG_DIR = '/home/david/alarmgui/logs/'

class RoboshifterLogDateForm(Form):
date = DateField(label="Log Date", render_kw={"onchange": "this.form.submit()"})

def get_roboshifter_log():
log = ""
with open(LOG_DIR + "roboshifter.log", "r") as f:
for line in reversed(list(f)):
log += line
return log

def get_historic_roboshifter_log(datestring):
log = ""
with open(LOG_DIR + "roboshifter.log" + "." + datestring) as f:
for line in reversed(list(f)):
log += line
return log
1 change: 1 addition & 0 deletions minard/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ul class='dropdown-menu'>
{{ nav_link('alarms', 'Level 2 Logs') }}
<li><a href="/monitoring/logs">DAQ Logs</a></li>
{{ nav_link('roboshifter_log', 'Roboshifter Logs') }}
</ul>
<!-- Dropdown Channel -->
<li class='dropdown'>
Expand Down
105 changes: 105 additions & 0 deletions minard/templates/roboshifter_log.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{% extends "layout.html" %}
{% block title %}Roboshifter Log{% endblock %}

{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}


{% block head %}
{{ super() }}
<style>
pre {
tab-size: 4;
counter-reset: linecounter;
}

pre span.line {
counter-increment: linecounter;
}

.line {
background: #0000;
color: #000;
opacity: 1.0;
}

pre span.line::before{
content: counter(linecounter);
width: 30px;
display: inline-block;
border-right: 1px dotted #fff;
padding-right: 3px;
margin-right: 5px;
text-align: right;
}
</style>
<script type="text/javascript">
function addLineClass(pre){
var lines = pre.innerText.split("\n");
while(pre.childNodes.length > 0) {
pre.removeChild(pre.childNodes[0]);
}
for(var i =0; i < lines.length; i++){
var span = document.createElement("span");
span.className = "line";
span.innerText = lines[i];
pre.appendChild(span);
pre.appendChild(document.createTextNode("\n"))
}
}
window.addEventListener("load", function () {
var pres = document.getElementsByTagName("pre");
for(var i = 0; i < pres.length; i++){
addLineClass(pres[i]);
}
}, false);
</script>


{% endblock %}

{% block body %}
{{ super() }}
<div class="row">
<div class="col-md-6 col-md-offset-3">
<center>
{% if logdate %}
<h2 style="display:inline">Roboshifter Logs for {{ logdate }} </h2>
{% else %}
<h2 style="display:inline">Roboshifter Logs</h2>
{% endif %}
</center>
</div>
</div>
<!-- Spacing the title and body here. Should figure out a proper way to do this. -->
<p></p>
<p></p>
<div class="container">
<div class="col-md-12">
<form method="get" action="{{ url_for('roboshifter_log') }}" role='form'>
{{ render_field(form.date) }}
</form>
</div>
</div>
<p></p>
<p></p>
<div class="container">
<div class="col-md-12">
<pre>
{%- for l in roboshifter_log -%}
{{ l }}
{%- endfor -%}
</pre>
</div>
</div>
{% endblock %}
15 changes: 15 additions & 0 deletions minard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
from functools import wraps, update_wrapper
from dead_time import get_dead_time, get_dead_time_runs, get_dead_time_run_by_key
from radon_monitor import get_radon_monitor
from roboshifter_log import get_roboshifter_log, get_historic_roboshifter_log, RoboshifterLogDateForm
from datetime import date

TRIGGER_NAMES = \
['100L',
Expand Down Expand Up @@ -215,6 +217,19 @@ def update_pmtic_resistors():
return redirect(url_for('calculate_resistors', crate=form.crate.data, slot=form.slot.data))
return render_template('update_pmtic_resistors.html', crate=crate, slot=slot, form=form, pc=pc)

@app.route('/roboshifter-log')
def roboshifter_log():
try:
selected_date = request.args.get("date")
log = get_roboshifter_log() if selected_date == date.today().strftime("%Y-%m-%d") else get_historic_roboshifter_log(selected_date)
except:
# get today's log if there is no argument with the request or the selected date does not exist
log = get_roboshifter_log()
# create a form instance
logdate = log.split(" ")[0]
form = RoboshifterLogDateForm()
return render_template('roboshifter_log.html', roboshifter_log=log, logdate=logdate, form=form)

@app.route('/calculate-resistors')
def calculate_resistors():
crate = request.args.get("crate", 0, type=int)
Expand Down