diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index cd9a44a..e442e00 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'rest_framework', 'corsheaders', # coreapi生成接口文档 'rest_framework.authtoken', # Token验证应用 + 'django_filters', 'update', ] @@ -93,7 +94,6 @@ CACHES = { 'BACKEND': 'django_redis.cache.RedisCache', # "BACKEND": "django.core.cache.backends.redis.RedisCache", "LOCATION": "redis://127.0.0.1:6379", - "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", @@ -102,8 +102,10 @@ CACHES = { } REST_FRAMEWORK_EXTENSIONS = { - 'DEFAULT_CACHE_RESPONSE_TIMEOUT': 10 * 5, # 缓存时间 + 'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 5, # 缓存时间 'DEFAULT_USE_CACHE': 'default', # 缓存存储 + 'DEFAULT_OBJECT_CACHE_KEY_FUNC': 'rest_framework_extensions.utils.default_object_cache_key_func', + 'DEFAULT_LIST_CACHE_KEY_FUNC': 'rest_framework_extensions.utils.default_list_cache_key_func', } SESSION_ENGINE = 'django.contrib.sessions.backends.cache' # 使用Redis做为session缓存 @@ -193,7 +195,8 @@ LOGGING = { REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework.authentication.TokenAuthentication',), # 配置验证方式 - 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' # 配置接口文档 + 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', # 配置接口文档 + 'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'], } # 跨域配置 diff --git a/update/urls.py b/update/urls.py index 2ae2dc1..b3bfed9 100644 --- a/update/urls.py +++ b/update/urls.py @@ -22,7 +22,7 @@ router = DefaultRouter() router.register('cinema', views.CinemaViewSet, 'cinema') urlpatterns = [ - path('cinema/search/', views.CinemaSearchAPIView.as_view()), + # path('cinema/search/', views.CinemaSearchAPIView.as_view()), path('', include(router.urls)), # re_path(r'^cinema/search/ip/(?P[0-9.]*?)/version/(?P[\S]*?)/$', views.CinemaSearchAPIView.as_view()) diff --git a/update/utils/__init__.py b/update/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/update/utils/get_version.py b/update/utils/get_version.py new file mode 100644 index 0000000..d206153 --- /dev/null +++ b/update/utils/get_version.py @@ -0,0 +1,79 @@ +import pymysql +from pymysql.cursors import DictCursor +from multiprocessing import cpu_count +from update.models import Cinema +import queue +import threading + + +class GetVersion(object): + def __init__(self, cinema_ip_list=None): + self.cinema_ip_list = cinema_ip_list + self.queue = queue.Queue() # 注册队列 + self.th_num = 0 # 获取CPU核心数量,-1后座位处理逻辑的线程数量 + + def get_all_cinema(self): + print('get_all_cinema') + if self.cinema_ip_list is None: + all_cinema_obj = Cinema.objects.all() + else: + all_cinema_obj = Cinema.objects.filter(ip__in=self.cinema_ip_list) + for cinema_obj in all_cinema_obj: + print(cinema_obj) + data = { + 'ip': cinema_obj.ip, + 'db_user': cinema_obj.db_user, + 'db_pwd': cinema_obj.db_pwd + } + self.queue.put(data) + cpu_num = cpu_count() - 1 if cpu_count() > 1 else 1 + cinema_num = len(all_cinema_obj) + self.th_num = cpu_num if cpu_num < cinema_num else cinema_num + print(self.queue) + + def main_process(self): + print('main_process') + self.get_all_cinema() + threads = [] + for i in range(self.th_num): + t = threading.Thread(target=self.get_cinema_ver, args=(self.queue,)) + threads.append(t) + for i in range(self.th_num): + threads[i].start() + for i in range(self.th_num): + threads[i].join() + + @staticmethod + def get_cinema_ver(q): + print('get_cinema_ver') + if q.empty(): + return + else: + cinema_config = q.get() + print(cinema_config) + sql_str = "SELECT * FROM cinema_version;" + ip = cinema_config.get('ip') + db_config = { + 'host': ip, + 'user': cinema_config.get('db_user'), + 'password': cinema_config.get('db_pwd'), + 'database': 'cine', + 'connect_timeout': 5, + } + print(db_config) + db_conn = pymysql.Connect(**db_config) + db_cursor = db_conn.cursor(cursor=DictCursor) + db_cursor.execute(sql_str) + res = db_cursor.fetchone() + print(res) + update_data = { + 'sys_ver': res['server_version'], + 'client_ver': res['client_version'] + } + print(update_data) + Cinema.objects.filter(ip=ip).update(**update_data) + + +if __name__ == '__main__': + v = GetVersion() + v.get_all_cinema() diff --git a/update/views.py b/update/views.py index 61a4bf9..1833889 100644 --- a/update/views.py +++ b/update/views.py @@ -9,6 +9,8 @@ from rest_framework_extensions.cache.mixins import CacheResponseMixin from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from django_filters.rest_framework import DjangoFilterBackend +from django.core.cache import cache +from update.utils.get_version import GetVersion # CacheResponseMixin 一定要放第一位 @@ -25,16 +27,17 @@ class CinemaViewSet(CacheResponseMixin, viewsets.ModelViewSet): serializer_class = CinemaSerializer permission_classes = (permissions.IsAuthenticated,) filter_backends = (filters.SearchFilter, DjangoFilterBackend) - search_fields = ('ip', 'sys_ver') - # filter_fields = ('ip', 'sys_ver') + search_fields = ('sys_ver',) + # filter_fields = ('ip',) + filterset_fields = ('ip',) + GetVersion().main_process() - -class CinemaSearchAPIView(APIView, CacheResponseMixin): - @method_decorator(cache_page(60 * 5)) - def get(self, request, *args, **kwargs): - query_params = request.query_params.dict() - print(query_params) - query_data = Cinema.objects.filter( - Q(ip__contains=query_params.get('ip')) & Q(sys_ver__icontains=query_params.get('version'))) - serializer = CinemaSerializer(instance=query_data, many=True) - return Response(serializer.data, status=status.HTTP_200_OK) +# class CinemaSearchAPIView(APIView, CacheResponseMixin): +# @method_decorator(cache_page(60 * 5)) +# def get(self, request, *args, **kwargs): +# query_params = request.query_params.dict() +# print(query_params) +# query_data = Cinema.objects.filter( +# Q(ip__contains=query_params.get('ip')) & Q(sys_ver__icontains=query_params.get('version'))) +# serializer = CinemaSerializer(instance=query_data, many=True) +# return Response(serializer.data, status=status.HTTP_200_OK)