Skip to content

Commit

Permalink
Added Parity Next Check Schedule attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
MrD3y5eL committed Dec 27, 2024
1 parent a7ed169 commit e7eaeeb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
72 changes: 72 additions & 0 deletions custom_components/unraid/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,78 @@ async def _async_update_data(self) -> Dict[str, Any]:
# Don't let Docker errors affect other data
data["docker_containers"] = []
data["docker_stats"] = {"containers": {}, "summary": {}}

# Step 6 Add parity schedule parsing
try:
result = await self.api.execute_command(
"cat /boot/config/plugins/dynamix/parity-check.cron"
)
next_check = "Unknown"

if result and result.exit_status == 0:
_LOGGER.debug("Found parity check cron: %s", result.stdout)

# Parse the cron entries
for line in result.stdout.splitlines():
if "mdcmd check" in line and not line.startswith('#'):
# Split the cron entry
parts = line.strip().split()
if len(parts) >= 5:
minute, hour, dom, month, dow = parts[:5]

now = dt_util.now()
next_run = None

# Parse based on the cron pattern
if dom == "1" and month == "1": # Yearly on Jan 1st
next_run = now.replace(
month=1,
day=1,
hour=int(hour),
minute=int(minute),
second=0,
microsecond=0
)
if next_run <= now:
next_run = next_run.replace(year=next_run.year + 1)
elif dom == "1" and month == "*": # Monthly on 1st
next_run = now.replace(
day=1,
hour=int(hour),
minute=int(minute),
second=0,
microsecond=0
)
if next_run <= now:
if next_run.month == 12:
next_run = next_run.replace(year=next_run.year + 1, month=1)
else:
next_run = next_run.replace(month=next_run.month + 1)

if next_run:
# Format the date nicely
if (next_run - now).days == 0:
next_check = f"Today at {next_run.strftime('%H:%M')}"
elif (next_run - now).days == 1:
next_check = f"Tomorrow at {next_run.strftime('%H:%M')}"
else:
next_check = next_run.strftime("%b %d %Y at %H:%M")
break

# If no cron schedule found, fall back to config file check
if next_check == "Unknown":
parity_config = data.get("disk_config", {})
if not parity_config.get("parity.mode") == "4": # If not manual mode
next_check = "Schedule configuration error"
else:
next_check = "Manual Only"

data["next_parity_check"] = next_check
_LOGGER.debug("Set next parity check to: %s", next_check)

except Exception as err:
_LOGGER.error("Error parsing parity schedule: %s", err)
data["next_parity_check"] = "Unknown"

_LOGGER.debug("Data update complete. Keys collected: %s", list(data.keys()))

Expand Down
2 changes: 2 additions & 0 deletions custom_components/unraid/diagnostics/parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def extra_state_attributes(self) -> dict[str, StateType]:
"speed": "N/A",
"errors": 0,
"last_check": "N/A",
"next_check": self.coordinator.data.get("next_parity_check", "Unknown"),
"duration": "N/A",
"last_status": "N/A"
}
Expand Down Expand Up @@ -591,6 +592,7 @@ def extra_state_attributes(self) -> dict[str, StateType]:
"speed": "N/A",
"errors": 0,
"last_check": "N/A",
"next_check": "Unknown",
"duration": "N/A",
"last_status": "N/A"
}
Expand Down

0 comments on commit e7eaeeb

Please sign in to comment.