Skip to content

Commit

Permalink
update db explorer and clean up database size
Browse files Browse the repository at this point in the history
  • Loading branch information
sequoia-hope committed Feb 8, 2024
1 parent afa3a96 commit a0d251f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 84 deletions.
205 changes: 121 additions & 84 deletions reporting/db_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
print("Options:")
print("1: Explore Database")
print("2: Import File")
print("3: Clean Database")
selection = int(input("Enter selection: "))

if selection == 2:
Expand Down Expand Up @@ -95,14 +96,17 @@
pathsection_list = []
robot_list = []
gpspolygon_list = []
energy_segment_list = []
for key in r.scan_iter():
# if 'acorn_test' in key.decode('utf-8') and 'robot' in key.decode('utf-8'):
# r.delete(key)
key_list.append(key.decode('utf-8'))
split_key = key.decode('utf-8').split(":")
if(len(split_key) < 3):
continue
if len(split_key[0]) > 0:
site_list.append(split_key[0])
if split_key[1]=="robot":
if split_key[1]=="robot" and split_key[3]!="energy_segment":
robot_list.append(split_key[2])
elif split_key[1]=="gpspolygon":
gpspolygon_list.append(split_key[2])
Expand All @@ -113,99 +117,132 @@
if isinstance(path[0], PathSection):
pathsection_list.append(split_key[2])
path_list.append(split_key[2])
elif split_key[3]=="energy_segment":
energy_segment_list.append(key.decode('utf-8'))

# print(type(pickle.loads(r.get(key))))
# for key in key_list:
# print(key)
site_list = list(set(site_list))
robot_list = list(set(robot_list))
gpspolygon_list = list(set(gpspolygon_list))

print(site_list)
print(robot_list)
print()
selector_index = 0
selectable_paths = []
for path in path_list:
selector_index += 1
selectable_paths.append(path)
if path in pathsection_list:
print(f"{selector_index}:(P) {path}")
else:
print(f"{selector_index}: {path}")
print()
pathnum = int(input("Enter a path number: "))
path_name = selectable_paths[pathnum-1]
key_tuple = (site_list[0], 'gpspath', path_name, 'key')

pathkey = ":".join(key_tuple)
print(f"Loading path with key: {pathkey}")
path = pickle.loads(r.get(pathkey))

print()
if isinstance(path[0], PathSection):
point_count = 0
for section in path:
point_count += len(section.points)
print(f"Path opened. Path has {len(path)} PathSection elements with a total of {point_count} points.")
else:
print(f"Path opened. Path has {len(path)} list elements.")
print()

print("What would you like to do?")
print("1: Sample (print first item)")
print("2: Export to disk")
print("3: Print in full")
print("4: Delete path")
print("5: Fix Control Values")

if selection == 1:
site_list = list(set(site_list))
robot_list = list(set(robot_list))
gpspolygon_list = list(set(gpspolygon_list))

print(site_list)
print(robot_list)
# print(energy_segment_list)
print()
selector_index = 0
selectable_paths = []
for path in path_list:
selector_index += 1
selectable_paths.append(path)
if path in pathsection_list:
print(f"{selector_index}:(P) {path}")
else:
print(f"{selector_index}: {path}")
print()
pathnum = int(input("Enter a path number: "))
path_name = selectable_paths[pathnum-1]
key_tuple = (site_list[0], 'gpspath', path_name, 'key')

selection = int(input("Enter selection: "))
pathkey = ":".join(key_tuple)
print(f"Loading path with key: {pathkey}")
path = pickle.loads(r.get(pathkey))

if(selection==1):
print()
if isinstance(path[0], PathSection):
for item in path:
print("--------------------------------------------------")
print(item)
else:
print(path[0])

if(selection==2):
# print(path[0])
date_string = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
filename = f"{path_name}_{date_string}.pickle"
with open(filename, 'wb') as handle:
pickle.dump(path, handle, protocol=pickle.HIGHEST_PROTOCOL)
print(f"Saved file as {filename}")

