Skip to content

Commit

Permalink
fix import script
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Dec 7, 2023
1 parent f6bcc7e commit d122a94
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 56 deletions.
2 changes: 2 additions & 0 deletions migration_scripts/import_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ tar -xzf $ARCHIVE_NAME

# rename the sqlite .backup
mv /home/pioreactor/.pioreactor/storage/pioreactor.sqlite.backup /home/pioreactor/.pioreactor/storage/pioreactor.sqlite
touch /home/pioreactor/.pioreactor/storage/pioreactor.sqlite-shm
touch /home/pioreactor/.pioreactor/storage/pioreactor.sqlite-wal

# check integrity, quickly
DB_CHECK=$(sqlite3 /home/pioreactor/.pioreactor/storage/pioreactor.sqlite "PRAGMA quick_check;")
Expand Down
55 changes: 20 additions & 35 deletions pioreactor/actions/pump_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ def which_pump_are_you_calibrating() -> tuple[str, Callable]:
waste_name = decode(cache["waste"], type=structs.WastePumpCalibration).name

if has_alt_media:
alt_media_timestamp = decode(
cache["alt_media"], type=structs.AltMediaPumpCalibration
).created_at
alt_media_timestamp = decode(cache["alt_media"], type=structs.AltMediaPumpCalibration).created_at
alt_media_name = decode(cache["alt_media"], type=structs.AltMediaPumpCalibration).name

