2016-10-16 14:12:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2016-11-13 14:34:38 +00:00
|
|
|
from django.utils import timezone
|
2016-10-16 14:12:13 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2016-11-13 14:34:38 +00:00
|
|
|
from .models import Terminal, TerminalHeatbeat
|
2016-10-19 10:33:14 +00:00
|
|
|
from .hands import ProxyLog
|
2016-10-16 14:12:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TerminalSerializer(serializers.ModelSerializer):
|
2016-11-10 18:13:13 +00:00
|
|
|
proxy_online = serializers.SerializerMethodField()
|
2016-11-13 14:34:38 +00:00
|
|
|
is_alive = serializers.SerializerMethodField()
|
2016-10-19 10:33:14 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
class Meta:
|
|
|
|
model = Terminal
|
2017-01-17 08:34:47 +00:00
|
|
|
fields = ['id', 'name', 'remote_addr', 'type', 'url', 'comment',
|
|
|
|
'is_accepted', 'is_active', 'get_type_display',
|
|
|
|
'proxy_online', 'is_alive']
|
2016-10-19 10:33:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-11-10 18:13:13 +00:00
|
|
|
def get_proxy_online(obj):
|
2016-10-19 10:33:14 +00:00
|
|
|
return ProxyLog.objects.filter(terminal=obj.name, is_finished=False).count()
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2016-11-13 14:34:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_is_alive(obj):
|
|
|
|
log = obj.terminalheatbeat_set.last()
|
2016-11-27 15:36:35 +00:00
|
|
|
if log and timezone.now() - log.date_created < timezone.timedelta(seconds=600):
|
2016-11-13 14:34:38 +00:00
|
|
|
return True
|
2016-11-27 15:36:35 +00:00
|
|
|
else:
|
|
|
|
return False
|
2016-11-13 14:34:38 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2016-10-18 15:49:04 +00:00
|
|
|
class TerminalHeatbeatSerializer(serializers.ModelSerializer):
|
2017-01-17 08:34:47 +00:00
|
|
|
date_start = serializers.DateTimeField
|
2016-10-18 15:49:04 +00:00
|
|
|
class Meta:
|
2016-11-13 14:34:38 +00:00
|
|
|
model = TerminalHeatbeat
|
2016-10-18 15:49:04 +00:00
|
|
|
|
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
pass
|