From c1a58d54e38d58fba3f23c100934bb3ea6e5d03c Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Mon, 27 Jun 2022 13:13:57 +0200 Subject: [PATCH] Adding views to view agents status, agent log and a view to change software mode. --- src/core/pyros_django/dashboard/forms.py | 11 ++++++++++- src/core/pyros_django/dashboard/templates/dashboard/agent_detail.html | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/core/pyros_django/dashboard/templates/dashboard/agents_state.html | 33 +++++++++++++++++++++++++++++++++ src/core/pyros_django/dashboard/templates/dashboard/settings.html | 22 ++++++++++++++++++++++ src/core/pyros_django/dashboard/templates/dashboard/soft_mode.html | 20 ++++++++++++++++++++ src/core/pyros_django/dashboard/templatetags/tags.py | 14 ++++++++++++-- src/core/pyros_django/dashboard/urls.py | 6 +++++- src/core/pyros_django/dashboard/views.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/core/pyros_django/misc/templates/base.html | 9 +++++---- 9 files changed, 213 insertions(+), 10 deletions(-) create mode 100644 src/core/pyros_django/dashboard/templates/dashboard/agent_detail.html create mode 100644 src/core/pyros_django/dashboard/templates/dashboard/agents_state.html create mode 100644 src/core/pyros_django/dashboard/templates/dashboard/soft_mode.html diff --git a/src/core/pyros_django/dashboard/forms.py b/src/core/pyros_django/dashboard/forms.py index d37929d..0630969 100644 --- a/src/core/pyros_django/dashboard/forms.py +++ b/src/core/pyros_django/dashboard/forms.py @@ -1,5 +1,5 @@ from django import forms -from common.models import Config, PyrosUser, UserLevel +from common.models import Config, PyrosUser, UserLevel, Majordome class ConfigForm(forms.ModelForm): class Meta: @@ -29,6 +29,15 @@ class ConfigForm(forms.ModelForm): 'plc_ip_address': ('Ip Address :'), } +class MajordomeForm(forms.ModelForm): + class Meta: + model = Majordome + fields = ( + "soft_mode", + ) + labels = { + "soft_mode": ("Software mode :") + } class UserForm(forms.ModelForm): class Meta: model = PyrosUser diff --git a/src/core/pyros_django/dashboard/templates/dashboard/agent_detail.html b/src/core/pyros_django/dashboard/templates/dashboard/agent_detail.html new file mode 100644 index 0000000..01c50e9 --- /dev/null +++ b/src/core/pyros_django/dashboard/templates/dashboard/agent_detail.html @@ -0,0 +1,45 @@ +{% extends "base.html" %} + +{% load static %} + +{% block title %} + Agent {{ agent_name }} detail : +{% endblock %} + +{% block content %} + +{% if not error_message %} +

Last logs of {{ agent_name }} :

+
 
+{% for line in log_last_30_lines  %}
+{{ line }}    
+    
+{% endfor %}
+
+{% else %} +

{{ error_message }}