echo(green(bold("Step 1")))
Expand Down Expand Up @@ -168,9 +166,7 @@ def setup(pump_type: str, execute_pump: Callable, hz: float, dc: float, unit: st
try:
channel_pump_is_configured_for = config.get("PWM_reverse", pump_type)
except KeyError:
echo(
f"❌ {pump_type} is not present in config.ini. Please add it to the [PWM] section and try again."
)
echo(f"❌ {pump_type} is not present in config.ini. Please add it to the [PWM] section and try again.")
raise click.Abort()
clear()
echo()
Expand All @@ -179,16 +175,21 @@ def setup(pump_type: str, execute_pump: Callable, hz: float, dc: float, unit: st
echo("1. Fill a container with water.")
echo("2. Submerge both ends of the pump's tubes into the water.")
echo(
"Make sure the pump's power is connected to "
+ bold(f"PWM channel {channel_pump_is_configured_for}.")
"Make sure the pump's power is connected to " + bold(f"PWM channel {channel_pump_is_configured_for}.")
)
echo(
"Run the pumps continuously until the tubes are completely filled with water and there are no air pockets in the tubes."
)
echo("We'll run the pumps continuously until the tubes are completely filled with water.")
echo()

while not confirm(green("Ready to start pumping?")):
pass

echo(bold("Press CTRL+C when the tubes are completely filled with water."))
echo(
bold(
"Press CTRL+C when the tubes are completely filled with water and there are no air pockets in the tubes."
)
)

try:
execute_pump(
Expand Down Expand Up @@ -236,9 +237,7 @@ def choose_settings() -> tuple[float, float]:
return hz, dc


def plot_data(
x, y, title, x_min=None, x_max=None, interpolation_curve=None, highlight_recent_point=True
):
def plot_data(x, y, title, x_min=None, x_max=None, interpolation_curve=None, highlight_recent_point=True):
import plotext as plt # type: ignore

plt.clf()
Expand Down Expand Up @@ -285,9 +284,7 @@ def run_tests(
)

results: list[float] = []
durations_to_test = (
[min_duration] * 4 + [(min_duration + max_duration) / 2] * 2 + [max_duration] * 4
)
durations_to_test = [min_duration] * 4 + [(min_duration + max_duration) / 2] * 2 + [max_duration] * 4
n_samples = len(durations_to_test)

for i, duration in enumerate(durations_to_test):
Expand All @@ -309,9 +306,7 @@ def run_tests(
"We will run the pump for a set amount of time, and you will measure how much liquid is expelled."
)
echo("Use a small container placed on top of an accurate weighing scale.")
echo(
"Hold the end of the outflow tube above so the container catches the expelled liquid."
)
echo("Hold the end of the outflow tube above so the container catches the expelled liquid.")
echo()
echo(
green(
Expand Down Expand Up @@ -461,9 +456,7 @@ def pump_calibration(min_duration: float, max_duration: float, json_file: str |
default=False,
)

durations, volumes = run_tests(
execute_pump, hz, dc, min_duration, max_duration, pump_type, unit
)
durations, volumes = run_tests(execute_pump, hz, dc, min_duration, max_duration, pump_type, unit)
else:
durations, volumes, hz, dc = get_data_from_data_file(json_file)

Expand Down Expand Up @@ -509,13 +502,9 @@ def pump_calibration(min_duration: float, max_duration: float, json_file: str |

# check parameters for problems
if correlation(durations, volumes) < 0:
logger.warning(
"Correlation is negative - you probably want to rerun this calibration..."
)
logger.warning("Correlation is negative - you probably want to rerun this calibration...")
if std_slope > 0.04:
logger.warning(
"Too much uncertainty in slope - you probably want to rerun this calibration..."
)
logger.warning("Too much uncertainty in slope - you probably want to rerun this calibration...")

echo(f"Finished {pump_type} pump calibration `{name}`.")

Expand Down Expand Up @@ -569,9 +558,7 @@ def change_current(name: str) -> bool:
all_calibrations[name], type=structs.subclass_union(structs.PumpCalibration)
) # decode name from list of all names
except KeyError:
create_logger("pump_calibration").error(
f"Failed to swap. Calibration `{name}` not found."
)
create_logger("pump_calibration").error(f"Failed to swap. Calibration `{name}` not found.")
raise click.Abort()

pump_type_from_new_calibration = new_calibration.pump # retrieve the pump type
Expand Down Expand Up @@ -600,9 +587,7 @@ def change_current(name: str) -> bool:
return False

if old_calibration:
echo(
f"Replaced {old_calibration.name} with {new_calibration.name} as current calibration."
)
echo(f"Replaced {old_calibration.name} with {new_calibration.name} as current calibration.")
else:
echo(f"Set {new_calibration.name} to current calibration.")
return True
Expand Down Expand Up @@ -645,7 +630,7 @@ def click_pump_calibration(
"""
if ctx.invoked_subcommand is None:
if max_duration is None and min_duration is None:
min_duration, max_duration = 0.5, 1.5
min_duration, max_duration = 0.65, 1.6
elif (max_duration is not None) and (min_duration is not None):
assert min_duration < max_duration, "min_duration >= max_duration"
else:
Expand Down
44 changes: 23 additions & 21 deletions pioreactor/background_jobs/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def pretty_version(info: tuple) -> str:
"timestamp": current_utc_timestamp(),
}

self.logger.debug(
f"Pioreactor software version: {pretty_version(version.software_version_info)}"
)
self.logger.debug(f"Pioreactor software version: {pretty_version(version.software_version_info)}")

if whoami.am_I_active_worker():
self.logger.debug(f"Pioreactor HAT version: {self.versions['hat']}")
Expand Down Expand Up @@ -240,6 +238,14 @@ def check_for_webserver(self):
)
status = result.stdout.strip()

# Check stderr if stdout is empty
if not status:
status = result.stderr.strip()

# Handle case where status is still empty
if not status:
raise ValueError("No output from systemctl command")

# Check if the output is okay
if status == "failed" or status == "inactive" or status == "deactivating":
self.logger.error("lighttpd is not running. Check `systemctl status lighttpd`.")
Expand All @@ -263,11 +269,17 @@ def check_for_webserver(self):
while attempt < retries:
attempt += 1
# Run the command 'systemctl is-active huey' and capture the output
result = subprocess.run(
["systemctl", "is-active", "huey"], capture_output=True, text=True
)
result = subprocess.run(["systemctl", "is-active", "huey"], capture_output=True, text=True)
status = result.stdout.strip()

# Check stderr if stdout is empty
if not status:
status = result.stderr.strip()

# Handle case where status is still empty
if not status:
raise ValueError("No output from systemctl command")

# Check if the output is okay
if status == "failed" or status == "inactive" or status == "deactivating":
self.logger.error("huey is not running. Check `systemctl status huey`.")
Expand Down Expand Up @@ -300,9 +312,7 @@ def check_for_webserver(self):

def check_for_required_jobs_running(self):
if not all(utils.is_pio_job_running(["watchdog", "mqtt_to_db_streaming"])):
self.logger.debug(
"watchdog and mqtt_to_db_streaming should be running on leader. Double check."
)
self.logger.debug("watchdog and mqtt_to_db_streaming should be running on leader. Double check.")

def check_for_HAT(self) -> None:
if not is_HAT_present():
Expand Down Expand Up @@ -442,9 +452,7 @@ def button_down_and_up(self, chip, gpio, level, tick) -> None:
try:
pre_function()
except Exception:
self.logger.debug(
f"Error in pre_function={pre_function.__name__}.", exc_info=True
)
self.logger.debug(f"Error in pre_function={pre_function.__name__}.", exc_info=True)

elif level == 0:
self.button_down = False
Expand All @@ -453,9 +461,7 @@ def button_down_and_up(self, chip, gpio, level, tick) -> None:
try:
post_function()
except Exception:
self.logger.debug(
f"Error in post_function={post_function.__name__}.", exc_info=True
)
self.logger.debug(f"Error in post_function={post_function.__name__}.", exc_info=True)

def rpi_is_having_power_problems(self) -> tuple[bool, float]:
from pioreactor.utils.rpi_bad_power import new_under_voltage
Expand All @@ -474,9 +480,7 @@ def rpi_is_having_power_problems(self) -> tuple[bool, float]:
def check_for_power_problems(self) -> None:
is_rpi_having_power_probems, voltage = self.rpi_is_having_power_problems()
self.logger.debug(f"PWM power supply at ~{voltage:.2f}V.")
self.voltage_on_pwm_rail = Voltage(
voltage=round(voltage, 2), timestamp=current_utc_datetime()
)
self.voltage_on_pwm_rail = Voltage(voltage=round(voltage, 2), timestamp=current_utc_datetime())
if is_rpi_having_power_probems:
self.logger.warning(
f"Low-voltage detected on rail. PWM power supply at {voltage:.1f}V. Suggestion: use a better power supply or an AUX power. See docs at: https://docs.pioreactor.com/user-guide/external-power"
Expand Down Expand Up @@ -603,9 +607,7 @@ def run_job_on_machine(self, msg: MQTTMessage) -> None:
payload = loads(msg.payload) if msg.payload else {"options": {}, "args": []}

if "options" not in payload:
self.logger.debug(
"`options` key missing from payload. You should provide an empty dictionary."
)
self.logger.debug("`options` key missing from payload. You should provide an empty dictionary.")
options = payload.get("options", {})

if "args" not in payload:
Expand Down

0 comments on commit d122a94

Please sign in to comment.