From 0d6aa42f48de27b77ee982da4609dd8bd4b92973 Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Wed, 28 Sep 2022 17:23:02 +0200 Subject: [PATCH] Add vuejs in agents_state (update informations every 4 secs) --- CHANGELOG | 6 +++++- VERSION | 2 +- src/core/pyros_django/api/serializers.py | 32 +++++++++++++++++++++++++++++--- src/core/pyros_django/api/urls.py | 1 + src/core/pyros_django/api/views.py | 15 +++++++++++++-- src/core/pyros_django/dashboard/templates/dashboard/agents_state.html | 43 +++++++++++++++++++++++++++++++++++++++++-- src/core/pyros_django/pyros/settings.py | 1 + 7 files changed, 91 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 041f5c9..df9a219 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,8 @@ -20-09-2022 (AKo): v0.5.5.0 +28-09-2022 (AKo): v0.5.6.0 + - Add vuejs in agents_state (update informations every 4 secs) + - Add new option for agentSST : start a specific agent + +23-09-2022 (AKo): v0.5.5.0 - Add flexible form for sending agent cmd - New implementation of Agent get_specific_cmds() - Adapt agentSST to new signature of specific_cmds, add type hints to cmd functions diff --git a/VERSION b/VERSION index 6c71685..0efac6f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.5.0 \ No newline at end of file +0.5.6.0 \ No newline at end of file diff --git a/src/core/pyros_django/api/serializers.py b/src/core/pyros_django/api/serializers.py index 5732b85..e5595ef 100644 --- a/src/core/pyros_django/api/serializers.py +++ b/src/core/pyros_django/api/serializers.py @@ -1,7 +1,7 @@ -from common.models import Institute, Period, PyrosUser, SP_Period, ScienceTheme, ScientificProgram, Sequence, Plan, Album +from common.models import AgentSurvey, Institute, Period, PyrosUser, SP_Period, ScienceTheme, ScientificProgram, Sequence, Plan, Album, AgentCmd from rest_framework import serializers - - +import datetime +from datetime import timezone class InstituteSerializer(serializers.ModelSerializer): class Meta: @@ -97,3 +97,29 @@ class FullSequenceSerializer(serializers.ModelSerializer): model = Sequence fields = "__all__" extra_fields = ["pyros_user", "albums"] + + + +class AgentSurveySerializer(serializers.ModelSerializer): + class Meta: + model = AgentSurvey + fields = "__all__" + + is_out_of_date = serializers.SerializerMethodField() + running_cmd = serializers.SerializerMethodField() + + def get_is_out_of_date(self, obj): + datetime_now = datetime.datetime.utcnow() + datetime_now_minus_thirty_sec = datetime_now - datetime.timedelta(seconds=30) + datetime_now_minus_thirty_sec = datetime_now_minus_thirty_sec.replace(tzinfo=timezone.utc) + + if datetime_now_minus_thirty_sec >= obj.updated: + return True + else: + return False + + def get_running_cmd(self,obj): + try: + return AgentCmd.objects.get(state="CMD_RUNNING",sender=obj.name).full_name + except AgentCmd.DoesNotExist: + return "None" \ No newline at end of file diff --git a/src/core/pyros_django/api/urls.py b/src/core/pyros_django/api/urls.py index 9d68e3a..41c7cf6 100644 --- a/src/core/pyros_django/api/urls.py +++ b/src/core/pyros_django/api/urls.py @@ -11,6 +11,7 @@ router.register(r'plans', views.PlanViewSet) router.register(r'full_sequences', views.FullSequenceViewSet) router.register(r'scientific_programs', views.ScientificProgramViewSet) router.register(r'sp_period', views.SPPeriodViewSet) +router.register(r'agent_survey', views.AgentSurveyViewSet) urlpatterns = [ path('', include(router.urls)), diff --git a/src/core/pyros_django/api/views.py b/src/core/pyros_django/api/views.py index 38511ae..64b0f61 100644 --- a/src/core/pyros_django/api/views.py +++ b/src/core/pyros_django/api/views.py @@ -6,8 +6,8 @@ from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.decorators import api_view, permission_classes, action from django.core.validators import ValidationError from src.core.pyros_django.user_manager import views as user_views -from api.serializers import AlbumSerializer, FullSequenceSerializer, PlanSerializer, SPPeriodSerializer, ScientificProgramSerializer, SequenceSerializer, UserSerializer -from common.models import PyrosUser, SP_Period, ScientificProgram, Sequence, Album, Plan, UserLevel, SP_Period_User +from api.serializers import AgentSurveySerializer, AlbumSerializer, FullSequenceSerializer, PlanSerializer, SPPeriodSerializer, ScientificProgramSerializer, SequenceSerializer, UserSerializer +from common.models import PyrosUser, SP_Period, ScientificProgram, Sequence, Album, Plan, UserLevel, SP_Period_User, AgentSurvey from routine_manager.functions import check_sequence_file_validity from rest_framework.request import Request from django.db.models import Q @@ -238,3 +238,14 @@ def submit_sequence_with_json(request): log.info( f"User {request.user} did action submit sequence {seq} for period {seq.period} ") return Response(response) + + + +class AgentSurveyViewSet(viewsets.ModelViewSet): + """ + API endpoint that allows users to view their sequences. + """ + queryset = AgentSurvey.objects.all() + serializer_class = AgentSurveySerializer + permission_classes = [IsAuthenticated] + http_method_names = ["get"] \ 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 index 5fb4da4..5c80cd8 100644 --- a/src/core/pyros_django/dashboard/templates/dashboard/agents_state.html +++ b/src/core/pyros_django/dashboard/templates/dashboard/agents_state.html @@ -7,13 +7,18 @@ Agents status : {% endblock %} -{% block content %} +{% block head %} + +{% endblock %} +{% block content %} + +
@@ -28,6 +33,7 @@ + {% comment %} {% for agent in agents %} @@ -43,6 +49,20 @@ {% endfor %} + {% endcomment %} + + + + + + + + + + + + +
{{ agent.name }} {{ agent.current_nb_restart }}
[[agent.name ]] [[ agent.status ]] [[ agent.mode ]] [[ agent.updated ]] [[ agent.updated ]] [[agent.running_cmd]] [[ agent.current_nb_restart ]]
@@ -61,6 +81,25 @@ }); } - setInterval(refresh_command_column,5000); + function refresh_status_table(){ + + fetch("/api/agent_survey/?format=json").then(response => response.json()).then(function(data){ + console.log(data); + app.agents = data; + }); + } + $(document).ready(function() { + + refresh_status_table(); + //setInterval(refresh_command_column,5000); + setInterval(refresh_status_table,4000); + }); + var app = new Vue({ + delimiters:["[[","]]"], + el: '#agents_state_table', + data: { + agents: [] + } + }) {% endblock %} \ No newline at end of file diff --git a/src/core/pyros_django/pyros/settings.py b/src/core/pyros_django/pyros/settings.py index ddf8a0c..6b28894 100644 --- a/src/core/pyros_django/pyros/settings.py +++ b/src/core/pyros_django/pyros/settings.py @@ -234,6 +234,7 @@ REST_FRAMEWORK = { 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ], + 'DATETIME_FORMAT': "%Y-%m-%dT%H:%M:%S", } MIDDLEWARE = [ -- libgit2 0.21.2