if(selection==3):
print(path)

if(selection==4):
print(f" WARNING! This will delete path {pathkey}.")
selection = input(f"Delete the key? y/(n): ")
if selection == "y":
r.delete(pathkey)
print("Path deleted! Exiting.")
point_count = 0
for section in path:
point_count += len(section.points)
print(f"Path opened. Path has {len(path)} PathSection elements with a total of {point_count} points.")
else:
print("Nothing changed. Exiting.")
sys.exit()
print(f"Path opened. Path has {len(path)} list elements.")
print()

if(selection==5):
if isinstance(path[0], PathSection):
path[0].control_values.lateral_p *= -1
path[0].control_values.lateral_d *= -1
for idx in range(len(path)):
path[idx].control_values = path[0].control_values
# item.control_values.lateral_d *= -1
print(path[idx])
selection = input("Values shown above will be committed to database. Continue? y/(n): ")
print("What would you like to do?")
print("1: Sample (print first item)")
print("2: Export to disk")
print("3: Print in full")
print("4: Delete path")
print("5: Fix Control Values")


selection = int(input("Enter selection: "))

if(selection==1):
if isinstance(path[0], PathSection):
for item in path:
print("--------------------------------------------------")
print(item)
else:
print(path[0])

if(selection==2):
# print(path[0])
date_string = datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
filename = f"{path_name}_{date_string}.pickle"
with open(filename, 'wb') as handle:
pickle.dump(path, handle, protocol=pickle.HIGHEST_PROTOCOL)
print(f"Saved file as {filename}")

if(selection==3):
print(path)

if(selection==4):
print(f" WARNING! This will delete path {pathkey}.")
selection = input(f"Delete the key? y/(n): ")
if selection == "y":
r.set(pathkey,pickle.dumps(path))
r.bgsave()
print("Changes saved to database! Exiting.")
r.delete(pathkey)
print("Path deleted! Exiting.")
else:
print("Nothing changed. Exiting.")
sys.exit()
else:
print(path[0])

if(selection==5):
if isinstance(path[0], PathSection):
path[0].control_values.lateral_p *= -1
path[0].control_values.lateral_d *= -1
for idx in range(len(path)):
path[idx].control_values = path[0].control_values
# item.control_values.lateral_d *= -1
print(path[idx])
selection = input("Values shown above will be committed to database. Continue? y/(n): ")
if selection == "y":
r.set(pathkey,pickle.dumps(path))
r.bgsave()
print("Changes saved to database! Exiting.")
else:
print("Nothing changed. Exiting.")
sys.exit()
else:
print(path[0])

if selection == 3:
print("Clean Database Selected.")
# print(energy_segment_list)
# key = energy_segment_list[0]
# r.rename(key,"energy_segment_old")
newkey = 'twistedfields:robot:acorn1:energy_segment:key'
# r.delete(newkey)
# sys.exit()
oldkey = "energy_segment_old"
# r.delete(oldkey)
sys.exit()
# list_length = r.llen(oldkey)
# print(f"List Length: {list_length}")
# last_seq = 0
# last_stamp = 0
# new_list_length = 0
# for idx in range(list_length):
# segment = pickle.loads(r.lindex(oldkey, idx))
# if segment.sequence_num != last_seq and segment.time_stamp != last_stamp:
# last_seq = segment.sequence_num
# last_stamp = segment.time_stamp
# # print(last_seq)
# r.rpush(newkey, pickle.dumps(segment))
# new_list_length+=1
# percent_complete = idx/list_length * 100
# print(f"new_list_length {new_list_length}, idx {idx}, {percent_complete:.2f}")
# print(segment.sequence_num)
# print(segment.time_stamp)
Binary file added server/dump.rdb
Binary file not shown.

0 comments on commit a0d251f

Please sign in to comment.