-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReadThread.py
More file actions
198 lines (183 loc) · 7.85 KB
/
Copy pathReadThread.py
File metadata and controls
198 lines (183 loc) · 7.85 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from PyQt5.QtCore import QThread, pyqtSignal
from rdsLoglib import Data, ErrorLine, WarningLine, ReadLog, FatalLine, NoticeLine, Service, RobotStatus, Memory
from datetime import timedelta
from datetime import datetime
import os
import json as js
import logging
import math
import time
def decide_old_imu(gx,gy,gz):
for v in gx:
if abs(round(v) - v) > 1e-5:
return True
for v in gy:
if abs(round(v) - v) > 1e-5:
return True
for v in gz:
if abs(round(v) - v) > 1e-5:
return True
return False
def rad2LSB(data):
new_data = [v/math.pi*180.0*16.4 for v in data]
return new_data
def Fdir2Flink(f):
flink = " <a href='file:///" + f + "'>"+f+"</a>"
return flink
def printData(data, fid):
try:
print(data, file= fid)
except UnicodeEncodeError:
data = data.encode(errors='ignore')
print(data, file= fid)
return
class ReadThread(QThread):
signal = pyqtSignal('PyQt_PyObject')
def __init__(self):
QThread.__init__(self)
self.filenames = []
self.log_config = "log_config.json"
self.js = dict()
self.content = dict()
self.err = ErrorLine()
self.war = WarningLine()
self.fatal = FatalLine()
self.notice = NoticeLine()
self.service = Service()
self.filtered_service = dict()
self.rstatus = RobotStatus()
self.memory = Memory()
self.log = []
self.tlist = []
self.cpu_num = 4
self.reader = None
self.data_keys = set()
self.group_keys = set()
self.robot_keys = []
self.service_keys = set()
try:
f = open('rds_log_config.json',encoding= 'UTF-8')
self.js = js.load(f)
except FileNotFoundError:
logging.error('Failed to open log_config.json')
self.log.append('Failed to open log_config.json')
# run method gets called when we start the thread
def run(self):
"""读取log"""
#初始化log数据
try:
f = open(self.log_config,encoding= 'UTF-8')
self.js = js.load(f)
f.close()
logging.error("Load {}".format(self.log_config))
self.log.append("Load {}".format(self.log_config))
except FileNotFoundError:
logging.error("Failed to open {}".format(self.log_config))
self.log.append("Failed to open {}".format(self.log_config))
self.content = dict()
content_delay = dict()
for k in self.js:
if "type" in self.js[k] and "content" in self.js[k]:
if isinstance(self.js[k]['type'], list):
for type in self.js[k]["type"]:
self.content[type] = Data(self.js[k], type)
elif isinstance(self.js[k]['type'], str):
self.content[self.js[k]['type']] = Data(self.js[k], self.js[k]['type'])
self.err = ErrorLine()
self.war = WarningLine()
self.fatal = FatalLine()
self.notice = NoticeLine()
self.service = Service()
self.memory = Memory()
self.tlist = []
self.log = []
if self.filenames:
self.reader = ReadLog(self.filenames)
self.reader.thread_num = self.cpu_num
time_start=time.time()
self.reader.parse(self.content, self.err,
self.war, self.fatal, self.notice, self.service,
self.rstatus, self.memory)
time_end=time.time()
self.log.append('read time cost: ' + str(time_end-time_start))
# self.content.update(content_delay)
tmax = self.reader.tmax
tmin = self.reader.tmin
if tmax is None or tmin is None:
logging.error("Failed to analysis {}".format(self.filenames))
self.tlist = [tmin, tmax]
#save Error
ts = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
output_fname = "Report_" + str(ts).replace(':','-').replace(' ','_') + ".txt"
path = os.path.dirname(self.filenames[0])
output_fname = path + "/" + output_fname
self.log.append("Report File:" + Fdir2Flink(output_fname))
robot_name = set()
self.data_keys = dict()
self.group_keys = set()
robot_name.add("global") # 没有车子的key
for k in self.content.keys():
for robot in self.content[k].data.keys():
robot_name.add(robot)
self.group_keys.add(k)
for name in self.content[k].data[robot].keys():
if name != 't' and name[0] != '_':
if len(self.content[k].data[robot][name]) > 0 \
and (isinstance(self.content[k].data[robot][name][0], int) \
or isinstance(self.content[k].data[robot][name][0], float)) \
or isinstance(self.content[k].data[robot][name][0], str):
if robot not in self.data_keys:
self.data_keys[robot] = set()
self.data_keys[robot].add(k+'.'+name)
self.group_keys.add("memory")
if "global" not in self.data_keys:
self.data_keys["global"] = set()
self.data_keys["global"].update({"memory.used_sys",
"memory.free_sys", "memory.rbk_phy",
"memory.rbk_vir","memory.rbk_max_phy",
"memory.rbk_max_vir","memory.rbk_cpu","memory.sys_cpu"})
self.robot_keys = list(robot_name)
for r in self.data_keys:
self.data_keys[r] = sorted(self.data_keys[r])
self.group_keys = sorted(self.group_keys)
self.service_keys = sorted(set(self.service.service_name()))
fid = open(output_fname,"w")
print("="*20, file = fid)
print("Files: ", file = fid)
for f in self.filenames:
print(f, file = fid)
print(len(self.fatal.content()[0]), " FATALs, ", len(self.err.content()[0]), " ERRORs, ",
len(self.war.content()[0]), " WARNINGs, ", len(self.notice.content()[0]), " NOTICEs", file = fid)
self.log.append(str(len(self.fatal.content()[0])) + " FATALs, " + str(len(self.err.content()[0])) +
" ERRORs, " + str(len(self.war.content()[0])) + " WARNINGs, " + str(len(self.notice.content()[0])) + " NOTICEs")
print("FATALs:", file = fid)
for data in self.fatal.content()[0]:
printData(data, fid)
print("ERRORs:", file = fid)
for data in self.err.content()[0]:
printData(data, fid)
print("WARNINGs:", file = fid)
for data in self.war.content()[0]:
printData(data, fid)
print("NOTICEs:", file = fid)
for data in self.notice.content()[0]:
printData(data, fid)
fid.close()
self.signal.emit(self.filenames)
def getData(self, vkey):
if vkey in self.data:
if not self.data[vkey][0]:
if vkey in self.data_org_key:
org_key = self.data_org_key[vkey]
if not self.content[org_key].parsed_flag:
# time_start=time.time()
self.content[org_key].parse_now(self.reader.lines)
# time_end=time.time()
# print('real read time cost: ' + str(time_end-time_start))
tmp = vkey.split(".")
k = tmp[0]
name = tmp[1]
self.data[vkey] = (self.content[k][name], self.content[k]['t'])
return self.data[vkey]
else:
return [[],[]]