This commit is contained in:
parent
6aa5af0078
commit
8d21fc09e3
|
@ -0,0 +1,18 @@
|
||||||
|
import json
|
||||||
|
from django.http import HttpResponse
|
||||||
|
from myapp.models import *
|
||||||
|
from myapp.utils import get_platform_info
|
||||||
|
|
||||||
|
def _render_json(data):
|
||||||
|
response = HttpResponse(json.dumps(data, ensure_ascii=False),
|
||||||
|
content_type="application/json; charset=utf-8")
|
||||||
|
return response
|
||||||
|
|
||||||
|
def crawl_info(request):
|
||||||
|
_crawl_info = CrawlInfos.objects.order_by('-id').first()
|
||||||
|
return _render_json(_crawl_info)
|
||||||
|
|
||||||
|
|
||||||
|
def platform_info(request):
|
||||||
|
_info = get_platform_info()
|
||||||
|
return _render_json(_info)
|
|
@ -1,9 +1,13 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
|
from . import apis
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.crawl, name='index'),
|
path('', views.crawl, name='index'),
|
||||||
path('crawl', views.crawl, name='crawl'),
|
path('crawl', views.crawl, name='crawl'),
|
||||||
path('result',views.result, name='result'),
|
path('result', views.result, name='result'),
|
||||||
]
|
path('api/crawl_info.json', apis.crawl_info),
|
||||||
|
path('api/platform_info.json', apis.crawl_info),
|
||||||
|
|
||||||
|
]
|
||||||
|
|
|
@ -1,53 +1,60 @@
|
||||||
#coding=utf-8
|
# coding=utf-8
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
def get_execute_out(cmd):
|
def get_execute_out(cmd):
|
||||||
return subprocess.getoutput(cmd)
|
return subprocess.getoutput(cmd)
|
||||||
|
|
||||||
|
|
||||||
def get_platform_info():
|
def get_platform_info():
|
||||||
if platform.system()=='Linux':
|
if platform.system() == 'Linux':
|
||||||
ls = get_execute_out('top -bn 1 -i -c')
|
ls = get_execute_out('top -bn 1 -i -c')
|
||||||
infos = ls.split('\n')
|
infos = ls.split('\n')
|
||||||
|
|
||||||
info = {}
|
|
||||||
|
|
||||||
m = re.match(r'top -.+?up\s+(.+?),', infos[0])
|
info = {}
|
||||||
if m:
|
|
||||||
info["up_time"] = m.group(1)
|
|
||||||
|
|
||||||
m = re.match(r'Tasks:\s+(.+?)\s+total', infos[1])
|
m = re.match(r'top -.+?up\s+(.+?),', infos[0])
|
||||||
if m:
|
if m:
|
||||||
info["tasks"] = m.group(1)
|
info["up_time"] = m.group(1)
|
||||||
|
|
||||||
m = re.match(r'%Cpu\(s\):\s+(.+?)\s+us,\s+(.+?)\s+sy,.+?ni,\s+(.+?)\s+id,', infos[2])
|
m = re.match(r'Tasks:\s+(.+?)\s+total', infos[1])
|
||||||
if not m:
|
if m:
|
||||||
m = re.match(r'Cpu\(s\):\s+(.+?)%us,\s+(.+?)%sy,.+?ni,\s+(.+?)%id,', infos[2])
|
info["tasks"] = m.group(1)
|
||||||
if m:
|
|
||||||
info["cpu_us"] = m.group(1)
|
|
||||||
info["cpu_sy"] = m.group(2)
|
|
||||||
info["cpu_id"] = m.group(3)
|
|
||||||
|
|
||||||
m = re.match(r'KiB Mem : (\d+)\s+total,\s+(\d+)\s+free,\s+(\d+)\s+used,', infos[3])
|
m = re.match(
|
||||||
if not m:
|
r'%Cpu\(s\):\s+(.+?)\s+us,\s+(.+?)\s+sy,.+?ni,\s+(.+?)\s+id,', infos[2])
|
||||||
m = re.match(r'Mem:\s+(\d+)k\s+total,\s+(\d+)k\s+used,\s+(\d+)k\s+free,', infos[3])
|
if not m:
|
||||||
if m:
|
m = re.match(
|
||||||
info["mem_total"] = m.group(1)
|
r'Cpu\(s\):\s+(.+?)%us,\s+(.+?)%sy,.+?ni,\s+(.+?)%id,', infos[2])
|
||||||
info["mem_free"] = m.group(2)
|
if m:
|
||||||
info["mem_used"] = m.group(3)
|
info["cpu_us"] = m.group(1)
|
||||||
|
info["cpu_sy"] = m.group(2)
|
||||||
|
info["cpu_id"] = m.group(3)
|
||||||
|
|
||||||
#硬盘使用
|
m = re.match(
|
||||||
out = get_execute_out('df -h')
|
r'KiB Mem : (\d+)\s+total,\s+(\d+)\s+free,\s+(\d+)\s+used,', infos[3])
|
||||||
m = re.findall(r'\/dev\/vda1\s+(.+?)G\s+(.+?)G\s+(.+?)G', out)
|
if not m:
|
||||||
if len(m)>0:
|
m = re.match(
|
||||||
info['disk_total'] = m[0][0]
|
r'Mem:\s+(\d+)k\s+total,\s+(\d+)k\s+used,\s+(\d+)k\s+free,', infos[3])
|
||||||
info['disk_used'] = m[0][1]
|
if m:
|
||||||
info['disk_freed'] = m[0][2]
|
info["mem_total"] = m.group(1)
|
||||||
|
info["mem_free"] = m.group(2)
|
||||||
|
info["mem_used"] = m.group(3)
|
||||||
|
|
||||||
return info
|
# 硬盘使用
|
||||||
|
out = get_execute_out('df -h')
|
||||||
|
m = re.findall(r'\/dev\/vda1\s+(.+?)G\s+(.+?)G\s+(.+?)G', out)
|
||||||
|
if len(m) > 0:
|
||||||
|
info['disk_total'] = m[0][0]
|
||||||
|
info['disk_used'] = m[0][1]
|
||||||
|
info['disk_freed'] = m[0][2]
|
||||||
|
|
||||||
|
return info
|
||||||
|
|
||||||
|
return {'up_time': '20:52', 'tasks': '127', 'cpu_us': '1.6', 'cpu_sy': '1.6', 'cpu_id': '96.9', 'mem_total': '12134268', 'mem_free': '5648868', 'mem_used': '4537556'}
|
||||||
|
|
||||||
return {'up_time': '20:52', 'tasks': '127', 'cpu_us': '1.6', 'cpu_sy': '1.6', 'cpu_id': '96.9', 'mem_total': '12134268', 'mem_free': '5648868', 'mem_used': '4537556'}
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(get_platform_info())
|
print(get_platform_info())
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
from django.shortcuts import render
|
|
||||||
from django.http import HttpResponse
|
|
||||||
from . import scrapy_client
|
|
||||||
from myapp.models import CrawlInfos, PlatformInfos, News, LastDayCounts, Datas, Top10Sells
|
|
||||||
from myapp.utils import get_platform_info
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
from myapp.models import CrawlInfos, Datas, LastDayCounts, News, Top10Sells
|
||||||
|
from myapp.utils import get_platform_info
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
return render(request, 'myapp/index.html')
|
return render(request, "myapp/index.html")
|
||||||
|
|
||||||
|
|
||||||
def crawl(request):
|
def crawl(request):
|
||||||
|
@ -41,9 +41,10 @@ def result(request):
|
||||||
key='last_month_sell').first().data)
|
key='last_month_sell').first().data)
|
||||||
|
|
||||||
hour_sell = json.loads(Datas.objects.filter(key='hour_sell').first().data)
|
hour_sell = json.loads(Datas.objects.filter(key='hour_sell').first().data)
|
||||||
sentiments = json.loads(Datas.objects.filter(key='sentiments').first().data)
|
sentiments = json.loads(Datas.objects.filter(
|
||||||
|
key='sentiments').first().data)
|
||||||
return render(request, 'myapp/result.html', {"from_type_info": from_type_info,
|
return render(request, 'myapp/result.html', {"from_type_info": from_type_info,
|
||||||
"top10_sells": top10_sells,
|
"top10_sells": top10_sells,
|
||||||
"last_month_sell": last_month_sell,
|
"last_month_sell": last_month_sell,
|
||||||
"hour_sell": hour_sell,
|
"hour_sell": hour_sell,
|
||||||
"sentiments": sentiments})
|
"sentiments": sentiments})
|
||||||
|
|
Loading…
Reference in New Issue