-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_data_work.py
131 lines (120 loc) · 3.77 KB
/
server_data_work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# coding=utf-8
# 服务端
from flask import Flask,request
from concurrent.futures import ThreadPoolExecutor
import sqlite3
from threading import Lock
import time
import json
from bananainstall import create_database,find_db,get_dblist
app=Flask(__name__)
pool = ThreadPoolExecutor(100)
lock=Lock()
@app.route('/api/',methods=['POST'])
def receive_api():
json_data=request.json
# pool.submit(thread_sql,json_data)
return thread_sql(json_data)
# if json_data['type']=='second':
# return thread_sql(json_data)
# elif json_data['type']=='hour':
# pass
# else:
# return 'data error'
@app.route('/api/history/',methods=['POST'])
def receive_history_api():
json_data=request.json
return thread_history_sql(json_data)
def thread_sql(json_data):
# lock.acquire()
# 加锁不会丢数据 一条4秒;不加锁丢一半数据,一条2秒
# print('-'*60+json_data['id']+'-'*20)
try:
id=json_data['id']
t1 = (str(json_data['id']),)
t2=(json_data['platform'],
json_data['ip'],
str(json_data['updatetime']),
json_data['cpu'],
json.dumps(json_data['mem']),
"")
# json.dumps(json_data['disk']))
if find_db(id):
conn = sqlite3.connect('db/db'+id+'.db',timeout=1)
c = conn.cursor()
sql='''UPDATE realtime SET
platform=?,
ip=?,
updatetime=?,
cpu=?,
mem=?,
disk=?
WHERE id=?'''
c.execute(sql,t2+t1)
else:
create_database(id)
conn = sqlite3.connect('db/db'+id+'.db',timeout=1)
c = conn.cursor()
sql='''INSERT INTO realtime VALUES (?,?,?,?,?,?,?,?,?)'''
c.execute(sql,t1+t2+('',''))
conn.commit()
conn.close()
return 'True'
except Exception as e:
conn.rollback()
conn.close()
print(type(e))
print(e)
return 'False'
finally:
pass
# lock.release()
def thread_history_sql(json_data):
# 向历史表格插入数据
# lock.acquire()
# 加锁不会丢数据 一条4秒;不加锁丢一半数据,一条2秒
# print('-'*60+json_data['id']+'-'*20)
try:
id=json_data['id']
t1 = (str(json_data['id']),)
t2=(str(json_data['updatetime']),
json_data['cpu'],
json_data['mem'],
json_data['disk'],
json_data['net'],)
if find_db(id):
conn = sqlite3.connect('db/db'+id+'.db',timeout=1)
c = conn.cursor()
real_res=c.execute('SELECT * FROM history WHERE id=?', t1).fetchone()
if real_res:
tmp=[]
for i in range(1,6):
tmp.append(str(eval(real_res[i])[1:]+[t2[i-1]])) # so bad
sql='''UPDATE history SET
updatetime=?,
cpu=?,
mem=?,
disk=?,
net=?
WHERE id=?'''
c.execute(sql,tuple(tmp)+t1)
else:
sql='''INSERT INTO history VALUES (?,?,?,?,?,?)'''
tmp=[0]*299 # 999 测试时存10条,生产环境1000
c.execute(sql,t1+tuple([str(tmp+[i]) for i in t2]))
conn.commit()
conn.close()
return 'True'
else:
return 'False',500
except IOError as e:
conn.rollback()
conn.close()
print(type(e))
print(e)
return 'False'
finally:
pass
# lock.release()
if __name__=='__main__':
app.run(host='127.0.0.1',port=5000)