+{% endif %} + Go back to agents state +{% csrf_token %} + +{% endblock %} \ No newline at end of file diff --git a/src/core/pyros_django/dashboard/templates/dashboard/agents_state.html b/src/core/pyros_django/dashboard/templates/dashboard/agents_state.html new file mode 100644 index 0000000..26c3950 --- /dev/null +++ b/src/core/pyros_django/dashboard/templates/dashboard/agents_state.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% load static %} + +{% block title %} + Agents status : +{% endblock %} + +{% block content %} +
+ + + + + + + + + + + {% for agent in agents %} + + + + + + + {% endfor %} + +
Agent Status Mode Udpated
{{ agent.name }} {{ agent.status }} {{ agent.mode }} {{ agent.updated }}
+
+{% endblock %} \ No newline at end of file diff --git a/src/core/pyros_django/dashboard/templates/dashboard/settings.html b/src/core/pyros_django/dashboard/templates/dashboard/settings.html index 2f70bf9..0181dab 100644 --- a/src/core/pyros_django/dashboard/templates/dashboard/settings.html +++ b/src/core/pyros_django/dashboard/templates/dashboard/settings.html @@ -125,6 +125,28 @@ {% endif %} + {% if CAN_CHANGE_SOFT_MODE %} +
  • + +
  • + {% endif %} + {% if CAN_VIEW_AGENTS_STATE %} +
  • + +
  • + {% endif %}
  • diff --git a/src/core/pyros_django/dashboard/templates/dashboard/soft_mode.html b/src/core/pyros_django/dashboard/templates/dashboard/soft_mode.html new file mode 100644 index 0000000..75d6f04 --- /dev/null +++ b/src/core/pyros_django/dashboard/templates/dashboard/soft_mode.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + +{% load static %} + +{% block title %} + Observation System Status : +{% endblock %} + +{% block content %} + +
    +{% csrf_token %} +
      +
    • {{ form.as_p }}
    • + +
    + +
    + +{% endblock %} \ No newline at end of file diff --git a/src/core/pyros_django/dashboard/templatetags/tags.py b/src/core/pyros_django/dashboard/templatetags/tags.py index 9aef0d8..73ccbd8 100644 --- a/src/core/pyros_django/dashboard/templatetags/tags.py +++ b/src/core/pyros_django/dashboard/templatetags/tags.py @@ -1,7 +1,7 @@ from django import template from django.contrib.auth.models import User #from common.models import Config, PyrosUser, PyrosState -from common.models import Config, PyrosUser, UserLevel +from common.models import Config, PyrosUser, UserLevel, Majordome from datetime import date from django.conf import settings @@ -133,4 +133,14 @@ def to_int(value): @register.filter def index(indexable, i): - return indexable[i] \ No newline at end of file + return indexable[i] + +@register.simple_tag +def soft_mode(request): + try: + soft_mode = Majordome.objects.get(id=1).soft_mode + if soft_mode: + return soft_mode + raise Majordome.DoesNotExist + except Majordome.DoesNotExist: + return ("No Majordome instance in DB") \ No newline at end of file diff --git a/src/core/pyros_django/dashboard/urls.py b/src/core/pyros_django/dashboard/urls.py index ea3bc83..48aee52 100644 --- a/src/core/pyros_django/dashboard/urls.py +++ b/src/core/pyros_django/dashboard/urls.py @@ -47,5 +47,9 @@ urlpatterns = [ path('simulator_lock', views.simulator_switch_lock, name="simulator_switch_lock"), path('simulator_majordome_restart', views.simulator_majordome_restart, name="simulator_majordome_restart"), path('simulator_majordome_shutdown', views.simulator_majordome_shutdown, name="simulator_majordome_shutdown"), - path('simulator_switch_globalmode', views.simulator_switch_globalMode, name="simulator_switch_globalMode") + path('simulator_switch_globalmode', views.simulator_switch_globalMode, name="simulator_switch_globalMode"), + path('soft_mode', views.soft_mode, name="soft_mode"), + path('agents_state', views.agents_state, name="agents_state"), + path('agent_detail/', views.agent_detail, name="agent_detail"), + path('retrieve_log_content', views.retrieve_log_content, name="retrieve_log_content"), ] diff --git a/src/core/pyros_django/dashboard/views.py b/src/core/pyros_django/dashboard/views.py index 272e990..06ca44c 100644 --- a/src/core/pyros_django/dashboard/views.py +++ b/src/core/pyros_django/dashboard/views.py @@ -5,12 +5,12 @@ from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required import datetime -from common.models import Log, SP_Period_Guest, WeatherWatch, SiteWatch, ScientificProgram, Config, PyrosUser, PlcDeviceStatus, Telescope, TelescopeCommand, UserLevel, WeatherWatchHistory +from common.models import Log, SP_Period_Guest, WeatherWatch, SiteWatch, ScientificProgram, Config, PyrosUser, PlcDeviceStatus, Telescope, TelescopeCommand, UserLevel, WeatherWatchHistory, Majordome, AgentSurvey from django.core import serializers import utils.Logger as l from django.forms import modelformset_factory from django.http import HttpResponseRedirect -from dashboard.forms import ConfigForm, UserForm +from dashboard.forms import ConfigForm, UserForm, MajordomeForm from dashboard.decorator import level_required from django.views.generic.edit import UpdateView from django.shortcuts import get_object_or_404 @@ -32,6 +32,7 @@ sys.path.append("../../..") from config.pyros.config_pyros import ConfigPyros #import utils.celme as celme import vendor.guitastro as guitastro +from django.contrib import messages from collections import OrderedDict @@ -177,8 +178,66 @@ def settings(request): View called to see the settings (for the software, observatory, users...) page ''' CAN_VIEW_PERIOD = request.session.get("role") in ("Admin", "Unit-PI", "Observer", "Unit-board") + CAN_CHANGE_SOFT_MODE = request.session.get("role") in ("Admin", "Unit-PI", "Operator") + CAN_VIEW_AGENTS_STATE = request.session.get("role") in ("Admin", "Unit-PI", "Operator") return(render(request, "dashboard/settings.html", locals())) +def agent_detail(request, agent_name): + agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log") + try: + log = open(agent_log_path,"r") + log_content = log.readlines() + + if len(log_content) > 30: + log_last_30_lines = log_content[-30:] + + else: + log_last_30_lines = log_content + log.close() + except FileNotFoundError: + error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})" + return render(request, "dashboard/agent_detail.html", locals()) + +def retrieve_log_content(request): + if request.POST: + agent_name = request.POST.get("agent_name") + agent_log_path = os.path.join(os.environ.get("PROJECT_ROOT_PATH"),f"logs/{agent_name}/",f"{agent_name}.log") + try: + log = open(agent_log_path,"r") + log_content = log.readlines() + + if len(log_content) > 30: + log_last_30_lines = log_content[-30:] + + else: + log_last_30_lines = log_content + log.close() + return HttpResponse(log_last_30_lines) + except FileNotFoundError: + error_message = f"Cannot find agent logs. (Agent log path tried : {agent_log_path})" + return HttpResponse(error_message) + + +@login_required +@level_required("Admin", "Unit-PI", "Operator") +def soft_mode(request): + if request.POST: + form = MajordomeForm(request.POST) + if form.is_valid(): + obj, created = Majordome.objects.get_or_create(id=1) + obj.soft_mode = form.cleaned_data["soft_mode"] + obj.save() + new_soft_mode = form.instance.soft_mode + messages.add_message(request, messages.INFO, f"Changed observatory mode to {new_soft_mode}") + obj, created = Majordome.objects.get_or_create(id=1) + form = MajordomeForm(instance=obj) + soft_mode = obj.soft_mode + is_auto = soft_mode == Majordome.AUTO_MODE + return render(request, "dashboard/soft_mode.html", locals()) + +def agents_state(request): + agents = AgentSurvey.objects.all() + return render(request, "dashboard/agents_state.html", locals()) def retrieve_main_icon(request): if request.is_ajax(): try: diff --git a/src/core/pyros_django/misc/templates/base.html b/src/core/pyros_django/misc/templates/base.html index f555efd..c511c92 100644 --- a/src/core/pyros_django/misc/templates/base.html +++ b/src/core/pyros_django/misc/templates/base.html @@ -339,12 +339,13 @@ footer{
  • -
  • {% global_mode request %}

    - {% endcomment %} -