Update terminal interval

pull/530/head
ibuler 8 years ago
parent 10aa8c40a7
commit c6fc3dfe91

@ -284,7 +284,6 @@ BROKER_URL = 'redis://%(password)s%(host)s:%(port)s/3' % {
}
CELERY_RESULT_BACKEND = BROKER_URL
# Cache use redis
CACHES = {
'default': {

@ -1,52 +1,63 @@
# -*- coding: utf-8 -*-
#
from django.core.cache import cache
from django.conf import settings
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.views import APIView, Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import AllowAny
from common.utils import signer, get_object_or_none
from .models import Terminal, TerminalHeatbeat
from .models import Terminal, HeatbeatFailedLog
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
from .hands import IsSuperUserOrTerminalUser
class TerminalCreateListApi(ListCreateAPIView):
class TerminalViewSet(ModelViewSet):
queryset = Terminal.objects.all()
serializer_class = TerminalSerializer
permission_classes = (AllowAny,)
def post(self, request, *args, **kwargs):
def create(self, request, *args, **kwargs):
name = signer.unsign(request.data.get('name', ''))
if name:
terminal = get_object_or_none(Terminal, name=name)
if terminal:
data = {
'data': {'name': name, 'id': terminal.id},
}
if terminal.is_active:
return Response(data={'data': {'name': name, 'id': terminal.id},
'msg': 'Success'},
status=200)
data['msg'] = 'Success'
return Response(data=data, status=200)
else:
return Response(data={'data': {'name': name, 'ip': terminal.ip},
'msg': 'Need admin active it'},
status=203)
data['msg'] = 'Need admin active this terminal'
return Response(data=data, status=203)
else:
ip = request.META.get('X-Real-IP') or request.META.get('REMOTE_ADDR')
terminal = Terminal.objects.create(name=name, ip=ip)
return Response(data={'data': {'name': name, 'ip': terminal.ip},
'msg': 'Need admin active it'},
status=201)
data = {
'data': {'name': name, 'id': terminal.id},
'msg': 'Need admin active this terminal',
}
return Response(data=data, status=201)
else:
return Response(data={'msg': 'Secrete key invalid'}, status=401)
class TerminalHeatbeatApi(ListCreateAPIView):
model = TerminalHeatbeat
serializer_class = TerminalHeatbeatSerializer
class TerminalHeatbeatApi(APIView):
# model = HeatbeatFailedLog
# serializer_class = TerminalHeatbeatSerializer
permission_classes = (IsSuperUserOrTerminalUser,)
def put(self, request, *args, **kwargs):
terminal_id = request.user.id
cache.set('terminal_heatbeat_%s' % terminal_id, settings.CONFIG.TERMINAL_HEATBEAT_INTERVAL * 3)
return Response({'msg': 'Success'})
class TerminalApiDetailUpdateDetailApi(RetrieveUpdateDestroyAPIView):
queryset = Terminal.objects.all()
serializer_class = TerminalSerializer
permission_classes = (IsSuperUserOrTerminalUser,)
# class TerminalApiDetailUpdateDetailApi(RetrieveUpdateDestroyAPIView):
# queryset = Terminal.objects.all()
# serializer_class = TerminalSerializer
# permission_classes = (IsSuperUserOrTerminalUser,)

@ -36,9 +36,10 @@ class Terminal(models.Model):
ordering = ['is_active']
class TerminalHeatbeat(models.Model):
class HeatbeatFailedLog(models.Model):
"""Terminal heatbeat failed log"""
terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'terminal_heatbeat'
db_table = 'heatbeat_failed_log'

@ -3,26 +3,26 @@
from rest_framework import serializers
from .models import Terminal, TerminalHeatbeat
from .models import Terminal, HeatbeatFailedLog
from .hands import ProxyLog
class TerminalSerializer(serializers.ModelSerializer):
proxy_amount = serializers.SerializerMethodField()
proxy_online = serializers.SerializerMethodField()
class Meta:
model = Terminal
fields = ['id', 'name', 'ip', 'type', 'url', 'comment', 'is_active',
'get_type_display', 'proxy_amount']
fields = ['id', 'name', 'ip', 'type', 'url', 'comment',
'is_active', 'get_type_display', 'proxy_online']
@staticmethod
def get_proxy_amount(obj):
def get_proxy_online(obj):
return ProxyLog.objects.filter(terminal=obj.name, is_finished=False).count()
class TerminalHeatbeatSerializer(serializers.ModelSerializer):
class Meta:
model = TerminalHeatbeat
model = HeatbeatFailedLog
fields = ['terminal']

@ -27,8 +27,9 @@
<th class="text-center">{% trans 'Name' %}</th>
<th class="text-center">{% trans 'IP' %}</th>
<th class="text-center">{% trans 'Type' %}</th>
<th class="text-center">{% trans 'proxy_amount' %}</th>
<th class="text-center">{% trans 'proxy online' %}</th>
<th class="text-center">{% trans 'Active' %}</th>
<th class="text-center">{% trans 'Alive' %}</th>
<th class="text-center">{% trans 'Action' %}</th>
</tr>
</thead>
@ -42,6 +43,7 @@
$(document).ready(function(){
var options = {
ele: $('#terminal_list_table'),
buttons: [],
columnDefs: [
{targets: 1, createdCell: function (td, cellData, rowData) {
var detail_btn = '<a href="{% url "users:user-detail" pk=99991937 %}">' + cellData + '</a>';
@ -54,7 +56,14 @@ $(document).ready(function(){
$(td).html('<i class="fa fa-check text-navy"></i>')
}
}},
{targets: 6, createdCell: function (td, cellData, rowData) {
{targets: 6, createdCell: function (td, cellData) {
if (!cellData) {
$(td).html('<i class="fa fa-circle text-danger"></i>')
} else {
$(td).html('<i class="fa fa-circle text-navy"></i>')
}
}},
{targets: 7, createdCell: function (td, cellData, rowData) {
console.log(rowData.name);
var update_btn = '<a href="{% url "terminal:terminal-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'
.replace('99991937', cellData);
@ -64,9 +73,9 @@ $(document).ready(function(){
$(td).html(update_btn + delete_btn)
}}
],
ajax_url: '{% url "terminal:terminal-list-create-api" %}',
ajax_url: '{% url "terminal:api-terminal-list" %}',
columns: [{data: function(){return ""}}, {data: "name" }, {data: "ip" }, {data: "get_type_display" },
{data: "proxy_amount"}, {data: "is_active" }, {data: "id"}],
{data: "proxy_online"}, {data: "is_active" }, {data: 'is_active'}, {data: "id"}],
op_html: $('#actions').html()
};
jumpserver.initDataTable(options);
@ -74,7 +83,7 @@ $(document).ready(function(){
var $this = $(this);
var uid = $this.data('uid');
var name = $(this).data('name');
var the_url = '{% url "terminal:terminal-detail-update-delete-api" pk=99991937 %}'.replace('99991937', uid);
var the_url = '{% url "terminal:api-terminal-detail" pk=99991937 %}'.replace('99991937', uid);
objectDelete($this, name, the_url)
})
</script>

@ -3,6 +3,7 @@
#
from django.conf.urls import url
from rest_framework import routers
import views
import api
@ -14,9 +15,11 @@ urlpatterns = [
url(r'^terminal/(?P<pk>\d+)/update$', views.TerminalUpdateView.as_view(), name='terminal-update'),
]
router = routers.DefaultRouter()
router.register(r'v1/terminal', api.TerminalViewSet, 'api-terminal')
urlpatterns += [
url(r'^v1/terminal/$', api.TerminalCreateListApi.as_view(), name='terminal-list-create-api'),
url(r'^v1/terminal/(?P<pk>\d+)/$', api.TerminalApiDetailUpdateDetailApi.as_view(),
name='terminal-detail-update-delete-api'),
url(r'^v1/terminal-heatbeat/$', api.TerminalHeatbeatApi.as_view(), name='terminal-heatbeat-api'),
url(r'^v1/terminal/heatbeat/$', api.TerminalHeatbeatApi.as_view(), name='terminal-heatbeat-api'),
]
urlpatterns += router.urls

Loading…
Cancel
Save