Skip to content

Commit

Permalink
adding PingCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
thanasipantazides committed Jan 4, 2025
1 parent 4b4e944 commit a4e894c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
50 changes: 50 additions & 0 deletions collections/PingCollection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Ping collection to handle the read-in Formatter ping data.
"""


class PingCollection:
"""
A container for ping data after being parsed.
Parameters
---------
parsed_data : `tuple`, length 2
Contains `error`, `data`, as returned from the parser.
old_data_time : `int`, `float`
The last time of the last data point previously extracted and
used. Default is `0` and so should take all data.
Default: 0
Example
-------
with BackwardsReader(file=self.data_file, blksize=self.buffer_size, forward=True) as f:
data = f.read_block()
error, data = Pingparser(raw_data)
ping_data = PingCollection((error, data))
plt.figure()
ping_data.plot_trace()
plt.show()
"""

def __init__(self, parsed_data, old_data_time=0):
# bring in the parsed data
self.output, self.error_flag = parsed_data

# used in the filter to only consider data with times > than this
self.last_data_time = old_data_time

def get_unixtime(self):
return self.output["unixtime"]

def get_global_errors_int(self):
return self.output["global_errors_int"]

def get_all_systems_state(self):
return {k: self.output[k]["system_state_str"] for k in self.output.keys() if type(k) is int}

def get_all_systems_error(self):
return {k: self.output[k]["system_error_str"] for k in self.output.keys() if type(k) is int}
8 changes: 4 additions & 4 deletions parsers/Pingparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def pingparser(data: bytes):
return {}, error_flag

unixtime_raw = int.from_bytes(data[0:4], "big")
global_errors_raw = int.from_bytes(data[5:6], "big")
global_errors_int = int.from_bytes(data[5:6], "big")

nsys = (len(data) - 6) // 4 # count systems in the rest of the packet
result = {"unixtime": unixtime_raw, "global_errors_int": global_errors_raw}
result = {"unixtime": unixtime_raw, "global_errors_int": global_errors_int}

for k in range(nsys): # k indexes systems
i = 6 + 4*k # i indexes the data array
Expand Down Expand Up @@ -133,8 +133,8 @@ def chunks(lst, n):

data = f.read()
for chunk in chunks(data, 46):
p = pingparser(chunk)
pprint(p[0][9]['system_error_str']['reading_packet'])
p, e = pingparser(chunk)
pprint(p)

else:
print("use like this:\n\t> python parsers/Pingparser.py\n\t\tor\n\t>python parsers/Pingparser.py path/to/ping/file.log")

0 comments on commit a4e894c

Please sign in to comment.