Update terminal interval

pull/530/head
ibuler 2016-11-11 02:13:13 +08:00
parent 10aa8c40a7
commit c6fc3dfe91
6 changed files with 60 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -27,8 +27,9 @@
<th class="text-center">{% trans 'Name' %}</th> <th class="text-center">{% trans 'Name' %}</th>
<th class="text-center">{% trans 'IP' %}</th> <th class="text-center">{% trans 'IP' %}</th>
<th class="text-center">{% trans 'Type' %}</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 'Active' %}</th>
<th class="text-center">{% trans 'Alive' %}</th>
<th class="text-center">{% trans 'Action' %}</th> <th class="text-center">{% trans 'Action' %}</th>
</tr> </tr>
</thead> </thead>
@ -42,6 +43,7 @@
$(document).ready(function(){ $(document).ready(function(){
var options = { var options = {
ele: $('#terminal_list_table'), ele: $('#terminal_list_table'),
buttons: [],
columnDefs: [ columnDefs: [
{targets: 1, createdCell: function (td, cellData, rowData) { {targets: 1, createdCell: function (td, cellData, rowData) {
var detail_btn = '<a href="{% url "users:user-detail" pk=99991937 %}">' + cellData + '</a>'; 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>') $(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); console.log(rowData.name);
var update_btn = '<a href="{% url "terminal:terminal-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>' var update_btn = '<a href="{% url "terminal:terminal-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'
.replace('99991937', cellData); .replace('99991937', cellData);
@ -64,9 +73,9 @@ $(document).ready(function(){
$(td).html(update_btn + delete_btn) $(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" }, 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() op_html: $('#actions').html()
}; };
jumpserver.initDataTable(options); jumpserver.initDataTable(options);
@ -74,7 +83,7 @@ $(document).ready(function(){
var $this = $(this); var $this = $(this);
var uid = $this.data('uid'); var uid = $this.data('uid');
var name = $(this).data('name'); 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) objectDelete($this, name, the_url)
}) })
</script> </script>

View File

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