-
Notifications
You must be signed in to change notification settings - Fork 1
/
statswebsocket.py
43 lines (35 loc) · 1.08 KB
/
statswebsocket.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
from datetime import datetime, timedelta
from tinydb import TinyDB, Query
from unicodedata import normalize
import psutil
import json
import os
db = TinyDB('src/assets/statistics.json')
query = Query()
def keepItFresh():
time = datetime.now()
ids = list()
for i in iter(db):
t = datetime.strptime(normalize('NFKD',json.loads(i['time'])).encode('ascii','ignore'), '%Y-%m-%d %H:%M:%S.%f')
res = time - t
if (res.total_seconds() >= 30) and len(db) > 15:
ids.append(i.doc_id)
db.remove(doc_ids=ids)
def main():
cpu = dict()
rest = dict()
cpu['time'] = json.dumps(str(datetime.now()))
cpu['percent'] = psutil.cpu_percent(interval=1)
rest['memory'] = psutil.virtual_memory().percent
rest['disk'] = psutil.disk_usage('/').percent
rest['swap'] = psutil.swap_memory().percent
if len(db) > 15:
keepItFresh()
db.insert(cpu)
else:
db.insert(cpu)
os.chdir('src/assets')
with open("rest.json","w") as outfile:
json.dump(rest, outfile)
if __name__ == "__main__":
main()